Revision: Handle all return values of Title::newFromId
authorSam Smith <me@samsmith.io>
Wed, 27 Dec 2017 14:17:04 +0000 (14:17 +0000)
committerSam Smith <me@samsmith.io>
Sun, 31 Dec 2017 20:44:18 +0000 (20:44 +0000)
In Revision::getKnownCurrent, fail early when Title::newFromId returns
null to avoid an uncatchable fatal error being triggered.

RevisionStore#getKnownCurrentRevision requires that the title parameter
be an instance of Title.

This follows on from Ia4c20a91.

Change-Id: I9bddafcc5df630d1dff1e2526194186cab7097e5

includes/Revision.php
tests/phpunit/includes/RevisionDbTestBase.php

index 8f36e88..b8b56a3 100644 (file)
@@ -1204,6 +1204,10 @@ class Revision implements IDBAccessObject {
                        ? $pageIdOrTitle
                        : Title::newFromID( $pageIdOrTitle );
 
+               if ( !$title ) {
+                       return false;
+               }
+
                $record = self::getRevisionStore()->getKnownCurrentRevision( $title, $revId );
                return $record ? new Revision( $record ) : false;
        }
index 9ab76c8..bd0dea6 100644 (file)
@@ -1363,6 +1363,24 @@ abstract class RevisionDbTestBase extends MediaWikiTestCase {
                $this->assertEquals( $rev->getId(), $cachedRow->rev_id );
        }
 
+       public function testNewKnownCurrent_withPageId() {
+               $db = wfGetDB( DB_MASTER );
+
+               $this->testPage->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ );
+               $rev = $this->testPage->getRevision();
+
+               $pageId = $this->testPage->getId();
+
+               $newRev = Revision::newKnownCurrent( $db, $pageId, $rev->getId() );
+               $this->assertRevEquals( $rev, $newRev );
+       }
+
+       public function testNewKnownCurrent_returnsFalseWhenTitleDoesntExist() {
+               $db = wfGetDB( DB_MASTER );
+
+               $this->assertFalse( Revision::newKnownCurrent( $db, 0 ) );
+       }
+
        public function provideUserCanBitfield() {
                yield [ 0, 0, [], null, true ];
                // Bitfields match, user has no permissions