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 public static function setUpBeforeClass() {
23 parent
::setUpBeforeClass();
25 // force service reset!
26 MediaWikiServices
::getInstance()->resetServiceForTesting( 'RevisionStore' );
29 private function getDummyTitle( $method ) {
30 return Title
::newFromText( $method, $this->getDefaultWikitextNS() );
36 * @return null|RecentChange
38 private function getRecentChangeFor( $revId ) {
39 $qi = RecentChange
::getQueryInfo();
40 $row = $this->db
->selectRow(
43 [ 'rc_this_oldid' => $revId ],
49 return $row ? RecentChange
::newFromRow( $row ) : null;
52 // TODO: test setAjaxEditStash();
55 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
56 * @covers \WikiPage::newPageUpdater()
58 public function testCreatePage() {
59 $user = $this->getTestUser()->getUser();
61 $title = $this->getDummyTitle( __METHOD__
);
62 $page = WikiPage
::factory( $title );
63 $updater = $page->newPageUpdater( $user );
65 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
67 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
68 $this->assertFalse( $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
69 $this->assertSame( 0, $updater->getUndidRevisionId(), 'getUndidRevisionId' );
71 $updater->addTag( 'foo' );
72 $updater->addTags( [ 'bar', 'qux' ] );
74 $tags = $updater->getExplicitTags();
76 $this->assertSame( [ 'bar', 'foo', 'qux' ], $tags, 'getExplicitTags' );
78 // TODO: MCR: test additional slots
79 $content = new TextContent( 'Lorem Ipsum' );
80 $updater->setContent( 'main', $content );
82 $parent = $updater->grabParentRevision();
84 $this->assertNull( $parent, 'getParentRevision' );
85 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
87 // TODO: test that hasEditConflict() grabs the parent revision
88 $this->assertFalse( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
89 $this->assertTrue( $updater->hasEditConflict( 1 ), 'hasEditConflict' );
91 // TODO: test failure with EDIT_UPDATE
92 // TODO: test EDIT_MINOR, EDIT_BOT, etc
93 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
94 $rev = $updater->saveRevision( $summary );
96 $this->assertNotNull( $rev );
97 $this->assertSame( 0, $rev->getParentId() );
98 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
99 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
101 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
102 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
103 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
104 $this->assertTrue( $updater->isNew(), 'isNew()' );
105 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
106 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
107 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
109 $rev = $updater->getNewRevision();
110 $revContent = $rev->getContent( 'main' );
111 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
113 // were the WikiPage and Title objects updated?
114 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
115 $this->assertTrue( $title->exists(), 'Title::exists()' );
116 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
117 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
120 $page2 = WikiPage
::factory( $title );
121 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
122 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
123 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
126 $rc = $this->getRecentChangeFor( $rev->getId() );
127 $this->assertNotNull( $rc, 'RecentChange' );
129 // check site stats - this asserts that derived data updates where run.
130 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
131 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
132 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
134 // re-edit with same content - should be a "null-edit"
135 $updater = $page->newPageUpdater( $user );
136 $updater->setContent( 'main', $content );
138 $summary = CommentStoreComment
::newUnsavedComment( 'to to re-edit' );
139 $rev = $updater->saveRevision( $summary );
140 $status = $updater->getStatus();
142 $this->assertNull( $rev, 'getNewRevision()' );
143 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
144 $this->assertTrue( $updater->isUnchanged(), 'isUnchanged' );
145 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
146 $this->assertTrue( $status->isOK(), 'getStatus()->isOK()' );
147 $this->assertTrue( $status->hasMessage( 'edit-no-change' ), 'edit-no-change' );
151 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
152 * @covers \WikiPage::newPageUpdater()
154 public function testUpdatePage() {
155 $user = $this->getTestUser()->getUser();
157 $title = $this->getDummyTitle( __METHOD__
);
158 $this->insertPage( $title );
160 $page = WikiPage
::factory( $title );
161 $parentId = $page->getLatest();
163 $updater = $page->newPageUpdater( $user );
165 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
167 $updater->setOriginalRevisionId( 7 );
168 $this->assertSame( 7, $updater->getOriginalRevisionId(), 'getOriginalRevisionId' );
170 $this->assertFalse( $updater->hasEditConflict( $parentId ), 'hasEditConflict' );
171 $this->assertTrue( $updater->hasEditConflict( $parentId - 1 ), 'hasEditConflict' );
172 $this->assertTrue( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
174 // TODO: MCR: test additional slots
175 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
177 // TODO: test all flags for saveRevision()!
178 $summary = CommentStoreComment
::newUnsavedComment( 'Just a test' );
179 $rev = $updater->saveRevision( $summary );
181 $this->assertNotNull( $rev );
182 $this->assertSame( $parentId, $rev->getParentId() );
183 $this->assertSame( $summary->text
, $rev->getComment( RevisionRecord
::RAW
)->text
);
184 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord
::RAW
)->getName() );
186 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
187 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
188 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
189 $this->assertFalse( $updater->isNew(), 'isNew()' );
190 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
191 $this->assertInstanceOf( Revision
::class, $updater->getStatus()->value
['revision'] );
192 $this->assertFalse( $updater->isUnchanged(), 'isUnchanged()' );
194 // TODO: Test null revision (with different user): new revision!
196 $rev = $updater->getNewRevision();
197 $revContent = $rev->getContent( 'main' );
198 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
200 // were the WikiPage and Title objects updated?
201 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
202 $this->assertTrue( $title->exists(), 'Title::exists()' );
203 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getRevision()' );
204 $this->assertNotNull( $page->getRevision(), 'WikiPage::getRevision()' );
207 $page2 = WikiPage
::factory( $title );
208 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
209 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getRevision()' );
210 $this->assertNotNull( $page2->getRevision(), 'WikiPage::getRevision()' );
213 $rc = $this->getRecentChangeFor( $rev->getId() );
214 $this->assertNotNull( $rc, 'RecentChange' );
217 $updater = $page->newPageUpdater( $user );
218 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
220 $summary = CommentStoreComment
::newUnsavedComment( 're-edit' );
221 $updater->saveRevision( $summary );
222 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
223 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
225 // check site stats - this asserts that derived data updates where run.
226 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
227 $this->assertNotNull( $stats, 'site_stats' );
228 $this->assertSame( $oldStats->ss_total_pages +
0, (int)$stats->ss_total_pages
);
229 $this->assertSame( $oldStats->ss_total_edits +
2, (int)$stats->ss_total_edits
);
233 * Creates a revision in the database.
235 * @param WikiPage $page
237 * @param null|string|Content $content
239 * @return RevisionRecord|null
241 private function createRevision( WikiPage
$page, $summary, $content = null ) {
242 $user = $this->getTestUser()->getUser();
243 $comment = CommentStoreComment
::newUnsavedComment( $summary );
245 if ( !$content instanceof Content
) {
246 $content = new TextContent( $content ??
$summary );
249 $updater = $page->newPageUpdater( $user );
250 $updater->setContent( 'main', $content );
251 $rev = $updater->saveRevision( $comment );
256 * @covers \MediaWiki\Storage\PageUpdater::grabParentRevision()
257 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
259 public function testCompareAndSwapFailure() {
260 $user = $this->getTestUser()->getUser();
262 $title = $this->getDummyTitle( __METHOD__
);
264 // start editing non-existing page
265 $page = WikiPage
::factory( $title );
266 $updater = $page->newPageUpdater( $user );
267 $updater->grabParentRevision();
269 // create page concurrently
270 $concurrentPage = WikiPage
::factory( $title );
271 $this->createRevision( $concurrentPage, __METHOD__
. '-one' );
273 // try creating the page - should trigger CAS failure.
274 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
275 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
276 $updater->saveRevision( $summary );
277 $status = $updater->getStatus();
279 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
280 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
281 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
282 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-conflict' );
284 // start editing existing page
285 $page = WikiPage
::factory( $title );
286 $updater = $page->newPageUpdater( $user );
287 $updater->grabParentRevision();
289 // update page concurrently
290 $concurrentPage = WikiPage
::factory( $title );
291 $this->createRevision( $concurrentPage, __METHOD__
. '-two' );
293 // try creating the page - should trigger CAS failure.
294 $summary = CommentStoreComment
::newUnsavedComment( 'edit?!' );
295 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
296 $updater->saveRevision( $summary );
297 $status = $updater->getStatus();
299 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
300 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
301 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
302 $this->assertTrue( $status->hasMessage( 'edit-conflict' ), 'edit-conflict' );
306 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
308 public function testFailureOnEditFlags() {
309 $user = $this->getTestUser()->getUser();
311 $title = $this->getDummyTitle( __METHOD__
);
313 // start editing non-existing page
314 $page = WikiPage
::factory( $title );
315 $updater = $page->newPageUpdater( $user );
317 // update with EDIT_UPDATE flag should fail
318 $summary = CommentStoreComment
::newUnsavedComment( 'udpate?!' );
319 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
320 $updater->saveRevision( $summary, EDIT_UPDATE
);
321 $status = $updater->getStatus();
323 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
324 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
325 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
326 $this->assertTrue( $status->hasMessage( 'edit-gone-missing' ), 'edit-gone-missing' );
329 $this->createRevision( $page, __METHOD__
);
331 // update with EDIT_NEW flag should fail
332 $summary = CommentStoreComment
::newUnsavedComment( 'create?!' );
333 $updater = $page->newPageUpdater( $user );
334 $updater->setContent( 'main', new TextContent( 'dolor sit amet' ) );
335 $updater->saveRevision( $summary, EDIT_NEW
);
336 $status = $updater->getStatus();
338 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
339 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
340 $this->assertFalse( $status->isOK(), 'getStatus()->isOK()' );
341 $this->assertTrue( $status->hasMessage( 'edit-already-exists' ), 'edit-already-exists' );
344 public function provideSetRcPatrolStatus( $patrolled ) {
345 yield
[ RecentChange
::PRC_UNPATROLLED
];
346 yield
[ RecentChange
::PRC_AUTOPATROLLED
];
350 * @dataProvider provideSetRcPatrolStatus
351 * @covers \MediaWiki\Storage\PageUpdater::setRcPatrolStatus()
353 public function testSetRcPatrolStatus( $patrolled ) {
354 $revisionStore = MediaWikiServices
::getInstance()->getRevisionStore();
356 $user = $this->getTestUser()->getUser();
358 $title = $this->getDummyTitle( __METHOD__
);
360 $page = WikiPage
::factory( $title );
361 $updater = $page->newPageUpdater( $user );
363 $summary = CommentStoreComment
::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
364 $updater->setContent( 'main', new TextContent( 'Lorem ipsum ' . $patrolled ) );
365 $updater->setRcPatrolStatus( $patrolled );
366 $rev = $updater->saveRevision( $summary );
368 $rc = $revisionStore->getRecentChange( $rev );
369 $this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
373 * @covers \MediaWiki\Storage\PageUpdater::inheritSlot()
374 * @covers \MediaWiki\Storage\PageUpdater::setContent()
376 public function testInheritSlot() {
377 $user = $this->getTestUser()->getUser();
378 $title = $this->getDummyTitle( __METHOD__
);
379 $page = WikiPage
::factory( $title );
381 $updater = $page->newPageUpdater( $user );
382 $summary = CommentStoreComment
::newUnsavedComment( 'one' );
383 $updater->setContent( 'main', new TextContent( 'Lorem ipsum' ) );
384 $rev1 = $updater->saveRevision( $summary, EDIT_NEW
);
386 $updater = $page->newPageUpdater( $user );
387 $summary = CommentStoreComment
::newUnsavedComment( 'two' );
388 $updater->setContent( 'main', new TextContent( 'Foo Bar' ) );
389 $rev2 = $updater->saveRevision( $summary, EDIT_UPDATE
);
391 $updater = $page->newPageUpdater( $user );
392 $summary = CommentStoreComment
::newUnsavedComment( 'three' );
393 $updater->inheritSlot( $rev1->getSlot( 'main' ) );
394 $rev3 = $updater->saveRevision( $summary, EDIT_UPDATE
);
396 $this->assertNotSame( $rev1->getId(), $rev3->getId() );
397 $this->assertNotSame( $rev2->getId(), $rev3->getId() );
399 $main1 = $rev1->getSlot( 'main' );
400 $main3 = $rev3->getSlot( 'main' );
402 $this->assertNotSame( $main1->getRevision(), $main3->getRevision() );
403 $this->assertSame( $main1->getAddress(), $main3->getAddress() );
404 $this->assertTrue( $main1->getContent()->equals( $main3->getContent() ) );
407 // TODO: MCR: test adding multiple slots, inheriting parent slots, and removing slots.
409 public function testSetUseAutomaticEditSummaries() {
410 $this->setContentLang( 'qqx' );
411 $user = $this->getTestUser()->getUser();
413 $title = $this->getDummyTitle( __METHOD__
);
414 $page = WikiPage
::factory( $title );
416 $updater = $page->newPageUpdater( $user );
417 $updater->setUseAutomaticEditSummaries( true );
418 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
420 // empty comment triggers auto-summary
421 $summary = CommentStoreComment
::newUnsavedComment( '' );
422 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
424 $rev = $updater->getNewRevision();
425 $comment = $rev->getComment( RevisionRecord
::RAW
);
426 $this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text
, 'comment text' );
428 // check that this also works when blanking the page
429 $updater = $page->newPageUpdater( $user );
430 $updater->setUseAutomaticEditSummaries( true );
431 $updater->setContent( 'main', new TextContent( '' ) );
433 $summary = CommentStoreComment
::newUnsavedComment( '' );
434 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
436 $rev = $updater->getNewRevision();
437 $comment = $rev->getComment( RevisionRecord
::RAW
);
438 $this->assertSame( '(autosumm-blank)', $comment->text
, 'comment text' );
440 // check that we can also disable edit-summaries
441 $title2 = $this->getDummyTitle( __METHOD__
. '/2' );
442 $page2 = WikiPage
::factory( $title2 );
444 $updater = $page2->newPageUpdater( $user );
445 $updater->setUseAutomaticEditSummaries( false );
446 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
448 $summary = CommentStoreComment
::newUnsavedComment( '' );
449 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY
);
451 $rev = $updater->getNewRevision();
452 $comment = $rev->getComment( RevisionRecord
::RAW
);
453 $this->assertSame( '', $comment->text
, 'comment text should still be lank' );
455 // check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
456 $updater = $page2->newPageUpdater( $user );
457 $updater->setUseAutomaticEditSummaries( true );
458 $updater->setContent( 'main', new TextContent( '' ) );
460 $summary = CommentStoreComment
::newUnsavedComment( '' );
461 $updater->saveRevision( $summary, 0 );
463 $rev = $updater->getNewRevision();
464 $comment = $rev->getComment( RevisionRecord
::RAW
);
465 $this->assertSame( '', $comment->text
, 'comment text' );
468 public function provideSetUsePageCreationLog() {
469 yield
[ true, [ [ 'create', 'create' ] ] ];
474 * @dataProvider provideSetUsePageCreationLog
477 public function testSetUsePageCreationLog( $use, $expected ) {
478 $user = $this->getTestUser()->getUser();
479 $title = $this->getDummyTitle( __METHOD__
. ( $use ?
'_logged' : '_unlogged' ) );
480 $page = WikiPage
::factory( $title );
482 $updater = $page->newPageUpdater( $user );
483 $updater->setUsePageCreationLog( $use );
484 $summary = CommentStoreComment
::newUnsavedComment( 'cmt' );
485 $updater->setContent( 'main', new TextContent( 'Lorem Ipsum' ) );
486 $updater->saveRevision( $summary, EDIT_NEW
);
488 $rev = $updater->getNewRevision();
491 [ 'log_type', 'log_action' ],
492 [ 'log_page' => $rev->getPageId() ],