Merge "Make the spelling and pluralization of "Active filters" consistent"
[lhc/web/wiklou.git] / tests / phpunit / includes / page / WikiPageDbTestBase.php
index fa15a12..dc805df 100644 (file)
@@ -1,5 +1,6 @@
 <?php
 
+use MediaWiki\Edit\PreparedEdit;
 use MediaWiki\MediaWikiServices;
 use MediaWiki\Storage\RevisionSlotsUpdate;
 use Wikimedia\TestingAccessWrapper;
@@ -164,14 +165,14 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
 
                // Re-using the prepared info if possible
                $sameEdit = $page->prepareContentForEdit( $content, null, $user, null, false );
-               $this->assertEquals( $edit, $sameEdit, 'equivalent PreparedEdit' );
+               $this->assertPreparedEditEquals( $edit, $sameEdit, 'equivalent PreparedEdit' );
                $this->assertSame( $edit->pstContent, $sameEdit->pstContent, 're-use output' );
                $this->assertSame( $edit->output, $sameEdit->output, 're-use output' );
 
                // Not re-using the same PreparedEdit if not possible
                $rev = $page->getRevision();
                $edit2 = $page->prepareContentForEdit( $content2, null, $user, null, false );
-               $this->assertNotEquals( $edit, $edit2 );
+               $this->assertPreparedEditNotEquals( $edit, $edit2 );
                $this->assertContains( 'At vero eos', $edit2->pstContent->serialize(), "content" );
 
                // Check pre-safe transform
@@ -179,7 +180,7 @@ abstract class WikiPageDbTestBase extends MediaWikiLangTestCase {
                $this->assertNotContains( '~~~~', $edit2->pstContent->serialize() );
 
                $edit3 = $page->prepareContentForEdit( $content2, null, $sysop, null, false );
-               $this->assertNotEquals( $edit2, $edit3 );
+               $this->assertPreparedEditNotEquals( $edit2, $edit3 );
 
                // TODO: test with passing revision, then same without revision.
        }
@@ -249,6 +250,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' );
@@ -259,9 +262,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
@@ -342,6 +350,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
@@ -1024,6 +1052,9 @@ more stuff
         * @covers WikiPage::commitRollback
         */
        public function testDoRollback() {
+               // FIXME: fails under postgres
+               $this->markTestSkippedIfDbType( 'postgres' );
+
                $admin = $this->getTestSysop()->getUser();
                $user1 = $this->getTestUser()->getUser();
                // Use the confirmed group for user2 to make sure the user is different
@@ -1542,6 +1573,9 @@ more stuff
                $expectedSuccess,
                $expectedRowCount
        ) {
+               // FIXME: fails under sqlite and postgres
+               $this->markTestSkippedIfDbType( 'sqlite' );
+               $this->markTestSkippedIfDbType( 'postgres' );
                static $pageCounter = 0;
                $pageCounter++;
 
@@ -2278,14 +2312,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 );
        }
 
        /**
@@ -2311,7 +2356,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(
@@ -2319,6 +2364,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 );
@@ -2346,4 +2397,25 @@ more stuff
                $this->assertNotSame( $updater5, $updater6 );
        }
 
+       protected function assertPreparedEditEquals(
+               PreparedEdit $edit, PreparedEdit $edit2, $message = ''
+       ) {
+               // suppress differences caused by a clock tick between generating the two PreparedEdits
+               if ( abs( $edit->timestamp - $edit2->timestamp ) < 3 ) {
+                       $edit2 = clone $edit2;
+                       $edit2->timestamp = $edit->timestamp;
+               }
+               $this->assertEquals( $edit, $edit2, $message );
+       }
+
+       protected function assertPreparedEditNotEquals(
+               PreparedEdit $edit, PreparedEdit $edit2, $message = ''
+       ) {
+               if ( abs( $edit->timestamp - $edit2->timestamp ) < 3 ) {
+                       $edit2 = clone $edit2;
+                       $edit2->timestamp = $edit->timestamp;
+               }
+               $this->assertNotEquals( $edit, $edit2, $message );
+       }
+
 }