use MediaWiki\MediaWikiServices;
use MediaWiki\Storage\RevisionRecord;
use MediaWikiTestCase;
+use ParserOptions;
use RecentChange;
use Revision;
use TextContent;
use Title;
+use User;
use WikiPage;
/**
// 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 );
}
);
}
+ 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();
+ },
+ 'subst'
+ ];
+
+ yield 'subst:PAGENAME' => [
+ 'Test {{subst:PAGENAME}} Test',
+ function ( RevisionRecord $rev ) {
+ return 'PageUpdaterTest::testMagicWords';
+ },
+ 'subst'
+ ];
+ }
+
+ /**
+ * @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, $subst = false ) {
+ $user = User::newFromName( 'A user for ' . __METHOD__ );
+ $user->addToDatabase();
+
+ $title = $this->getDummyTitle( __METHOD__ . '-' . $this->getName() );
+ $this->insertPage( $title );
+
+ $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_UPDATE );
+
+ if ( !$rev ) {
+ $this->fail( $updater->getStatus()->getWikiText() );
+ }
+
+ $expected = strval( $callback( $rev ) );
+
+ $output = $page->getParserOutput( ParserOptions::newCanonical( 'canonical' ) );
+ $html = $output->getText();
+ $text = $rev->getContent( 'main' )->serialize();
+
+ if ( $subst ) {
+ $this->assertContains( $expected, $text, 'In Wikitext' );
+ }
+
+ $this->assertContains( $expected, $html, 'In HTML' );
+ }
+
}