X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fwatcheditem%2FWatchedItemStoreUnitTest.php;h=948517067ca26a8e942c855f9b977058caa1c921;hp=6dbb10697e2a5046e2624456caa522e7a076a470;hb=a2c8c2969420a0f150c03f76e3a0bf9028fcda43;hpb=74426f3cf796b149f1ae445e41815bbe148640b2 diff --git a/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php b/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php index 6dbb10697e..948517067c 100644 --- a/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php +++ b/tests/phpunit/includes/watcheditem/WatchedItemStoreUnitTest.php @@ -2,6 +2,7 @@ use MediaWiki\Linker\LinkTarget; use Wikimedia\Rdbms\LoadBalancer; use Wikimedia\ScopedCallback; +use Wikimedia\TestingAccessWrapper; /** * @author Addshore @@ -104,10 +105,82 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase { return new WatchedItemStore( $loadBalancer, $cache, - $readOnlyMode + $readOnlyMode, + 1000 ); } + public function testClearWatchedItems() { + $user = $this->getMockNonAnonUserWithId( 7 ); + + $mockDb = $this->getMockDb(); + $mockDb->expects( $this->once() ) + ->method( 'selectField' ) + ->with( + 'watchlist', + 'COUNT(*)', + [ + 'wl_user' => $user->getId(), + ], + $this->isType( 'string' ) + ) + ->will( $this->returnValue( 12 ) ); + $mockDb->expects( $this->once() ) + ->method( 'delete' ) + ->with( + 'watchlist', + [ 'wl_user' => 7 ], + $this->isType( 'string' ) + ); + + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->once() ) + ->method( 'delete' ) + ->with( 'RM-KEY' ); + + $store = $this->newWatchedItemStore( + $this->getMockLoadBalancer( $mockDb ), + $mockCache, + $this->getMockReadOnlyMode() + ); + TestingAccessWrapper::newFromObject( $store ) + ->cacheIndex = [ 0 => [ 'F' => [ 7 => 'RM-KEY', 9 => 'KEEP-KEY' ] ] ]; + + $this->assertTrue( $store->clearUserWatchedItems( $user ) ); + } + + public function testClearWatchedItems_tooManyItemsWatched() { + $user = $this->getMockNonAnonUserWithId( 7 ); + + $mockDb = $this->getMockDb(); + $mockDb->expects( $this->once() ) + ->method( 'selectField' ) + ->with( + 'watchlist', + 'COUNT(*)', + [ + 'wl_user' => $user->getId(), + ], + $this->isType( 'string' ) + ) + ->will( $this->returnValue( 99999 ) ); + + $mockCache = $this->getMockCache(); + $mockCache->expects( $this->never() )->method( 'get' ); + $mockCache->expects( $this->never() )->method( 'set' ); + $mockCache->expects( $this->never() )->method( 'delete' ); + + $store = $this->newWatchedItemStore( + $this->getMockLoadBalancer( $mockDb ), + $mockCache, + $this->getMockReadOnlyMode() + ); + + $this->assertFalse( $store->clearUserWatchedItems( $user ) ); + } + public function testCountWatchedItems() { $user = $this->getMockNonAnonUserWithId( 1 ); @@ -370,7 +443,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase { ) ->will( $this->returnCallback( function ( $a, $conj ) { $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR '; - return join( $sqlConj, array_map( function ( $s ) { + return implode( $sqlConj, array_map( function ( $s ) { return '(' . $s . ')'; }, $a ) ); @@ -467,7 +540,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase { ) ->will( $this->returnCallback( function ( $a, $conj ) { $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR '; - return join( $sqlConj, array_map( function ( $s ) { + return implode( $sqlConj, array_map( function ( $s ) { return '(' . $s . ')'; }, $a ) );