X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fwatcheditem%2FWatchedItemQueryServiceUnitTest.php;h=b6cf2396e13a20d35d92a6d544198bf30ac85927;hb=8624538de243da3779db5eb3362bedf78d2e2931;hp=62ba5f68fe0eb3bab1850029ac13bff1ab207b69;hpb=cc122862fa411dbd5f2958ad91d568862f06d79e;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/watcheditem/WatchedItemQueryServiceUnitTest.php b/tests/phpunit/includes/watcheditem/WatchedItemQueryServiceUnitTest.php index 62ba5f68fe..b6cf2396e1 100644 --- a/tests/phpunit/includes/watcheditem/WatchedItemQueryServiceUnitTest.php +++ b/tests/phpunit/includes/watcheditem/WatchedItemQueryServiceUnitTest.php @@ -1,12 +1,45 @@ getMockBuilder( CommentStore::class ) + ->disableOriginalConstructor() + ->getMock(); + $mockStore->expects( $this->any() ) + ->method( 'getFields' ) + ->willReturn( [ 'commentstore' => 'fields' ] ); + $mockStore->expects( $this->any() ) + ->method( 'getJoin' ) + ->willReturn( [ + 'tables' => [ 'commentstore' => 'table' ], + 'fields' => [ 'commentstore' => 'field' ], + 'joins' => [ 'commentstore' => 'join' ], + ] ); + return $mockStore; + } + + /** + * @param PHPUnit_Framework_MockObject_MockObject|Database $mockDb + * @return WatchedItemQueryService + */ + private function newService( $mockDb ) { + return new WatchedItemQueryService( + $this->getMockLoadBalancer( $mockDb ), + $this->getMockCommentStore() + ); + } /** * @return PHPUnit_Framework_MockObject_MockObject|Database @@ -24,7 +57,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->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 ) ); @@ -230,7 +263,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ] ), ] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 ); $startFrom = null; @@ -390,7 +423,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $startFrom = [ '20160203123456', 42 ]; } ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); TestingAccessWrapper::newFromObject( $queryService )->extensions = [ $mockExtension ]; $startFrom = null; @@ -475,58 +508,11 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { [ [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_COMMENT ] ], null, - [], - [ - 'rc_comment_text' => 'rc_comment', - 'rc_comment_data' => 'NULL', - 'rc_comment_cid' => 'NULL', - ], - [], + [ 'commentstore' => 'table' ], + [ 'commentstore' => 'field' ], [], [], - [ 'wgCommentTableSchemaMigrationStage' => MIGRATION_OLD ], - ], - [ - [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_COMMENT ] ], - null, - [ 'comment_rc_comment' => 'comment' ], - [ - 'rc_comment_text' => 'COALESCE( comment_rc_comment.comment_text, rc_comment )', - 'rc_comment_data' => 'comment_rc_comment.comment_data', - 'rc_comment_cid' => 'comment_rc_comment.comment_id', - ], - [], - [], - [ 'comment_rc_comment' => [ 'LEFT JOIN', 'comment_rc_comment.comment_id = rc_comment_id' ] ], - [ 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_BOTH ], - ], - [ - [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_COMMENT ] ], - null, - [ 'comment_rc_comment' => 'comment' ], - [ - 'rc_comment_text' => 'COALESCE( comment_rc_comment.comment_text, rc_comment )', - 'rc_comment_data' => 'comment_rc_comment.comment_data', - 'rc_comment_cid' => 'comment_rc_comment.comment_id', - ], - [], - [], - [ 'comment_rc_comment' => [ 'LEFT JOIN', 'comment_rc_comment.comment_id = rc_comment_id' ] ], - [ 'wgCommentTableSchemaMigrationStage' => MIGRATION_WRITE_NEW ], - ], - [ - [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_COMMENT ] ], - null, - [ 'comment_rc_comment' => 'comment' ], - [ - 'rc_comment_text' => 'comment_rc_comment.comment_text', - 'rc_comment_data' => 'comment_rc_comment.comment_data', - 'rc_comment_cid' => 'comment_rc_comment.comment_id', - ], - [], - [], - [ 'comment_rc_comment' => [ 'JOIN', 'comment_rc_comment.comment_id = rc_comment_id' ] ], - [ 'wgCommentTableSchemaMigrationStage' => MIGRATION_NEW ], + [ 'commentstore' => 'join' ], ], [ [ 'includeFields' => [ WatchedItemQueryService::INCLUDE_PATROL_INFO ] ], @@ -834,23 +820,8 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { array $expectedExtraFields, array $expectedExtraConds, array $expectedDbOptions, - array $expectedExtraJoinConds, - array $globals = [] + array $expectedExtraJoinConds ) { - // Sigh. This test class doesn't extend MediaWikiTestCase, so we have to reinvent setMwGlobals(). - if ( $globals ) { - $resetGlobals = []; - foreach ( $globals as $k => $v ) { - $resetGlobals[$k] = $GLOBALS[$k]; - $GLOBALS[$k] = $v; - } - $reset = new ScopedCallback( function () use ( $resetGlobals ) { - foreach ( $resetGlobals as $k => $v ) { - $GLOBALS[$k] = $v; - } - } ); - } - $expectedTables = array_merge( [ 'recentchanges', 'watchlist', 'page' ], $expectedExtraTables ); $expectedFields = array_merge( [ @@ -902,7 +873,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom ); @@ -939,7 +910,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $user = $this->getMockNonAnonUserWithIdAndNoPatrolRights( 1 ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, [ 'filters' => [ $filtersOption ] ] @@ -1000,7 +971,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ->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 ); @@ -1099,7 +1070,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $user = $this->getMockNonAnonUserWithIdAndRestrictedPermissions( 1, $notAllowedAction ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options ); $this->assertEmpty( $items ); @@ -1139,7 +1110,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, [ 'allRevisions' => true ] ); @@ -1224,7 +1195,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $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 ); @@ -1266,7 +1237,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( @@ -1308,7 +1279,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 ); $items = $queryService->getWatchedItemsWithRecentChangeInfo( @@ -1336,7 +1307,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->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() ) @@ -1367,7 +1338,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $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() ) @@ -1404,7 +1375,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ] ), ] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $user = $this->getMockNonAnonUserWithId( 1 ); $items = $queryService->getWatchedItemsForUser( $user ); @@ -1504,7 +1475,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $items = $queryService->getWatchedItemsForUser( $user, $options ); $this->assertEmpty( $items ); @@ -1601,7 +1572,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->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 ) ); @@ -1617,7 +1588,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { ) ->will( $this->returnValue( [] ) ); - $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) ); + $queryService = $this->newService( $mockDb ); $items = $queryService->getWatchedItemsForUser( $user, $options ); $this->assertEmpty( $items ); @@ -1655,7 +1626,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { 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 ); @@ -1667,7 +1638,7 @@ class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase { $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 );