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
{
22 private function getDummyTitle( $method ) {
23 return Title
::newFromText( $method, $this->getDefaultWikitextNS() );
29 * @return null|RecentChange
31 private function getRecentChangeFor( $revId ) {
32 $qi = RecentChange
::getQueryInfo();
33 $row = $this->db
->selectRow(
36 [ 'rc_this_oldid' => $revId ],
42 return $row ? RecentChange
::newFromRow( $row ) : null;
45 // TODO: test setAjaxEditStash();
48 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
49 * @covers \WikiPage::newPageUpdater()
51 public function testCreatePage() {
52 $user = $this->getTestUser()->getUser();
54 $title = $this->getDummyTitle( __METHOD__
);
55 $page = WikiPage
::factory( $title );
56 $updater = $page->newPageUpdater( $user );
58 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
60 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
61 $this->assertFalse( $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
62 $this->assertSame( 0, $updater->getUndidRevisionId(), 'getUndidRevisionId' );
64 $updater->addTag( 'foo' );
65 $updater->addTags( [ 'bar', 'qux' ] );
67 $tags = $updater->getExplicitTags();
69 $this->assertSame( [ 'bar', 'foo', 'qux' ], $tags, 'getExplicitTags' );
71 // TODO: MCR: test additional slots
72 $content = new TextContent( 'Lorem Ipsum' );
73 $updater->setContent( 'main', $content );
75 $parent = $updater->grabParentRevision();
77 $this->assertNull( $parent, 'getParentRevision' );
78 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
80 // TODO: test that hasEditConflict() grabs the parent revision
81 $this->assertFalse( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
82 $this->assertTrue( $updater->hasEditConflict( 1 ), 'hasEditConflict' );
84 // TODO: test failure with EDIT_UPDATE
85 // TODO: test EDIT_MINOR, EDIT_BOT, etc
86 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
87 $rev = $updater->saveRevision( $summary );
89 $this->assertNotNull( $rev );
90 $this->assertSame( 0, $rev->getParentId() );
91 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
92 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
94 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
95 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
96 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
97 $this->assertTrue( $updater->isNew(), 'isNew()' );
98 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
99 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
100 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
102 $rev = $updater->getNewRevision();
103 $revContent = $rev->getContent( 'main' );
104 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
106 // were the WikiPage and Title objects updated?
107 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
108 $this->assertTrue( $title->exists(), 'Title::exists()' );
109 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
110 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
113 $page2 = WikiPage
::factory( $title );
114 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
115 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
116 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
119 $rc = $this->getRecentChangeFor( $rev->getId() );
120 $this->assertNotNull( $rc, 'RecentChange' );
122 // check site stats - this asserts that derived data updates where run.
123 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
124 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
125 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
127 // re-edit with same content - should be a "null-edit"
128 $updater = $page->newPageUpdater( $user );
129 $updater->setContent( 'main', $content );
131 $summary = CommentStoreComment
::newUnsavedComment( 'to to re-edit' );
132 $rev = $updater->saveRevision( $summary );
133 $status = $updater->getStatus();
135 $this->assertNull( $rev, 'getNewRevision()' );
136 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
137 $this->assertTrue( $updater->isUnchanged(), 'isUnchanged' );
138 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
139 $this->assertTrue( $status->isOK(), 'getStatus()->isOK()' );
140 $this->assertTrue( $status->hasMessage( 'edit-no-change' ), 'edit-no-change' );
144 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
145 * @covers \WikiPage::newPageUpdater()
147 public function testUpdatePage() {
148 $user = $this->getTestUser()->getUser();
150 $title = $this->getDummyTitle( __METHOD__
);
151 $this->insertPage( $title );
153 $page = WikiPage
::factory( $title );
154 $parentId = $page->getLatest();
156 $updater = $page->newPageUpdater( $user );
158 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
160 $updater->setOriginalRevisionId( 7 );
161 $this->assertSame( 7, $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
163 $this->assertFalse( $updater->hasEditConflict( $parentId ), 'hasEditConflict' );
164 $this->assertTrue( $updater->hasEditConflict( $parentId - 1 ), 'hasEditConflict' );
165 $this->assertTrue( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
167 // TODO: MCR: test additional slots
168 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
170 // TODO: test all flags for saveRevision()!
171 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
172 $rev = $updater->saveRevision( $summary );
174 $this->assertNotNull( $rev );
175 $this->assertSame( $parentId, $rev->getParentId() );
176 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
177 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
179 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
180 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
181 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
182 $this->assertFalse( $updater->isNew(), 'isNew()' );
183 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
184 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
185 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
187 // TODO: Test null revision (with different user): new revision!
189 $rev = $updater->getNewRevision();
190 $revContent = $rev->getContent( 'main' );
191 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
193 // were the WikiPage and Title objects updated?
194 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
195 $this->assertTrue( $title->exists(), 'Title::exists()' );
196 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
197 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
200 $page2 = WikiPage
::factory( $title );
201 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
202 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
203 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
206 $rc = $this->getRecentChangeFor( $rev->getId() );
207 $this->assertNotNull( $rc, 'RecentChange' );
210 $updater = $page->newPageUpdater( $user );
211 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
213 $summary = CommentStoreComment
::newUnsavedComment( 're-edit' );
214 $updater->saveRevision( $summary );
215 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
216 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
218 // check site stats - this asserts that derived data updates where run.
219 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
220 $this->assertNotNull( $stats, 'site_stats' );
221 $this->assertSame( $oldStats->ss_total_pages +
0, (int)$stats->ss_total_pages
);
222 $this->assertSame( $oldStats->ss_total_edits +
2, (int)$stats->ss_total_edits
);
226 * Creates a revision in the database.
228 * @param WikiPage $page
230 * @param null|string|Content $content
232 * @return RevisionRecord|null
234 private function createRevision( WikiPage
$page, $summary, $content = null ) {
235 $user = $this->getTestUser()->getUser();
236 $comment = CommentStoreComment
::newUnsavedComment( $summary );
238 if ( !$content instanceof Content
) {
239 $content = new TextContent( $content ??
$summary );
242 $updater = $page->newPageUpdater( $user );
243 $updater->setContent( 'main', $content );
244 $rev = $updater->saveRevision( $comment );
249 * @covers \MediaWiki\Storage\PageUpdater::grabParentRevision()
250 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
252 public function testCompareAndSwapFailure() {
253 $user = $this->getTestUser()->getUser();
255 $title = $this->getDummyTitle( __METHOD__
);
257 // start editing non-existing page
258 $page = WikiPage
::factory( $title );
259 $updater = $page->newPageUpdater( $user );
260 $updater->grabParentRevision();
262 // create page concurrently
263 $concurrentPage = WikiPage
::factory( $title );
264 $this->createRevision( $concurrentPage, __METHOD__
. '-one' );
266 // try creating the page - should trigger CAS failure.
267 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
268 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
269 $updater->saveRevision( $summary );
270 $status = $updater->getStatus();
272 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
273 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
274 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
275 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-conflict' );
277 // start editing existing page
278 $page = WikiPage
::factory( $title );
279 $updater = $page->newPageUpdater( $user );
280 $updater->grabParentRevision();
282 // update page concurrently
283 $concurrentPage = WikiPage
::factory( $title );
284 $this->createRevision( $concurrentPage, __METHOD__
. '-two' );
286 // try creating the page - should trigger CAS failure.
287 $summary = CommentStoreComment
::newUnsavedComment( 'edit?!' );
288 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
289 $updater->saveRevision( $summary );
290 $status = $updater->getStatus();
292 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
293 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
294 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
295 $this->assertTrue( $status->hasMessage( 'edit-conflict' ), 'edit-conflict' );
299 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
301 public function testFailureOnEditFlags() {
302 $user = $this->getTestUser()->getUser();
304 $title = $this->getDummyTitle( __METHOD__
);
306 // start editing non-existing page
307 $page = WikiPage
::factory( $title );
308 $updater = $page->newPageUpdater( $user );
310 // update with EDIT_UPDATE flag should fail
311 $summary = CommentStoreComment
::newUnsavedComment( 'udpate?!' );
312 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
313 $updater->saveRevision( $summary, EDIT_UPDATE
);
314 $status = $updater->getStatus();
316 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
317 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
318 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
319 $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
322 $this->createRevision( $page, __METHOD__
);
324 // update with EDIT_NEW flag should fail
325 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
326 $updater = $page->newPageUpdater( $user );
327 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
328 $updater->saveRevision( $summary, EDIT_NEW
);
329 $status = $updater->getStatus();
331 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
332 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
333 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
334 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
337 public function provideSetRcPatrolStatus( $patrolled ) {
338 yield
[ RecentChange
::PRC_UNPATROLLED
];
339 yield
[ RecentChange
::PRC_AUTOPATROLLED
];
343 * @dataProvider provideSetRcPatrolStatus
344 * @covers \MediaWiki\Storage\PageUpdater::setRcPatrolStatus()
346 public function testSetRcPatrolStatus( $patrolled ) {
347 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
349 $user = $this->getTestUser()->getUser();
351 $title = $this->getDummyTitle( __METHOD__
);
353 $page = WikiPage
::factory( $title );
354 $updater = $page->newPageUpdater( $user );
356 $summary = CommentStoreComment
::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
357 $updater->setContent( 'main', new TextContent( 'Lorem ipsum ' . $patrolled ) );
358 $updater->setRcPatrolStatus( $patrolled );
359 $rev = $updater->saveRevision( $summary );
361 $rc = $revisionStore->getRecentChange( $rev );
362 $this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
366 * @covers \MediaWiki\Storage\PageUpdater::inheritSlot()
367 * @covers \MediaWiki\Storage\PageUpdater::setContent()
369 public function testInheritSlot() {
370 $user = $this->getTestUser()->getUser();
371 $title = $this->getDummyTitle( __METHOD__
);
372 $page = WikiPage
::factory( $title );
374 $updater = $page->newPageUpdater( $user );
375 $summary = CommentStoreComment
::newUnsavedComment( 'one' );
376 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
377 $rev1 = $updater->saveRevision( $summary, EDIT_NEW
);
379 $updater = $page->newPageUpdater( $user );
380 $summary = CommentStoreComment
::newUnsavedComment( 'two' );
381 $updater->setContent( 'main', new TextContent( 'Foo Bar' ) );
382 $rev2 = $updater->saveRevision( $summary, EDIT_UPDATE
);
384 $updater = $page->newPageUpdater( $user );
385 $summary = CommentStoreComment
::newUnsavedComment( 'three' );
386 $updater->inheritSlot( $rev1->getSlot( 'main' ) );
387 $rev3 = $updater->saveRevision( $summary, EDIT_UPDATE
);
389 $this->assertNotSame( $rev1->getId(), $rev3->getId() );
390 $this->assertNotSame( $rev2->getId(), $rev3->getId() );
392 $main1 = $rev1->getSlot( 'main' );
393 $main3 = $rev3->getSlot( 'main' );
395 $this->assertNotSame( $main1->getRevision(), $main3->getRevision() );
396 $this->assertSame( $main1->getAddress(), $main3->getAddress() );
397 $this->assertTrue( $main1->getContent()->equals( $main3->getContent() ) );
400 // TODO: MCR: test adding multiple slots, inheriting parent slots, and removing slots.
402 public function testSetUseAutomaticEditSummaries() {
403 $this->setContentLang( 'qqx' );
404 $user = $this->getTestUser()->getUser();
406 $title = $this->getDummyTitle( __METHOD__
);
407 $page = WikiPage
::factory( $title );
409 $updater = $page->newPageUpdater( $user );
410 $updater->setUseAutomaticEditSummaries( true );
411 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
413 // empty comment triggers auto-summary
414 $summary = CommentStoreComment
::newUnsavedComment( '' );
415 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
417 $rev = $updater->getNewRevision();
418 $comment = $rev->getComment( RevisionRecord
::RAW
);
419 $this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text
, 'comment text' );
421 // check that this also works when blanking the page
422 $updater = $page->newPageUpdater( $user );
423 $updater->setUseAutomaticEditSummaries( true );
424 $updater->setContent( 'main', new TextContent( '' ) );
426 $summary = CommentStoreComment
::newUnsavedComment( '' );
427 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
429 $rev = $updater->getNewRevision();
430 $comment = $rev->getComment( RevisionRecord
::RAW
);
431 $this->assertSame( '(autosumm-blank)', $comment->text
, 'comment text' );
433 // check that we can also disable edit-summaries
434 $title2 = $this->getDummyTitle( __METHOD__
. '/2' );
435 $page2 = WikiPage
::factory( $title2 );
437 $updater = $page2->newPageUpdater( $user );
438 $updater->setUseAutomaticEditSummaries( false );
439 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
441 $summary = CommentStoreComment
::newUnsavedComment( '' );
442 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
444 $rev = $updater->getNewRevision();
445 $comment = $rev->getComment( RevisionRecord
::RAW
);
446 $this->assertSame( '', $comment->text
, 'comment text should still be lank' );
448 // check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
449 $updater = $page2->newPageUpdater( $user );
450 $updater->setUseAutomaticEditSummaries( true );
451 $updater->setContent( 'main', new TextContent( '' ) );
453 $summary = CommentStoreComment
::newUnsavedComment( '' );
454 $updater->saveRevision( $summary, 0 );
456 $rev = $updater->getNewRevision();
457 $comment = $rev->getComment( RevisionRecord
::RAW
);
458 $this->assertSame( '', $comment->text
, 'comment text' );
461 public function provideSetUsePageCreationLog() {
462 yield
[ true, [ [ 'create', 'create' ] ] ];
467 * @dataProvider provideSetUsePageCreationLog
470 public function testSetUsePageCreationLog( $use, $expected ) {
471 $user = $this->getTestUser()->getUser();
472 $title = $this->getDummyTitle( __METHOD__
. ( $use ?
'_logged' : '_unlogged' ) );
473 $page = WikiPage
::factory( $title );
475 $updater = $page->newPageUpdater( $user );
476 $updater->setUsePageCreationLog( $use );
477 $summary = CommentStoreComment
::newUnsavedComment( 'cmt' );
478 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
479 $updater->saveRevision( $summary, EDIT_NEW
);
481 $rev = $updater->getNewRevision();
484 [ 'log_type', 'log_action' ],
485 [ 'log_page' => $rev->getPageId() ],
490 public function provideMagicWords() {
492 'Test {{PAGEID}} Test',
493 function ( RevisionRecord
$rev ) {
494 return $rev->getPageId();
498 yield
'REVISIONID' => [
499 'Test {{REVISIONID}} Test',
500 function ( RevisionRecord
$rev ) {
501 return $rev->getId();
505 yield
'REVISIONUSER' => [
506 'Test {{REVISIONUSER}} Test',
507 function ( RevisionRecord
$rev ) {
508 return $rev->getUser()->getName();
512 yield
'REVISIONTIMESTAMP' => [
513 'Test {{REVISIONTIMESTAMP}} Test',
514 function ( RevisionRecord
$rev ) {
515 return $rev->getTimestamp();
519 yield
'subst:REVISIONUSER' => [
520 'Test {{subst:REVISIONUSER}} Test',
521 function ( RevisionRecord
$rev ) {
522 return $rev->getUser()->getName();
526 yield
'subst:PAGENAME' => [
527 'Test {{subst:PAGENAME}} Test',
528 function ( RevisionRecord
$rev ) {
529 return 'PageUpdaterTest::testMagicWords';
535 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
537 * Integration test for PageUpdater, DerivedPageDataUpdater, RevisionRenderer
538 * and RenderedRevision, that ensures that magic words depending on revision meta-data
539 * are handled correctly. Note that each magic word needs to be tested separately,
540 * to assert correct behavior for each "vary" flag in the ParserOutput.
542 * @dataProvider provideMagicWords
544 public function testMagicWords( $wikitext, $callback ) {
545 $user = $this->getTestUser()->getUser();
547 $title = $this->getDummyTitle( __METHOD__
. '-' . $this->getName() );
548 $page = WikiPage
::factory( $title );
549 $updater = $page->newPageUpdater( $user );
551 $updater->setContent( 'main', new \
WikitextContent( $wikitext ) );
553 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
554 $rev = $updater->saveRevision( $summary, EDIT_NEW
);
557 $this->fail( $updater->getStatus()->getWikiText() );
560 $expected = strval( $callback( $rev ) );
562 $cache = MediaWikiServices
::getInstance()->getParserCache();
563 $output = $cache->get(
565 ParserOptions
::newCanonical(
570 $this->assertNotNull( $output, 'ParserCache::get' );
572 $this->assertContains( $expected, $output->getText() );