Namespace LinkTarget under MediaWiki\Linker
[lhc/web/wiklou.git] / tests / phpunit / includes / WatchedItemStoreUnitTest.php
index 78231c2..d9fd4de 100644 (file)
@@ -1,11 +1,12 @@
 <?php
+use MediaWiki\Linker\LinkTarget;
 
 /**
  * @author Addshore
  *
  * @covers WatchedItemStore
  */
-class WatchedItemStoreUnitTest extends MediaWikiTestCase {
+class WatchedItemStoreUnitTest extends PHPUnit_Framework_TestCase {
 
        /**
         * @return PHPUnit_Framework_MockObject_MockObject|IDatabase
@@ -17,7 +18,11 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
        /**
         * @return PHPUnit_Framework_MockObject_MockObject|LoadBalancer
         */
-       private function getMockLoadBalancer( $mockDb, $expectedConnectionType = null ) {
+       private function getMockLoadBalancer(
+               $mockDb,
+               $expectedConnectionType = null,
+               $readOnlyReason = false
+       ) {
                $mock = $this->getMockBuilder( LoadBalancer::class )
                        ->disableOriginalConstructor()
                        ->getMock();
@@ -33,7 +38,7 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                }
                $mock->expects( $this->any() )
                        ->method( 'getReadOnlyReason' )
-                       ->will( $this->returnValue( false ) );
+                       ->will( $this->returnValue( $readOnlyReason ) );
                return $mock;
        }
 
@@ -95,6 +100,17 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                $this->assertSame( $instanceOne, $instanceTwo );
        }
 
+       public function testOverrideDefaultInstance() {
+               $instance = WatchedItemStore::getDefaultInstance();
+               $scopedOverride = $instance->overrideDefaultInstance( null );
+
+               $this->assertNotSame( $instance, WatchedItemStore::getDefaultInstance() );
+
+               unset( $scopedOverride );
+
+               $this->assertSame( $instance, WatchedItemStore::getDefaultInstance() );
+       }
+
        public function testCountWatchedItems() {
                $user = $this->getMockNonAnonUserWithId( 1 );
 
@@ -798,7 +814,20 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                );
        }
 
-       public function testDuplicateAllAssociatedEntries_somethingToDuplicate() {
+       public function provideLinkTargetPairs() {
+               return [
+                       [ Title::newFromText( 'Old_Title' ), Title::newFromText( 'New_Title' ) ],
+                       [ new TitleValue( 0, 'Old_Title' ),  new TitleValue( 0, 'New_Title' ) ],
+               ];
+       }
+
+       /**
+        * @dataProvider provideLinkTargetPairs
+        */
+       public function testDuplicateAllAssociatedEntries_somethingToDuplicate(
+               LinkTarget $oldTarget,
+               LinkTarget $newTarget
+       ) {
                $fakeRows = [
                        $this->getFakeRow( [ 'wl_user' => 1, 'wl_notificationtimestamp' => '20151212010101' ] ),
                ];
@@ -813,8 +842,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                        'wl_notificationtimestamp',
                                ],
                                [
-                                       'wl_namespace' => 0,
-                                       'wl_title' => 'Old_Title',
+                                       'wl_namespace' => $oldTarget->getNamespace(),
+                                       'wl_title' => $oldTarget->getDBkey(),
                                ]
                        )
                        ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
@@ -826,8 +855,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                [
                                        [
                                                'wl_user' => 1,
-                                               'wl_namespace' => 0,
-                                               'wl_title' => 'New_Title',
+                                               'wl_namespace' => $newTarget->getNamespace(),
+                                               'wl_title' => $newTarget->getDBkey(),
                                                'wl_notificationtimestamp' => '20151212010101',
                                        ],
                                ],
@@ -842,8 +871,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                        'wl_notificationtimestamp',
                                ],
                                [
-                                       'wl_namespace' => 1,
-                                       'wl_title' => 'Old_Title',
+                                       'wl_namespace' => $oldTarget->getNamespace() + 1,
+                                       'wl_title' => $oldTarget->getDBkey(),
                                ]
                        )
                        ->will( $this->returnValue( new FakeResultWrapper( $fakeRows ) ) );
@@ -855,8 +884,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                                [
                                        [
                                                'wl_user' => 1,
-                                               'wl_namespace' => 1,
-                                               'wl_title' => 'New_Title',
+                                               'wl_namespace' => $newTarget->getNamespace() + 1,
+                                               'wl_title' => $newTarget->getDBkey(),
                                                'wl_notificationtimestamp' => '20151212010101',
                                        ],
                                ],
@@ -873,8 +902,8 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                );
 
                $store->duplicateAllAssociatedEntries(
-                       Title::newFromText( 'Old_Title' ),
-                       Title::newFromText( 'New_Title' )
+                       $oldTarget,
+                       $newTarget
                );
        }
 
@@ -930,6 +959,20 @@ class WatchedItemStoreUnitTest extends MediaWikiTestCase {
                );
        }
 
+       public function testAddWatchBatchForUser_readOnlyDBReturnsFalse() {
+               $store = $this->newWatchedItemStore(
+                       $this->getMockLoadBalancer( $this->getMockDb(), null, 'Some Reason' ),
+                       $this->getMockCache()
+               );
+
+               $this->assertFalse(
+                       $store->addWatchBatchForUser(
+                               $this->getMockNonAnonUserWithId( 1 ),
+                               [ new TitleValue( 0, 'Some_Page' ), new TitleValue( 1, 'Some_Page' ) ]
+                       )
+               );
+       }
+
        public function testAddWatchBatchForUser_nonAnonymousUser() {
                $mockDb = $this->getMockDb();
                $mockDb->expects( $this->once() )