Merge "Follow-up 267d99fa8: Fix RELEASE-NOTES-1.32 line length"
[lhc/web/wiklou.git] / tests / phpunit / includes / page / WikiPageDbTestBase.php
index 63cf02f..aaaa73b 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\MediaWikiServices;
 use MediaWiki\Storage\RevisionSlotsUpdate;
 use Wikimedia\TestingAccessWrapper;
 
@@ -103,7 +104,7 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
        /**
         * @param string|Title|WikiPage $page
         * @param string $text
-        * @param int $model
+        * @param int|null $model
         *
         * @return WikiPage
         */
@@ -248,6 +249,8 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                        CONTENT_MODEL_WIKITEXT
                );
 
+               $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user1 );
+
                $status = $page->doEditContent( $content, "[[testing]] 1", EDIT_NEW, false, $user1 );
 
                $this->assertTrue( $status->isOK(), 'OK' );
@@ -258,9 +261,14 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                $this->assertTrue( $status->value['revision']->getContent()->equals( $content ), 'equals' );
 
                $rev = $page->getRevision();
+               $preparedEditAfter = $page->prepareContentForEdit( $content, $rev, $user1 );
+
                $this->assertNotNull( $rev->getRecentChange() );
                $this->assertSame( $rev->getId(), (int)$rev->getRecentChange()->getAttribute( 'rc_this_oldid' ) );
 
+               // make sure that cached ParserOutput gets re-used throughout
+               $this->assertSame( $preparedEditBefore->output, $preparedEditAfter->output );
+
                $id = $page->getId();
 
                // Test page creation logging
@@ -341,6 +349,26 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                $this->assertEquals( 2, $n, 'pagelinks should contain two links from the page' );
        }
 
