Add tests for Revision::getTitle
[lhc/web/wiklou.git] / tests / phpunit / includes / RevisionIntegrationTest.php
index ac7331a..ad211c6 100644 (file)
@@ -152,24 +152,75 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
        }
 
        /**
-        * @covers Revision::__construct
+        * @covers Revision::insertOn
         */
-       public function testConstructFromRow() {
-               $latestRevisionId = $this->testPage->getLatest();
-               $latestRevision = $this->testPage->getRevision();
+       public function testInsertOn_success() {
+               $parentId = $this->testPage->getLatest();
 
-               $dbr = wfGetDB( DB_REPLICA );
-               $res = $dbr->select(
+               // If an ExternalStore is set don't use it.
+               $this->setMwGlobals( 'wgDefaultExternalStore', false );
+
+               $rev = new Revision( [
+                       'page' => $this->testPage->getId(),
+                       'title' => $this->testPage->getTitle(),
+                       'text' => 'Revision Text',
+                       'comment' => 'Revision comment',
+               ] );
+
+               $revId = $rev->insertOn( wfGetDB( DB_MASTER ) );
+
+               $this->assertInternalType( 'integer', $revId );
+               $this->assertInternalType( 'integer', $rev->getTextId() );
+               $this->assertSame( $revId, $rev->getId() );
+
+               $this->assertSelect(
+                       'text',
+                       [ 'old_id', 'old_text' ],
+                       "old_id = {$rev->getTextId()}",
+                       [ [ strval( $rev->getTextId() ), 'Revision Text' ] ]
+               );
+               $this->assertSelect(
                        'revision',
-                       Revision::selectFields(),
-                       [ 'rev_id' => $latestRevisionId ]
+                       [
+                               'rev_id',
+                               'rev_page',
+                               'rev_text_id',
+                               'rev_user',
+                               'rev_minor_edit',
+                               'rev_deleted',
+                               'rev_len',
+                               'rev_parent_id',
+                               'rev_sha1',
+                       ],
+                       "rev_id = {$rev->getId()}",
+                       [ [
+                               strval( $rev->getId() ),
+                               strval( $this->testPage->getId() ),
+                               strval( $rev->getTextId() ),
+                               '0',
+                               '0',
+                               '0',
+                               '13',
+                               strval( $parentId ),
+                               's0ngbdoxagreuf2vjtuxzwdz64n29xm',
+                       ] ]
                );
-               $this->assertTrue( is_object( $res ), 'query failed' );
+       }
 
-               $row = $res->fetchObject();
-               $res->free();
+       /**
+        * @covers Revision::insertOn
+        */
+       public function testInsertOn_exceptionOnNoPage() {
+               // If an ExternalStore is set don't use it.
+               $this->setMwGlobals( 'wgDefaultExternalStore', false );
+               $this->setExpectedException(
+                       MWException::class,
+                       "Cannot insert revision: page ID must be nonzero"
+               );
 
-               $this->assertRevEquals( $latestRevision, new Revision( $row ) );
+               $rev = new Revision( [] );
+
+               $rev->insertOn( wfGetDB( DB_MASTER ) );
        }
 
        /**
@@ -214,7 +265,9 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
                $orig = $this->makeRevisionWithProps();
 
                $dbr = wfGetDB( DB_REPLICA );
-               $res = $dbr->select( 'revision', Revision::selectFields(), [ 'rev_id' => $orig->getId() ] );
+               $revQuery = Revision::getQueryInfo();
+               $res = $dbr->select( $revQuery['tables'], $revQuery['fields'], [ 'rev_id' => $orig->getId() ],
+                  __METHOD__, [], $revQuery['joins'] );
                $this->assertTrue( is_object( $res ), 'query failed' );
 
                $row = $res->fetchObject();
@@ -225,16 +278,52 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
                $this->assertRevEquals( $orig, $rev );
        }
 
-       public function provideTrueFalse() {
-               yield [ true ];
-               yield [ false ];
+       public function provideNewFromArchiveRow() {
+               yield [
+                       true,
+                       function ( $f ) {
+                               return $f;
+                       },
+               ];
+               yield [
+                       false,
+                       function ( $f ) {
+                               return $f;
+                       },
+               ];
+               yield [
+                       true,
+                       function ( $f ) {
+                               return $f + [ 'ar_namespace', 'ar_title' ];
+                       },
+               ];
+               yield [
+                       false,
+                       function ( $f ) {
+                               return $f + [ 'ar_namespace', 'ar_title' ];
+                       },
+               ];
+               yield [
+                       true,
+                       function ( $f ) {
+                               unset( $f['ar_text_id'] );
+                               return $f;
+                       },
+               ];
+               yield [
+                       false,
+                       function ( $f ) {
+                               unset( $f['ar_text_id'] );
+                               return $f;
+                       },
+               ];
        }
 
        /**
-        * @dataProvider provideTrueFalse
+        * @dataProvider provideNewFromArchiveRow
         * @covers Revision::newFromArchiveRow
         */
-       public function testNewFromArchiveRow( $contentHandlerUseDB ) {
+       public function testNewFromArchiveRow( $contentHandlerUseDB, $selectModifier ) {
                $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
 
                $page = $this->createPage(
@@ -246,8 +335,11 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
                $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
 
                $dbr = wfGetDB( DB_REPLICA );
+               $arQuery = Revision::getArchiveQueryInfo();
+               $arQuery['fields'] = $selectModifier( $arQuery['fields'] );
                $res = $dbr->select(
-                       'archive', Revision::selectArchiveFields(), [ 'ar_rev_id' => $orig->getId() ]
+                       $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
+                       __METHOD__, [], $arQuery['joins']
                );
                $this->assertTrue( is_object( $res ), 'query failed' );
 
@@ -259,6 +351,35 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
                $this->assertRevEquals( $orig, $rev );
        }
 
+       /**
+        * @covers Revision::newFromArchiveRow
+        */
+       public function testNewFromArchiveRowOverrides() {
+               $page = $this->createPage(
+                       'RevisionStorageTest_testNewFromArchiveRow',
+                       'Lorem Ipsum',
+                       CONTENT_MODEL_WIKITEXT
+               );
+               $orig = $page->getRevision();
+               $page->doDeleteArticle( 'test Revision::newFromArchiveRow' );
+
+               $dbr = wfGetDB( DB_REPLICA );
+               $arQuery = Revision::getArchiveQueryInfo();
+               $res = $dbr->select(
+                       $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ],
+                       __METHOD__, [], $arQuery['joins']
+               );
+               $this->assertTrue( is_object( $res ), 'query failed' );
+
+               $row = $res->fetchObject();
+               $res->free();
+
+               $rev = Revision::newFromArchiveRow( $row, [ 'comment' => 'SOMEOVERRIDE' ] );
+
+               $this->assertNotEquals( $orig->getComment(), $rev->getComment() );
+               $this->assertEquals( 'SOMEOVERRIDE', $rev->getComment() );
+       }
+
        /**
         * @covers Revision::newFromId
         */
@@ -870,4 +991,90 @@ class RevisionIntegrationTest extends MediaWikiTestCase {
                );
        }
 
+       /**
+        * @covers Revision::getParentLengths
+        */
+       public function testGetParentLengths_noRevIds() {
+               $this->assertSame(
+                       [],
+                       Revision::getParentLengths(
+                               wfGetDB( DB_MASTER ),
+                               []
+                       )
+               );
+       }
+
+       /**
+        * @covers Revision::getParentLengths
+        */
+       public function testGetParentLengths_oneRevId() {
+               $text = '831jr091jr0921kr21kr0921kjr0921j09rj1';
+               $textLength = strlen( $text );
+
+               $this->testPage->doEditContent( new WikitextContent( $text ), __METHOD__ );
+               $rev[1] = $this->testPage->getLatest();
+
+               $this->assertSame(
+                       [ $rev[1] => strval( $textLength ) ],
+                       Revision::getParentLengths(
+                               wfGetDB( DB_MASTER ),
+                               [ $rev[1] ]
+                       )
+               );
+       }
+
+       /**
+        * @covers Revision::getParentLengths
+        */
+       public function testGetParentLengths_multipleRevIds() {
+               $textOne = '831jr091jr0921kr21kr0921kjr0921j09rj1';
+               $textOneLength = strlen( $textOne );
+               $textTwo = '831jr091jr092121j09rj1';
+               $textTwoLength = strlen( $textTwo );
+
+               $this->testPage->doEditContent( new WikitextContent( $textOne ), __METHOD__ );
+               $rev[1] = $this->testPage->getLatest();
+               $this->testPage->doEditContent( new WikitextContent( $textTwo ), __METHOD__ );
+               $rev[2] = $this->testPage->getLatest();
+
+               $this->assertSame(
+                       [ $rev[1] => strval( $textOneLength ), $rev[2] => strval( $textTwoLength ) ],
+                       Revision::getParentLengths(
+                               wfGetDB( DB_MASTER ),
+                               [ $rev[1], $rev[2] ]
+                       )
+               );
+       }
+
+       /**
+        * @covers Revision::getTitle
+        */
+       public function testGetTitle_fromExistingRevision() {
+               $this->assertTrue(
+                       $this->testPage->getTitle()->equals(
+                               $this->testPage->getRevision()->getTitle()
+                       )
+               );
+       }
+
+       /**
+        * @covers Revision::getTitle
+        */
+       public function testGetTitle_fromRevisionWhichWillLoadTheTitle() {
+               $rev = new Revision( [ 'id' => $this->testPage->getLatest() ] );
+               $this->assertTrue(
+                       $this->testPage->getTitle()->equals(
+                               $rev->getTitle()
+                       )
+               );
+       }
+
+       /**
+        * @covers Revision::getTitle
+        */
+       public function testGetTitle_forBadRevision() {
+               $rev = new Revision( [] );
+               $this->assertNull( $rev->getTitle() );
+       }
+
 }