Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / tests / phpunit / includes / watcheditem / WatchedItemStoreUnitTest.php
index 43b4fe9..9485170 100644 (file)
@@ -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 );
 
@@ -122,7 +195,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                ],
                                $this->isType( 'string' )
                        )
-                       ->will( $this->returnValue( 12 ) );
+                       ->will( $this->returnValue( '12' ) );
 
                $mockCache = $this->getMockCache();
                $mockCache->expects( $this->never() )->method( 'get' );
@@ -153,7 +226,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                ],
                                $this->isType( 'string' )
                        )
-                       ->will( $this->returnValue( 7 ) );
+                       ->will( $this->returnValue( '7' ) );
 
                $mockCache = $this->getMockCache();
                $mockCache->expects( $this->never() )->method( 'get' );
@@ -179,9 +252,9 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                $mockDb = $this->getMockDb();
 
                $dbResult = [
-                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
+                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
                        ),
                ];
                $mockDb->expects( $this->once() )
@@ -245,9 +318,9 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                $mockDb = $this->getMockDb();
 
                $dbResult = [
-                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ]
+                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
                        ),
                ];
                $mockDb->expects( $this->once() )
@@ -311,7 +384,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                ],
                                $this->isType( 'string' )
                        )
-                       ->will( $this->returnValue( 7 ) );
+                       ->will( $this->returnValue( '7' ) );
                $mockDb->expects( $this->exactly( 1 ) )
                        ->method( 'addQuotes' )
                        ->will( $this->returnCallback( function ( $value ) {
@@ -345,9 +418,11 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                ];
 
                $dbResult = [
-                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
+                       $this->getFakeRow(
+                               [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
+                       ),
                ];
                $mockDb = $this->getMockDb();
                $mockDb->expects( $this->exactly( 2 * 3 ) )
@@ -368,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
                                ) );
@@ -434,14 +509,16 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                ];
 
                $dbResult = [
-                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => 0, 'watchers' => 100 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => 0, 'watchers' => 300 ] ),
-                       $this->getFakeRow( [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => 1, 'watchers' => 500 ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'SomeDbKey', 'wl_namespace' => '0', 'watchers' => '100' ] ),
+                       $this->getFakeRow( [ 'wl_title' => 'OtherDbKey', 'wl_namespace' => '0', 'watchers' => '300' ] ),
+                       $this->getFakeRow(
+                               [ 'wl_title' => 'AnotherDbKey', 'wl_namespace' => '1', 'watchers' => '500' ]
+                       ),
                        $this->getFakeRow(
-                               [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 100 ]
+                               [ 'wl_title' => 'SomeNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '100' ]
                        ),
                        $this->getFakeRow(
-                               [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => 0, 'watchers' => 200 ]
+                               [ 'wl_title' => 'OtherNotExisitingDbKey', 'wl_namespace' => '0', 'watchers' => '200' ]
                        ),
                ];
                $mockDb = $this->getMockDb();
@@ -463,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
                                ) );
@@ -596,7 +673,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                ],
                                $this->isType( 'string' )
                        )
-                       ->will( $this->returnValue( 9 ) );
+                       ->will( $this->returnValue( '9' ) );
 
                $mockCache = $this->getMockCache();
                $mockCache->expects( $this->never() )->method( 'set' );
@@ -631,7 +708,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                $this->isType( 'string' ),
                                [ 'LIMIT' => 50 ]
                        )
-                       ->will( $this->returnValue( 50 ) );
+                       ->will( $this->returnValue( '50' ) );
 
                $mockCache = $this->getMockCache();
                $mockCache->expects( $this->never() )->method( 'set' );
@@ -669,7 +746,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                $this->isType( 'string' ),
                                [ 'LIMIT' => 50 ]
                        )
-                       ->will( $this->returnValue( 9 ) );
+                       ->will( $this->returnValue( '9' ) );
 
                $mockCache = $this->getMockCache();
                $mockCache->expects( $this->never() )->method( 'set' );
@@ -721,8 +798,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
 
        public function testDuplicateEntry_somethingToDuplicate() {
                $fakeRows = [
-                       $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
-                       $this->getFakeRow( [ 'wl_user' => 2, 'wl_notificationtimestamp' => null ] ),
+                       $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
+                       $this->getFakeRow( [ 'wl_user' => '2', 'wl_notificationtimestamp' => null ] ),
                ];
 
                $mockDb = $this->getMockDb();
@@ -840,7 +917,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                LinkTarget $newTarget
        ) {
                $fakeRows = [
-                       $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
+                       $this->getFakeRow( [ 'wl_user' => '1', 'wl_notificationtimestamp' => '20151212010101' ] ),
                ];
 
                $mockDb = $this->getMockDb();
@@ -1114,7 +1191,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                        $this->getMockNonAnonUserWithId( 1 ),
                        new TitleValue( 0, 'SomeDbKey' )
                );
-               $this->assertInstanceOf( 'WatchedItem', $watchedItem );
+               $this->assertInstanceOf( WatchedItem::class, $watchedItem );
                $this->assertEquals( 1, $watchedItem->getUser()->getId() );
                $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
                $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
@@ -1313,7 +1390,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                        $this->getMockNonAnonUserWithId( 1 ),
                        new TitleValue( 0, 'SomeDbKey' )
                );
-               $this->assertInstanceOf( 'WatchedItem', $watchedItem );
+               $this->assertInstanceOf( WatchedItem::class, $watchedItem );
                $this->assertEquals( 1, $watchedItem->getUser()->getId() );
                $this->assertEquals( 'SomeDbKey', $watchedItem->getLinkTarget()->getDBkey() );
                $this->assertEquals( 0, $watchedItem->getLinkTarget()->getNamespace() );
@@ -1453,7 +1530,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                $this->assertInternalType( 'array', $watchedItems );
                $this->assertCount( 2, $watchedItems );
                foreach ( $watchedItems as $watchedItem ) {
-                       $this->assertInstanceOf( 'WatchedItem', $watchedItem );
+                       $this->assertInstanceOf( WatchedItem::class, $watchedItem );
                }
                $this->assertEquals(
                        new WatchedItem( $user, new TitleValue( 0, 'Foo1' ), '20151212010101' ),
@@ -1512,7 +1589,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                        $this->getMockReadOnlyMode()
                );
 
-               $this->setExpectedException( 'InvalidArgumentException' );
+               $this->setExpectedException( InvalidArgumentException::class );
                $store->getWatchedItemsForUser(
                        $this->getMockNonAnonUserWithId( 1 ),
                        [ 'sort' => 'foo' ]
@@ -1632,13 +1709,13 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                $mockDb = $this->getMockDb();
                $dbResult = [
                        $this->getFakeRow( [
-                               'wl_namespace' => 0,
+                               'wl_namespace' => '0',
                                'wl_title' => 'SomeDbKey',
                                'wl_notificationtimestamp' => '20151212010101',
                        ] ),
                        $this->getFakeRow(
                                [
-                                       'wl_namespace' => 1,
+                                       'wl_namespace' => '1',
                                        'wl_title' => 'AnotherDbKey',
                                        'wl_notificationtimestamp' => null,
                                ]
@@ -1774,7 +1851,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [
                                $this->getFakeRow(
-                                       [ 'wl_namespace' => 1, 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
+                                       [ 'wl_namespace' => '1', 'wl_title' => 'AnotherDbKey', 'wl_notificationtimestamp' => null, ]
                                )
                        ] ) );