Merge "Do not run exact db match if offset is > 0"
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemUnitTest.php
index c33ba7e..7e1ff3d 100644 (file)
@@ -6,13 +6,30 @@ use MediaWiki\Linker\LinkTarget;
  *
  * @covers WatchedItem
  */
-class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
+class WatchedItemUnitTest extends MediaWikiTestCase {
+
+       /**
+        * @param int $id
+        *
+        * @return PHPUnit_Framework_MockObject_MockObject|User
+        */
+       private function getMockUser( $id ) {
+               $user = $this->getMock( User::class );
+               $user->expects( $this->any() )
+                       ->method( 'getId' )
+                       ->will( $this->returnValue( $id ) );
+               $user->expects( $this->any() )
+                       ->method( 'isAllowed' )
+                       ->will( $this->returnValue( true ) );
+               return $user;
+       }
 
        public function provideUserTitleTimestamp() {
+               $user = $this->getMockUser( 111 );
                return [
-                       [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), null ],
-                       [ User::newFromId( 111 ), Title::newFromText( 'SomeTitle' ), '20150101010101' ],
-                       [ User::newFromId( 111 ), new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
+                       [ $user, Title::newFromText( 'SomeTitle' ), null ],
+                       [ $user, Title::newFromText( 'SomeTitle' ), '20150101010101' ],
+                       [ $user, new TitleValue( 0, 'TVTitle', 'frag' ), '20150101010101' ],
                ];
        }
 
@@ -52,46 +69,13 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                        ->method( 'loadWatchedItem' )
                        ->with( $user, $linkTarget )
                        ->will( $this->returnValue( new WatchedItem( $user, $linkTarget, $timestamp ) ) );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                $item = WatchedItem::fromUserTitle( $user, $linkTarget, User::IGNORE_USER_RIGHTS );
 
                $this->assertEquals( $user, $item->getUser() );
                $this->assertEquals( $linkTarget, $item->getLinkTarget() );
                $this->assertEquals( $timestamp, $item->getNotificationTimestamp() );
-
-               ScopedCallback::consume( $scopedOverride );
-       }
-
-       /**
-        * @dataProvider provideUserTitleTimestamp
-        */
-       public function testResetNotificationTimestamp( $user, $linkTarget, $timestamp ) {
-               $force = 'XXX';
-               $oldid = 999;
-
-               $store = $this->getMockWatchedItemStore();
-               $store->expects( $this->once() )
-                       ->method( 'resetNotificationTimestamp' )
-                       ->with( $user, $this->isInstanceOf( Title::class ), $force, $oldid )
-                       ->will( $this->returnCallback(
-                               function ( $user, Title $title, $force, $oldid ) use ( $linkTarget ) {
-                                       /** @var LinkTarget $linkTarget */
-                                       $this->assertInstanceOf( 'Title', $title );
-                                       $this->assertSame( $linkTarget->getDBkey(), $title->getDBkey() );
-                                       $this->assertSame( $linkTarget->getFragment(), $title->getFragment() );
-                                       $this->assertSame( $linkTarget->getNamespace(), $title->getNamespace() );
-                                       $this->assertSame( $linkTarget->getText(), $title->getText() );
-
-                                       return true;
-                               }
-                       ) );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
-
-               $item = new WatchedItem( $user, $linkTarget, $timestamp );
-               $item->resetNotificationTimestamp( $force, $oldid );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
        public function testAddWatch() {
@@ -158,47 +142,9 @@ class WatchedItemUnitTest extends PHPUnit_Framework_TestCase {
                $store->expects( $this->once() )
                        ->method( 'duplicateAllAssociatedEntries' )
                        ->with( $oldTitle, $newTitle );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
+               $this->setService( 'WatchedItemStore', $store );
 
                WatchedItem::duplicateEntries( $oldTitle, $newTitle );
-
-               ScopedCallback::consume( $scopedOverride );
-       }
-
-       public function testBatchAddWatch() {
-               $itemOne = new WatchedItem( User::newFromId( 1 ), new TitleValue( 0, 'Title1' ), null );
-               $itemTwo = new WatchedItem(
-                       User::newFromId( 3 ),
-                       Title::newFromText( 'Title2' ),
-                       '20150101010101'
-               );
-
-               $store = $this->getMockWatchedItemStore();
-               $store->expects( $this->exactly( 2 ) )
-                       ->method( 'addWatchBatchForUser' );
-               $store->expects( $this->at( 0 ) )
-                       ->method( 'addWatchBatchForUser' )
-                       ->with(
-                               $itemOne->getUser(),
-                               [
-                                       $itemOne->getTitle()->getSubjectPage(),
-                                       $itemOne->getTitle()->getTalkPage(),
-                               ]
-                       );
-               $store->expects( $this->at( 1 ) )
-                       ->method( 'addWatchBatchForUser' )
-                       ->with(
-                               $itemTwo->getUser(),
-                               [
-                                       $itemTwo->getTitle()->getSubjectPage(),
-                                       $itemTwo->getTitle()->getTalkPage(),
-                               ]
-                       );
-               $scopedOverride = WatchedItemStore::overrideDefaultInstance( $store );
-
-               WatchedItem::batchAddWatch( [ $itemOne, $itemTwo ] );
-
-               ScopedCallback::consume( $scopedOverride );
        }
 
 }