Debian Codegraph @

tests/phpunit/includes/Storage/PageUpdaterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use LogicException;
6 use MediaWiki\CommentStore\CommentStoreComment;
7 use MediaWiki\Content\Content;
8 use MediaWiki\Content\TextContent;
9 use MediaWiki\Content\WikitextContent;
10 use MediaWiki\Deferred\DeferredUpdates;
11 use MediaWiki\Json\FormatJson;
12 use MediaWiki\Message\Message;
13 use MediaWiki\Page\PageIdentityValue;
14 use MediaWiki\Parser\ParserOptions;
15 use MediaWiki\Revision\RenderedRevision;
16 use MediaWiki\Revision\RevisionRecord;
17 use MediaWiki\Revision\SlotRecord;
18 use MediaWiki\Status\Status;
19 use MediaWiki\Storage\EditResult;
20 use MediaWiki\Title\Title;
21 use MediaWiki\User\User;
22 use MediaWiki\User\UserIdentity;
23 use MediaWikiIntegrationTestCase;
24 use RecentChange;
25 use WikiPage;
26
27 /**
28 * @covers \MediaWiki\Storage\PageUpdater
29 * @group Database
30 */
31 class PageUpdaterTest extends MediaWikiIntegrationTestCase {
32
33 protected function setUp(): void {
34 parent::setUp();
35
36 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
37
38 if ( !$slotRoleRegistry->isDefinedRole( 'aux' ) ) {
39 $slotRoleRegistry->defineRoleWithModel(
40 'aux',
41 CONTENT_MODEL_WIKITEXT
42 );
43 }
44
45 if ( !$slotRoleRegistry->isDefinedRole( 'derivedslot' ) ) {
46 $slotRoleRegistry->defineRoleWithModel(
47 'derivedslot',
48 CONTENT_MODEL_WIKITEXT,
49 [],
50 true
51 );
52 }
53 }
54
55 private function getDummyTitle( $method ) {
56 return Title::newFromText( $method, $this->getDefaultWikitextNS() );
57 }
58
59 /**
60 * @param int $revId
61 *
62 * @return null|RecentChange
63 */
64 private function getRecentChangeFor( $revId ) {
65 $qi = RecentChange::getQueryInfo();
66 $row = $this->getDb()->newSelectQueryBuilder()
67 ->queryInfo( $qi )
68 ->where( [ 'rc_this_oldid' => $revId ] )
69 ->caller( __METHOD__ )
70 ->fetchRow();
71
72 return $row ? RecentChange::newFromRow( $row ) : null;
73 }
74
75 /**
76 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
77 * @covers \WikiPage::newPageUpdater()
78 */
79 public function testCreatePage() {
80 $user = $this->getTestUser()->getUser();
81 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
82
83 $title = $this->getDummyTitle( __METHOD__ );
84 $page = $wikiPageFactory->newFromTitle( $title );
85 $updater = $page->newPageUpdater( $user );
86
87 $oldStats = $this->getDb()->newSelectQueryBuilder()
88 ->select( '*' )
89 ->from( 'site_stats' )
90 ->where( '1=1' )
91 ->fetchRow();
92
93 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
94
95 $updater->addTag( 'foo' );
96 $updater->addTags( [ 'bar', 'qux' ] );
97
98 $tags = $updater->getExplicitTags();
99 sort( $tags );
100 $this->assertSame( [ 'bar', 'foo', 'qux' ], $tags, 'getExplicitTags' );
101
102 // TODO: MCR: test additional slots
103 $content = new TextContent( 'Lorem Ipsum' );
104 $updater->setContent( SlotRecord::MAIN, $content );
105
106 $parent = $updater->grabParentRevision();
107
108 $this->assertNull( $parent, 'getParentRevision' );
109 $this->assertFalse( $updater->wasCommitted(), 'wasCommitted' );
110
111 // TODO: test that hasEditConflict() grabs the parent revision
112 $this->assertFalse( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
113 $this->assertTrue( $updater->hasEditConflict( 1 ), 'hasEditConflict' );
114
115 // TODO: test failure with EDIT_UPDATE
116 // TODO: test EDIT_BOT, etc
117 $updater->setFlags( EDIT_MINOR );
118 $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
119 $rev = $updater->saveRevision( $summary );
120
121 $this->assertNotNull( $rev );
122 $this->assertSame( 0, $rev->getParentId() );
123 $this->assertSame( $summary->text, $rev->getComment( RevisionRecord::RAW )->text );
124 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord::RAW )->getName() );
125
126 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
127 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
128 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
129 $this->assertTrue( $updater->isNew(), 'isNew()' );
130 $this->assertTrue( $updater->wasRevisionCreated(), 'wasRevisionCreated()' );
131 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
132 $this->assertInstanceOf(
133 RevisionRecord::class,
134 $updater->getStatus()->getNewRevision()
135 );
136
137 // check the EditResult object
138 $this->assertFalse( $updater->getEditResult()->getOriginalRevisionId(),
139 'EditResult::getOriginalRevisionId()' );
140 $this->assertSame( 0, $updater->getEditResult()->getUndidRevId(),
141 'EditResult::getUndidRevId()' );
142 $this->assertTrue( $updater->getEditResult()->isNew(), 'EditResult::isNew()' );
143 $this->assertFalse( $updater->getEditResult()->isRevert(), 'EditResult::isRevert()' );
144
145 $rev = $updater->getNewRevision();
146 $revContent = $rev->getContent( SlotRecord::MAIN );
147 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
148 $this->assertTrue( $rev->isMinor(), 'RevisionRecord::isMinor()' );
149
150 // were the WikiPage and Title objects updated?
151 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
152 $this->assertTrue( $title->exists(), 'Title::exists()' );
153 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getLatest()' );
154 $this->assertNotNull( $page->getRevisionRecord(), 'WikiPage::getRevisionRecord()' );
155
156 // re-load
157 $page2 = $wikiPageFactory->newFromTitle( $title );
158 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
159 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getLatest()' );
160 $this->assertNotNull( $page2->getRevisionRecord(), 'WikiPage::getRevisionRecord()' );
161
162 // Check RC entry
163 $rc = $this->getRecentChangeFor( $rev->getId() );
164 $this->assertNotNull( $rc, 'RecentChange' );
165
166 // check site stats - this asserts that derived data updates where run.
167 $stats = $this->getDb()->newSelectQueryBuilder()
168 ->select( '*' )
169 ->from( 'site_stats' )
170 ->where( '1=1' )
171 ->fetchRow();
172 $this->assertSame( $oldStats->ss_total_pages + 1, (int)$stats->ss_total_pages );
173 $this->assertSame( $oldStats->ss_total_edits + 1, (int)$stats->ss_total_edits );
174
175 // re-edit with same content - should be a "null-edit"
176 $updater = $page->newPageUpdater( $user )
177 ->setContent( SlotRecord::MAIN, $content );
178
179 $summary = CommentStoreComment::newUnsavedComment( 're-edit' );
180 $rev = $updater->saveRevision( $summary );
181 $status = $updater->getStatus();
182
183 $this->assertNull( $rev, 'getNewRevision()' );
184 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
185 $this->assertFalse( $updater->wasRevisionCreated(), 'wasRevisionCreated' );
186 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
187 $this->assertStatusWarning( 'edit-no-change', $status );
188 }
189
190 /**
191 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
192 * @covers \WikiPage::newPageUpdater()
193 */
194 public function testUpdatePage() {
195 $user = $this->getTestUser()->getUser();
196
197 $title = $this->getDummyTitle( __METHOD__ );
198 $this->insertPage( $title );
199 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
200
201 $page = $wikiPageFactory->newFromTitle( $title );
202 $parentId = $page->getLatest();
203
204 $updater = $page->newPageUpdater( $user );
205
206 $oldStats = $this->getDb()->newSelectQueryBuilder()
207 ->select( '*' )
208 ->from( 'site_stats' )
209 ->where( '1=1' )
210 ->fetchRow();
211
212 $updater->setOriginalRevisionId( 7 );
213
214 $this->assertFalse( $updater->hasEditConflict( $parentId ), 'hasEditConflict' );
215 $this->assertTrue( $updater->hasEditConflict( $parentId - 1 ), 'hasEditConflict' );
216 $this->assertTrue( $updater->hasEditConflict( 0 ), 'hasEditConflict' );
217
218 // TODO: MCR: test additional slots
219 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
220
221 // Check that prepareUpdate() does not fail, and the flag is applied.
222 $updater->prepareUpdate( EDIT_MINOR );
223
224 // TODO: test all flags for saveRevision()!
225 $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
226 $rev = $updater->saveRevision( $summary );
227
228 $this->assertNotNull( $rev );
229 $this->assertSame( $parentId, $rev->getParentId() );
230 $this->assertSame( $summary->text, $rev->getComment( RevisionRecord::RAW )->text );
231 $this->assertSame( $user->getName(), $rev->getUser( RevisionRecord::RAW )->getName() );
232
233 $this->assertTrue( $updater->wasCommitted(), 'wasCommitted()' );
234 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
235 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
236 $this->assertFalse( $updater->isNew(), 'isNew()' );
237 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
238 $this->assertInstanceOf(
239 RevisionRecord::class,
240 $updater->getStatus()->getNewRevision()
241 );
242 $this->assertTrue( $updater->wasRevisionCreated(), 'wasRevisionCreated()' );
243
244 // check the EditResult object
245 $this->assertSame( 7, $updater->getEditResult()->getOriginalRevisionId(),
246 'EditResult::getOriginalRevisionId()' );
247 $this->assertSame( 0, $updater->getEditResult()->getUndidRevId(),
248 'EditResult::getUndidRevId()' );
249 $this->assertFalse( $updater->getEditResult()->isNew(), 'EditResult::isNew()' );
250 $this->assertFalse( $updater->getEditResult()->isRevert(), 'EditResult::isRevert()' );
251
252 // TODO: Test null revision (with different user): new revision!
253
254 $rev = $updater->getNewRevision();
255 $revContent = $rev->getContent( SlotRecord::MAIN );
256 $this->assertSame( 'Lorem Ipsum', $revContent->serialize(), 'revision content' );
257 $this->assertTrue( $rev->isMinor(), 'RevisionRecord::isMinor()' );
258
259 // were the WikiPage and Title objects updated?
260 $this->assertTrue( $page->exists(), 'WikiPage::exists()' );
261 $this->assertTrue( $title->exists(), 'Title::exists()' );
262 $this->assertSame( $rev->getId(), $page->getLatest(), 'WikiPage::getLatest()' );
263 $this->assertNotNull( $page->getRevisionRecord(), 'WikiPage::getRevisionRecord()' );
264
265 // re-load
266 $page2 = $wikiPageFactory->newFromTitle( $title );
267 $this->assertTrue( $page2->exists(), 'WikiPage::exists()' );
268 $this->assertSame( $rev->getId(), $page2->getLatest(), 'WikiPage::getLatest()' );
269 $this->assertNotNull( $page2->getRevisionRecord(), 'WikiPage::getRevisionRecord()' );
270
271 // Check RC entry
272 $rc = $this->getRecentChangeFor( $rev->getId() );
273 $this->assertNotNull( $rc, 'RecentChange' );
274
275 // re-edit
276 $updater = $page->newPageUpdater( $user )
277 ->setContent( SlotRecord::MAIN, new TextContent( 'dolor sit amet' ) );
278
279 $summary = CommentStoreComment::newUnsavedComment( 're-edit' );
280 $updater->saveRevision( $summary );
281 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
282 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
283 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
284
285 $topRevisionId = $updater->getNewRevision()->getId();
286
287 // perform a null edit
288 $updater = $page->newPageUpdater( $user )
289 ->setContent( SlotRecord::MAIN, new TextContent( 'dolor sit amet' ) );
290 $summary = CommentStoreComment::newUnsavedComment( 'null edit' );
291 $updater->saveRevision( $summary );
292
293 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
294 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
295 $this->assertFalse( $updater->wasRevisionCreated(), 'wasRevisionCreated()' );
296 $this->assertTrue(
297 $updater->getEditResult()->isNullEdit(),
298 'getEditResult()->isNullEdit()'
299 );
300 $this->assertSame(
301 $topRevisionId,
302 $updater->getEditResult()->getOriginalRevisionId(),
303 'getEditResult()->getOriginalRevisionId()'
304 );
305
306 // check site stats - this asserts that derived data updates where run.
307 $stats = $this->getDb()->newSelectQueryBuilder()
308 ->select( '*' )
309 ->from( 'site_stats' )
310 ->where( '1=1' )
311 ->fetchRow();
312 $this->assertNotNull( $stats, 'site_stats' );
313 $this->assertSame( $oldStats->ss_total_pages + 0, (int)$stats->ss_total_pages );
314 $this->assertSame( $oldStats->ss_total_edits + 2, (int)$stats->ss_total_edits );
315 }
316
317 public function testSetForceEmptyRevisionSetsOriginalRevisionId() {
318 $user = $this->getTestUser()->getUser();
319 $title = $this->getDummyTitle( __METHOD__ );
320 $this->insertPage( $title );
321 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
322 $parentId = $page->getLatest();
323 $updater = $page->newPageUpdater( $user );
324 $updater->setForceEmptyRevision( true );
325 // Saving without changing the content should now create a new revisiopns
326 $summary = CommentStoreComment::newUnsavedComment( 'dummy revision' );
327 $rev = $updater->saveRevision( $summary );
328 $status = $updater->getStatus();
329 $this->assertNotNull( $rev, 'getNewRevision()' );
330 $this->assertNotNull( $updater->getNewRevision(), 'getNewRevision()' );
331 $this->assertNotSame( $parentId, $rev->getId(), 'new revision ID' );
332 $this->assertTrue( $updater->wasRevisionCreated(), 'wasRevisionCreated' );
333 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
334 $this->assertStatusGood( $status );
335 // Setting setForceEmptyRevision causes the original revision to be set.
336 $this->assertEquals( $parentId, $updater->getEditResult()->getOriginalRevisionId() );
337 }
338
339 public function testSetForceEmptyRevisionCausesSaveToFailWithChangedContent() {
340 $user = $this->getTestUser()->getUser();
341 $title = $this->getDummyTitle( __METHOD__ );
342 $this->insertPage( $title );
343 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
344 $updater = $page->newPageUpdater( $user );
345 $updater->setForceEmptyRevision( true );
346 // Setting setForceEmptyRevision causes saveRevision() to fail if the content is changed.
347 // The positive case with setForceEmptyRevision() causing a new revision to be created
348 // is tested
349 $this->expectException( LogicException::class );
350 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Changed Content' ) );
351 $summary = CommentStoreComment::newUnsavedComment( 'dummy revision' );
352 $updater->saveRevision( $summary );
353 }
354
355 public function testRevert() {
356 // Setup a page with some edits
357 $page = $this->getExistingTestPage( __METHOD__ );
358
359 $user = $this->getTestUser()->getUser();
360
361 $summary = CommentStoreComment::newUnsavedComment( '1' );
362 $updater = $page->newPageUpdater( $user )
363 ->setContent( SlotRecord::MAIN, new TextContent( '1' ) );
364 $updater->saveRevision( $summary );
365 $revId1 = $updater->getNewRevision()->getId();
366
367 $summary = CommentStoreComment::newUnsavedComment( '2' );
368 $updater = $page->newPageUpdater( $user )
369 ->setContent( SlotRecord::MAIN, new TextContent( '2' ) );
370 $updater->saveRevision( $summary );
371 $revId2 = $updater->getNewRevision()->getId();
372
373 // Perform a rollback
374 $updater = $page->newPageUpdater( $this->getTestSysop()->getUser() )
375 ->setContent( SlotRecord::MAIN, new TextContent( '1' ) )
376 ->markAsRevert( EditResult::REVERT_ROLLBACK, $revId2, $revId1 );
377 $summary = CommentStoreComment::newUnsavedComment( 'revert' );
378 $updater->saveRevision( $summary );
379
380 // Do some basic assertions on PageUpdater
381 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
382 $this->assertTrue( $updater->getStatus()->isOK(), 'getStatus()->isOK()' );
383
384 $editResult = $updater->getEditResult();
385 $this->assertNotNull( $editResult, 'getEditResult()' );
386 $this->assertTrue( $editResult->isRevert(), 'EditResult::isRevert()' );
387 $this->assertTrue( $editResult->isExactRevert(), 'EditResult::isExactRevert()' );
388 $this->assertSame(
389 $revId1,
390 $editResult->getOriginalRevisionId(),
391 'EditResult::getOriginalRevisionId()'
392 );
393 $this->assertSame(
394 EditResult::REVERT_ROLLBACK,
395 $editResult->getRevertMethod(),
396 'EditResult::getRevertMethod()'
397 );
398 $this->assertSame(
399 $revId2,
400 $editResult->getOldestRevertedRevisionId(),
401 'EditResult::getOldestRevertedRevisionId()'
402 );
403 $this->assertSame(
404 $revId2,
405 $editResult->getNewestRevertedRevisionId(),
406 'EditResult::getNewestRevertedRevisionId()'
407 );
408
409 // Ensure all deferred updates are run
410 DeferredUpdates::doUpdates();
411
412 // Retrieve the mw-rollback change tag and verify it
413 $newRevId = $updater->getNewRevision()->getId();
414 $this->newSelectQueryBuilder()
415 ->select( 'ct_params' )
416 ->from( 'change_tag' )
417 ->where( [ 'ct_rev_id' => $newRevId ] )
418 ->assertFieldValue( FormatJson::encode( $editResult ) );
419 }
420
421 /**
422 * Creates a revision in the database.
423 *
424 * @param WikiPage $page
425 * @param string|Message|CommentStoreComment $summary
426 * @param null|string|Content $content
427 *
428 * @return RevisionRecord|null
429 */
430 private function createRevision( WikiPage $page, $summary, $content = null ) {
431 $user = $this->getTestUser()->getUser();
432
433 $comment = CommentStoreComment::newUnsavedComment( $summary );
434
435 if ( !$content instanceof Content ) {
436 $content = new TextContent( $content ?? $summary );
437 }
438
439 return $page->newPageUpdater( $user )
440 ->setContent( SlotRecord::MAIN, $content )
441 ->saveRevision( $comment );
442 }
443
444 /**
445 * Verify that MultiContentSave hook is called by saveRevision() with correct parameters.
446 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
447 */
448 public function testMultiContentSaveHook() {
449 $user = $this->getTestUser()->getUser();
450
451 $title = $this->getDummyTitle( __METHOD__ );
452
453 // TODO: MCR: test additional slots
454 $slots = [
455 SlotRecord::MAIN => new TextContent( 'Lorem Ipsum' )
456 ];
457
458 // start editing non-existing page
459 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
460 $updater = $page->newPageUpdater( $user );
461 foreach ( $slots as $slot => $content ) {
462 $updater->setContent( $slot, $content );
463 }
464
465 $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
466
467 $expected = [
468 'user' => $user,
469 'title' => $title,
470 'slots' => $slots,
471 'summary' => $summary
472 ];
473 $hookFired = false;
474 $this->setTemporaryHook( 'MultiContentSave',
475 function ( RenderedRevision $renderedRevision, UserIdentity $user,
476 $summary, $flags, Status $hookStatus
477 ) use ( &$hookFired, $expected ) {
478 $hookFired = true;
479
480 $this->assertSame( $expected['summary'], $summary );
481 $this->assertSame( EDIT_NEW, $flags );
482
483 $this->assertTrue(
484 $expected['title']->isSamePageAs( $renderedRevision->getRevision()->getPage() )
485 );
486 $this->assertTrue(
487 $expected['title']->isSameLinkAs( $renderedRevision->getRevision()->getPageAsLinkTarget() )
488 );
489
490 $slots = $renderedRevision->getRevision()->getSlots();
491 foreach ( $expected['slots'] as $slot => $content ) {
492 $this->assertSame( $content, $slots->getSlot( $slot )->getContent() );
493 }
494
495 // Don't abort this edit.
496 return true;
497 }
498 );
499
500 $rev = $updater->saveRevision( $summary );
501 $this->assertTrue( $hookFired, "MultiContentSave hook wasn't called." );
502 $this->assertNotNull( $rev,
503 "MultiContentSave returned true, but revision wasn't created." );
504 }
505
506 /**
507 * Verify that MultiContentSave hook can abort saveRevision() by returning false.
508 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
509 */
510 public function testMultiContentSaveHookAbort() {
511 $user = $this->getTestUser()->getUser();
512 $title = $this->getDummyTitle( __METHOD__ );
513
514 // start editing non-existing page
515 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
516 $updater = $page->newPageUpdater( $user )
517 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
518
519 $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
520
521 $expectedError = 'aborted-by-test-hook';
522 $this->setTemporaryHook( 'MultiContentSave',
523 static function ( RenderedRevision $renderedRevision, UserIdentity $user,
524 $summary, $flags, Status $hookStatus
525 ) use ( $expectedError ) {
526 $hookStatus->fatal( $expectedError );
527
528 // Returning false should disallow saveRevision() to continue saving this revision.
529 return false;
530 }
531 );
532
533 $rev = $updater->saveRevision( $summary );
534 $this->assertNull( $rev,
535 "MultiContentSave returned false, but revision was still created." );
536
537 $status = $updater->getStatus();
538 $this->assertStatusError( $expectedError, $status,
539 "MultiContentSave returned false, but Status is not fatal." );
540 }
541
542 /**
543 * @covers \MediaWiki\Storage\PageUpdater::grabParentRevision()
544 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
545 */
546 public function testCompareAndSwapFailure() {
547 $user = $this->getTestUser()->getUser();
548
549 $title = $this->getDummyTitle( __METHOD__ );
550 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
551
552 // start editing non-existing page
553 $page = $wikiPageFactory->newFromTitle( $title );
554 $updater = $page->newPageUpdater( $user );
555 $updater->grabParentRevision();
556
557 // create page concurrently
558 $concurrentPage = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
559 $this->createRevision( $concurrentPage, __METHOD__ . '-one' );
560
561 // try creating the page - should trigger CAS failure.
562 $summary = CommentStoreComment::newUnsavedComment( 'create?!' );
563 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
564 $updater->saveRevision( $summary );
565 $status = $updater->getStatus();
566
567 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
568 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
569 $this->assertStatusError( 'edit-already-exists', $status, 'edit-conflict' );
570
571 // start editing existing page
572 $page = $wikiPageFactory->newFromTitle( $title );
573 $updater = $page->newPageUpdater( $user );
574 $updater->grabParentRevision();
575
576 // update page concurrently
577 $concurrentPage = $wikiPageFactory->newFromTitle( $title );
578 $this->createRevision( $concurrentPage, __METHOD__ . '-two' );
579
580 // try creating the page - should trigger CAS failure.
581 $summary = CommentStoreComment::newUnsavedComment( 'edit?!' );
582 $updater->setContent( SlotRecord::MAIN, new TextContent( 'dolor sit amet' ) );
583 $updater->saveRevision( $summary );
584 $status = $updater->getStatus();
585
586 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
587 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
588 $this->assertStatusError( 'edit-conflict', $status, 'edit-conflict' );
589 }
590
591 /**
592 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
593 */
594 public function testFailureOnEditFlags() {
595 $user = $this->getTestUser()->getUser();
596
597 $title = $this->getDummyTitle( __METHOD__ );
598
599 // start editing non-existing page
600 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
601 $updater = $page->newPageUpdater( $user );
602
603 // update with EDIT_UPDATE flag should fail
604 $summary = CommentStoreComment::newUnsavedComment( 'udpate?!' );
605 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
606 $updater->saveRevision( $summary, EDIT_UPDATE );
607 $status = $updater->getStatus();
608
609 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
610 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
611 $this->assertStatusError( 'edit-gone-missing', $status, 'edit-gone-missing' );
612
613 // create the page
614 $this->createRevision( $page, __METHOD__ );
615
616 // update with EDIT_NEW flag should fail
617 $summary = CommentStoreComment::newUnsavedComment( 'create?!' );
618 $updater = $page->newPageUpdater( $user )
619 ->setContent( SlotRecord::MAIN, new TextContent( 'dolor sit amet' ) );
620 $updater->saveRevision( $summary, EDIT_NEW );
621 $status = $updater->getStatus();
622
623 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
624 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
625 $this->assertStatusError( 'edit-already-exists', $status, 'edit-already-exists' );
626 }
627
628 /**
629 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
630 */
631 public function testFailureOnBadContentModel() {
632 $user = $this->getTestUser()->getUser();
633
634 $title = $this->getDummyTitle( __METHOD__ );
635
636 // start editing non-existing page
637 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
638
639 // plain text content should fail in aux slot (the main slot doesn't care)
640 $updater = $page->newPageUpdater( $user )
641 ->setContent( SlotRecord::MAIN, new TextContent( 'Main Content' ) )
642 ->setContent( 'aux', new TextContent( 'Aux Content' ) );
643
644 $summary = CommentStoreComment::newUnsavedComment( 'udpate?!' );
645 $updater->saveRevision( $summary, EDIT_UPDATE );
646 $status = $updater->getStatus();
647
648 $this->assertFalse( $updater->wasSuccessful(), 'wasSuccessful()' );
649 $this->assertNull( $updater->getNewRevision(), 'getNewRevision()' );
650 $this->assertStatusError( 'content-not-allowed-here', $status );
651 }
652
653 public static function provideSetRcPatrolStatus( $patrolled ) {
654 yield [ RecentChange::PRC_UNPATROLLED ];
655 yield [ RecentChange::PRC_AUTOPATROLLED ];
656 }
657
658 /**
659 * @dataProvider provideSetRcPatrolStatus
660 * @covers \MediaWiki\Storage\PageUpdater::setRcPatrolStatus()
661 */
662 public function testSetRcPatrolStatus( $patrolled ) {
663 $revisionStore = $this->getServiceContainer()->getRevisionStore();
664
665 $user = $this->getTestUser()->getUser();
666
667 $title = $this->getDummyTitle( __METHOD__ );
668
669 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
670 $summary = CommentStoreComment::newUnsavedComment( 'Lorem ipsum ' . $patrolled );
671 $rev = $page->newPageUpdater( $user )
672 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum ' . $patrolled ) )
673 ->setRcPatrolStatus( $patrolled )
674 ->saveRevision( $summary );
675
676 $rc = $revisionStore->getRecentChange( $rev );
677 $this->assertEquals( $patrolled, $rc->getAttribute( 'rc_patrolled' ) );
678 }
679
680 /**
681 * @covers \MediaWiki\Storage\PageUpdater::makeNewRevision()
682 */
683 public function testStalePageID() {
684 $user = $this->getTestUser()->getUser();
685
686 $title = $this->getDummyTitle( __METHOD__ );
687 $summary = CommentStoreComment::newUnsavedComment( 'testing...' );
688 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
689
690 // Create page
691 $page = $wikiPageFactory->newFromTitle( $title );
692 $updater = $page->newPageUpdater( $user )
693 ->setContent( SlotRecord::MAIN, new TextContent( 'Content 1' ) );
694 $updater->saveRevision( $summary, EDIT_NEW );
695 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
696
697 // Create a clone of $title and $page.
698 $title = Title::makeTitle( $title->getNamespace(), $title->getDBkey() );
699 $page = $wikiPageFactory->newFromTitle( $title );
700
701 // start editing existing page using bad page ID
702 $updater = $page->newPageUpdater( $user );
703 $updater->grabParentRevision();
704
705 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Content 2' ) );
706
707 // Force the article ID to something invalid,
708 // to emulate confusion due to a page move.
709 $title->resetArticleID( 886655 );
710
711 @$updater->saveRevision( $summary, EDIT_UPDATE );
712
713 $this->assertTrue( $updater->wasSuccessful(), 'wasSuccessful()' );
714 }
715
716 /**
717 * @covers \MediaWiki\Storage\PageUpdater::inheritSlot()
718 * @covers \MediaWiki\Storage\PageUpdater::setContent()
719 */
720 public function testInheritSlot() {
721 $user = $this->getTestUser()->getUser();
722
723 $title = $this->getDummyTitle( __METHOD__ );
724 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
725
726 $updater = $page->newPageUpdater( $user );
727 $summary = CommentStoreComment::newUnsavedComment( 'one' );
728 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
729 $rev1 = $updater->saveRevision( $summary, EDIT_NEW );
730
731 $updater = $page->newPageUpdater( $user );
732 $summary = CommentStoreComment::newUnsavedComment( 'two' );
733 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Foo Bar' ) );
734 $rev2 = $updater->saveRevision( $summary, EDIT_UPDATE );
735
736 $updater = $page->newPageUpdater( $user );
737 $summary = CommentStoreComment::newUnsavedComment( 'three' );
738 $updater->inheritSlot( $rev1->getSlot( SlotRecord::MAIN ) );
739 $rev3 = $updater->saveRevision( $summary, EDIT_UPDATE );
740
741 $this->assertNotSame( $rev1->getId(), $rev3->getId() );
742 $this->assertNotSame( $rev2->getId(), $rev3->getId() );
743
744 $main1 = $rev1->getSlot( SlotRecord::MAIN );
745 $main3 = $rev3->getSlot( SlotRecord::MAIN );
746
747 $this->assertNotSame( $main1->getRevision(), $main3->getRevision() );
748 $this->assertSame( $main1->getAddress(), $main3->getAddress() );
749 $this->assertTrue( $main1->getContent()->equals( $main3->getContent() ) );
750 }
751
752 /**
753 * @covers \MediaWiki\Storage\PageUpdater::setSlot()
754 * @covers \MediaWiki\Storage\PageUpdater::updateRevision()
755 */
756 public function testUpdatingDerivedSlot() {
757 $user = $this->getTestUser()->getUser();
758 $title = $this->getDummyTitle( __METHOD__ );
759 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
760
761 $updater = $page->newPageUpdater( $user );
762 $summary = CommentStoreComment::newUnsavedComment( 'one' );
763 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
764 $updater->saveRevision( $summary, EDIT_NEW );
765
766 $updater = $page->newPageUpdater( $user );
767 $content = new WikitextContent( 'A' );
768 $derived = SlotRecord::newDerived( 'derivedslot', $content );
769 $updater->setSlot( $derived );
770 $updater->updateRevision();
771
772 $status = $updater->getStatus();
773 $this->assertStatusGood( $status );
774 $rev = $status->getNewRevision();
775 $slot = $rev->getSlot( 'derivedslot' );
776 $this->assertTrue( $slot->getContent()->equals( $content ) );
777 }
778
779 /**
780 * @covers \MediaWiki\Storage\PageUpdater::setSlot()
781 * @covers \MediaWiki\Storage\PageUpdater::updateRevision()
782 */
783 public function testUpdatingDerivedSlotCurrentRevision() {
784 $user = $this->getTestUser()->getUser();
785 $title = $this->getDummyTitle( __METHOD__ );
786 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
787
788 $updater = $page->newPageUpdater( $user );
789 $summary = CommentStoreComment::newUnsavedComment( 'one' );
790 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
791 $rev1 = $updater->saveRevision( $summary, EDIT_NEW );
792
793 $updater = $page->newPageUpdater( $user );
794 $content = new WikitextContent( 'A' );
795 $derived = SlotRecord::newDerived( 'derivedslot', $content );
796 $updater->setSlot( $derived );
797 $updater->updateRevision( $rev1->getId( $rev1->getWikiId() ) );
798
799 $rev2 = $updater->getStatus()->getNewRevision();
800 $slot = $rev2->getSlot( 'derivedslot' );
801 $this->assertTrue( $slot->getContent()->equals( $content ) );
802 }
803
804 /**
805 * @covers \MediaWiki\Storage\PageUpdater::setSlot()
806 * @covers \MediaWiki\Storage\PageUpdater::updateRevision()
807 */
808 public function testUpdatingDerivedSlotOldRevision() {
809 $user = $this->getTestUser()->getUser();
810 $title = $this->getDummyTitle( __METHOD__ );
811 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
812
813 $updater = $page->newPageUpdater( $user );
814 $summary = CommentStoreComment::newUnsavedComment( 'one' );
815 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
816 $rev1 = $updater->saveRevision( $summary, EDIT_NEW );
817
818 $updater = $page->newPageUpdater( $user );
819 $summary = CommentStoreComment::newUnsavedComment( 'two' );
820 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Something different' ) );
821 $updater->saveRevision( $summary, EDIT_UPDATE );
822
823 $updater = $page->newPageUpdater( $user );
824 $content = new WikitextContent( 'A' );
825 $derived = SlotRecord::newDerived( 'derivedslot', $content );
826 $updater->setSlot( $derived );
827 $updater->updateRevision( $rev1->getId( $rev1->getWikiId() ) );
828
829 $rev3 = $updater->getStatus()->getNewRevision();
830 $slot = $rev3->getSlot( 'derivedslot' );
831 $this->assertTrue( $slot->getContent()->equals( $content ) );
832 }
833
834 // TODO: MCR: test adding multiple slots, inheriting parent slots, and removing slots.
835
836 public function testSetUseAutomaticEditSummaries() {
837 $this->setContentLang( 'qqx' );
838 $user = $this->getTestUser()->getUser();
839
840 $title = $this->getDummyTitle( __METHOD__ );
841 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
842 $page = $wikiPageFactory->newFromTitle( $title );
843
844 $updater = $page->newPageUpdater( $user )
845 ->setUseAutomaticEditSummaries( true )
846 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
847
848 // empty comment triggers auto-summary
849 $summary = CommentStoreComment::newUnsavedComment( '' );
850 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY );
851
852 $rev = $updater->getNewRevision();
853 $comment = $rev->getComment( RevisionRecord::RAW );
854 $this->assertSame( '(autosumm-new: Lorem Ipsum)', $comment->text, 'comment text' );
855
856 // check that this also works when blanking the page
857 $updater = $page->newPageUpdater( $user )
858 ->setUseAutomaticEditSummaries( true )
859 ->setContent( SlotRecord::MAIN, new TextContent( '' ) );
860
861 $summary = CommentStoreComment::newUnsavedComment( '' );
862 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY );
863
864 $rev = $updater->getNewRevision();
865 $comment = $rev->getComment( RevisionRecord::RAW );
866 $this->assertSame( '(autosumm-blank)', $comment->text, 'comment text' );
867
868 // check that we can also disable edit-summaries
869 $title2 = $this->getDummyTitle( __METHOD__ . '/2' );
870 $page2 = $wikiPageFactory->newFromTitle( $title2 );
871
872 $updater = $page2->newPageUpdater( $user )
873 ->setUseAutomaticEditSummaries( false )
874 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
875
876 $summary = CommentStoreComment::newUnsavedComment( '' );
877 $updater->saveRevision( $summary, EDIT_AUTOSUMMARY );
878
879 $rev = $updater->getNewRevision();
880 $comment = $rev->getComment( RevisionRecord::RAW );
881 $this->assertSame( '', $comment->text, 'comment text should still be blank' );
882
883 // check that we don't do auto.summaries without the EDIT_AUTOSUMMARY flag
884 $updater = $page2->newPageUpdater( $user )
885 ->setUseAutomaticEditSummaries( true )
886 ->setContent( SlotRecord::MAIN, new TextContent( '' ) );
887
888 $summary = CommentStoreComment::newUnsavedComment( '' );
889 $updater->saveRevision( $summary, 0 );
890
891 $rev = $updater->getNewRevision();
892 $comment = $rev->getComment( RevisionRecord::RAW );
893 $this->assertSame( '', $comment->text, 'comment text' );
894 }
895
896 public static function provideSetUsePageCreationLog() {
897 yield [ true, [ [ 'create', 'create' ] ] ];
898 yield [ false, [] ];
899 }
900
901 /**
902 * @dataProvider provideSetUsePageCreationLog
903 */
904 public function testSetUsePageCreationLog( $use, $expected ) {
905 $user = $this->getTestUser()->getUser();
906
907 $title = $this->getDummyTitle( __METHOD__ . ( $use ? '_logged' : '_unlogged' ) );
908 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
909
910 $summary = CommentStoreComment::newUnsavedComment( 'cmt' );
911 $updater = $page->newPageUpdater( $user )
912 ->setUsePageCreationLog( $use )
913 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) );
914 $updater->saveRevision( $summary, EDIT_NEW );
915
916 $rev = $updater->getNewRevision();
917 $this->newSelectQueryBuilder()
918 ->select( [ 'log_type', 'log_action' ] )
919 ->from( 'logging' )
920 ->where( [ 'log_page' => $rev->getPageId() ] )
921 ->assertResultSet( $expected );
922 }
923
924 public static function provideMagicWords() {
925 yield 'PAGEID' => [
926 'Test {{PAGEID}} Test',
927 static function ( RevisionRecord $rev ) {
928 return $rev->getPageId();
929 }
930 ];
931
932 yield 'REVISIONID' => [
933 'Test {{REVISIONID}} Test',
934 static function ( RevisionRecord $rev ) {
935 return $rev->getId();
936 }
937 ];
938
939 yield 'REVISIONUSER' => [
940 'Test {{REVISIONUSER}} Test',
941 static function ( RevisionRecord $rev ) {
942 return $rev->getUser()->getName();
943 }
944 ];
945
946 yield 'REVISIONTIMESTAMP' => [
947 'Test {{REVISIONTIMESTAMP}} Test',
948 static function ( RevisionRecord $rev ) {
949 return $rev->getTimestamp();
950 }
951 ];
952
953 yield 'subst:REVISIONUSER' => [
954 'Test {{subst:REVISIONUSER}} Test',
955 static function ( RevisionRecord $rev ) {
956 return $rev->getUser()->getName();
957 },
958 'subst'
959 ];
960
961 yield 'subst:PAGENAME' => [
962 'Test {{subst:PAGENAME}} Test',
963 static function ( RevisionRecord $rev ) {
964 return 'PageUpdaterTest::testMagicWords';
965 },
966 'subst'
967 ];
968 }
969
970 /**
971 * @covers \MediaWiki\Storage\PageUpdater::saveRevision()
972 *
973 * Integration test for PageUpdater, DerivedPageDataUpdater, RevisionRenderer
974 * and RenderedRevision, that ensures that magic words depending on revision meta-data
975 * are handled correctly. Note that each magic word needs to be tested separately,
976 * to assert correct behavior for each "vary" flag in the ParserOutput.
977 *
978 * @dataProvider provideMagicWords
979 */
980 public function testMagicWords( $wikitext, $callback, $subst = false ) {
981 $user = User::newFromName( 'A user for ' . __METHOD__ );
982 $user->addToDatabase();
983
984 $title = $this->getDummyTitle( __METHOD__ . '-' . $this->getName() );
985 $this->insertPage( $title );
986
987 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
988 $updater = $page->newPageUpdater( $user )
989 ->setContent( SlotRecord::MAIN, new \MediaWiki\Content\WikitextContent( $wikitext ) );
990
991 $summary = CommentStoreComment::newUnsavedComment( 'Just a test' );
992 $rev = $updater->saveRevision( $summary, EDIT_UPDATE );
993
994 if ( !$rev ) {
995 $this->fail( $updater->getStatus()->getWikiText() );
996 }
997
998 $expected = strval( $callback( $rev ) );
999
1000 $output = $page->getParserOutput( ParserOptions::newFromAnon() );
1001 $html = $output->getRawText();
1002 $text = $rev->getContent( SlotRecord::MAIN )->serialize();
1003
1004 if ( $subst ) {
1005 $this->assertStringContainsString( $expected, $text, 'In Wikitext' );
1006 }
1007
1008 $this->assertStringContainsString( $expected, $html, 'In HTML' );
1009 }
1010
1011 public function testChangeTagsSuppressRecentChange() {
1012 $page = PageIdentityValue::localIdentity( 0, NS_MAIN, __METHOD__ );
1013 $wikiPageFactory = $this->getServiceContainer()->getWikiPageFactory();
1014 $revision = $this->getServiceContainer()
1015 ->getPageUpdaterFactory()
1016 ->newPageUpdater(
1017 $wikiPageFactory->newFromTitle( $page ),
1018 $this->getTestUser()->getUser()
1019 )
1020 ->setContent( SlotRecord::MAIN, new TextContent( 'Lorem Ipsum' ) )
1021 ->addTag( 'foo' )
1022 ->setFlags( EDIT_SUPPRESS_RC )
1023 ->saveRevision( CommentStoreComment::newUnsavedComment( 'Comment' ) );
1024 $this->assertArrayEquals(
1025 [ 'foo' ],
1026 $this->getServiceContainer()->getChangeTagsStore()->getTags(
1027 $this->getDb(), null, $revision->getId()
1028 )
1029 );
1030
1031 $revision2 = $this->getServiceContainer()
1032 ->getPageUpdaterFactory()
1033 ->newPageUpdater(
1034 $wikiPageFactory->newFromTitle( $page ),
1035 $this->getTestUser()->getUser()
1036 )
1037 ->setContent( SlotRecord::MAIN, new TextContent( 'Other content' ) )
1038 ->addTag( 'bar' )
1039 ->setFlags( EDIT_SUPPRESS_RC )
1040 ->saveRevision( CommentStoreComment::newUnsavedComment( 'Comment' ) );
1041 $this->assertArrayEquals(
1042 [ 'bar' ],
1043 $this->getServiceContainer()->getChangeTagsStore()->getTags(
1044 $this->getDb(), null, $revision2->getId()
1045 )
1046 );
1047 }
1048
1049 /**
1050 * @covers \MediaWiki\Storage\PageUpdater::prepareUpdate()
1051 * @covers \WikiPage::getCurrentUpdate()
1052 */
1053 public function testPrepareUpdate() {
1054 $user = $this->getTestUser()->getUser();
1055
1056 $title = $this->getDummyTitle( __METHOD__ );
1057 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1058 $updater = $page->newPageUpdater( $user );
1059
1060 $this->assertSame( $page->getCurrentUpdate(), $updater->prepareUpdate() );
1061 }
1062
1063 /**
1064 * @covers \MediaWiki\Storage\PageUpdater::preventChange
1065 * @covers \MediaWiki\Storage\PageUpdater::doModify
1066 * @covers \MediaWiki\Storage\PageUpdater::isChange
1067 */
1068 public function testPreventChange_modify() {
1069 $user = $this->getTestUser()->getUser();
1070 $title = $this->getDummyTitle( __METHOD__ );
1071 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1072 $updater = $page->newPageUpdater( $user );
1073
1074 // Creation
1075 $summary = CommentStoreComment::newUnsavedComment( 'one' );
1076 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
1077 $updater->prepareUpdate();
1078 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1079 $this->assertInstanceOf( RevisionRecord::class, $rev );
1080
1081 // Null edit
1082 $updater = $page->newPageUpdater( $user );
1083 $summary = CommentStoreComment::newUnsavedComment( 'one' );
1084 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
1085 $updater->prepareUpdate();
1086 $updater->preventChange();
1087 $this->assertFalse( $updater->isChange() );
1088 $rev = $updater->saveRevision( $summary );
1089 $this->assertNull( $rev );
1090
1091 // Prevented edit
1092 $updater = $page->newPageUpdater( $user );
1093 $summary = CommentStoreComment::newUnsavedComment( 'one' );
1094 $updater->setContent( SlotRecord::MAIN, new TextContent( 'dolor sit amet' ) );
1095 $updater->prepareUpdate();
1096 $updater->preventChange();
1097 $this->expectException( LogicException::class );
1098 $updater->saveRevision( $summary );
1099 }
1100
1101 /**
1102 * @covers \MediaWiki\Storage\PageUpdater::preventChange
1103 * @covers \MediaWiki\Storage\PageUpdater::doCreate
1104 * @covers \MediaWiki\Storage\PageUpdater::isChange
1105 */
1106 public function testPreventChange_create() {
1107 $user = $this->getTestUser()->getUser();
1108 $title = $this->getDummyTitle( __METHOD__ );
1109 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1110
1111 $updater = $page->newPageUpdater( $user );
1112 $summary = CommentStoreComment::newUnsavedComment( 'one' );
1113 $updater->setContent( SlotRecord::MAIN, new TextContent( 'Lorem ipsum' ) );
1114 $updater->prepareUpdate();
1115 $updater->preventChange();
1116 $this->assertTrue( $updater->isChange() );
1117 $this->expectException( LogicException::class );
1118 $updater->saveRevision( $summary, EDIT_NEW );
1119 }
1120
1121 public function testUpdateAuthor() {
1122 $title = $this->getDummyTitle( __METHOD__ );
1123 $page = $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
1124 $user = new User;
1125 $user->setName( 'PageUpdaterTest' );
1126 $updater = $page->newPageUpdater( $user );
1127 $summary = CommentStoreComment::newUnsavedComment( 'one' );
1128 $updater->setContent( SlotRecord::MAIN, new TextContent( '~~~~' ) );
1129
1130 $user = User::createNew( $user->getName() );
1131 $updater->updateAuthor( $user );
1132 $rev = $updater->saveRevision( $summary, EDIT_NEW );
1133 $this->assertGreaterThan( 0, $rev->getUser()->getId() );
1134 }
1135 }