Remove covers validator trait where it is not needed
[lhc/web/wiklou.git] / tests / phpunit / includes / watcheditem / WatchedItemQueryServiceUnitTest.php
index be51626..0b1d013 100644 (file)
@@ -1,5 +1,7 @@
 <?php
 
+use MediaWiki\User\UserIdentityValue;
+use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
 use Wikimedia\TestingAccessWrapper;
 
@@ -8,8 +10,6 @@ use Wikimedia\TestingAccessWrapper;
  */
 class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
-       use MediaWikiCoversValidator;
-
        /**
         * @return PHPUnit_Framework_MockObject_MockObject|CommentStore
         */
@@ -72,17 +72,16 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                return new WatchedItemQueryService(
                        $this->getMockLoadBalancer( $mockDb ),
                        $this->getMockCommentStore(),
-                       $this->getMockActorMigration()
+                       $this->getMockActorMigration(),
+                       $this->getMockWatchedItemStore()
                );
        }
 
        /**
-        * @return PHPUnit_Framework_MockObject_MockObject|Database
+        * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
         */
        private function getMockDb() {
-               $mock = $this->getMockBuilder( Database::class )
-                       ->disableOriginalConstructor()
-                       ->getMock();
+               $mock = $this->createMock( IDatabase::class );
 
                $mock->expects( $this->any() )
                        ->method( 'makeList' )
@@ -125,7 +124,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
        }
 
        /**
-        * @param PHPUnit_Framework_MockObject_MockObject|Database $mockDb
+        * @param PHPUnit_Framework_MockObject_MockObject|IDatabase $mockDb
         * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
         */
        private function getMockLoadBalancer( $mockDb ) {
@@ -139,36 +138,49 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                return $mock;
        }
 
