3 namespace MediaWiki\Tests\Storage
;
5 use CommentStoreComment
;
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
;
19 use PHPUnit\Framework\MockObject\MockObject
;
21 use TextContentHandler
;
24 use Wikimedia\TestingAccessWrapper
;
31 * @covers \MediaWiki\Storage\DerivedPageDataUpdater
33 class DerivedPageDataUpdaterTest
extends MediaWikiTestCase
{
35 public function tearDown() {
36 MWTimestamp
::setFakeTime( false );
42 * @param string $title
46 private function getTitle( $title ) {
47 return Title
::makeTitleSafe( $this->getDefaultWikitextNS(), $title );
51 * @param string|Title $title
55 private function getPage( $title ) {
56 $title = ( $title instanceof Title
) ?
$title : $this->getTitle( $title );
58 return WikiPage
::factory( $title );
62 * @param string|Title|WikiPage $page
64 * @return DerivedPageDataUpdater
66 private function getDerivedPageDataUpdater( $page, RevisionRecord
$rec = null ) {
67 if ( is_string( $page ) ||
$page instanceof Title
) {
68 $page = $this->getPage( $page );
71 $page = TestingAccessWrapper
::newFromObject( $page );
72 return $page->getDerivedDataUpdater( null, $rec );
76 * Creates a revision in the database.
78 * @param WikiPage $page
80 * @param null|string|Content $content
82 * @return RevisionRecord|null
84 private function createRevision( WikiPage
$page, $summary, $content = null ) {
85 $user = $this->getTestUser()->getUser();
86 $comment = CommentStoreComment
::newUnsavedComment( $summary );
88 if ( $content === null ||
is_string( $content ) ) {
89 $content = new WikitextContent( $content ??
$summary );
92 if ( !is_array( $content ) ) {
93 $content = [ 'main' => $content ];
96 $this->getDerivedPageDataUpdater( $page ); // flush cached instance before.
98 $updater = $page->newPageUpdater( $user );
100 foreach ( $content as $role => $c ) {
101 $updater->setContent( $role, $c );
104 $rev = $updater->saveRevision( $comment );
105 if ( !$updater->wasSuccessful() ) {
106 $this->fail( $updater->getStatus()->getWikiText() );
109 $this->getDerivedPageDataUpdater( $page ); // flush cached instance after.
113 // TODO: test setArticleCountMethod() and isCountable();
114 // TODO: test isRedirect() and wasRedirect()
117 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOptions()
119 public function testGetCanonicalParserOptions() {
120 $user = $this->getTestUser()->getUser();
121 $page = $this->getPage( __METHOD__
);
123 $parentRev = $this->createRevision( $page, 'first' );
125 $mainContent = new WikitextContent( 'Lorem ipsum' );
127 $update = new RevisionSlotsUpdate();
128 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
129 $updater = $this->getDerivedPageDataUpdater( $page );
130 $updater->prepareContent( $user, $update, false );
132 $options1 = $updater->getCanonicalParserOptions();
133 $this->assertSame( MediaWikiServices
::getInstance()->getContentLanguage(),
134 $options1->getUserLangObj() );
136 $speculativeId = $options1->getSpeculativeRevId();
137 $this->assertSame( $parentRev->getId() +
1, $speculativeId );
139 $rev = $this->makeRevision(
143 $parentRev->getId() +
7,
146 $updater->prepareUpdate( $rev );
148 $options2 = $updater->getCanonicalParserOptions();
150 $currentRev = call_user_func( $options2->getCurrentRevisionCallback(), $page->getTitle() );
151 $this->assertSame( $rev->getId(), $currentRev->getId() );
155 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::grabCurrentRevision()
156 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
158 public function testGrabCurrentRevision() {
159 $page = $this->getPage( __METHOD__
);
161 $updater0 = $this->getDerivedPageDataUpdater( $page );
162 $this->assertNull( $updater0->grabCurrentRevision() );
163 $this->assertFalse( $updater0->pageExisted() );
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() );
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() );
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()
191 public function testPrepareContent() {
192 MediaWikiServices
::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
194 CONTENT_MODEL_WIKITEXT
197 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
198 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
200 $this->assertFalse( $updater->isContentPrepared() );
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(
208 SlotRecord
::newUnsaved( 'aux', $auxContent )
211 $update = new RevisionSlotsUpdate();
212 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
213 $update->modifySlot( SlotRecord
::newInherited( $auxSlot ) );
214 // TODO: MCR: test removing slots!
216 $updater->prepareContent( $sysop, $update, false );
218 // second be ok to call again with the same params
219 $updater->prepareContent( $sysop, $update, false );
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() );
229 $this->assertNotNull( $updater->getRevision() );
230 $this->assertNotNull( $updater->getRenderedRevision() );
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() );
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() );
243 $auxSlot = $updater->getRawSlot( 'aux' );
244 $this->assertInstanceOf( SlotRecord
::class, $auxSlot );
245 $this->assertContains( '~~~', $auxSlot->getContent()->serialize(), 'No PST should apply.' );
247 $mainOutput = $updater->getCanonicalParserOutput();
248 $this->assertContains( 'first', $mainOutput->getText() );
249 $this->assertContains( '<a ', $mainOutput->getText() );
250 $this->assertNotEmpty( $mainOutput->getLinks() );
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() );
260 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
261 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
262 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
263 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
265 public function testPrepareContentInherit() {
266 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
267 $page = $this->getPage( __METHOD__
);
269 $mainContent1 = new WikitextContent( 'first [[main]] ({{REVISIONUSER}}) #~~~#' );
270 $mainContent2 = new WikitextContent( 'second ({{subst:REVISIONUSER}}) #~~~#' );
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();
277 $update = new RevisionSlotsUpdate();
278 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
279 $updater1 = $this->getDerivedPageDataUpdater( $page );
280 $updater1->prepareContent( $sysop, $update, false );
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() );
288 $this->assertNotNull( $updater1->getRevision() );
289 $this->assertNotNull( $updater1->getRenderedRevision() );
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 ~~~' );
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 );
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 ~~~' );
312 $this->assertFalse( $updater2->isCreation() );
313 $this->assertTrue( $updater2->isChange() );
316 // TODO: test failure of prepareContent() when called again...
317 // - with different user
318 // - with different update
319 // - after calling prepareUpdate()
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()
333 public function testPrepareUpdate() {
334 $page = $this->getPage( __METHOD__
);
336 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
337 $rev1 = $this->createRevision( $page, 'first', $mainContent1 );
338 $updater1 = $this->getDerivedPageDataUpdater( $page, $rev1 );
340 $options = []; // TODO: test *all* the options...
341 $updater1->prepareUpdate( $rev1, $options );
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() );
349 $this->assertNotNull( $updater1->getRevision() );
350 $this->assertNotNull( $updater1->getRenderedRevision() );
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() );
358 // TODO: MCR: test multiple slots, test slot removal!
360 $this->assertInstanceOf( SlotRecord
::class, $updater1->getRawSlot( SlotRecord
::MAIN
) );
361 $this->assertNotContains( '~~~~', $updater1->getRawContent( SlotRecord
::MAIN
)->serialize() );
363 $mainOutput = $updater1->getCanonicalParserOutput();
364 $this->assertContains( 'first', $mainOutput->getText() );
365 $this->assertContains( '<a ', $mainOutput->getText() );
366 $this->assertNotEmpty( $mainOutput->getLinks() );
368 $canonicalOutput = $updater1->getCanonicalParserOutput();
369 $this->assertContains( 'first', $canonicalOutput->getText() );
370 $this->assertContains( '<a ', $canonicalOutput->getText() );
371 $this->assertNotEmpty( $canonicalOutput->getLinks() );
373 $mainContent2 = new WikitextContent( 'second' );
374 $rev2 = $this->createRevision( $page, 'second', $mainContent2 );
375 $updater2 = $this->getDerivedPageDataUpdater( $page, $rev2 );
377 $options = []; // TODO: test *all* the options...
378 $updater2->prepareUpdate( $rev2, $options );
380 $this->assertFalse( $updater2->isCreation() );
381 $this->assertTrue( $updater2->isChange() );
383 $canonicalOutput = $updater2->getCanonicalParserOutput();
384 $this->assertContains( 'second', $canonicalOutput->getText() );
388 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
390 public function testPrepareUpdateReusesParserOutput() {
391 $user = $this->getTestUser()->getUser();
392 $page = $this->getPage( __METHOD__
);
394 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
396 $update = new RevisionSlotsUpdate();
397 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
398 $updater = $this->getDerivedPageDataUpdater( $page );
399 $updater->prepareContent( $user, $update, false );
401 $mainOutput = $updater->getSlotParserOutput( SlotRecord
::MAIN
);
402 $canonicalOutput = $updater->getCanonicalParserOutput();
404 $rev = $this->createRevision( $page, 'first', $mainContent1 );
406 $options = []; // TODO: test *all* the options...
407 $updater->prepareUpdate( $rev, $options );
409 $this->assertTrue( $updater->isUpdatePrepared() );
410 $this->assertTrue( $updater->isContentPrepared() );
412 $this->assertSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord
::MAIN
) );
413 $this->assertSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
417 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
418 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
420 public function testPrepareUpdateOutputReset() {
421 $user = $this->getTestUser()->getUser();
422 $page = $this->getPage( __METHOD__
);
424 $mainContent1 = new WikitextContent( 'first --{{REVISIONID}}--' );
426 $update = new RevisionSlotsUpdate();
427 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
428 $updater = $this->getDerivedPageDataUpdater( $page );
429 $updater->prepareContent( $user, $update, false );
431 $mainOutput = $updater->getSlotParserOutput( SlotRecord
::MAIN
);
432 $canonicalOutput = $updater->getCanonicalParserOutput();
434 // prevent optimization on matching speculative ID
435 $mainOutput->setSpeculativeRevIdUsed( 0 );
436 $canonicalOutput->setSpeculativeRevIdUsed( 0 );
438 $rev = $this->createRevision( $page, 'first', $mainContent1 );
440 $options = []; // TODO: test *all* the options...
441 $updater->prepareUpdate( $rev, $options );
443 $this->assertTrue( $updater->isUpdatePrepared() );
444 $this->assertTrue( $updater->isContentPrepared() );
446 // ParserOutput objects should have been flushed.
447 $this->assertNotSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord
::MAIN
) );
448 $this->assertNotSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
450 $html = $updater->getCanonicalParserOutput()->getText();
451 $this->assertContains( '--' . $rev->getId() . '--', $html );
453 // TODO: MCR: ensure that when the main slot uses {{REVISIONID}} but another slot is
454 // updated, the main slot is still re-rendered!
457 // TODO: test failure of prepareUpdate() when called again with a different revision
458 // TODO: test failure of prepareUpdate() on inconsistency with prepareContent.
461 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
463 public function testGetPreparedEditAfterPrepareContent() {
464 $user = $this->getTestUser()->getUser();
466 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
467 $update = new RevisionSlotsUpdate();
468 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
470 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
471 $updater->prepareContent( $user, $update, false );
473 $canonicalOutput = $updater->getCanonicalParserOutput();
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
);
485 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
487 public function testGetPreparedEditAfterPrepareUpdate() {
488 $clock = MWTimestamp
::convert( TS_UNIX
, '20100101000000' );
489 MWTimestamp
::setFakeTime( function () use ( &$clock ) {
493 $page = $this->getPage( __METHOD__
);
495 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
496 $update = new MutableRevisionSlots();
497 $update->setContent( SlotRecord
::MAIN
, $mainContent );
499 $rev = $this->createRevision( $page, __METHOD__
);
501 $updater = $this->getDerivedPageDataUpdater( $page );
502 $updater->prepareUpdate( $rev );
504 $canonicalOutput = $updater->getCanonicalParserOutput();
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
);
514 public function testGetSecondaryDataUpdatesAfterPrepareContent() {
515 $user = $this->getTestUser()->getUser();
516 $page = $this->getPage( __METHOD__
);
517 $this->createRevision( $page, __METHOD__
);
519 $mainContent1 = new WikitextContent( 'first' );
521 $update = new RevisionSlotsUpdate();
522 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
523 $updater = $this->getDerivedPageDataUpdater( $page );
524 $updater->prepareContent( $user, $update, false );
526 $dataUpdates = $updater->getSecondaryDataUpdates();
528 $this->assertNotEmpty( $dataUpdates );
530 $linksUpdates = array_filter( $dataUpdates, function ( $du ) {
531 return $du instanceof LinksUpdate
;
533 $this->assertCount( 1, $linksUpdates );
537 * @param string $name
539 * @return ContentHandler
541 private function defineMockContentModelForUpdateTesting( $name ) {
542 /** @var ContentHandler|MockObject $handler */
543 $handler = $this->getMockBuilder( TextContentHandler
::class )
544 ->setConstructorArgs( [ $name ] )
546 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
550 $dataUpdate = new MWCallableUpdate( 'time' );
551 $dataUpdate->_name
= "$name data update";
553 $deletionUpdate = new MWCallableUpdate( 'time' );
554 $deletionUpdate->_name
= "$name deletion update";
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 );
564 $this->mergeMwGlobalArrayValue(
565 'wgContentHandlers', [
566 $name => function () use ( $handler ){
576 * @param ContentHandler $handler
577 * @param string $text
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' ] )
588 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
589 $content->method( 'getContentHandler' )->willReturn( $handler );
594 public function testGetSecondaryDataUpdatesWithSlotRemoval() {
595 if ( !$this->hasMultiSlotSupport() ) {
596 $this->markTestSkipped( 'Slot removal cannot happen with MCR being enabled' );
599 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
600 $a1 = $this->defineMockContentModelForUpdateTesting( 'A1' );
601 $m2 = $this->defineMockContentModelForUpdateTesting( 'M2' );
603 MediaWikiServices
::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
608 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
609 $auxContent1 = $this->createMockContent( $a1, 'aux 1' );
610 $mainContent2 = $this->createMockContent( $m2, 'main 2' );
612 $user = $this->getTestUser()->getUser();
613 $page = $this->getPage( __METHOD__
);
614 $this->createRevision(
617 [ 'main' => $mainContent1, 'aux' => $auxContent1 ]
620 $update = new RevisionSlotsUpdate();
621 $update->modifyContent( SlotRecord
::MAIN
, $mainContent2 );
622 $update->removeSlot( 'aux' );
624 $page = $this->getPage( __METHOD__
);
625 $updater = $this->getDerivedPageDataUpdater( $page );
626 $updater->prepareContent( $user, $update, false );
628 $dataUpdates = $updater->getSecondaryDataUpdates();
630 $this->assertNotEmpty( $dataUpdates );
632 $updateNames = array_map( function ( $du ) {
633 return isset( $du->_name
) ?
$du->_name
: get_class( $du );
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 );
643 * Creates a dummy revision object without touching the database.
645 * @param Title $title
646 * @param RevisionSlotsUpdate $update
648 * @param string $comment
650 * @param int $parentId
652 * @return MutableRevisionRecord
654 private function makeRevision(
656 RevisionSlotsUpdate
$update,
662 $rev = new MutableRevisionRecord( $title );
664 $rev->applyUpdate( $update );
665 $rev->setUser( $user );
666 $rev->setComment( CommentStoreComment
::newUnsavedComment( $comment ) );
668 $rev->setPageId( $title->getArticleID() );
669 $rev->setParentId( $parentId );
678 private function getMockTitle( $id = 23 ) {
679 $mock = $this->getMockBuilder( Title
::class )
680 ->disableOriginalConstructor()
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 ) );
692 public function provideIsReusableFor() {
693 $title = $this->getMockTitle();
695 $user1 = User
::newFromName( 'Alice' );
696 $user2 = User
::newFromName( 'Bob' );
698 $content1 = new WikitextContent( 'one' );
699 $content2 = new WikitextContent( 'two' );
701 $update1 = new RevisionSlotsUpdate();
702 $update1->modifyContent( SlotRecord
::MAIN
, $content1 );
704 $update1b = new RevisionSlotsUpdate();
705 $update1b->modifyContent( 'xyz', $content1 );
707 $update2 = new RevisionSlotsUpdate();
708 $update2->modifyContent( SlotRecord
::MAIN
, $content2 );
710 $rev1 = $this->makeRevision( $title, $update1, $user1, 'rev1', 11 );
711 $rev1b = $this->makeRevision( $title, $update1b, $user1, 'rev1', 11 );
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 );
719 '$prepRevision' => null,
720 '$prepUpdate' => null,
722 '$forRevision' => null,
723 '$forUpdate' => null,
724 '$forParent' => null,
725 '$isReusable' => true,
728 '$prepUser' => $user1,
729 '$prepRevision' => $rev1,
730 '$prepUpdate' => $update1,
732 '$forRevision' => null,
733 '$forUpdate' => null,
734 '$forParent' => null,
735 '$isReusable' => true,
737 yield
'unprepared' => [
739 '$prepRevision' => null,
740 '$prepUpdate' => null,
741 '$forUser' => $user1,
742 '$forRevision' => $rev1,
743 '$forUpdate' => $update1,
745 '$isReusable' => true,
747 yield
'match prepareContent' => [
748 '$prepUser' => $user1,
749 '$prepRevision' => null,
750 '$prepUpdate' => $update1,
751 '$forUser' => $user1,
752 '$forRevision' => null,
753 '$forUpdate' => $update1,
755 '$isReusable' => true,
757 yield
'match prepareUpdate' => [
759 '$prepRevision' => $rev1,
760 '$prepUpdate' => null,
761 '$forUser' => $user1,
762 '$forRevision' => $rev1,
763 '$forUpdate' => null,
765 '$isReusable' => true,
767 yield
'match all' => [
768 '$prepUser' => $user1,
769 '$prepRevision' => $rev1,
770 '$prepUpdate' => $update1,
771 '$forUser' => $user1,
772 '$forRevision' => $rev1,
773 '$forUpdate' => $update1,
775 '$isReusable' => true,
777 yield
'mismatch prepareContent update' => [
778 '$prepUser' => $user1,
779 '$prepRevision' => null,
780 '$prepUpdate' => $update1,
781 '$forUser' => $user1,
782 '$forRevision' => null,
783 '$forUpdate' => $update1b,
785 '$isReusable' => false,
787 yield
'mismatch prepareContent user' => [
788 '$prepUser' => $user1,
789 '$prepRevision' => null,
790 '$prepUpdate' => $update1,
791 '$forUser' => $user2,
792 '$forRevision' => null,
793 '$forUpdate' => $update1,
795 '$isReusable' => false,
797 yield
'mismatch prepareContent parent' => [
798 '$prepUser' => $user1,
799 '$prepRevision' => null,
800 '$prepUpdate' => $update1,
801 '$forUser' => $user1,
802 '$forRevision' => null,
803 '$forUpdate' => $update1,
805 '$isReusable' => false,
807 yield
'mismatch prepareUpdate revision update' => [
809 '$prepRevision' => $rev1,
810 '$prepUpdate' => null,
812 '$forRevision' => $rev1b,
813 '$forUpdate' => null,
815 '$isReusable' => false,
817 yield
'mismatch prepareUpdate revision id' => [
819 '$prepRevision' => $rev2,
820 '$prepUpdate' => null,
822 '$forRevision' => $rev2y,
823 '$forUpdate' => null,
825 '$isReusable' => false,
830 * @dataProvider provideIsReusableFor
831 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isReusableFor()
833 * @param User|null $prepUser
834 * @param RevisionRecord|null $prepRevision
835 * @param RevisionSlotsUpdate|null $prepUpdate
836 * @param User|null $forUser
837 * @param RevisionRecord|null $forRevision
838 * @param RevisionSlotsUpdate|null $forUpdate
839 * @param int|null $forParent
840 * @param bool $isReusable
842 public function testIsReusableFor(
843 User
$prepUser = null,
844 RevisionRecord
$prepRevision = null,
845 RevisionSlotsUpdate
$prepUpdate = null,
846 User
$forUser = null,
847 RevisionRecord
$forRevision = null,
848 RevisionSlotsUpdate
$forUpdate = null,
852 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
855 $updater->prepareContent( $prepUser, $prepUpdate, false );
858 if ( $prepRevision ) {
859 $updater->prepareUpdate( $prepRevision );
864 $updater->isReusableFor( $forUser, $forRevision, $forUpdate, $forParent )
869 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
870 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
871 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
873 public function testDoUpdates() {
874 $page = $this->getPage( __METHOD__
);
876 $content = [ 'main' => new WikitextContent( 'first [[main]]' ) ];
878 if ( $this->hasMultiSlotSupport() ) {
879 $content['aux'] = new WikitextContent( 'Aux [[Nix]]' );
881 MediaWikiServices
::getInstance()->getSlotRoleRegistry()->defineRoleWithModel(
883 CONTENT_MODEL_WIKITEXT
887 $rev = $this->createRevision( $page, 'first', $content );
888 $pageId = $page->getId();
890 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
891 $this->db
->delete( 'pagelinks', '*' );
893 $pcache = MediaWikiServices
::getInstance()->getParserCache();
894 $pcache->deleteOptionsKey( $page );
896 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
897 $updater->setArticleCountMethod( 'link' );
899 $options = []; // TODO: test *all* the options...
900 $updater->prepareUpdate( $rev, $options );
902 $updater->doUpdates();
904 // links table update
905 $pageLinks = $this->db
->select(
908 [ 'pl_from' => $pageId ],
910 [ 'ORDER BY' => 'pl_namespace, pl_title' ]
913 $pageLinksRow = $pageLinks->fetchObject();
914 $this->assertInternalType( 'object', $pageLinksRow );
915 $this->assertSame( 'Main', $pageLinksRow->pl_title
);
917 if ( $this->hasMultiSlotSupport() ) {
918 $pageLinksRow = $pageLinks->fetchObject();
919 $this->assertInternalType( 'object', $pageLinksRow );
920 $this->assertSame( 'Nix', $pageLinksRow->pl_title
);
923 // parser cache update
924 $cached = $pcache->get( $page, $updater->getCanonicalParserOptions() );
925 $this->assertInternalType( 'object', $cached );
926 $this->assertSame( $updater->getCanonicalParserOutput(), $cached );
929 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
930 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
931 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
932 $this->assertSame( $oldStats->ss_good_articles +
1, (int)$stats->ss_good_articles
);
934 // TODO: MCR: test data updates for additional slots!
935 // TODO: test update for edit without page creation
936 // TODO: test message cache purge
937 // TODO: test module cache purge
938 // TODO: test CDN purge
939 // TODO: test newtalk update
940 // TODO: test search update
941 // TODO: test site stats good_articles while turning the page into (or back from) a redir.
942 // TODO: test category membership update (with setRcWatchCategoryMembership())
945 private function hasMultiSlotSupport() {
946 global $wgMultiContentRevisionSchemaMigrationStage;
948 return ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
)
949 && ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
);