<?php
-use Wikimedia\ScopedCallback;
+use Wikimedia\Rdbms\LoadBalancer;
use Wikimedia\TestingAccessWrapper;
/**
* @covers WatchedItemQueryService
*/
-class WatchedItemQueryServiceUnitTest extends PHPUnit_Framework_TestCase {
+class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
+
+ use MediaWikiCoversValidator;
+
+ /**
+ * @return PHPUnit_Framework_MockObject_MockObject|CommentStore
+ */
+ private function getMockCommentStore() {
+ $mockStore = $this->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
)
->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
) );
] ),
] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
$startFrom = null;
$startFrom = [ '20160203123456', 42 ];
} ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
TestingAccessWrapper::newFromObject( $queryService )->extensions = [ $mockExtension ];
$startFrom = null;
[
[ '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 ] ],
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(
[
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
$items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options, $startFrom );
$user = $this->getMockNonAnonUserWithIdAndNoPatrolRights( 1 );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$items = $queryService->getWatchedItemsWithRecentChangeInfo(
$user,
[ 'filters' => [ $filtersOption ] ]
->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 );
$user = $this->getMockNonAnonUserWithIdAndRestrictedPermissions( 1, $notAllowedAction );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, $options );
$this->assertEmpty( $items );
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
$items = $queryService->getWatchedItemsWithRecentChangeInfo( $user, [ 'allRevisions' => true ] );
$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 );
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
$items = $queryService->getWatchedItemsWithRecentChangeInfo(
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
$items = $queryService->getWatchedItemsWithRecentChangeInfo(
)
->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() )
$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() )
] ),
] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$user = $this->getMockNonAnonUserWithId( 1 );
$items = $queryService->getWatchedItemsForUser( $user );
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$items = $queryService->getWatchedItemsForUser( $user, $options );
$this->assertEmpty( $items );
)
->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
) );
)
->will( $this->returnValue( [] ) );
- $queryService = new WatchedItemQueryService( $this->getMockLoadBalancer( $mockDb ) );
+ $queryService = $this->newService( $mockDb );
$items = $queryService->getWatchedItemsForUser( $user, $options );
$this->assertEmpty( $items );
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 );
$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 );