Make Revision::__construct work with bad page ID
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
index 566dc92..7ce43ed 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\Storage\BlobStoreFactory;
 use MediaWiki\Storage\RevisionStore;
 use MediaWiki\Storage\SqlBlobStore;
 use Wikimedia\Rdbms\IDatabase;
@@ -42,7 +43,7 @@ class RevisionTest extends MediaWikiTestCase {
                        ->method( 'getPrefixedText' )
                        ->will( $this->returnValue( 'RevisionTest' ) );
                $mock->expects( $this->any() )
-                       ->method( 'getDBKey' )
+                       ->method( 'getDBkey' )
                        ->will( $this->returnValue( 'RevisionTest' ) );
                $mock->expects( $this->any() )
                        ->method( 'getArticleID' )
@@ -75,6 +76,17 @@ class RevisionTest extends MediaWikiTestCase {
                $this->assertNull( $rev->getContent(), 'no content object should be available' );
        }
 
+       /**
+        * @covers Revision::__construct
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+        */
+       public function testConstructFromArrayWithBadPageId() {
+               MediaWiki\suppressWarnings();
+               $rev = new Revision( [ 'page' => 77777777 ] );
+               $this->assertSame( 77777777, $rev->getPage() );
+               MediaWiki\restoreWarnings();
+       }
+
        public function provideConstructFromArray_userSetAsExpected() {
                yield 'no user defaults to wgUser' => [
                        [
@@ -289,13 +301,24 @@ class RevisionTest extends MediaWikiTestCase {
                        ) );
 
                // Note override internal service, so RevisionStore uses it as well.
-               $this->setService( '_SqlBlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $row = (object)$arrayData;
                $rev = new Revision( $row, 0, $this->getMockTitle() );
                $assertions( $this, $rev );
        }
 
+       /**
+        * @covers Revision::__construct
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+        */
+       public function testConstructFromRowWithBadPageId() {
+               MediaWiki\suppressWarnings();
+               $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
+               $this->assertSame( 77777777, $rev->getPage() );
+               MediaWiki\restoreWarnings();
+       }
+
        public function provideGetRevisionText() {
                yield 'Generic test' => [
                        'This is a goat of revision text.',
@@ -435,6 +458,20 @@ class RevisionTest extends MediaWikiTestCase {
                return $blobStore;
        }
 
+       private function mockBlobStoreFactory( $blobStore ) {
+               /** @var LoadBalancer $lb */
+               $factory = $this->getMockBuilder( BlobStoreFactory::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+               $factory->expects( $this->any() )
+                       ->method( 'newBlobStore' )
+                       ->willReturn( $blobStore );
+               $factory->expects( $this->any() )
+                       ->method( 'newSqlBlobStore' )
+                       ->willReturn( $blobStore );
+               return $factory;
+       }
+
        /**
         * @return RevisionStore
         */
@@ -478,7 +515,7 @@ class RevisionTest extends MediaWikiTestCase {
        public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
                $blobStore = $this->getBlobStore();
                $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $this->testGetRevisionText( $expected, $rowData );
        }
@@ -518,7 +555,7 @@ class RevisionTest extends MediaWikiTestCase {
 
                $blobStore = $this->getBlobStore();
                $blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $this->testGetRevisionText( $expected, $rowData );
        }
@@ -548,7 +585,7 @@ class RevisionTest extends MediaWikiTestCase {
 
                $blobStore = $this->getBlobStore();
                $blobStore->setCompressBlobs( true );
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $row = new stdClass;
                $row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
@@ -693,7 +730,7 @@ class RevisionTest extends MediaWikiTestCase {
                        $blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
                }
 
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
                $this->assertSame(
                        $expected,
                        Revision::decompressRevisionText( $text, $flags )
@@ -802,7 +839,7 @@ class RevisionTest extends MediaWikiTestCase {
                        ->getMock();
 
                $blobStore = new SqlBlobStore( $lb, $cache );
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $this->assertSame(
                        'AAAABBAAA',