Merge "Localize namespaces to inh"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
index 01762b9..73d69a5 100644 (file)
@@ -1,6 +1,11 @@
 <?php
 
+use MediaWiki\Storage\BlobStoreFactory;
+use MediaWiki\Storage\MutableRevisionRecord;
+use MediaWiki\Storage\RevisionAccessException;
+use MediaWiki\Storage\RevisionRecord;
 use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\SlotRecord;
 use MediaWiki\Storage\SqlBlobStore;
 use Wikimedia\Rdbms\IDatabase;
 use Wikimedia\Rdbms\LoadBalancer;
@@ -42,7 +47,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' )
@@ -57,7 +62,7 @@ class RevisionTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideConstructFromArray
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         */
        public function testConstructFromArray( $rowArray ) {
                $rev = new Revision( $rowArray, 0, $this->getMockTitle() );
@@ -68,13 +73,24 @@ class RevisionTest extends MediaWikiTestCase {
 
        /**
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         */
        public function testConstructFromEmptyArray() {
                $rev = new Revision( [], 0, $this->getMockTitle() );
                $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' => [
                        [
@@ -106,7 +122,7 @@ class RevisionTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideConstructFromArray_userSetAsExpected
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         *
         * @param array $rowArray
         * @param mixed $expectedUserId null to expect the current wgUser ID
@@ -166,7 +182,7 @@ class RevisionTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideConstructFromArrayThrowsExceptions
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         */
        public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
                $this->setExpectedException(
@@ -179,7 +195,7 @@ class RevisionTest extends MediaWikiTestCase {
 
        /**
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         */
        public function testConstructFromNothing() {
                $this->setExpectedException(
@@ -265,7 +281,7 @@ class RevisionTest extends MediaWikiTestCase {
        /**
         * @dataProvider provideConstructFromRow
         * @covers Revision::__construct
-        * @covers RevisionStore::newMutableRevisionFromArray
+        * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
         */
        public function testConstructFromRow( array $arrayData, $assertions ) {
                $data = 'Hello goat.'; // needs to match model and format
@@ -289,13 +305,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 +462,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 +519,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 +559,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 +589,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 +734,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 +843,7 @@ class RevisionTest extends MediaWikiTestCase {
                        ->getMock();
 
                $blobStore = new SqlBlobStore( $lb, $cache );
-               $this->setService( 'BlobStore', $blobStore );
+               $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
 
                $this->assertSame(
                        'AAAABBAAA',
@@ -1552,4 +1593,102 @@ class RevisionTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * @covers Revision::getSize
+        */
+       public function testGetSize() {
+               $title = $this->getMockTitle();
+
+               $rec = new MutableRevisionRecord( $title );
+               $rev = new Revision( $rec, 0, $title );
+
+               $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
+
+               $rec->setSize( 13 );
+               $this->assertSame( 13, $rev->getSize() );
+       }
+
+       /**
+        * @covers Revision::getSize
+        */
+       public function testGetSize_failure() {
+               $title = $this->getMockTitle();
+
+               $rec = $this->getMockBuilder( RevisionRecord::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $rec->method( 'getSize' )
+                       ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+               $rev = new Revision( $rec, 0, $title );
+               $this->assertNull( $rev->getSize() );
+       }
+
+       /**
+        * @covers Revision::getSha1
+        */
+       public function testGetSha1() {
+               $title = $this->getMockTitle();
+
+               $rec = new MutableRevisionRecord( $title );
+               $rev = new Revision( $rec, 0, $title );
+
+               $emptyHash = SlotRecord::base36Sha1( '' );
+               $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
+
+               $rec->setSha1( 'deadbeef' );
+               $this->assertSame( 'deadbeef', $rev->getSha1() );
+       }
+
+       /**
+        * @covers Revision::getSha1
+        */
+       public function testGetSha1_failure() {
+               $title = $this->getMockTitle();
+
+               $rec = $this->getMockBuilder( RevisionRecord::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $rec->method( 'getSha1' )
+                       ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+               $rev = new Revision( $rec, 0, $title );
+               $this->assertNull( $rev->getSha1() );
+       }
+
+       /**
+        * @covers Revision::getContent
+        */
+       public function testGetContent() {
+               $title = $this->getMockTitle();
+
+               $rec = new MutableRevisionRecord( $title );
+               $rev = new Revision( $rec, 0, $title );
+
+               $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
+
+               $content = new TextContent( 'Hello Kittens!' );
+               $rec->setContent( 'main', $content );
+               $this->assertSame( $content, $rev->getContent() );
+       }
+
+       /**
+        * @covers Revision::getContent
+        */
+       public function testGetContent_failure() {
+               $title = $this->getMockTitle();
+
+               $rec = $this->getMockBuilder( RevisionRecord::class )
+                       ->disableOriginalConstructor()
+                       ->getMock();
+
+               $rec->method( 'getContent' )
+                       ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+               $rev = new Revision( $rec, 0, $title );
+               $this->assertNull( $rev->getContent() );
+       }
+
 }