Merge "Allow partially blocked users to import images"
[lhc/web/wiklou.git] / tests / phpunit / includes / Revision / RevisionStoreTest.php
index efc2952..83872e3 100644 (file)
@@ -12,15 +12,20 @@ use MediaWiki\Revision\RevisionStore;
 use MediaWiki\Revision\SlotRoleRegistry;
 use MediaWiki\Revision\SlotRecord;
 use MediaWiki\Storage\SqlBlobStore;
+use Wikimedia\Rdbms\ILoadBalancer;
+use Wikimedia\Rdbms\MaintainableDBConnRef;
 use MediaWikiTestCase;
 use MWException;
 use Title;
 use WANObjectCache;
-use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
 use Wikimedia\TestingAccessWrapper;
 use WikitextContent;
 
+/**
+ * Tests RevisionStore
+ */
 class RevisionStoreTest extends MediaWikiTestCase {
 
        private function useTextId() {
@@ -67,13 +72,24 @@ class RevisionStoreTest extends MediaWikiTestCase {
        }
 
        /**
-        * @return \PHPUnit_Framework_MockObject_MockObject|Database
+        * @return \PHPUnit_Framework_MockObject_MockObject|IDatabase
         */
        private function getMockDatabase() {
-               return $this->getMockBuilder( Database::class )
+               return $this->getMockBuilder( IDatabase::class )
                        ->disableOriginalConstructor()->getMock();
        }
 
+       /**
+        * @param ILoadBalancer $mockLoadBalancer
+        * @param Database $db
+        * @return callable
+        */
+       private function getMockDBConnRefCallback( ILoadBalancer $mockLoadBalancer, IDatabase $db ) {
+               return function ( $i, $g, $domain, $flg ) use ( $mockLoadBalancer, $db ) {
+                       return new MaintainableDBConnRef( $mockLoadBalancer, $db, $i );
+               };
+       }
+
        /**
         * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore
         */
@@ -146,16 +162,23 @@ class RevisionStoreTest extends MediaWikiTestCase {
                $this->assertSame( $contentHandlerDb, $store->getContentHandlerUseDB() );
        }
 
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::getTitle
+        */
        public function testGetTitle_successFromPageId() {
                $mockLoadBalancer = $this->getMockLoadBalancer();
                // Title calls wfGetDB() so we have to set the main service
                $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
 
                $db = $this->getMockDatabase();
-               // Title calls wfGetDB() which uses a regular Connection
+               // RevisionStore uses getConnectionRef
+               $mockLoadBalancer->expects( $this->any() )
+                       ->method( 'getConnectionRef' )
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
                $mockLoadBalancer->expects( $this->atLeastOnce() )
-                       ->method( 'getConnection' )
-                       ->willReturn( $db );
+                       ->method( 'getMaintenanceConnectionRef' )
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
 
                // First call to Title::newFromID, faking no result (db lag?)
                $db->expects( $this->at( 0 ) )
@@ -177,21 +200,24 @@ class RevisionStoreTest extends MediaWikiTestCase {
                $this->assertSame( 'Food', $title->getDBkey() );
        }
 
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::getTitle
+        */
        public function testGetTitle_successFromPageIdOnFallback() {
                $mockLoadBalancer = $this->getMockLoadBalancer();
                // Title calls wfGetDB() so we have to set the main service
                $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
 
                $db = $this->getMockDatabase();
-               // Title calls wfGetDB() which uses a regular Connection
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
                // Assert that the first call uses a REPLICA and the second falls back to master
-               $mockLoadBalancer->expects( $this->exactly( 2 ) )
-                       ->method( 'getConnection' )
-                       ->willReturn( $db );
-               // RevisionStore getTitle uses a ConnectionRef
                $mockLoadBalancer->expects( $this->atLeastOnce() )
                        ->method( 'getConnectionRef' )
-                       ->willReturn( $db );
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
+               $mockLoadBalancer->expects( $this->exactly( 2 ) )
+                       ->method( 'getMaintenanceConnectionRef' )
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
 
                // First call to Title::newFromID, faking no result (db lag?)
                $db->expects( $this->at( 0 ) )
@@ -233,20 +259,23 @@ class RevisionStoreTest extends MediaWikiTestCase {
                $this->assertSame( 'Foodey', $title->getDBkey() );
        }
 
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::getTitle
+        */
        public function testGetTitle_successFromRevId() {
                $mockLoadBalancer = $this->getMockLoadBalancer();
                // Title calls wfGetDB() so we have to set the main service
                $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
 
                $db = $this->getMockDatabase();
-               // Title calls wfGetDB() which uses a regular Connection
-               $mockLoadBalancer->expects( $this->atLeastOnce() )
-                       ->method( 'getConnection' )
-                       ->willReturn( $db );
-               // RevisionStore getTitle uses a ConnectionRef
                $mockLoadBalancer->expects( $this->atLeastOnce() )
                        ->method( 'getConnectionRef' )
-                       ->willReturn( $db );
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
+               // RevisionStore getTitle uses getMaintenanceConnectionRef
+               $mockLoadBalancer->expects( $this->atLeastOnce() )
+                       ->method( 'getMaintenanceConnectionRef' )
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
 
                // First call to Title::newFromID, faking no result (db lag?)
                $db->expects( $this->at( 0 ) )
@@ -278,21 +307,24 @@ class RevisionStoreTest extends MediaWikiTestCase {
                $this->assertSame( 'Food2', $title->getDBkey() );
        }
 
+       /**
+        * @covers \MediaWiki\Revision\RevisionStore::getTitle
+        */
        public function testGetTitle_successFromRevIdOnFallback() {
                $mockLoadBalancer = $this->getMockLoadBalancer();
                // Title calls wfGetDB() so we have to set the main service
                $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
 
                $db = $this->getMockDatabase();
-               // Title calls wfGetDB() which uses a regular Connection
                // Assert that the first call uses a REPLICA and the second falls back to master
-               $mockLoadBalancer->expects( $this->exactly( 2 ) )
-                       ->method( 'getConnection' )
-                       ->willReturn( $db );
-               // RevisionStore getTitle uses a ConnectionRef
+               // RevisionStore uses getMaintenanceConnectionRef
                $mockLoadBalancer->expects( $this->atLeastOnce() )
                        ->method( 'getConnectionRef' )
-                       ->willReturn( $db );
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
+               $mockLoadBalancer->expects( $this->exactly( 2 ) )
+                       ->method( 'getMaintenanceConnectionRef' )
+                       ->willReturnCallback( $this->getMockDBConnRefCallback( $mockLoadBalancer, $db ) );
 
                // First call to Title::newFromID, faking no result (db lag?)
                $db->expects( $this->at( 0 ) )
@@ -353,12 +385,14 @@ class RevisionStoreTest extends MediaWikiTestCase {
                $this->setService( 'DBLoadBalancer', $mockLoadBalancer );
 
                $db = $this->getMockDatabase();
-               // Title calls wfGetDB() which uses a regular Connection
+               // Title calls wfGetDB() which uses getMaintenanceConnectionRef
                // Assert that the first call uses a REPLICA and the second falls back to master
 
                // RevisionStore getTitle uses getConnectionRef
-               // Title::newFromID uses getConnection
-               foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) {
+               // Title::newFromID uses getMaintenanceConnectionRef
+               foreach ( [
+                       'getConnectionRef', 'getMaintenanceConnectionRef'
+               ] as $method ) {
                        $mockLoadBalancer->expects( $this->exactly( 2 ) )
                                ->method( $method )
                                ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) {
@@ -435,9 +469,12 @@ class RevisionStoreTest extends MediaWikiTestCase {
                }
 
                $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
-               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+               $services = MediaWikiServices::getInstance();
+               $lb = $services->getDBLoadBalancer();
+               $access = $services->getExternalStoreAccess();
+
+               $blobStore = new SqlBlobStore( $lb, $access, $cache );
 
-               $blobStore = new SqlBlobStore( $lb, $cache );
                $blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) );
 
                $store = $this->getRevisionStore( $lb, $blobStore, $cache );
@@ -465,9 +502,11 @@ class RevisionStoreTest extends MediaWikiTestCase {
                ];
 
                $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] );
-               $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
+               $services = MediaWikiServices::getInstance();
+               $lb = $services->getDBLoadBalancer();
+               $access = $services->getExternalStoreAccess();
 
-               $blobStore = new SqlBlobStore( $lb, $cache );
+               $blobStore = new SqlBlobStore( $lb, $access, $cache );
                $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) );
 
                $store = $this->getRevisionStore( $lb, $blobStore, $cache );
@@ -513,12 +552,10 @@ class RevisionStoreTest extends MediaWikiTestCase {
                                'old_text' => 'Hello World',
                                'old_flags' => 'utf-8',
                        ];
-               } else {
-                       if ( !isset( $row['content'] ) && isset( $array['old_text'] ) ) {
-                               $row['content'] = [
-                                       'main' => new WikitextContent( $array['old_text'] ),
-                               ];
-                       }
+               } elseif ( !isset( $row['content'] ) && isset( $array['old_text'] ) ) {
+                       $row['content'] = [
+                               'main' => new WikitextContent( $array['old_text'] ),
+                       ];
                }
 
                return (object)$row;