+       /**
+        * @return PHPUnit_Framework_MockObject_MockObject|WatchedItemStore
+        */
+       private function getMockWatchedItemStore() {
+               $mock = $this->getMockBuilder( WatchedItemStore::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $mock->expects( $this->any() )
+                       ->method( 'getLatestNotificationTimestamp' )
+                       ->will( $this->returnCallback( function ( $timestamp ) {
+                               return $timestamp;
+                       } ) );
+               return $mock;
+       }
+
        /**
         * @param int $id
+        * @param string[] $extraMethods Extra methods that are expected might be called
         * @return PHPUnit_Framework_MockObject_MockObject|User
         */
-       private function getMockNonAnonUserWithId( $id ) {
+       private function getMockNonAnonUserWithId( $id, array $extraMethods = [] ) {
                $mock = $this->getMockBuilder( User::class )->getMock();
-               $mock->expects( $this->any() )
-                       ->method( 'isAnon' )
-                       ->will( $this->returnValue( false ) );
-               $mock->expects( $this->any() )
-                       ->method( 'getId' )
-                       ->will( $this->returnValue( $id ) );
+               $mock->method( 'isRegistered' )->willReturn( true );
+               $mock->method( 'getId' )->willReturn( $id );
+               $methods = array_merge( [
+                       'isRegistered',
+                       'getId',
+               ], $extraMethods );
+               $mock->expects( $this->never() )->method( $this->anythingBut( ...$methods ) );
                return $mock;
        }
 
        /**
         * @param int $id
+        * @param string[] $extraMethods Extra methods that are expected might be called
         * @return PHPUnit_Framework_MockObject_MockObject|User
         */
-       private function getMockUnrestrictedNonAnonUserWithId( $id ) {
-               $mock = $this->getMockNonAnonUserWithId( $id );
-               $mock->expects( $this->any() )
-                       ->method( 'isAllowed' )
-                       ->will( $this->returnValue( true ) );
-               $mock->expects( $this->any() )
-                       ->method( 'isAllowedAny' )
-                       ->will( $this->returnValue( true ) );
-               $mock->expects( $this->any() )
-                       ->method( 'useRCPatrol' )
-                       ->will( $this->returnValue( true ) );
+       private function getMockUnrestrictedNonAnonUserWithId( $id, array $extraMethods = [] ) {
+               $mock = $this->getMockNonAnonUserWithId( $id,
+                       array_merge( [ 'isAllowed', 'isAllowedAny', 'useRCPatrol' ], $extraMethods ) );
+               $mock->method( 'isAllowed' )->willReturn( true );
+               $mock->method( 'isAllowedAny' )->willReturn( true );
+               $mock->method( 'useRCPatrol' )->willReturn( true );
                return $mock;
        }
 
@@ -178,19 +190,19 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
         * @return PHPUnit_Framework_MockObject_MockObject|User
         */
        private function getMockNonAnonUserWithIdAndRestrictedPermissions( $id, $notAllowedAction ) {
-               $mock = $this->getMockNonAnonUserWithId( $id );
+               $mock = $this->getMockNonAnonUserWithId( $id,
+                       [ 'isAllowed', 'isAllowedAny', 'useRCPatrol', 'useNPPatrol' ] );
 
-               $mock->expects( $this->any() )
-                       ->method( 'isAllowed' )
+               $mock->method( 'isAllowed' )
                        ->will( $this->returnCallback( function ( $action ) use ( $notAllowedAction ) {
                                return $action !== $notAllowedAction;
                        } ) );
-               $mock->expects( $this->any() )
-                       ->method( 'isAllowedAny' )
-                       ->will( $this->returnCallback( function () use ( $notAllowedAction ) {
-                               $actions = func_get_args();
+               $mock->method( 'isAllowedAny' )
+                       ->will( $this->returnCallback( function ( ...$actions ) use ( $notAllowedAction ) {
                                return !in_array( $notAllowedAction, $actions );
                        } ) );
+               $mock->method( 'useRCPatrol' )->willReturn( false );
+               $mock->method( 'useNPPatrol' )->willReturn( false );
 
                return $mock;
        }
@@ -200,7 +212,8 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
         * @return PHPUnit_Framework_MockObject_MockObject|User
         */
        private function getMockNonAnonUserWithIdAndNoPatrolRights( $id ) {
-               $mock = $this->getMockNonAnonUserWithId( $id );
+               $mock = $this->getMockNonAnonUserWithId( $id,
+                       [ 'isAllowed', 'isAllowedAny', 'useRCPatrol', 'useNPPatrol' ] );
 
                $mock->expects( $this->any() )
                        ->method( 'isAllowed' )
@@ -219,14 +232,6 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                return $mock;
        }
 
-       private function getMockAnonUser() {
-               $mock = $this->getMockBuilder( User::class )->getMock();
-               $mock->expects( $this->any() )
-                       ->method( 'isAnon' )
-                       ->will( $this->returnValue( true ) );
-               return $mock;
-       }
-
        private function getFakeRow( array $rowValues ) {
                $fakeRow = new stdClass();
                foreach ( $rowValues as $valueName => $value ) {
@@ -263,7 +268,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                ],
                                [
                                        'watchlist' => [
-                                               'INNER JOIN',
+                                               'JOIN',
                                                [
                                                        'wl_namespace=rc_namespace',
                                                        'wl_title=rc_title'
@@ -386,7 +391,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                ],
                                [
                                        'watchlist' => [
-                                               'INNER JOIN',
+                                               'JOIN',
                                                [
                                                        'wl_namespace=rc_namespace',
                                                        'wl_title=rc_title'
@@ -776,7 +781,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                null,
                                [],
                                [],
-                               [ 'rc_patrolled = 0' ],
+                               [ 'rc_patrolled' => 0 ],
                                [],
                                [],
                        ],
@@ -888,7 +893,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                $expectedJoinConds = array_merge(
                        [
                                'watchlist' => [
-                                       'INNER JOIN',
+                                       'JOIN',
                                        [
                                                'wl_namespace=rc_namespace',
                                                'wl_title=rc_title'
@@ -1121,7 +1126,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                $this->isType( 'string' ),
                                $this->isType( 'array' ),
                                array_merge( [
-                                       'watchlist' => [ 'INNER JOIN', [ 'wl_namespace=rc_namespace', 'wl_title=rc_title' ] ],
+                                       'watchlist' => [ 'JOIN', [ 'wl_namespace=rc_namespace', 'wl_title=rc_title' ] ],
                                        'page' => [ 'LEFT JOIN', 'rc_cur_id=page_id' ],
                                ], $expectedExtraJoins )
                        )
@@ -1159,7 +1164,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                [],
                                [
                                        'watchlist' => [
-                                               'INNER JOIN',
+                                               'JOIN',
                                                [
                                                        'wl_namespace=rc_namespace',
                                                        'wl_title=rc_title'
@@ -1282,7 +1287,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                [],
                                [
                                        'watchlist' => [
-                                               'INNER JOIN',
+                                               'JOIN',
                                                [
                                                        'wl_namespace=rc_namespace',
                                                        'wl_title=rc_title'
@@ -1328,7 +1333,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
                                [],
                                [
                                        'watchlist' => [
-                                               'INNER JOIN',
+                                               'JOIN',
                                                [
                                                        'wl_namespace=rc_namespace',
                                                        'wl_title=rc_title'
@@ -1368,7 +1373,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
                $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
-               $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2 );
+               $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2, [ 'getOption' ] );
                $otherUser->expects( $this->once() )
                        ->method( 'getOption' )
                        ->with( 'watchlisttoken' )
@@ -1399,7 +1404,7 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
                $queryService = $this->newService( $mockDb );
                $user = $this->getMockUnrestrictedNonAnonUserWithId( 1 );
-               $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2 );
+               $otherUser = $this->getMockUnrestrictedNonAnonUserWithId( 2, [ 'getOption' ] );
                $otherUser->expects( $this->once() )
                        ->method( 'getOption' )
                        ->with( 'watchlisttoken' )
@@ -1699,7 +1704,8 @@ class WatchedItemQueryServiceUnitTest extends MediaWikiTestCase {
 
                $queryService = $this->newService( $mockDb );
 
-               $items = $queryService->getWatchedItemsForUser( $this->getMockAnonUser() );
+               $items = $queryService->getWatchedItemsForUser(
+                       new UserIdentityValue( 0, 'AnonUser', 0 ) );
                $this->assertEmpty( $items );
        }