Merge "JavaScriptMinifier: Enable phpcs and fix violations"
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionTest.php
index 80257cc..73d69a5 100644 (file)
@@ -1,7 +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;
@@ -43,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' )
@@ -76,6 +80,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' => [
                        [
@@ -297,6 +312,17 @@ class RevisionTest extends MediaWikiTestCase {
                $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.',
@@ -1567,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() );
+       }
+
 }