+       /**
+        * @covers WikiPage::doEditContent
+        */
+       public function testDoEditContent_twice() {
+               $title = Title::newFromText( __METHOD__ );
+               $page = WikiPage::factory( $title );
+               $content = ContentHandler::makeContent( '$1 van $2', $title );
+
+               // Make sure we can do the exact same save twice.
+               // This tests checks that internal caches are reset as appropriate.
+               $status1 = $page->doEditContent( $content, __METHOD__ );
+               $status2 = $page->doEditContent( $content, __METHOD__ );
+
+               $this->assertTrue( $status1->isOK(), 'OK' );
+               $this->assertTrue( $status2->isOK(), 'OK' );
+
+               $this->assertTrue( isset( $status1->value['revision'] ), 'OK' );
+               $this->assertFalse( isset( $status2->value['revision'] ), 'OK' );
+       }
+
        /**
         * Undeletion is covered in PageArchiveTest::testUndeleteRevisions()
         * TODO: Revision deletion
@@ -1028,6 +1056,9 @@ more stuff
                // Use the confirmed group for user2 to make sure the user is different
                $user2 = $this->getTestUser( [ 'confirmed' ] )->getUser();
 
+               // make sure we can test autopatrolling
+               $this->setMwGlobals( 'wgUseRCPatrol', true );
+
                // TODO: MCR: test rollback of multiple slots!
                $page = $this->newPage( __METHOD__ );
 
@@ -1085,61 +1116,21 @@ more stuff
                        "rollback did not revert to the correct revision" );
                $this->assertEquals( "one\n\ntwo", $page->getContent()->getNativeData() );
 
-               // TODO: MCR: assert origin once we write slot data
-               // $mainSlot = $page->getRevision()->getRevisionRecord()->getSlot( 'main' );
-               // $this->assertTrue( $mainSlot->isInherited(), 'isInherited' );
-               // $this->assertSame( $rev2->getId(), $mainSlot->getOrigin(), 'getOrigin' );
-       }
-
-       /**
-        * @covers WikiPage::doRollback
-        * @covers WikiPage::commitRollback
-        */
-       public function testDoRollback_simple() {
-               $admin = $this->getTestSysop()->getUser();
-
-               $text = "one";
-               $page = $this->newPage( __METHOD__ );
-               $page->doEditContent(
-                       ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
-                       "section one",
-                       EDIT_NEW,
-                       false,
-                       $admin
+               $rc = MediaWikiServices::getInstance()->getRevisionStore()->getRecentChange(
+                       $page->getRevision()->getRevisionRecord()
                );
-               $rev1 = $page->getRevision();
 
-               $user1 = $this->getTestUser()->getUser();
-               $text .= "\n\ntwo";
-               $page = new WikiPage( $page->getTitle() );
-               $page->doEditContent(
-                       ContentHandler::makeContent( $text, $page->getTitle(), CONTENT_MODEL_WIKITEXT ),
-                       "adding section two",
-                       0,
-                       false,
-                       $user1
-               );
-
-               # now, try the rollback
-               $token = $admin->getEditToken( 'rollback' );
-               $errors = $page->doRollback(
-                       $user1->getName(),
-                       "testing revert",
-                       $token,
-                       false,
-                       $details,
-                       $admin
+               $this->assertNotNull( $rc, 'RecentChanges entry' );
+               $this->assertEquals(
+                       RecentChange::PRC_AUTOPATROLLED,
+                       $rc->getAttribute( 'rc_patrolled' ),
+                       'rc_patrolled'
                );
 
-               if ( $errors ) {
-                       $this->fail( "Rollback failed:\n" . print_r( $errors, true )
-                               . ";\n" . print_r( $details, true ) );
-               }
-
-               $page = new WikiPage( $page->getTitle() );
-               $this->assertEquals( $rev1->getSha1(), $page->getRevision()->getSha1(),
-                       "rollback did not revert to the correct revision" );
-               $this->assertEquals( "one", $page->getContent()->getNativeData() );
+               // TODO: MCR: assert origin once we write slot data
+               // $mainSlot = $page->getRevision()->getRevisionRecord()->getSlot( 'main' );
+               // $this->assertTrue( $mainSlot->isInherited(), 'isInherited' );
+               // $this->assertSame( $rev2->getId(), $mainSlot->getOrigin(), 'getOrigin' );
        }
 
        /**
@@ -2314,14 +2305,25 @@ more stuff
                        ->method( 'getParserOutput' )
                        ->willReturn( new ParserOutput( 'HTML' ) );
 
-               $updater = $page->newPageUpdater( $user );
+               $preparedEditBefore = $page->prepareContentForEdit( $content, null, $user );
+
+               // provide context, so the cache can be kept in place
+               $slotsUpdate = new revisionSlotsUpdate();
+               $slotsUpdate->modifyContent( 'main', $content );
+
+               $updater = $page->newPageUpdater( $user, $slotsUpdate );
                $updater->setContent( 'main', $content );
                $revision = $updater->saveRevision(
                        CommentStoreComment::newUnsavedComment( 'test' ),
                        EDIT_NEW
                );
 
+               $preparedEditAfter = $page->prepareContentForEdit( $content, $revision, $user );
+
                $this->assertSame( $revision->getId(), $page->getLatest() );
+
+               // Parsed output must remain cached throughout.
+               $this->assertSame( $preparedEditBefore->output, $preparedEditAfter->output );
        }
 
        /**
@@ -2347,7 +2349,7 @@ more stuff
 
                $updater1->prepareUpdate( $revision );
 
-               // Re-use updater with same revision or content
+               // Re-use updater with same revision or content, even if base changed
                $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, $revision ) );
 
                $slotsUpdate = RevisionSlotsUpdate::newFromContent(
@@ -2355,6 +2357,12 @@ more stuff
                );
                $this->assertSame( $updater1, $page->getDerivedDataUpdater( $user, null, $slotsUpdate ) );
 
+               // Don't re-use for edit if base revision ID changed
+               $this->assertNotSame(
+                       $updater1,
+                       $page->getDerivedDataUpdater( $user, null, $slotsUpdate, true )
+               );
+
                // Don't re-use with different user
                $updater2a = $page->getDerivedDataUpdater( $admin, null, $slotsUpdate );
                $updater2a->prepareContent( $admin, $slotsUpdate, false );