Merge "Handle failure to load content in Revision getSize, etc"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 11 Jan 2018 14:50:00 +0000 (14:50 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 11 Jan 2018 14:50:00 +0000 (14:50 +0000)
includes/Revision.php
includes/Storage/RevisionArchiveRecord.php
includes/Storage/RevisionRecord.php
includes/Storage/RevisionStoreRecord.php
tests/phpunit/includes/RevisionTest.php

index b653f7f..66b4173 100644 (file)
@@ -608,20 +608,27 @@ class Revision implements IDBAccessObject {
        /**
         * Returns the length of the text in this revision, or null if unknown.
         *
-        * @return int
+        * @return int|null
         */
        public function getSize() {
-               return $this->mRecord->getSize();
+               try {
+                       return $this->mRecord->getSize();
+               } catch ( RevisionAccessException $ex ) {
+                       return null;
+               }
        }
 
        /**
         * Returns the base36 sha1 of the content in this revision, or null if unknown.
         *
-        * @return string
+        * @return string|null
         */
        public function getSha1() {
-               // XXX: we may want to drop all the hashing logic, it's not worth the overhead.
-               return $this->mRecord->getSha1();
+               try {
+                       return $this->mRecord->getSha1();
+               } catch ( RevisionAccessException $ex ) {
+                       return null;
+               }
        }
 
        /**
index 419cb95..9999179 100644 (file)
@@ -107,6 +107,7 @@ class RevisionArchiveRecord extends RevisionRecord {
        }
 
        /**
+        * @throws RevisionAccessException if the size was unknown and could not be calculated.
         * @return int The nominal revision size, never null. May be computed on the fly.
         */
        public function getSize() {
@@ -120,6 +121,7 @@ class RevisionArchiveRecord extends RevisionRecord {
        }
 
        /**
+        * @throws RevisionAccessException if the hash was unknown and could not be calculated.
         * @return string The revision hash, never null. May be computed on the fly.
         */
        public function getSha1() {
index f490f9b..8734f48 100644 (file)
@@ -242,6 +242,7 @@ abstract class RevisionRecord {
         *
         * MCR migration note: this replaces Revision::getSize
         *
+        * @throws RevisionAccessException if the size was unknown and could not be calculated.
         * @return int
         */
        abstract public function getSize();
@@ -254,6 +255,7 @@ abstract class RevisionRecord {
         *
         * MCR migration note: this replaces Revision::getSha1
         *
+        * @throws RevisionAccessException if the hash was unknown and could not be calculated.
         * @return string
         */
        abstract public function getSha1();
index 341855d..e8efcfa 100644 (file)
@@ -150,6 +150,7 @@ class RevisionStoreRecord extends RevisionRecord {
        }
 
        /**
+        * @throws RevisionAccessException if the size was unknown and could not be calculated.
         * @return string The nominal revision size, never null. May be computed on the fly.
         */
        public function getSize() {
@@ -163,6 +164,7 @@ class RevisionStoreRecord extends RevisionRecord {
        }
 
        /**
+        * @throws RevisionAccessException if the hash was unknown and could not be calculated.
         * @return string The revision hash, never null. May be computed on the fly.
         */
        public function getSha1() {
index 0db76ff..a467346 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;
@@ -1567,4 +1571,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() );
+       }
+
 }