3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
7 use MediaWiki\MediaWikiServices
;
8 use MediaWiki\Storage\RevisionRecord
;
17 * @covers \MediaWiki\Storage\PageUpdater
20 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->getBaseRevisionId(), 'getBaseRevisionId' );
62 $this->assertSame( 0, $updater->getUndidRevisionId(), 'getUndidRevisionId' );
64 $updater->setBaseRevisionId( 0 );
65 $this->assertSame( 0, $updater->getBaseRevisionId(), 'getBaseRevisionId' );
67 $updater->addTag( 'foo' );
68 $updater->addTags( [ 'bar', 'qux' ] );
70 $tags = $updater->getExplicitTags();
72 $this->assertSame( [ 'bar', 'foo', 'qux' ], $tags, 'getExplicitTags' );
74 // TODO: MCR: test additional slots
75 $content = new TextContent( 'Lorem Ipsum' );
76 $updater->setContent( 'main', $content );
78 $parent = $updater->grabParentRevision();
80 // TODO: test that hasEditConflict() grabs the parent revision
81 $this->assertNull( $parent, 'getParentRevision' );
82 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
83 $this->assertFalse( $updater->hasEditConflict(), 'hasEditConflict' );
85 // TODO: test failure with EDIT_UPDATE
86 // TODO: test EDIT_MINOR, EDIT_BOT, etc
87 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
88 $rev = $updater->saveRevision( $summary );
90 $this->assertNotNull( $rev );
91 $this->assertSame( 0, $rev->getParentId() );
92 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
93 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
95 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
96 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
97 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
98 $this->assertTrue( $updater->isNew(), 'isNew()' );
99 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
100 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
101 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
103 $rev = $updater->getNewRevision();
104 $revContent = $rev->getContent( 'main' );
105 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
107 // were the WikiPage and Title objects updated?
108 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
109 $this->assertTrue( $title->exists(), 'Title::exists()' );
110 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
111 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
114 $page2 = WikiPage
::factory( $title );
115 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
116 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
117 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
120 $rc = $this->getRecentChangeFor( $rev->getId() );
121 $this->assertNotNull( $rc, 'RecentChange' );
123 // check site stats - this asserts that derived data updates where run.
124 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
125 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
126 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
128 // re-edit with same content - should be a "null-edit"
129 $updater = $page->newPageUpdater( $user );
130 $updater->setContent( 'main', $content );
132 $summary = CommentStoreComment
::newUnsavedComment( 'to to re-edit' );
133 $rev = $updater->saveRevision( $summary );
134 $status = $updater->getStatus();
136 $this->assertNull( $rev, 'getNewRevision()' );
137 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
138 $this->assertTrue( $updater->isUnchanged(), 'isUnchanged' );
139 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
140 $this->assertTrue( $status->isOK(), 'getStatus()->isOK()' );
141 $this->assertTrue( $status->hasMessage( 'edit-no-change' ), 'edit-no-change' );
145 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
146 * @covers \WikiPage::newPageUpdater()
148 public function testUpdatePage() {
149 $user = $this->getTestUser()->getUser();
151 $title = $this->getDummyTitle( __METHOD__
);
152 $this->insertPage( $title );
154 $page = WikiPage
::factory( $title );
155 $parentId = $page->getLatest();
157 $updater = $page->newPageUpdater( $user );
159 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
161 // TODO: test page update does not fail with mismatching base rev ID
162 $baseRev = $title->getLatestRevID( Title
::GAID_FOR_UPDATE
);
163 $updater->setBaseRevisionId( $baseRev );
164 $this->assertSame( $baseRev, $updater->getBaseRevisionId(), 'getBaseRevisionId' );
166 // TODO: MCR: test additional slots
167 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
169 // TODO: test all flags for saveRevision()!
170 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
171 $rev = $updater->saveRevision( $summary );
173 $this->assertNotNull( $rev );
174 $this->assertSame( $parentId, $rev->getParentId() );
175 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
176 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
178 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
179 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
180 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
181 $this->assertFalse( $updater->isNew(), 'isNew()' );
182 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
183 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
184 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
186 // TODO: Test null revision (with different user): new revision!
188 $rev = $updater->getNewRevision();
189 $revContent = $rev->getContent( 'main' );
190 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
192 // were the WikiPage and Title objects updated?
193 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
194 $this->assertTrue( $title->exists(), 'Title::exists()' );
195 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
196 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
199 $page2 = WikiPage
::factory( $title );
200 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
201 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
202 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
205 $rc = $this->getRecentChangeFor( $rev->getId() );
206 $this->assertNotNull( $rc, 'RecentChange' );
209 $updater = $page->newPageUpdater( $user );
210 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
212 $summary = CommentStoreComment
::newUnsavedComment( 're-edit' );
213 $updater->saveRevision( $summary );
214 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
215 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
217 // check site stats - this asserts that derived data updates where run.
218 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
219 $this->assertSame( $oldStats->ss_total_pages +
0, (int)$stats->ss_total_pages
);
220 $this->assertSame( $oldStats->ss_total_edits +
2, (int)$stats->ss_total_edits
);
224 * Creates a revision in the database.
226 * @param WikiPage $page
228 * @param null|string|Content $content
230 * @return RevisionRecord|null
232 private function createRevision( WikiPage
$page, $summary, $content = null ) {
233 $user = $this->getTestUser()->getUser();
234 $comment = CommentStoreComment
::newUnsavedComment( $summary );
236 if ( !$content instanceof Content
) {
237 $content = new TextContent( $content === null ?
$summary : $content );
240 $updater = $page->newPageUpdater( $user );
241 $updater->setContent( 'main', $content );
242 $rev = $updater->saveRevision( $comment );
247 * @covers \MediaWiki\Storage\PageUpdater::grabParentRevision()
248 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
250 public function testCompareAndSwapFailure() {
251 $user = $this->getTestUser()->getUser();
253 $title = $this->getDummyTitle( __METHOD__
);
255 // start editing non-existing page
256 $page = WikiPage
::factory( $title );
257 $updater = $page->newPageUpdater( $user );
258 $updater->grabParentRevision();
260 // create page concurrently
261 $concurrentPage = WikiPage
::factory( $title );
262 $this->createRevision( $concurrentPage, __METHOD__
. '-one' );
264 // try creating the page - should trigger CAS failure.
265 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
266 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
267 $updater->saveRevision( $summary );
268 $status = $updater->getStatus();
270 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
271 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
272 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
273 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-conflict' );
275 // start editing existing page
276 $page = WikiPage
::factory( $title );
277 $updater = $page->newPageUpdater( $user );
278 $updater->grabParentRevision();
280 // update page concurrently
281 $concurrentPage = WikiPage
::factory( $title );
282 $this->createRevision( $concurrentPage, __METHOD__
. '-two' );
284 // try creating the page - should trigger CAS failure.
285 $summary = CommentStoreComment
::newUnsavedComment( 'edit?!' );
286 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
287 $updater->saveRevision( $summary );
288 $status = $updater->getStatus();
290 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
291 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
292 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
293 $this->assertTrue( $status->hasMessage( 'edit-conflict' ), 'edit-conflict' );
297 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
299 public function testFailureOnEditFlags() {
300 $user = $this->getTestUser()->getUser();
302 $title = $this->getDummyTitle( __METHOD__
);
304 // start editing non-existing page
305 $page = WikiPage
::factory( $title );
306 $updater = $page->newPageUpdater( $user );
308 // update with EDIT_UPDATE flag should fail
309 $summary = CommentStoreComment
::newUnsavedComment( 'udpate?!' );
310 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
311 $updater->saveRevision( $summary, EDIT_UPDATE
);
312 $status = $updater->getStatus();
314 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
315 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
316 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
317 $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
320 $this->createRevision( $page, __METHOD__
);
322 // update with EDIT_NEW flag should fail
323 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
324 $updater = $page->newPageUpdater( $user );
325 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
326 $updater->saveRevision( $summary, EDIT_NEW
);
327 $status = $updater->getStatus();
329 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
330 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
331 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
332 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
336 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
337 * @covers \MediaWiki\Storage\PageUpdater::setBaseRevisionId()
339 public function testFailureOnBaseRevision() {
340 $user = $this->getTestUser()->getUser();
342 $title = $this->getDummyTitle( __METHOD__
);
344 // start editing non-existing page
345 $page = WikiPage
::factory( $title );
346 $updater = $page->newPageUpdater( $user );
348 // update for base revision 7 should fail
349 $summary = CommentStoreComment
::newUnsavedComment( 'udpate?!' );
350 $updater->setBaseRevisionId( 7 ); // expect page to exist
351 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
352 $updater->saveRevision( $summary );
353 $status = $updater->getStatus();
355 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
356 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
357 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
358 $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
361 $this->createRevision( $page, __METHOD__
);
363 // update for base revision 0 should fail
364 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
365 $updater = $page->newPageUpdater( $user );
366 $updater->setBaseRevisionId( 0 ); // expect page to not exist
367 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
368 $updater->saveRevision( $summary );
369 $status = $updater->getStatus();
371 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
372 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
373 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
374 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
377 public function provideSetRcPatrolStatus( $patrolled ) {
378 yield
[ RecentChange
::PRC_UNPATROLLED
];
379 yield
[ RecentChange
::PRC_AUTOPATROLLED
];
383 * @dataProvider provideSetRcPatrolStatus
384 * @covers \MediaWiki\Storage\PageUpdater::setRcPatrolStatus()
386 public function testSetRcPatrolStatus( $patrolled ) {
387 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
389 $user = $this->getTestUser()->getUser();
391 $title = $this->getDummyTitle( __METHOD__
);
393 $page = WikiPage
::factory( $title );
394 $updater = $page->newPageUpdater( $user );
396 $summary = CommentStoreComment
::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
397 $updater->setContent( 'main', new TextContent( 'Lorem ipsum ' . $patrolled ) );
398 $updater->setRcPatrolStatus( $patrolled );
399 $rev = $updater->saveRevision( $summary );
401 $rc = $revisionStore->getRecentChange( $rev );
402 $this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
406 * @covers \MediaWiki\Storage\PageUpdater::inheritSlot()
407 * @covers \MediaWiki\Storage\PageUpdater::setContent()
409 public function testInheritSlot() {
410 $user = $this->getTestUser()->getUser();
411 $title = $this->getDummyTitle( __METHOD__
);
412 $page = WikiPage
::factory( $title );
414 $updater = $page->newPageUpdater( $user );
415 $summary = CommentStoreComment
::newUnsavedComment( 'one' );
416 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
417 $rev1 = $updater->saveRevision( $summary, EDIT_NEW
);
419 $updater = $page->newPageUpdater( $user );
420 $summary = CommentStoreComment
::newUnsavedComment( 'two' );
421 $updater->setContent( 'main', new TextContent( 'Foo Bar' ) );
422 $rev2 = $updater->saveRevision( $summary, EDIT_UPDATE
);
424 $updater = $page->newPageUpdater( $user );
425 $summary = CommentStoreComment
::newUnsavedComment( 'three' );
426 $updater->inheritSlot( $rev1->getSlot( 'main' ) );
427 $rev3 = $updater->saveRevision( $summary, EDIT_UPDATE
);
429 $this->assertNotSame( $rev1->getId(), $rev3->getId() );
430 $this->assertNotSame( $rev2->getId(), $rev3->getId() );
432 $main1 = $rev1->getSlot( 'main' );
433 $main3 = $rev3->getSlot( 'main' );
435 $this->assertNotSame( $main1->getRevision(), $main3->getRevision() );
436 $this->assertSame( $main1->getAddress(), $main3->getAddress() );
437 $this->assertTrue( $main1->getContent()->equals( $main3->getContent() ) );
440 // TODO: MCR: test adding multiple slots, inheriting parent slots, and removing slots.
442 public function testSetUseAutomaticEditSummaries() {
443 $this->setContentLang( 'qqx' );
444 $user = $this->getTestUser()->getUser();
446 $title = $this->getDummyTitle( __METHOD__
);
447 $page = WikiPage
::factory( $title );
449 $updater = $page->newPageUpdater( $user );
450 $updater->setUseAutomaticEditSummaries( true );
451 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
453 // empty comment triggers auto-summary
454 $summary = CommentStoreComment
::newUnsavedComment( '' );
455 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
457 $rev = $updater->getNewRevision();
458 $comment = $rev->getComment( RevisionRecord
::RAW
);
459 $this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text
, 'comment text' );
461 // check that this also works when blanking the page
462 $updater = $page->newPageUpdater( $user );
463 $updater->setUseAutomaticEditSummaries( true );
464 $updater->setContent( 'main', new TextContent( '' ) );
466 $summary = CommentStoreComment
::newUnsavedComment( '' );
467 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
469 $rev = $updater->getNewRevision();
470 $comment = $rev->getComment( RevisionRecord
::RAW
);
471 $this->assertSame( '(autosumm-blank)', $comment->text
, 'comment text' );
473 // check that we can also disable edit-summaries
474 $title2 = $this->getDummyTitle( __METHOD__
. '/2' );
475 $page2 = WikiPage
::factory( $title2 );
477 $updater = $page2->newPageUpdater( $user );
478 $updater->setUseAutomaticEditSummaries( false );
479 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
481 $summary = CommentStoreComment
::newUnsavedComment( '' );
482 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
484 $rev = $updater->getNewRevision();
485 $comment = $rev->getComment( RevisionRecord
::RAW
);
486 $this->assertSame( '', $comment->text
, 'comment text should still be lank' );
488 // check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
489 $updater = $page2->newPageUpdater( $user );
490 $updater->setUseAutomaticEditSummaries( true );
491 $updater->setContent( 'main', new TextContent( '' ) );
493 $summary = CommentStoreComment
::newUnsavedComment( '' );
494 $updater->saveRevision( $summary, 0 );
496 $rev = $updater->getNewRevision();
497 $comment = $rev->getComment( RevisionRecord
::RAW
);
498 $this->assertSame( '', $comment->text
, 'comment text' );
501 public function provideSetUsePageCreationLog() {
502 yield
[ true, [ [ 'create', 'create' ] ] ];
507 * @dataProvider provideSetUsePageCreationLog
510 public function testSetUsePageCreationLog( $use, $expected ) {
511 $user = $this->getTestUser()->getUser();
512 $title = $this->getDummyTitle( __METHOD__
. ( $use ?
'_logged' : '_unlogged' ) );
513 $page = WikiPage
::factory( $title );
515 $updater = $page->newPageUpdater( $user );
516 $updater->setUsePageCreationLog( $use );
517 $summary = CommentStoreComment
::newUnsavedComment( 'cmt' );
518 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
519 $updater->saveRevision( $summary, EDIT_NEW
);
521 $rev = $updater->getNewRevision();
524 [ 'log_type', 'log_action' ],
525 [ 'log_page' => $rev->getPageId() ],