[MCR] Introduce SlotRoleHandler and SlotRoleRegistry
[lhc/web/wiklou.git] / tests / phpunit / includes / Storage / DerivedPageDataUpdaterTest.php
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use CommentStoreComment;
6 use Content;
7 use ContentHandler;
8 use LinksUpdate;
9 use MediaWiki\MediaWikiServices;
10 use MediaWiki\Revision\MutableRevisionRecord;
11 use MediaWiki\Revision\MutableRevisionSlots;
12 use MediaWiki\Revision\RevisionRecord;
13 use MediaWiki\Revision\SlotRecord;
14 use MediaWiki\Storage\DerivedPageDataUpdater;
15 use MediaWiki\Storage\RevisionSlotsUpdate;
16 use MediaWikiTestCase;
17 use MWCallableUpdate;
18 use MWTimestamp;
19 use PHPUnit\Framework\MockObject\MockObject;
20 use TextContent;
21 use TextContentHandler;
22 use Title;
23 use User;
24 use Wikimedia\TestingAccessWrapper;
25 use WikiPage;
26 use WikitextContent;
27
28 /**
29 * @group Database
30 *
31 * @covers \MediaWiki\Storage\DerivedPageDataUpdater
32 */
33 class DerivedPageDataUpdaterTest extends MediaWikiTestCase {
34
35 public function tearDown() {
36 MWTimestamp::setFakeTime( false );
37
38 parent::tearDown();
39 }
40
41 /**
42 * @param string $title
43 *
44 * @return Title
45 */
46 private function getTitle( $title ) {
47 return Title::makeTitleSafe( $this->getDefaultWikitextNS(), $title );
48 }
49
50 /**
51 * @param string|Title $title
52 *
53 * @return WikiPage
54 */
55 private function getPage( $title ) {
56 $title = ( $title instanceof Title ) ? $title : $this->getTitle( $title );
57
58 return WikiPage::factory( $title );
59 }
60
61 /**
62 * @param string|Title|WikiPage $page
63 *
64 * @return DerivedPageDataUpdater
65 */
66 private function getDerivedPageDataUpdater( $page, RevisionRecord $rec = null ) {
67 if ( is_string( $page ) || $page instanceof Title ) {
68 $page = $this->getPage( $page );
69 }
70
71 $page = TestingAccessWrapper::newFromObject( $page );
72 return $page->getDerivedDataUpdater( null, $rec );
73 }
74
75 /**
76 * Creates a revision in the database.
77 *
78 * @param WikiPage $page
79 * @param $summary
80 * @param null|string|Content $content
81 *
82 * @return RevisionRecord|null
83 */
84 private function createRevision( WikiPage $page, $summary, $content = null ) {
85 $user = $this->getTestUser()->getUser();
86 $comment = CommentStoreComment::newUnsavedComment( $summary );
87
88 if ( $content === null || is_string( $content ) ) {
89 $content = new WikitextContent( $content ?? $summary );
90 }
91
92 if ( !is_array( $content ) ) {
93 $content = [ 'main' => $content ];
94 }
95
96 $this->getDerivedPageDataUpdater( $page ); // flush cached instance before.
97
98 $updater = $page->newPageUpdater( $user );
99
100 foreach ( $content as $role => $c ) {
101 $updater->setContent( $role, $c );
102 }
103
104 $rev = $updater->saveRevision( $comment );
105 if ( !$updater->wasSuccessful() ) {
106 $this->fail( $updater->getStatus()->getWikiText() );
107 }
108
109 $this->getDerivedPageDataUpdater( $page ); // flush cached instance after.
110 return $rev;
111 }
112
113 // TODO: test setArticleCountMethod() and isCountable();
114 // TODO: test isRedirect() and wasRedirect()
115
116 /**
117 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOptions()
118 */
119 public function testGetCanonicalParserOptions() {
120 $user = $this->getTestUser()->getUser();
121 $page = $this->getPage( __METHOD__ );
122
123 $parentRev = $this->createRevision( $page, 'first' );
124
125 $mainContent = new WikitextContent( 'Lorem ipsum' );
126
127 $update = new RevisionSlotsUpdate();
128 $update->modifyContent( SlotRecord::MAIN, $mainContent );
129 $updater = $this->getDerivedPageDataUpdater( $page );
130 $updater->prepareContent( $user, $update, false );
131
132 $options1 = $updater->getCanonicalParserOptions();
133 $this->assertSame( MediaWikiServices::getInstance()->getContentLanguage(),
134 $options1->getUserLangObj() );
135
136 $speculativeId = $options1->getSpeculativeRevId();
137 $this->assertSame( $parentRev->getId() + 1, $speculativeId );
138
139 $rev = $this->makeRevision(
140 $page->getTitle(),
141 $update,
142 $user,
143 $parentRev->getId() + 7,
144 $parentRev->getId()
145 );
146 $updater->prepareUpdate( $rev );
147
148 $options2 = $updater->getCanonicalParserOptions();
149
150 $currentRev = call_user_func( $options2->getCurrentRevisionCallback(), $page->getTitle() );
151 $this->assertSame( $rev->getId(), $currentRev->getId() );
152 }
153
154 /**
155 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::grabCurrentRevision()
156 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
157 */
158 public function testGrabCurrentRevision() {
159 $page = $this->getPage( __METHOD__ );
160
161 $updater0 = $this->getDerivedPageDataUpdater( $page );
162 $this->assertNull( $updater0->grabCurrentRevision() );
163 $this->assertFalse( $updater0->pageExisted() );
164
165 $rev1 = $this->createRevision( $page, 'first' );
166 $updater1 = $this->getDerivedPageDataUpdater( $page );
167 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
168 $this->assertFalse( $updater0->pageExisted() );
169 $this->assertTrue( $updater1->pageExisted() );
170
171 $rev2 = $this->createRevision( $page, 'second' );
172 $updater2 = $this->getDerivedPageDataUpdater( $page );
173 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
174 $this->assertSame( $rev2->getId(), $updater2->grabCurrentRevision()->getId() );
175 }
176
177 /**
178 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
179 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isContentPrepared()
180 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
181 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
182 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
183 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
184 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
185 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
186 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
187 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
188 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
189 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
190 */
191 public function testPrepareContent() {
192 MediaWikiServices::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
193 'aux',
194 CONTENT_MODEL_WIKITEXT
195 );
196
197 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
198 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
199
200 $this->assertFalse( $updater->isContentPrepared() );
201
202 // TODO: test stash
203 // TODO: MCR: Test multiple slots. Test slot removal.
204 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
205 $auxContent = new WikitextContent( 'inherited ~~~ content' );
206 $auxSlot = SlotRecord::newSaved(
207 10, 7, 'tt:7',
208 SlotRecord::newUnsaved( 'aux', $auxContent )
209 );
210
211 $update = new RevisionSlotsUpdate();
212 $update->modifyContent( SlotRecord::MAIN, $mainContent );
213 $update->modifySlot( SlotRecord::newInherited( $auxSlot ) );
214 // TODO: MCR: test removing slots!
215
216 $updater->prepareContent( $sysop, $update, false );
217
218 // second be ok to call again with the same params
219 $updater->prepareContent( $sysop, $update, false );
220
221 $this->assertNull( $updater->grabCurrentRevision() );
222 $this->assertTrue( $updater->isContentPrepared() );
223 $this->assertFalse( $updater->isUpdatePrepared() );
224 $this->assertFalse( $updater->pageExisted() );
225 $this->assertTrue( $updater->isCreation() );
226 $this->assertTrue( $updater->isChange() );
227 $this->assertFalse( $updater->isContentDeleted() );
228
229 $this->assertNotNull( $updater->getRevision() );
230 $this->assertNotNull( $updater->getRenderedRevision() );
231
232 $this->assertEquals( [ 'main', 'aux' ], $updater->getSlots()->getSlotRoles() );
233 $this->assertEquals( [ 'main' ], array_keys( $updater->getSlots()->getOriginalSlots() ) );
234 $this->assertEquals( [ 'aux' ], array_keys( $updater->getSlots()->getInheritedSlots() ) );
235 $this->assertEquals( [ 'main', 'aux' ], $updater->getModifiedSlotRoles() );
236 $this->assertEquals( [ 'main', 'aux' ], $updater->getTouchedSlotRoles() );
237
238 $mainSlot = $updater->getRawSlot( SlotRecord::MAIN );
239 $this->assertInstanceOf( SlotRecord::class, $mainSlot );
240 $this->assertNotContains( '~~~', $mainSlot->getContent()->serialize(), 'PST should apply.' );
241 $this->assertContains( $sysop->getName(), $mainSlot->getContent()->serialize() );
242
243 $auxSlot = $updater->getRawSlot( 'aux' );
244 $this->assertInstanceOf( SlotRecord::class, $auxSlot );
245 $this->assertContains( '~~~', $auxSlot->getContent()->serialize(), 'No PST should apply.' );
246
247 $mainOutput = $updater->getCanonicalParserOutput();
248 $this->assertContains( 'first', $mainOutput->getText() );
249 $this->assertContains( '<a ', $mainOutput->getText() );
250 $this->assertNotEmpty( $mainOutput->getLinks() );
251
252 $canonicalOutput = $updater->getCanonicalParserOutput();
253 $this->assertContains( 'first', $canonicalOutput->getText() );
254 $this->assertContains( '<a ', $canonicalOutput->getText() );
255 $this->assertContains( 'inherited ', $canonicalOutput->getText() );
256 $this->assertNotEmpty( $canonicalOutput->getLinks() );
257 }
258
259 /**
260 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
261 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
262 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
263 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
264 */
265 public function testPrepareContentInherit() {
266 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
267 $page = $this->getPage( __METHOD__ );
268
269 $mainContent1 = new WikitextContent( 'first [[main]] ({{REVISIONUSER}}) #~~~#' );
270 $mainContent2 = new WikitextContent( 'second ({{subst:REVISIONUSER}}) #~~~#' );
271
272 $rev = $this->createRevision( $page, 'first', $mainContent1 );
273 $mainContent1 = $rev->getContent( SlotRecord::MAIN ); // get post-pst content
274 $userName = $rev->getUser()->getName();
275 $sysopName = $sysop->getName();
276
277 $update = new RevisionSlotsUpdate();
278 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
279 $updater1 = $this->getDerivedPageDataUpdater( $page );
280 $updater1->prepareContent( $sysop, $update, false );
281
282 $this->assertNotNull( $updater1->grabCurrentRevision() );
283 $this->assertTrue( $updater1->isContentPrepared() );
284 $this->assertTrue( $updater1->pageExisted() );
285 $this->assertFalse( $updater1->isCreation() );
286 $this->assertFalse( $updater1->isChange() );
287
288 $this->assertNotNull( $updater1->getRevision() );
289 $this->assertNotNull( $updater1->getRenderedRevision() );
290
291 // parser-output for null-edit uses the original author's name
292 $html = $updater1->getRenderedRevision()->getRevisionParserOutput()->getText();
293 $this->assertNotContains( $sysopName, $html, '{{REVISIONUSER}}' );
294 $this->assertNotContains( '{{REVISIONUSER}}', $html, '{{REVISIONUSER}}' );
295 $this->assertNotContains( '~~~', $html, 'signature ~~~' );
296 $this->assertContains( '(' . $userName . ')', $html, '{{REVISIONUSER}}' );
297 $this->assertContains( '>' . $userName . '<', $html, 'signature ~~~' );
298
299 // TODO: MCR: test inheritance from parent
300 $update = new RevisionSlotsUpdate();
301 $update->modifyContent( SlotRecord::MAIN, $mainContent2 );
302 $updater2 = $this->getDerivedPageDataUpdater( $page );
303 $updater2->prepareContent( $sysop, $update, false );
304
305 // non-null edit use the new user name in PST
306 $pstText = $updater2->getSlots()->getContent( SlotRecord::MAIN )->serialize();
307 $this->assertNotContains( '{{subst:REVISIONUSER}}', $pstText, '{{subst:REVISIONUSER}}' );
308 $this->assertNotContains( '~~~', $pstText, 'signature ~~~' );
309 $this->assertContains( '(' . $sysopName . ')', $pstText, '{{subst:REVISIONUSER}}' );
310 $this->assertContains( ':' . $sysopName . '|', $pstText, 'signature ~~~' );
311
312 $this->assertFalse( $updater2->isCreation() );
313 $this->assertTrue( $updater2->isChange() );
314 }
315
316 // TODO: test failure of prepareContent() when called again...
317 // - with different user
318 // - with different update
319 // - after calling prepareUpdate()
320
321 /**
322 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
323 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isUpdatePrepared()
324 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
325 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
326 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
327 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
328 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
329 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
330 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
331 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
332 */
333 public function testPrepareUpdate() {
334 $page = $this->getPage( __METHOD__ );
335
336 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
337 $rev1 = $this->createRevision( $page, 'first', $mainContent1 );
338 $updater1 = $this->getDerivedPageDataUpdater( $page, $rev1 );
339
340 $options = []; // TODO: test *all* the options...
341 $updater1->prepareUpdate( $rev1, $options );
342
343 $this->assertTrue( $updater1->isUpdatePrepared() );
344 $this->assertTrue( $updater1->isContentPrepared() );
345 $this->assertTrue( $updater1->isCreation() );
346 $this->assertTrue( $updater1->isChange() );
347 $this->assertFalse( $updater1->isContentDeleted() );
348
349 $this->assertNotNull( $updater1->getRevision() );
350 $this->assertNotNull( $updater1->getRenderedRevision() );
351
352 $this->assertEquals( [ 'main' ], $updater1->getSlots()->getSlotRoles() );
353 $this->assertEquals( [ 'main' ], array_keys( $updater1->getSlots()->getOriginalSlots() ) );
354 $this->assertEquals( [], array_keys( $updater1->getSlots()->getInheritedSlots() ) );
355 $this->assertEquals( [ 'main' ], $updater1->getModifiedSlotRoles() );
356 $this->assertEquals( [ 'main' ], $updater1->getTouchedSlotRoles() );
357
358 // TODO: MCR: test multiple slots, test slot removal!
359
360 $this->assertInstanceOf( SlotRecord::class, $updater1->getRawSlot( SlotRecord::MAIN ) );
361 $this->assertNotContains( '~~~~', $updater1->getRawContent( SlotRecord::MAIN )->serialize() );
362
363 $mainOutput = $updater1->getCanonicalParserOutput();
364 $this->assertContains( 'first', $mainOutput->getText() );
365 $this->assertContains( '<a ', $mainOutput->getText() );
366 $this->assertNotEmpty( $mainOutput->getLinks() );
367
368 $canonicalOutput = $updater1->getCanonicalParserOutput();
369 $this->assertContains( 'first', $canonicalOutput->getText() );
370 $this->assertContains( '<a ', $canonicalOutput->getText() );
371 $this->assertNotEmpty( $canonicalOutput->getLinks() );
372
373 $mainContent2 = new WikitextContent( 'second' );
374 $rev2 = $this->createRevision( $page, 'second', $mainContent2 );
375 $updater2 = $this->getDerivedPageDataUpdater( $page, $rev2 );
376
377 $options = []; // TODO: test *all* the options...
378 $updater2->prepareUpdate( $rev2, $options );
379
380 $this->assertFalse( $updater2->isCreation() );
381 $this->assertTrue( $updater2->isChange() );
382
383 $canonicalOutput = $updater2->getCanonicalParserOutput();
384 $this->assertContains( 'second', $canonicalOutput->getText() );
385 }
386
387 /**
388 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
389 */
390 public function testPrepareUpdateReusesParserOutput() {
391 $user = $this->getTestUser()->getUser();
392 $page = $this->getPage( __METHOD__ );
393
394 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
395
396 $update = new RevisionSlotsUpdate();
397 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
398 $updater = $this->getDerivedPageDataUpdater( $page );
399 $updater->prepareContent( $user, $update, false );
400
401 $mainOutput = $updater->getSlotParserOutput( SlotRecord::MAIN );
402 $canonicalOutput = $updater->getCanonicalParserOutput();
403
404 $rev = $this->createRevision( $page, 'first', $mainContent1 );
405
406 $options = []; // TODO: test *all* the options...
407 $updater->prepareUpdate( $rev, $options );
408
409 $this->assertTrue( $updater->isUpdatePrepared() );
410 $this->assertTrue( $updater->isContentPrepared() );
411
412 $this->assertSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord::MAIN ) );
413 $this->assertSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
414 }
415
416 /**
417 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
418 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
419 */
420 public function testPrepareUpdateOutputReset() {
421 $user = $this->getTestUser()->getUser();
422 $page = $this->getPage( __METHOD__ );
423
424 $mainContent1 = new WikitextContent( 'first --{{REVISIONID}}--' );
425
426 $update = new RevisionSlotsUpdate();
427 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
428 $updater = $this->getDerivedPageDataUpdater( $page );
429 $updater->prepareContent( $user, $update, false );
430
431 $mainOutput = $updater->getSlotParserOutput( SlotRecord::MAIN );
432 $canonicalOutput = $updater->getCanonicalParserOutput();
433
434 // prevent optimization on matching speculative ID
435 $mainOutput->setSpeculativeRevIdUsed( 0 );
436 $canonicalOutput->setSpeculativeRevIdUsed( 0 );
437
438 $rev = $this->createRevision( $page, 'first', $mainContent1 );
439
440 $options = []; // TODO: test *all* the options...
441 $updater->prepareUpdate( $rev, $options );
442
443 $this->assertTrue( $updater->isUpdatePrepared() );
444 $this->assertTrue( $updater->isContentPrepared() );
445
446 // ParserOutput objects should have been flushed.
447 $this->assertNotSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord::MAIN ) );
448 $this->assertNotSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
449
450 $html = $updater->getCanonicalParserOutput()->getText();
451 $this->assertContains( '--' . $rev->getId() . '--', $html );
452
453 // TODO: MCR: ensure that when the main slot uses {{REVISIONID}} but another slot is
454 // updated, the main slot is still re-rendered!
455 }
456
457 // TODO: test failure of prepareUpdate() when called again with a different revision
458 // TODO: test failure of prepareUpdate() on inconsistency with prepareContent.
459
460 /**
461 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
462 */
463 public function testGetPreparedEditAfterPrepareContent() {
464 $user = $this->getTestUser()->getUser();
465
466 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
467 $update = new RevisionSlotsUpdate();
468 $update->modifyContent( SlotRecord::MAIN, $mainContent );
469
470 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
471 $updater->prepareContent( $user, $update, false );
472
473 $canonicalOutput = $updater->getCanonicalParserOutput();
474
475 $preparedEdit = $updater->getPreparedEdit();
476 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp );
477 $this->assertSame( $canonicalOutput, $preparedEdit->output );
478 $this->assertSame( $mainContent, $preparedEdit->newContent );
479 $this->assertSame( $updater->getRawContent( SlotRecord::MAIN ), $preparedEdit->pstContent );
480 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts );
481 $this->assertSame( null, $preparedEdit->revid );
482 }
483
484 /**
485 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
486 */
487 public function testGetPreparedEditAfterPrepareUpdate() {
488 $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' );
489 MWTimestamp::setFakeTime( function () use ( &$clock ) {
490 return $clock++;
491 } );
492
493 $page = $this->getPage( __METHOD__ );
494
495 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
496 $update = new MutableRevisionSlots();
497 $update->setContent( SlotRecord::MAIN, $mainContent );
498
499 $rev = $this->createRevision( $page, __METHOD__ );
500
501 $updater = $this->getDerivedPageDataUpdater( $page );
502 $updater->prepareUpdate( $rev );
503
504 $canonicalOutput = $updater->getCanonicalParserOutput();
505
506 $preparedEdit = $updater->getPreparedEdit();
507 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp );
508 $this->assertSame( $canonicalOutput, $preparedEdit->output );
509 $this->assertSame( $updater->getRawContent( SlotRecord::MAIN ), $preparedEdit->pstContent );
510 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts );
511 $this->assertSame( $rev->getId(), $preparedEdit->revid );
512 }
513
514 public function testGetSecondaryDataUpdatesAfterPrepareContent() {
515 $user = $this->getTestUser()->getUser();
516 $page = $this->getPage( __METHOD__ );
517 $this->createRevision( $page, __METHOD__ );
518
519 $mainContent1 = new WikitextContent( 'first' );
520
521 $update = new RevisionSlotsUpdate();
522 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
523 $updater = $this->getDerivedPageDataUpdater( $page );
524 $updater->prepareContent( $user, $update, false );
525
526 $dataUpdates = $updater->getSecondaryDataUpdates();
527
528 $this->assertNotEmpty( $dataUpdates );
529
530 $linksUpdates = array_filter( $dataUpdates, function ( $du ) {
531 return $du instanceof LinksUpdate;
532 } );
533 $this->assertCount( 1, $linksUpdates );
534 }
535
536 /**
537 * @param string $name
538 *
539 * @return ContentHandler
540 */
541 private function defineMockContentModelForUpdateTesting( $name ) {
542 /** @var ContentHandler|MockObject $handler */
543 $handler = $this->getMockBuilder( TextContentHandler::class )
544 ->setConstructorArgs( [ $name ] )
545 ->setMethods(
546 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
547 )
548 ->getMock();
549
550 $dataUpdate = new MWCallableUpdate( 'time' );
551 $dataUpdate->_name = "$name data update";
552
553 $deletionUpdate = new MWCallableUpdate( 'time' );
554 $deletionUpdate->_name = "$name deletion update";
555
556 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] );
557 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] );
558 $handler->method( 'unserializeContent' )->willReturnCallback(
559 function ( $text ) use ( $handler ) {
560 return $this->createMockContent( $handler, $text );
561 }
562 );
563
564 $this->mergeMwGlobalArrayValue(
565 'wgContentHandlers', [
566 $name => function () use ( $handler ){
567 return $handler;
568 }
569 ]
570 );
571
572 return $handler;
573 }
574
575 /**
576 * @param ContentHandler $handler
577 * @param string $text
578 *
579 * @return Content
580 */
581 private function createMockContent( ContentHandler $handler, $text ) {
582 /** @var Content|MockObject $content */
583 $content = $this->getMockBuilder( TextContent::class )
584 ->setConstructorArgs( [ $text ] )
585 ->setMethods( [ 'getModel', 'getContentHandler' ] )
586 ->getMock();
587
588 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
589 $content->method( 'getContentHandler' )->willReturn( $handler );
590
591 return $content;
592 }
593
594 public function testGetSecondaryDataUpdatesWithSlotRemoval() {
595 if ( !$this->hasMultiSlotSupport() ) {
596 $this->markTestSkipped( 'Slot removal cannot happen with MCR being enabled' );
597 }
598
599 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
600 $a1 = $this->defineMockContentModelForUpdateTesting( 'A1' );
601 $m2 = $this->defineMockContentModelForUpdateTesting( 'M2' );
602
603 MediaWikiServices::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
604 'aux',
605 $a1->getModelID()
606 );
607
608 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
609 $auxContent1 = $this->createMockContent( $a1, 'aux 1' );
610 $mainContent2 = $this->createMockContent( $m2, 'main 2' );
611
612 $user = $this->getTestUser()->getUser();
613 $page = $this->getPage( __METHOD__ );
614 $this->createRevision(
615 $page,
616 __METHOD__,
617 [ 'main' => $mainContent1, 'aux' => $auxContent1 ]
618 );
619
620 $update = new RevisionSlotsUpdate();
621 $update->modifyContent( SlotRecord::MAIN, $mainContent2 );
622 $update->removeSlot( 'aux' );
623
624 $page = $this->getPage( __METHOD__ );
625 $updater = $this->getDerivedPageDataUpdater( $page );
626 $updater->prepareContent( $user, $update, false );
627
628 $dataUpdates = $updater->getSecondaryDataUpdates();
629
630 $this->assertNotEmpty( $dataUpdates );
631
632 $updateNames = array_map( function ( $du ) {
633 return isset( $du->_name ) ? $du->_name : get_class( $du );
634 }, $dataUpdates );
635
636 $this->assertContains( LinksUpdate::class, $updateNames );
637 $this->assertContains( 'A1 deletion update', $updateNames );
638 $this->assertContains( 'M2 data update', $updateNames );
639 $this->assertNotContains( 'M1 data update', $updateNames );
640 }
641
642 /**
643 * Creates a dummy revision object without touching the database.
644 *
645 * @param Title $title
646 * @param RevisionSlotsUpdate $update
647 * @param User $user
648 * @param string $comment
649 * @param int $id
650 * @param int $parentId
651 *
652 * @return MutableRevisionRecord
653 */
654 private function makeRevision(
655 Title $title,
656 RevisionSlotsUpdate $update,
657 User $user,
658 $comment,
659 $id,
660 $parentId = 0
661 ) {
662 $rev = new MutableRevisionRecord( $title );
663
664 $rev->applyUpdate( $update );
665 $rev->setUser( $user );
666 $rev->setComment( CommentStoreComment::newUnsavedComment( $comment ) );
667 $rev->setId( $id );
668 $rev->setPageId( $title->getArticleID() );
669 $rev->setParentId( $parentId );
670
671 return $rev;
672 }
673
674 /**
675 * @param int $id
676 * @return Title
677 */
678 private function getMockTitle( $id = 23 ) {
679 $mock = $this->getMockBuilder( Title::class )
680 ->disableOriginalConstructor()
681 ->getMock();
682 $mock->expects( $this->any() )
683 ->method( 'getDBkey' )
684 ->will( $this->returnValue( __CLASS__ ) );
685 $mock->expects( $this->any() )
686 ->method( 'getArticleID' )
687 ->will( $this->returnValue( $id ) );
688
689 return $mock;
690 }
691
692 public function provideIsReusableFor() {
693 $title = $this->getMockTitle();
694
695 $user1 = User::newFromName( 'Alice' );
696 $user2 = User::newFromName( 'Bob' );
697
698 $content1 = new WikitextContent( 'one' );
699 $content2 = new WikitextContent( 'two' );
700
701 $update1 = new RevisionSlotsUpdate();
702 $update1->modifyContent( SlotRecord::MAIN, $content1 );
703
704 $update1b = new RevisionSlotsUpdate();
705 $update1b->modifyContent( 'xyz', $content1 );
706
707 $update2 = new RevisionSlotsUpdate();
708 $update2->modifyContent( SlotRecord::MAIN, $content2 );
709
710 $rev1 = $this->makeRevision( $title, $update1, $user1, 'rev1', 11 );
711 $rev1b = $this->makeRevision( $title, $update1b, $user1, 'rev1', 11 );
712
713 $rev2 = $this->makeRevision( $title, $update2, $user1, 'rev2', 12 );
714 $rev2x = $this->makeRevision( $title, $update2, $user2, 'rev2', 12 );
715 $rev2y = $this->makeRevision( $title, $update2, $user1, 'rev2', 122 );
716
717 yield 'any' => [
718 '$prepUser' => null,
719 '$prepRevision' => null,
720 '$prepUpdate' => null,
721 '$forUser' => null,
722 '$forRevision' => null,
723 '$forUpdate' => null,
724 '$forParent' => null,
725 '$isReusable' => true,
726 ];
727 yield 'for any' => [
728 '$prepUser' => $user1,
729 '$prepRevision' => $rev1,
730 '$prepUpdate' => $update1,
731 '$forUser' => null,
732 '$forRevision' => null,
733 '$forUpdate' => null,
734 '$forParent' => null,
735 '$isReusable' => true,
736 ];
737 yield 'unprepared' => [
738 '$prepUser' => null,
739 '$prepRevision' => null,
740 '$prepUpdate' => null,
741 '$forUser' => $user1,
742 '$forRevision' => $rev1,
743 '$forUpdate' => $update1,
744 '$forParent' => 0,
745 '$isReusable' => true,
746 ];
747 yield 'match prepareContent' => [
748 '$prepUser' => $user1,
749 '$prepRevision' => null,
750 '$prepUpdate' => $update1,
751 '$forUser' => $user1,
752 '$forRevision' => null,
753 '$forUpdate' => $update1,
754 '$forParent' => 0,
755 '$isReusable' => true,
756 ];
757 yield 'match prepareUpdate' => [
758 '$prepUser' => null,
759 '$prepRevision' => $rev1,
760 '$prepUpdate' => null,
761 '$forUser' => $user1,
762 '$forRevision' => $rev1,
763 '$forUpdate' => null,
764 '$forParent' => 0,
765 '$isReusable' => true,
766 ];
767 yield 'match all' => [
768 '$prepUser' => $user1,
769 '$prepRevision' => $rev1,
770 '$prepUpdate' => $update1,
771 '$forUser' => $user1,
772 '$forRevision' => $rev1,
773 '$forUpdate' => $update1,
774 '$forParent' => 0,
775 '$isReusable' => true,
776 ];
777 yield 'mismatch prepareContent update' => [
778 '$prepUser' => $user1,
779 '$prepRevision' => null,
780 '$prepUpdate' => $update1,
781 '$forUser' => $user1,
782 '$forRevision' => null,
783 '$forUpdate' => $update1b,
784 '$forParent' => 0,
785 '$isReusable' => false,
786 ];
787 yield 'mismatch prepareContent user' => [
788 '$prepUser' => $user1,
789 '$prepRevision' => null,
790 '$prepUpdate' => $update1,
791 '$forUser' => $user2,
792 '$forRevision' => null,
793 '$forUpdate' => $update1,
794 '$forParent' => 0,
795 '$isReusable' => false,
796 ];
797 yield 'mismatch prepareContent parent' => [
798 '$prepUser' => $user1,
799 '$prepRevision' => null,
800 '$prepUpdate' => $update1,
801 '$forUser' => $user1,
802 '$forRevision' => null,
803 '$forUpdate' => $update1,
804 '$forParent' => 7,
805 '$isReusable' => false,
806 ];
807 yield 'mismatch prepareUpdate revision update' => [
808 '$prepUser' => null,
809 '$prepRevision' => $rev1,
810 '$prepUpdate' => null,
811 '$forUser' => null,
812 '$forRevision' => $rev1b,
813 '$forUpdate' => null,
814 '$forParent' => 0,
815 '$isReusable' => false,
816 ];
817 yield 'mismatch prepareUpdate revision user' => [
818 '$prepUser' => null,
819 '$prepRevision' => $rev2,
820 '$prepUpdate' => null,
821 '$forUser' => null,
822 '$forRevision' => $rev2x,
823 '$forUpdate' => null,
824 '$forParent' => 0,
825 '$isReusable' => false,
826 ];
827 yield 'mismatch prepareUpdate revision id' => [
828 '$prepUser' => null,
829 '$prepRevision' => $rev2,
830 '$prepUpdate' => null,
831 '$forUser' => null,
832 '$forRevision' => $rev2y,
833 '$forUpdate' => null,
834 '$forParent' => 0,
835 '$isReusable' => false,
836 ];
837 }
838
839 /**
840 * @dataProvider provideIsReusableFor
841 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isReusableFor()
842 *
843 * @param User|null $prepUser
844 * @param RevisionRecord|null $prepRevision
845 * @param RevisionSlotsUpdate|null $prepUpdate
846 * @param User|null $forUser
847 * @param RevisionRecord|null $forRevision
848 * @param RevisionSlotsUpdate|null $forUpdate
849 * @param int|null $forParent
850 * @param bool $isReusable
851 */
852 public function testIsReusableFor(
853 User $prepUser = null,
854 RevisionRecord $prepRevision = null,
855 RevisionSlotsUpdate $prepUpdate = null,
856 User $forUser = null,
857 RevisionRecord $forRevision = null,
858 RevisionSlotsUpdate $forUpdate = null,
859 $forParent = null,
860 $isReusable = null
861 ) {
862 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
863
864 if ( $prepUpdate ) {
865 $updater->prepareContent( $prepUser, $prepUpdate, false );
866 }
867
868 if ( $prepRevision ) {
869 $updater->prepareUpdate( $prepRevision );
870 }
871
872 $this->assertSame(
873 $isReusable,
874 $updater->isReusableFor( $forUser, $forRevision, $forUpdate, $forParent )
875 );
876 }
877
878 /**
879 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
880 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
881 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
882 */
883 public function testDoUpdates() {
884 $page = $this->getPage( __METHOD__ );
885
886 $content = [ 'main' => new WikitextContent( 'first [[main]]' ) ];
887
888 if ( $this->hasMultiSlotSupport() ) {
889 $content['aux'] = new WikitextContent( 'Aux [[Nix]]' );
890
891 MediaWikiServices::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
892 'aux',
893 CONTENT_MODEL_WIKITEXT
894 );
895 }
896
897 $rev = $this->createRevision( $page, 'first', $content );
898 $pageId = $page->getId();
899
900 $oldStats = $this->db->selectRow( 'site_stats', '*', '1=1' );
901 $this->db->delete( 'pagelinks', '*' );
902
903 $pcache = MediaWikiServices::getInstance()->getParserCache();
904 $pcache->deleteOptionsKey( $page );
905
906 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
907 $updater->setArticleCountMethod( 'link' );
908
909 $options = []; // TODO: test *all* the options...
910 $updater->prepareUpdate( $rev, $options );
911
912 $updater->doUpdates();
913
914 // links table update
915 $pageLinks = $this->db->select(
916 'pagelinks',
917 '*',
918 [ 'pl_from' => $pageId ],
919 __METHOD__,
920 [ 'ORDER BY' => 'pl_namespace, pl_title' ]
921 );
922
923 $pageLinksRow = $pageLinks->fetchObject();
924 $this->assertInternalType( 'object', $pageLinksRow );
925 $this->assertSame( 'Main', $pageLinksRow->pl_title );
926
927 if ( $this->hasMultiSlotSupport() ) {
928 $pageLinksRow = $pageLinks->fetchObject();
929 $this->assertInternalType( 'object', $pageLinksRow );
930 $this->assertSame( 'Nix', $pageLinksRow->pl_title );
931 }
932
933 // parser cache update
934 $cached = $pcache->get( $page, $updater->getCanonicalParserOptions() );
935 $this->assertInternalType( 'object', $cached );
936 $this->assertSame( $updater->getCanonicalParserOutput(), $cached );
937
938 // site stats
939 $stats = $this->db->selectRow( 'site_stats', '*', '1=1' );
940 $this->assertSame( $oldStats->ss_total_pages + 1, (int)$stats->ss_total_pages );
941 $this->assertSame( $oldStats->ss_total_edits + 1, (int)$stats->ss_total_edits );
942 $this->assertSame( $oldStats->ss_good_articles + 1, (int)$stats->ss_good_articles );
943
944 // TODO: MCR: test data updates for additional slots!
945 // TODO: test update for edit without page creation
946 // TODO: test message cache purge
947 // TODO: test module cache purge
948 // TODO: test CDN purge
949 // TODO: test newtalk update
950 // TODO: test search update
951 // TODO: test site stats good_articles while turning the page into (or back from) a redir.
952 // TODO: test category membership update (with setRcWatchCategoryMembership())
953 }
954
955 private function hasMultiSlotSupport() {
956 global $wgMultiContentRevisionSchemaMigrationStage;
957
958 return ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW )
959 && ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW );
960 }
961
962 }