Reset services before every test
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / PageUpdaterTest.php
index 24107b1..517e7c6 100644 (file)
@@ -7,6 +7,7 @@ use Content;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Storage\RevisionRecord;
 use MediaWikiTestCase;
+use ParserOptions;
 use RecentChange;
 use Revision;
 use TextContent;
@@ -18,7 +19,6 @@ use WikiPage;
  * @group Database
  */
 class PageUpdaterTest extends MediaWikiTestCase {
-
        private function getDummyTitle( $method ) {
                return Title::newFromText( $method, $this->getDefaultWikitextNS() );
        }
@@ -58,12 +58,9 @@ class PageUpdaterTest extends MediaWikiTestCase {
                $oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
 
                $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
-               $this->assertFalse( $updater->getBaseRevisionId(), 'getBaseRevisionId' );
+               $this->assertFalse( $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
                $this->assertSame( 0, $updater->getUndidRevisionId(), 'getUndidRevisionId' );
 
-               $updater->setBaseRevisionId( 0 );
-               $this->assertSame( 0, $updater->getBaseRevisionId(), 'getBaseRevisionId' );
-
                $updater->addTag( 'foo' );
                $updater->addTags( [ 'bar', 'qux' ] );
 
@@ -77,10 +74,12 @@ class PageUpdaterTest extends MediaWikiTestCase {
 
                $parent = $updater->grabParentRevision();
 
-               // TODO: test that hasEditConflict() grabs the parent revision
                $this->assertNull( $parent, 'getParentRevision' );
                $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
-               $this->assertFalse( $updater->hasEditConflict(), 'hasEditConflict' );
+
+               // TODO: test that hasEditConflict() grabs the parent revision
+               $this->assertFalse( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
+               $this->assertTrue( $updater->hasEditConflict( 1 ), 'hasEditConflict' );
 
                // TODO: test failure with EDIT_UPDATE
                // TODO: test EDIT_MINOR, EDIT_BOT, etc
@@ -158,10 +157,12 @@ class PageUpdaterTest extends MediaWikiTestCase {
 
                $oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
 
-               // TODO: test page update does not fail with mismatching base rev ID
-               $baseRev = $title->getLatestRevID( Title::GAID_FOR_UPDATE );
-               $updater->setBaseRevisionId( $baseRev );
-               $this->assertSame( $baseRev, $updater->getBaseRevisionId(), 'getBaseRevisionId' );
+               $updater->setOriginalRevisionId( 7 );
+               $this->assertSame( 7, $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
+
+               $this->assertFalse( $updater->hasEditConflict( $parentId ), 'hasEditConflict' );
+               $this->assertTrue( $updater->hasEditConflict( $parentId - 1 ), 'hasEditConflict' );
+               $this->assertTrue( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
 
                // TODO: MCR: test additional slots
                $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
@@ -216,6 +217,7 @@ class PageUpdaterTest extends MediaWikiTestCase {
 
                // check site stats - this asserts that derived data updates where run.
                $stats = $this->db->selectRow( 'site_stats', '*', '1=1' );
+               $this->assertNotNull( $stats, 'site_stats' );
                $this->assertSame( $oldStats->ss_total_pages + 0, (int)$stats->ss_total_pages );
                $this->assertSame( $oldStats->ss_total_edits + 2, (int)$stats->ss_total_edits );
        }
@@ -234,7 +236,7 @@ class PageUpdaterTest extends MediaWikiTestCase {
                $comment = CommentStoreComment::newUnsavedComment( $summary );
 
                if ( !$content instanceof Content ) {
-                       $content = new TextContent( $content === null ? $summary : $content );
+                       $content = new TextContent( $content ?? $summary );
                }
 
                $updater = $page->newPageUpdater( $user );
@@ -332,48 +334,6 @@ class PageUpdaterTest extends MediaWikiTestCase {
                $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
        }
 
-       /**
-        * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
-        * @covers \MediaWiki\Storage\PageUpdater::setBaseRevisionId()
-        */
-       public function testFailureOnBaseRevision() {
-               $user = $this->getTestUser()->getUser();
-
-               $title = $this->getDummyTitle( __METHOD__ );
-
-               // start editing non-existing page
-               $page = WikiPage::factory( $title );
-               $updater = $page->newPageUpdater( $user );
-
-               // update for base revision 7 should fail
-               $summary = CommentStoreComment::newUnsavedComment( 'udpate?!' );
-               $updater->setBaseRevisionId( 7 ); // expect page to exist
-               $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
-               $updater->saveRevision( $summary );
-               $status = $updater->getStatus();
-
-               $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
-               $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
-               $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
-               $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
-
-               // create the page
-               $this->createRevision( $page, __METHOD__ );
-
-               // update for base revision 0 should fail
-               $summary = CommentStoreComment::newUnsavedComment( 'create?!' );
-               $updater = $page->newPageUpdater( $user );
-               $updater->setBaseRevisionId( 0 ); // expect page to not exist
-               $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
-               $updater->saveRevision( $summary );
-               $status = $updater->getStatus();
-
-               $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
-               $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
-               $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
-               $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
-       }
-
        public function provideSetRcPatrolStatus( $patrolled ) {
                yield [ RecentChange::PRC_UNPATROLLED ];
                yield [ RecentChange::PRC_AUTOPATROLLED ];
@@ -527,4 +487,89 @@ class PageUpdaterTest extends MediaWikiTestCase {
                );
        }
 
+       public function provideMagicWords() {
+               yield 'PAGEID' => [
+                       'Test {{PAGEID}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return $rev->getPageId();
+                       }
+               ];
+
+               yield 'REVISIONID' => [
+                       'Test {{REVISIONID}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return $rev->getId();
+                       }
+               ];
+
+               yield 'REVISIONUSER' => [
+                       'Test {{REVISIONUSER}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return $rev->getUser()->getName();
+                       }
+               ];
+
+               yield 'REVISIONTIMESTAMP' => [
+                       'Test {{REVISIONTIMESTAMP}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return $rev->getTimestamp();
+                       }
+               ];
+
+               yield 'subst:REVISIONUSER' => [
+                       'Test {{subst:REVISIONUSER}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return $rev->getUser()->getName();
+                       }
+               ];
+
+               yield 'subst:PAGENAME' => [
+                       'Test {{subst:PAGENAME}} Test',
+                       function ( RevisionRecord $rev ) {
+                               return 'PageUpdaterTest::testMagicWords';
+                       }
+               ];
+       }
+
+       /**
+        * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
+        *
+        * Integration test for PageUpdater, DerivedPageDataUpdater, RevisionRenderer
+        * and RenderedRevision, that ensures that magic words depending on revision meta-data
+        * are handled correctly. Note that each magic word needs to be tested separately,
+        * to assert correct behavior for each "vary" flag in the ParserOutput.
+        *
+        * @dataProvider provideMagicWords
+        */
+       public function testMagicWords( $wikitext, $callback ) {
+               $user = $this->getTestUser()->getUser();
+
+               $title = $this->getDummyTitle( __METHOD__ . '-' . $this->getName() );
+               $page = WikiPage::factory( $title );
+               $updater = $page->newPageUpdater( $user );
+
+               $updater->setContent( 'main', new \WikitextContent( $wikitext ) );
+
+               $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
+               $rev = $updater->saveRevision( $summary, EDIT_NEW );
+
+               if ( !$rev ) {
+                       $this->fail( $updater->getStatus()->getWikiText() );
+               }
+
+               $expected = strval( $callback( $rev ) );
+
+               $cache = MediaWikiServices::getInstance()->getParserCache();
+               $output = $cache->get(
+                       $page,
+                       ParserOptions::newCanonical(
+                               'canonical'
+                       )
+               );
+
+               $this->assertNotNull( $output, 'ParserCache::get' );
+
+               $this->assertContains( $expected, $output->getText() );
+       }
+
 }