3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Storage\RevisionRecord
;
18 * @covers \MediaWiki\Storage\PageUpdater
21 class PageUpdaterTest
extends MediaWikiTestCase
{
23 public static function setUpBeforeClass() {
24 parent
::setUpBeforeClass();
26 // force service reset!
27 MediaWikiServices
::getInstance()->resetServiceForTesting( 'RevisionStore' );
30 private function getDummyTitle( $method ) {
31 return Title
::newFromText( $method, $this->getDefaultWikitextNS() );
37 * @return null|RecentChange
39 private function getRecentChangeFor( $revId ) {
40 $qi = RecentChange
::getQueryInfo();
41 $row = $this->db
->selectRow(
44 [ 'rc_this_oldid' => $revId ],
50 return $row ? RecentChange
::newFromRow( $row ) : null;
53 // TODO: test setAjaxEditStash();
56 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
57 * @covers \WikiPage::newPageUpdater()
59 public function testCreatePage() {
60 $user = $this->getTestUser()->getUser();
62 $title = $this->getDummyTitle( __METHOD__
);
63 $page = WikiPage
::factory( $title );
64 $updater = $page->newPageUpdater( $user );
66 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
68 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
69 $this->assertFalse( $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
70 $this->assertSame( 0, $updater->getUndidRevisionId(), 'getUndidRevisionId' );
72 $updater->addTag( 'foo' );
73 $updater->addTags( [ 'bar', 'qux' ] );
75 $tags = $updater->getExplicitTags();
77 $this->assertSame( [ 'bar', 'foo', 'qux' ], $tags, 'getExplicitTags' );
79 // TODO: MCR: test additional slots
80 $content = new TextContent( 'Lorem Ipsum' );
81 $updater->setContent( 'main', $content );
83 $parent = $updater->grabParentRevision();
85 $this->assertNull( $parent, 'getParentRevision' );
86 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
88 // TODO: test that hasEditConflict() grabs the parent revision
89 $this->assertFalse( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
90 $this->assertTrue( $updater->hasEditConflict( 1 ), 'hasEditConflict' );
92 // TODO: test failure with EDIT_UPDATE
93 // TODO: test EDIT_MINOR, EDIT_BOT, etc
94 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
95 $rev = $updater->saveRevision( $summary );
97 $this->assertNotNull( $rev );
98 $this->assertSame( 0, $rev->getParentId() );
99 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
100 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
102 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
103 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
104 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
105 $this->assertTrue( $updater->isNew(), 'isNew()' );
106 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
107 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
108 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
110 $rev = $updater->getNewRevision();
111 $revContent = $rev->getContent( 'main' );
112 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
114 // were the WikiPage and Title objects updated?
115 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
116 $this->assertTrue( $title->exists(), 'Title::exists()' );
117 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
118 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
121 $page2 = WikiPage
::factory( $title );
122 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
123 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
124 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
127 $rc = $this->getRecentChangeFor( $rev->getId() );
128 $this->assertNotNull( $rc, 'RecentChange' );
130 // check site stats - this asserts that derived data updates where run.
131 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
132 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
133 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
135 // re-edit with same content - should be a "null-edit"
136 $updater = $page->newPageUpdater( $user );
137 $updater->setContent( 'main', $content );
139 $summary = CommentStoreComment
::newUnsavedComment( 'to to re-edit' );
140 $rev = $updater->saveRevision( $summary );
141 $status = $updater->getStatus();
143 $this->assertNull( $rev, 'getNewRevision()' );
144 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
145 $this->assertTrue( $updater->isUnchanged(), 'isUnchanged' );
146 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
147 $this->assertTrue( $status->isOK(), 'getStatus()->isOK()' );
148 $this->assertTrue( $status->hasMessage( 'edit-no-change' ), 'edit-no-change' );
152 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
153 * @covers \WikiPage::newPageUpdater()
155 public function testUpdatePage() {
156 $user = $this->getTestUser()->getUser();
158 $title = $this->getDummyTitle( __METHOD__
);
159 $this->insertPage( $title );
161 $page = WikiPage
::factory( $title );
162 $parentId = $page->getLatest();
164 $updater = $page->newPageUpdater( $user );
166 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
168 $updater->setOriginalRevisionId( 7 );
169 $this->assertSame( 7, $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
171 $this->assertFalse( $updater->hasEditConflict( $parentId ), 'hasEditConflict' );
172 $this->assertTrue( $updater->hasEditConflict( $parentId - 1 ), 'hasEditConflict' );
173 $this->assertTrue( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
175 // TODO: MCR: test additional slots
176 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
178 // TODO: test all flags for saveRevision()!
179 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
180 $rev = $updater->saveRevision( $summary );
182 $this->assertNotNull( $rev );
183 $this->assertSame( $parentId, $rev->getParentId() );
184 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
185 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
187 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
188 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
189 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
190 $this->assertFalse( $updater->isNew(), 'isNew()' );
191 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
192 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
193 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
195 // TODO: Test null revision (with different user): new revision!
197 $rev = $updater->getNewRevision();
198 $revContent = $rev->getContent( 'main' );
199 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
201 // were the WikiPage and Title objects updated?
202 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
203 $this->assertTrue( $title->exists(), 'Title::exists()' );
204 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
205 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
208 $page2 = WikiPage
::factory( $title );
209 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
210 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
211 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
214 $rc = $this->getRecentChangeFor( $rev->getId() );
215 $this->assertNotNull( $rc, 'RecentChange' );
218 $updater = $page->newPageUpdater( $user );
219 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
221 $summary = CommentStoreComment
::newUnsavedComment( 're-edit' );
222 $updater->saveRevision( $summary );
223 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
224 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
226 // check site stats - this asserts that derived data updates where run.
227 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
228 $this->assertNotNull( $stats, 'site_stats' );
229 $this->assertSame( $oldStats->ss_total_pages +
0, (int)$stats->ss_total_pages
);
230 $this->assertSame( $oldStats->ss_total_edits +
2, (int)$stats->ss_total_edits
);
234 * Creates a revision in the database.
236 * @param WikiPage $page
238 * @param null|string|Content $content
240 * @return RevisionRecord|null
242 private function createRevision( WikiPage
$page, $summary, $content = null ) {
243 $user = $this->getTestUser()->getUser();
244 $comment = CommentStoreComment
::newUnsavedComment( $summary );
246 if ( !$content instanceof Content
) {
247 $content = new TextContent( $content ??
$summary );
250 $updater = $page->newPageUpdater( $user );
251 $updater->setContent( 'main', $content );
252 $rev = $updater->saveRevision( $comment );
257 * @covers \MediaWiki\Storage\PageUpdater::grabParentRevision()
258 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
260 public function testCompareAndSwapFailure() {
261 $user = $this->getTestUser()->getUser();
263 $title = $this->getDummyTitle( __METHOD__
);
265 // start editing non-existing page
266 $page = WikiPage
::factory( $title );
267 $updater = $page->newPageUpdater( $user );
268 $updater->grabParentRevision();
270 // create page concurrently
271 $concurrentPage = WikiPage
::factory( $title );
272 $this->createRevision( $concurrentPage, __METHOD__
. '-one' );
274 // try creating the page - should trigger CAS failure.
275 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
276 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
277 $updater->saveRevision( $summary );
278 $status = $updater->getStatus();
280 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
281 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
282 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
283 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-conflict' );
285 // start editing existing page
286 $page = WikiPage
::factory( $title );
287 $updater = $page->newPageUpdater( $user );
288 $updater->grabParentRevision();
290 // update page concurrently
291 $concurrentPage = WikiPage
::factory( $title );
292 $this->createRevision( $concurrentPage, __METHOD__
. '-two' );
294 // try creating the page - should trigger CAS failure.
295 $summary = CommentStoreComment
::newUnsavedComment( 'edit?!' );
296 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
297 $updater->saveRevision( $summary );
298 $status = $updater->getStatus();
300 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
301 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
302 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
303 $this->assertTrue( $status->hasMessage( 'edit-conflict' ), 'edit-conflict' );
307 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
309 public function testFailureOnEditFlags() {
310 $user = $this->getTestUser()->getUser();
312 $title = $this->getDummyTitle( __METHOD__
);
314 // start editing non-existing page
315 $page = WikiPage
::factory( $title );
316 $updater = $page->newPageUpdater( $user );
318 // update with EDIT_UPDATE flag should fail
319 $summary = CommentStoreComment
::newUnsavedComment( 'udpate?!' );
320 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
321 $updater->saveRevision( $summary, EDIT_UPDATE
);
322 $status = $updater->getStatus();
324 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
325 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
326 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
327 $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
330 $this->createRevision( $page, __METHOD__
);
332 // update with EDIT_NEW flag should fail
333 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
334 $updater = $page->newPageUpdater( $user );
335 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
336 $updater->saveRevision( $summary, EDIT_NEW
);
337 $status = $updater->getStatus();
339 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
340 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
341 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
342 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
345 public function provideSetRcPatrolStatus( $patrolled ) {
346 yield
[ RecentChange
::PRC_UNPATROLLED
];
347 yield
[ RecentChange
::PRC_AUTOPATROLLED
];
351 * @dataProvider provideSetRcPatrolStatus
352 * @covers \MediaWiki\Storage\PageUpdater::setRcPatrolStatus()
354 public function testSetRcPatrolStatus( $patrolled ) {
355 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
357 $user = $this->getTestUser()->getUser();
359 $title = $this->getDummyTitle( __METHOD__
);
361 $page = WikiPage
::factory( $title );
362 $updater = $page->newPageUpdater( $user );
364 $summary = CommentStoreComment
::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
365 $updater->setContent( 'main', new TextContent( 'Lorem ipsum ' . $patrolled ) );
366 $updater->setRcPatrolStatus( $patrolled );
367 $rev = $updater->saveRevision( $summary );
369 $rc = $revisionStore->getRecentChange( $rev );
370 $this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
374 * @covers \MediaWiki\Storage\PageUpdater::inheritSlot()
375 * @covers \MediaWiki\Storage\PageUpdater::setContent()
377 public function testInheritSlot() {
378 $user = $this->getTestUser()->getUser();
379 $title = $this->getDummyTitle( __METHOD__
);
380 $page = WikiPage
::factory( $title );
382 $updater = $page->newPageUpdater( $user );
383 $summary = CommentStoreComment
::newUnsavedComment( 'one' );
384 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
385 $rev1 = $updater->saveRevision( $summary, EDIT_NEW
);
387 $updater = $page->newPageUpdater( $user );
388 $summary = CommentStoreComment
::newUnsavedComment( 'two' );
389 $updater->setContent( 'main', new TextContent( 'Foo Bar' ) );
390 $rev2 = $updater->saveRevision( $summary, EDIT_UPDATE
);
392 $updater = $page->newPageUpdater( $user );
393 $summary = CommentStoreComment
::newUnsavedComment( 'three' );
394 $updater->inheritSlot( $rev1->getSlot( 'main' ) );
395 $rev3 = $updater->saveRevision( $summary, EDIT_UPDATE
);
397 $this->assertNotSame( $rev1->getId(), $rev3->getId() );
398 $this->assertNotSame( $rev2->getId(), $rev3->getId() );
400 $main1 = $rev1->getSlot( 'main' );
401 $main3 = $rev3->getSlot( 'main' );
403 $this->assertNotSame( $main1->getRevision(), $main3->getRevision() );
404 $this->assertSame( $main1->getAddress(), $main3->getAddress() );
405 $this->assertTrue( $main1->getContent()->equals( $main3->getContent() ) );
408 // TODO: MCR: test adding multiple slots, inheriting parent slots, and removing slots.
410 public function testSetUseAutomaticEditSummaries() {
411 $this->setContentLang( 'qqx' );
412 $user = $this->getTestUser()->getUser();
414 $title = $this->getDummyTitle( __METHOD__
);
415 $page = WikiPage
::factory( $title );
417 $updater = $page->newPageUpdater( $user );
418 $updater->setUseAutomaticEditSummaries( true );
419 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
421 // empty comment triggers auto-summary
422 $summary = CommentStoreComment
::newUnsavedComment( '' );
423 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
425 $rev = $updater->getNewRevision();
426 $comment = $rev->getComment( RevisionRecord
::RAW
);
427 $this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text
, 'comment text' );
429 // check that this also works when blanking the page
430 $updater = $page->newPageUpdater( $user );
431 $updater->setUseAutomaticEditSummaries( true );
432 $updater->setContent( 'main', new TextContent( '' ) );
434 $summary = CommentStoreComment
::newUnsavedComment( '' );
435 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
437 $rev = $updater->getNewRevision();
438 $comment = $rev->getComment( RevisionRecord
::RAW
);
439 $this->assertSame( '(autosumm-blank)', $comment->text
, 'comment text' );
441 // check that we can also disable edit-summaries
442 $title2 = $this->getDummyTitle( __METHOD__
. '/2' );
443 $page2 = WikiPage
::factory( $title2 );
445 $updater = $page2->newPageUpdater( $user );
446 $updater->setUseAutomaticEditSummaries( false );
447 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
449 $summary = CommentStoreComment
::newUnsavedComment( '' );
450 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
452 $rev = $updater->getNewRevision();
453 $comment = $rev->getComment( RevisionRecord
::RAW
);
454 $this->assertSame( '', $comment->text
, 'comment text should still be lank' );
456 // check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
457 $updater = $page2->newPageUpdater( $user );
458 $updater->setUseAutomaticEditSummaries( true );
459 $updater->setContent( 'main', new TextContent( '' ) );
461 $summary = CommentStoreComment
::newUnsavedComment( '' );
462 $updater->saveRevision( $summary, 0 );
464 $rev = $updater->getNewRevision();
465 $comment = $rev->getComment( RevisionRecord
::RAW
);
466 $this->assertSame( '', $comment->text
, 'comment text' );
469 public function provideSetUsePageCreationLog() {
470 yield
[ true, [ [ 'create', 'create' ] ] ];
475 * @dataProvider provideSetUsePageCreationLog
478 public function testSetUsePageCreationLog( $use, $expected ) {
479 $user = $this->getTestUser()->getUser();
480 $title = $this->getDummyTitle( __METHOD__
. ( $use ?
'_logged' : '_unlogged' ) );
481 $page = WikiPage
::factory( $title );
483 $updater = $page->newPageUpdater( $user );
484 $updater->setUsePageCreationLog( $use );
485 $summary = CommentStoreComment
::newUnsavedComment( 'cmt' );
486 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
487 $updater->saveRevision( $summary, EDIT_NEW
);
489 $rev = $updater->getNewRevision();
492 [ 'log_type', 'log_action' ],
493 [ 'log_page' => $rev->getPageId() ],
498 public function provideMagicWords() {
500 'Test {{PAGEID}} Test',
501 function ( RevisionRecord
$rev ) {
502 return $rev->getPageId();
506 yield
'REVISIONID' => [
507 'Test {{REVISIONID}} Test',
508 function ( RevisionRecord
$rev ) {
509 return $rev->getId();
513 yield
'REVISIONUSER' => [
514 'Test {{REVISIONUSER}} Test',
515 function ( RevisionRecord
$rev ) {
516 return $rev->getUser()->getName();
520 yield
'REVISIONTIMESTAMP' => [
521 'Test {{REVISIONTIMESTAMP}} Test',
522 function ( RevisionRecord
$rev ) {
523 return $rev->getTimestamp();
527 yield
'subst:REVISIONUSER' => [
528 'Test {{subst:REVISIONUSER}} Test',
529 function ( RevisionRecord
$rev ) {
530 return $rev->getUser()->getName();
534 yield
'subst:PAGENAME' => [
535 'Test {{subst:PAGENAME}} Test',
536 function ( RevisionRecord
$rev ) {
537 return 'PageUpdaterTest::testMagicWords';
543 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
545 * Integration test for PageUpdater, DerivedPageDataUpdater, RevisionRenderer
546 * and RenderedRevision, that ensures that magic words depending on revision meta-data
547 * are handled correctly. Note that each magic word needs to be tested separately,
548 * to assert correct behavior for each "vary" flag in the ParserOutput.
550 * @dataProvider provideMagicWords
552 public function testMagicWords( $wikitext, $callback ) {
553 $user = $this->getTestUser()->getUser();
555 $title = $this->getDummyTitle( __METHOD__
. '-' . $this->getName() );
556 $page = WikiPage
::factory( $title );
557 $updater = $page->newPageUpdater( $user );
559 $updater->setContent( 'main', new \
WikitextContent( $wikitext ) );
561 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
562 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
565 $this->fail( $updater->getStatus()->getWikiText() );
568 $expected = strval( $callback( $rev ) );
570 $cache = MediaWikiServices
::getInstance()->getParserCache();
571 $output = $cache->get(
573 ParserOptions
::newCanonical(
578 $this->assertNotNull( $output, 'ParserCache::get' );
580 $this->assertContains( $expected, $output->getText() );