Merge "Define pt as fallback for tet"
[lhc/web/wiklou.git] / tests / phpunit / includes / watcheditem / WatchedItemQueryServiceUnitTest.php
index cb0e376..50e6c20 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use Wikimedia\Rdbms\LoadBalancer;
 use Wikimedia\TestingAccessWrapper;
 
 /**
@@ -9,7 +10,10 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
        use MediaWikiCoversValidator;
 
-       private function overrideCommentStore() {
+       /**
+        * @return PHPUnit_Framework_MockObject_MockObject|CommentStore
+        */
+       private function getMockCommentStore() {
                $mockStore = $this->getMockBuilder( CommentStore::class )
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -23,8 +27,53 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                'fields' => [ 'commentstore' => 'field' ],
                                'joins' => [ 'commentstore' => 'join' ],
                        ] );
+               return $mockStore;
+       }
+
+       /**
+        * @return PHPUnit_Framework_MockObject_MockObject|ActorMigration
+        */
+       private function getMockActorMigration() {
+               $mockStore = $this->getMockBuilder( ActorMigration::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $mockStore->expects( $this->any() )
+                       ->method( 'getJoin' )
+                       ->willReturn( [
+                               'tables' => [ 'actormigration' => 'table' ],
+                               'fields' => [
+                                       'rc_user' => 'actormigration_user',
+                                       'rc_user_text' => 'actormigration_user_text',
+                                       'rc_actor' => 'actormigration_actor',
+                               ],
+                               'joins' => [ 'actormigration' => 'join' ],
+                       ] );
+               $mockStore->expects( $this->any() )
+                       ->method( 'getWhere' )
+                       ->willReturn( [
+                               'tables' => [ 'actormigration' => 'table' ],
+                               'conds' => 'actormigration_conds',
+                               'joins' => [ 'actormigration' => 'join' ],
+                       ] );
+               $mockStore->expects( $this->any() )
+                       ->method( 'isAnon' )
+                       ->willReturn( 'actormigration is anon' );
+               $mockStore->expects( $this->any() )
+                       ->method( 'isNotAnon' )
+                       ->willReturn( 'actormigration is not anon' );
+               return $mockStore;
+       }
 
-               $this->setService( 'CommentStore', $mockStore );
+       /**
+        * @param PHPUnit_Framework_MockObject_MockObject|Database $mockDb
+        * @return WatchedItemQueryService
+        */
+       private function newService( $mockDb ) {
+               return new WatchedItemQueryService(
+                       $this->getMockLoadBalancer( $mockDb ),
+                       $this->getMockCommentStore(),
+                       $this->getMockActorMigration()
+               );
        }
 
        /**
@@ -43,10 +92,17 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnCallback( function ( $a, $conj ) {
                                $sqlConj = $conj === LIST_AND ? ' AND ' : ' OR ';
-                               return join( $sqlConj, array_map( function ( $s ) {
-                                       return '(' . $s . ')';
-                               }, $a
-                               ) );
+                               $conds = [];
+                               foreach ( $a as $k => $v ) {
+                                       if ( is_int( $k ) ) {
+                                               $conds[] = "($v)";
+                                       } elseif ( is_array( $v ) ) {
+                                               $conds[] = "($k IN ('" . implode( "','", $v ) . "'))";
+                                       } else {
+                                               $conds[] = "($k = '$v')";
+                                       }
+                               }
+                               return implode( $sqlConj, $conds );
                        } ) );
 
                $mock->expects( $this->any() )
@@ -249,7 +305,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                ] ),
                        ] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $startFrom = null;
@@ -409,7 +465,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                $startFrom = [ '20160203123456', 42 ];
                        } ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                TestingAccessWrapper::newFromObject( $queryService )->extensions = [ $mockExtension ];
 
                $startFrom = null;
@@ -476,20 +532,20 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        [
                                [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_USER ] ],
                                null,
-                               [],
-                               [ 'rc_user_text' ],
-                               [],
+                               [ 'actormigration' => 'table' ],
+                               [ 'rc_user_text' => 'actormigration_user_text' ],
                                [],
                                [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_USER_ID ] ],
                                null,
-                               [],
-                               [ 'rc_user' ],
-                               [],
+                               [ 'actormigration' => 'table' ],
+                               [ 'rc_user' => 'actormigration_user' ],
                                [],
                                [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_COMMENT ] ],
@@ -691,20 +747,20 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        [
                                [ 'filters' => [ WatchedItemQueryService::FILTER_ANON ] ],
                                null,
+                               [ 'actormigration' => 'table' ],
                                [],
+                               [ 'actormigration is anon' ],
                                [],
-                               [ 'rc_user = 0' ],
-                               [],
-                               [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'filters' => [ WatchedItemQueryService::FILTER_NOT_ANON ] ],
                                null,
+                               [ 'actormigration' => 'table' ],
                                [],
+                               [ 'actormigration is not anon' ],
                                [],
-                               [ 'rc_user != 0' ],
-                               [],
-                               [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'filters' => [ WatchedItemQueryService::FILTER_PATROLLED ] ],
@@ -720,7 +776,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                null,
                                [],
                                [],
-                               [ 'rc_patrolled = 0' ],
+                               [ 'rc_patrolled' => 0 ],
                                [],
                                [],
                        ],
@@ -745,20 +801,20 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        [
                                [ 'onlyByUser' => 'SomeOtherUser' ],
                                null,
+                               [ 'actormigration' => 'table' ],
                                [],
+                               [ 'actormigration_conds' ],
                                [],
-                               [ 'rc_user_text' => 'SomeOtherUser' ],
-                               [],
-                               [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'notByUser' => 'SomeOtherUser' ],
                                null,
+                               [ 'actormigration' => 'table' ],
                                [],
+                               [ 'NOT(actormigration_conds)' ],
                                [],
-                               [ "rc_user_text != 'SomeOtherUser'" ],
-                               [],
-                               [],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'dir' => WatchedItemQueryService::DIR_OLDER ],
@@ -808,8 +864,6 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                array $expectedDbOptions,
                array $expectedExtraJoinConds
        ) {
-               $this->overrideCommentStore();
-
                $expectedTables = array_merge( [ 'recentchanges', 'watchlist', 'page' ], $expectedExtraTables );
                $expectedFields = array_merge(
                        [
@@ -861,7 +915,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom );
@@ -898,7 +952,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
                $user = $this->getMockNonAnonUserWithIdAndNoPatrolRights( 1 );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $items = $queryService->getWatchedItemsWithRecentChangeInfo(
                        $user,
                        [ 'filters' => [ $filtersOption ] ]
@@ -959,7 +1013,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        ->method( 'getType' )
                        ->will( $this->returnValue( $dbType ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options );
@@ -972,62 +1026,74 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        [
                                [],
                                'deletedhistory',
+                               [],
                                [
                                        '(rc_type != ' . RC_LOG . ') OR ((rc_deleted & ' . LogPage::DELETED_ACTION . ') != ' .
                                                LogPage::DELETED_ACTION . ')'
                                ],
+                               [],
                        ],
                        [
                                [],
                                'suppressrevision',
+                               [],
                                [
                                        '(rc_type != ' . RC_LOG . ') OR (' .
                                                '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
                                                ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
                                ],
+                               [],
                        ],
                        [
                                [],
                                'viewsuppressed',
+                               [],
                                [
                                        '(rc_type != ' . RC_LOG . ') OR (' .
                                                '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
                                                ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
                                ],
+                               [],
                        ],
                        [
                                [ 'onlyByUser' => 'SomeOtherUser' ],
                                'deletedhistory',
+                               [ 'actormigration' => 'table' ],
                                [
-                                       'rc_user_text' => 'SomeOtherUser',
+                                       'actormigration_conds',
                                        '(rc_deleted & ' . Revision::DELETED_USER . ') != ' . Revision::DELETED_USER,
                                        '(rc_type != ' . RC_LOG . ') OR ((rc_deleted & ' . LogPage::DELETED_ACTION . ') != ' .
                                                LogPage::DELETED_ACTION . ')'
                                ],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'onlyByUser' => 'SomeOtherUser' ],
                                'suppressrevision',
+                               [ 'actormigration' => 'table' ],
                                [
-                                       'rc_user_text' => 'SomeOtherUser',
+                                       'actormigration_conds',
                                        '(rc_deleted & ' . ( Revision::DELETED_USER | Revision::DELETED_RESTRICTED ) . ') != ' .
                                                ( Revision::DELETED_USER | Revision::DELETED_RESTRICTED ),
                                        '(rc_type != ' . RC_LOG . ') OR (' .
                                                '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
                                                ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
                                ],
+                               [ 'actormigration' => 'join' ],
                        ],
                        [
                                [ 'onlyByUser' => 'SomeOtherUser' ],
                                'viewsuppressed',
+                               [ 'actormigration' => 'table' ],
                                [
-                                       'rc_user_text' => 'SomeOtherUser',
+                                       'actormigration_conds',
                                        '(rc_deleted & ' . ( Revision::DELETED_USER | Revision::DELETED_RESTRICTED ) . ') != ' .
                                                ( Revision::DELETED_USER | Revision::DELETED_RESTRICTED ),
                                        '(rc_type != ' . RC_LOG . ') OR (' .
                                                '(rc_deleted & ' . ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ') != ' .
                                                ( LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED ) . ')'
                                ],
+                               [ 'actormigration' => 'join' ],
                        ],
                ];
        }
@@ -1038,7 +1104,9 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
        public function testGetWatchedItemsWithRecentChangeInfo_userPermissionRelatedExtraChecks(
                array $options,
                $notAllowedAction,
-               array $expectedExtraConds
+               array $expectedExtraTables,
+               array $expectedExtraConds,
+               array $expectedExtraJoins
        ) {
                $commonConds = [ 'wl_user' => 1, '(rc_this_oldid=page_latest) OR (rc_type=3)' ];
                $conds = array_merge( $commonConds, $expectedExtraConds );
@@ -1047,18 +1115,21 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                $mockDb->expects( $this->once() )
                        ->method( 'select' )
                        ->with(
-                               [ 'recentchanges', 'watchlist', 'page' ],
+                               array_merge( [ 'recentchanges', 'watchlist', 'page' ], $expectedExtraTables ),
                                $this->isType( 'array' ),
                                $conds,
                                $this->isType( 'string' ),
                                $this->isType( 'array' ),
-                               $this->isType( 'array' )
+                               array_merge( [
+                                       'watchlist' => [ 'INNER JOIN', [ 'wl_namespace=rc_namespace', 'wl_title=rc_title' ] ],
+                                       'page' => [ 'LEFT JOIN', 'rc_cur_id=page_id' ],
+                               ], $expectedExtraJoins )
                        )
                        ->will( $this->returnValue( [] ) );
 
                $user = $this->getMockNonAnonUserWithIdAndRestrictedPermissions( 1, $notAllowedAction );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options );
 
                $this->assertEmpty( $items );
@@ -1098,7 +1169,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, [ 'allRevisions' => true ] );
@@ -1183,7 +1254,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                $mockDb->expects( $this->never() )
                        ->method( $this->anything() );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $this->setExpectedException( InvalidArgumentException::class, $expectedInExceptionMessage );
@@ -1225,7 +1296,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsWithRecentChangeInfo(
@@ -1267,7 +1338,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsWithRecentChangeInfo(
@@ -1295,7 +1366,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
                $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2 );
                $otherUser->expects( $this->once() )
@@ -1326,7 +1397,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                $mockDb->expects( $this->never() )
                        ->method( $this->anything() );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
                $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2 );
                $otherUser->expects( $this->once() )
@@ -1363,7 +1434,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                ] ),
                        ] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
                $user = $this->getMockNonAnonUserWithId( 1 );
 
                $items = $queryService->getWatchedItemsForUser( $user );
@@ -1463,7 +1534,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
 
                $items = $queryService->getWatchedItemsForUser( $user, $options );
                $this->assertEmpty( $items );
@@ -1560,7 +1631,7 @@ class WatchedItemQueryServiceUnitTest 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
                                ) );
@@ -1576,7 +1647,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                        )
                        ->will( $this->returnValue( [] ) );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
 
                $items = $queryService->getWatchedItemsForUser( $user, $options );
                $this->assertEmpty( $items );
@@ -1614,7 +1685,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                array $options,
                $expectedInExceptionMessage
        ) {
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $this->getMockDb() ) );
+               $queryService = $this->newService( $this->getMockDb() );
 
                $this->setExpectedException( InvalidArgumentException::class, $expectedInExceptionMessage );
                $queryService->getWatchedItemsForUser( $this->getMockNonAnonUserWithId( 1 ), $options );
@@ -1626,7 +1697,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                $mockDb->expects( $this->never() )
                        ->method( $this->anything() );
 
-               $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+               $queryService = $this->newService( $mockDb );
 
                $items = $queryService->getWatchedItemsForUser( $this->getMockAnonUser() );
                $this->assertEmpty( $items );