SlotRecord:compute sha1 if empty.
authordaniel <dkinzler@wikimedia.org>
Thu, 4 Jul 2019 08:52:55 +0000 (10:52 +0200)
committerDaniel Kinzler <dkinzler@wikimedia.org>
Thu, 4 Jul 2019 08:57:17 +0000 (08:57 +0000)
The SHA1 should be computed automatically if empty, not just if it is
missing entirely. This is needed since rev_sha1 may contain an empty
string for old revisions.

Bug: T200653
Bug: T219816
Change-Id: Ia6870a828bc9661fb05085e36315a86483ec48c4

includes/Revision/SlotRecord.php
tests/phpunit/includes/Revision/SlotRecordTest.php

index 064f7a4..7465f79 100644 (file)
@@ -539,6 +539,11 @@ class SlotRecord {
                try {
                        $sha1 = $this->getStringField( 'content_sha1' );
                } catch ( IncompleteRevisionException $ex ) {
+                       $sha1 = null;
+               }
+
+               // Compute if missing. Missing could mean null or empty.
+               if ( $sha1 === null || $sha1 === '' ) {
                        $format = $this->hasField( 'format_name' )
                                ? $this->getStringField( 'format_name' )
                                : null;
index 1b6ff2a..6495967 100644 (file)
@@ -77,7 +77,7 @@ class SlotRecordTest extends MediaWikiTestCase {
                $this->assertFalse( $record->isInherited() );
                $this->assertSame( 'A', $record->getContent()->getText() );
                $this->assertSame( 1, $record->getSize() );
-               $this->assertNotNull( $record->getSha1() );
+               $this->assertNotEmpty( $record->getSha1() );
                $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
                $this->assertSame( 2, $record->getRevision() );
                $this->assertSame( 2, $record->getRevision() );
@@ -96,7 +96,7 @@ class SlotRecordTest extends MediaWikiTestCase {
                $this->assertFalse( $record->hasOrigin() );
                $this->assertSame( 'A', $record->getContent()->getText() );
                $this->assertSame( 1, $record->getSize() );
-               $this->assertNotNull( $record->getSha1() );
+               $this->assertNotEmpty( $record->getSha1() );
                $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() );
                $this->assertSame( 'myRole', $record->getRole() );
        }
@@ -177,6 +177,14 @@ class SlotRecordTest extends MediaWikiTestCase {
                $this->assertSame( $hash, $record->getSha1() );
        }
 
+       public function testHashComputed() {
+               $row = $this->makeRow();
+               $row->content_sha1 = '';
+
+               $rec = new SlotRecord( $row, new WikitextContent( 'A' ) );
+               $this->assertNotEmpty( $rec->getSha1() );
+       }
+
        public function testNewWithSuppressedContent() {
                $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) );
                $output = SlotRecord::newWithSuppressedContent( $input );