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 );
106 $this->getDerivedPageDataUpdater( $page ); // flush cached instance after.
110 // TODO: test setArticleCountMethod() and isCountable();
111 // TODO: test isRedirect() and wasRedirect()
114 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOptions()
116 public function testGetCanonicalParserOptions() {
117 $user = $this->getTestUser()->getUser();
118 $page = $this->getPage( __METHOD__
);
120 $parentRev = $this->createRevision( $page, 'first' );
122 $mainContent = new WikitextContent( 'Lorem ipsum' );
124 $update = new RevisionSlotsUpdate();
125 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
126 $updater = $this->getDerivedPageDataUpdater( $page );
127 $updater->prepareContent( $user, $update, false );
129 $options1 = $updater->getCanonicalParserOptions();
130 $this->assertSame( MediaWikiServices
::getInstance()->getContentLanguage(),
131 $options1->getUserLangObj() );
133 $speculativeId = $options1->getSpeculativeRevId();
134 $this->assertSame( $parentRev->getId() +
1, $speculativeId );
136 $rev = $this->makeRevision(
140 $parentRev->getId() +
7,
143 $updater->prepareUpdate( $rev );
145 $options2 = $updater->getCanonicalParserOptions();
147 $currentRev = call_user_func( $options2->getCurrentRevisionCallback(), $page->getTitle() );
148 $this->assertSame( $rev->getId(), $currentRev->getId() );
152 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::grabCurrentRevision()
153 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
155 public function testGrabCurrentRevision() {
156 $page = $this->getPage( __METHOD__
);
158 $updater0 = $this->getDerivedPageDataUpdater( $page );
159 $this->assertNull( $updater0->grabCurrentRevision() );
160 $this->assertFalse( $updater0->pageExisted() );
162 $rev1 = $this->createRevision( $page, 'first' );
163 $updater1 = $this->getDerivedPageDataUpdater( $page );
164 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
165 $this->assertFalse( $updater0->pageExisted() );
166 $this->assertTrue( $updater1->pageExisted() );
168 $rev2 = $this->createRevision( $page, 'second' );
169 $updater2 = $this->getDerivedPageDataUpdater( $page );
170 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
171 $this->assertSame( $rev2->getId(), $updater2->grabCurrentRevision()->getId() );
175 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
176 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isContentPrepared()
177 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
178 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
179 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
180 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
181 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
182 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
183 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
184 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
185 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
186 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
188 public function testPrepareContent() {
189 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
190 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
192 $this->assertFalse( $updater->isContentPrepared() );
195 // TODO: MCR: Test multiple slots. Test slot removal.
196 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
197 $auxContent = new WikitextContent( 'inherited ~~~ content' );
198 $auxSlot = SlotRecord
::newSaved(
200 SlotRecord
::newUnsaved( 'aux', $auxContent )
203 $update = new RevisionSlotsUpdate();
204 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
205 $update->modifySlot( SlotRecord
::newInherited( $auxSlot ) );
206 // TODO: MCR: test removing slots!
208 $updater->prepareContent( $sysop, $update, false );
210 // second be ok to call again with the same params
211 $updater->prepareContent( $sysop, $update, false );
213 $this->assertNull( $updater->grabCurrentRevision() );
214 $this->assertTrue( $updater->isContentPrepared() );
215 $this->assertFalse( $updater->isUpdatePrepared() );
216 $this->assertFalse( $updater->pageExisted() );
217 $this->assertTrue( $updater->isCreation() );
218 $this->assertTrue( $updater->isChange() );
219 $this->assertFalse( $updater->isContentDeleted() );
221 $this->assertNotNull( $updater->getRevision() );
222 $this->assertNotNull( $updater->getRenderedRevision() );
224 $this->assertEquals( [ 'main', 'aux' ], $updater->getSlots()->getSlotRoles() );
225 $this->assertEquals( [ 'main' ], array_keys( $updater->getSlots()->getOriginalSlots() ) );
226 $this->assertEquals( [ 'aux' ], array_keys( $updater->getSlots()->getInheritedSlots() ) );
227 $this->assertEquals( [ 'main', 'aux' ], $updater->getModifiedSlotRoles() );
228 $this->assertEquals( [ 'main', 'aux' ], $updater->getTouchedSlotRoles() );
230 $mainSlot = $updater->getRawSlot( SlotRecord
::MAIN
);
231 $this->assertInstanceOf( SlotRecord
::class, $mainSlot );
232 $this->assertNotContains( '~~~', $mainSlot->getContent()->serialize(), 'PST should apply.' );
233 $this->assertContains( $sysop->getName(), $mainSlot->getContent()->serialize() );
235 $auxSlot = $updater->getRawSlot( 'aux' );
236 $this->assertInstanceOf( SlotRecord
::class, $auxSlot );
237 $this->assertContains( '~~~', $auxSlot->getContent()->serialize(), 'No PST should apply.' );
239 $mainOutput = $updater->getCanonicalParserOutput();
240 $this->assertContains( 'first', $mainOutput->getText() );
241 $this->assertContains( '<a ', $mainOutput->getText() );
242 $this->assertNotEmpty( $mainOutput->getLinks() );
244 $canonicalOutput = $updater->getCanonicalParserOutput();
245 $this->assertContains( 'first', $canonicalOutput->getText() );
246 $this->assertContains( '<a ', $canonicalOutput->getText() );
247 $this->assertContains( 'inherited ', $canonicalOutput->getText() );
248 $this->assertNotEmpty( $canonicalOutput->getLinks() );
252 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
253 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
254 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
255 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
257 public function testPrepareContentInherit() {
258 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
259 $page = $this->getPage( __METHOD__
);
261 $mainContent1 = new WikitextContent( 'first [[main]] ({{REVISIONUSER}}) #~~~#' );
262 $mainContent2 = new WikitextContent( 'second ({{subst:REVISIONUSER}}) #~~~#' );
264 $rev = $this->createRevision( $page, 'first', $mainContent1 );
265 $mainContent1 = $rev->getContent( SlotRecord
::MAIN
); // get post-pst content
266 $userName = $rev->getUser()->getName();
267 $sysopName = $sysop->getName();
269 $update = new RevisionSlotsUpdate();
270 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
271 $updater1 = $this->getDerivedPageDataUpdater( $page );
272 $updater1->prepareContent( $sysop, $update, false );
274 $this->assertNotNull( $updater1->grabCurrentRevision() );
275 $this->assertTrue( $updater1->isContentPrepared() );
276 $this->assertTrue( $updater1->pageExisted() );
277 $this->assertFalse( $updater1->isCreation() );
278 $this->assertFalse( $updater1->isChange() );
280 $this->assertNotNull( $updater1->getRevision() );
281 $this->assertNotNull( $updater1->getRenderedRevision() );
283 // parser-output for null-edit uses the original author's name
284 $html = $updater1->getRenderedRevision()->getRevisionParserOutput()->getText();
285 $this->assertNotContains( $sysopName, $html, '{{REVISIONUSER}}' );
286 $this->assertNotContains( '{{REVISIONUSER}}', $html, '{{REVISIONUSER}}' );
287 $this->assertNotContains( '~~~', $html, 'signature ~~~' );
288 $this->assertContains( '(' . $userName . ')', $html, '{{REVISIONUSER}}' );
289 $this->assertContains( '>' . $userName . '<', $html, 'signature ~~~' );
291 // TODO: MCR: test inheritance from parent
292 $update = new RevisionSlotsUpdate();
293 $update->modifyContent( SlotRecord
::MAIN
, $mainContent2 );
294 $updater2 = $this->getDerivedPageDataUpdater( $page );
295 $updater2->prepareContent( $sysop, $update, false );
297 // non-null edit use the new user name in PST
298 $pstText = $updater2->getSlots()->getContent( SlotRecord
::MAIN
)->serialize();
299 $this->assertNotContains( '{{subst:REVISIONUSER}}', $pstText, '{{subst:REVISIONUSER}}' );
300 $this->assertNotContains( '~~~', $pstText, 'signature ~~~' );
301 $this->assertContains( '(' . $sysopName . ')', $pstText, '{{subst:REVISIONUSER}}' );
302 $this->assertContains( ':' . $sysopName . '|', $pstText, 'signature ~~~' );
304 $this->assertFalse( $updater2->isCreation() );
305 $this->assertTrue( $updater2->isChange() );
308 // TODO: test failure of prepareContent() when called again...
309 // - with different user
310 // - with different update
311 // - after calling prepareUpdate()
314 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
315 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isUpdatePrepared()
316 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
317 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
318 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
319 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
320 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
321 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
322 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
323 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
325 public function testPrepareUpdate() {
326 $page = $this->getPage( __METHOD__
);
328 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
329 $rev1 = $this->createRevision( $page, 'first', $mainContent1 );
330 $updater1 = $this->getDerivedPageDataUpdater( $page, $rev1 );
332 $options = []; // TODO: test *all* the options...
333 $updater1->prepareUpdate( $rev1, $options );
335 $this->assertTrue( $updater1->isUpdatePrepared() );
336 $this->assertTrue( $updater1->isContentPrepared() );
337 $this->assertTrue( $updater1->isCreation() );
338 $this->assertTrue( $updater1->isChange() );
339 $this->assertFalse( $updater1->isContentDeleted() );
341 $this->assertNotNull( $updater1->getRevision() );
342 $this->assertNotNull( $updater1->getRenderedRevision() );
344 $this->assertEquals( [ 'main' ], $updater1->getSlots()->getSlotRoles() );
345 $this->assertEquals( [ 'main' ], array_keys( $updater1->getSlots()->getOriginalSlots() ) );
346 $this->assertEquals( [], array_keys( $updater1->getSlots()->getInheritedSlots() ) );
347 $this->assertEquals( [ 'main' ], $updater1->getModifiedSlotRoles() );
348 $this->assertEquals( [ 'main' ], $updater1->getTouchedSlotRoles() );
350 // TODO: MCR: test multiple slots, test slot removal!
352 $this->assertInstanceOf( SlotRecord
::class, $updater1->getRawSlot( SlotRecord
::MAIN
) );
353 $this->assertNotContains( '~~~~', $updater1->getRawContent( SlotRecord
::MAIN
)->serialize() );
355 $mainOutput = $updater1->getCanonicalParserOutput();
356 $this->assertContains( 'first', $mainOutput->getText() );
357 $this->assertContains( '<a ', $mainOutput->getText() );
358 $this->assertNotEmpty( $mainOutput->getLinks() );
360 $canonicalOutput = $updater1->getCanonicalParserOutput();
361 $this->assertContains( 'first', $canonicalOutput->getText() );
362 $this->assertContains( '<a ', $canonicalOutput->getText() );
363 $this->assertNotEmpty( $canonicalOutput->getLinks() );
365 $mainContent2 = new WikitextContent( 'second' );
366 $rev2 = $this->createRevision( $page, 'second', $mainContent2 );
367 $updater2 = $this->getDerivedPageDataUpdater( $page, $rev2 );
369 $options = []; // TODO: test *all* the options...
370 $updater2->prepareUpdate( $rev2, $options );
372 $this->assertFalse( $updater2->isCreation() );
373 $this->assertTrue( $updater2->isChange() );
375 $canonicalOutput = $updater2->getCanonicalParserOutput();
376 $this->assertContains( 'second', $canonicalOutput->getText() );
380 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
382 public function testPrepareUpdateReusesParserOutput() {
383 $user = $this->getTestUser()->getUser();
384 $page = $this->getPage( __METHOD__
);
386 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
388 $update = new RevisionSlotsUpdate();
389 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
390 $updater = $this->getDerivedPageDataUpdater( $page );
391 $updater->prepareContent( $user, $update, false );
393 $mainOutput = $updater->getSlotParserOutput( SlotRecord
::MAIN
);
394 $canonicalOutput = $updater->getCanonicalParserOutput();
396 $rev = $this->createRevision( $page, 'first', $mainContent1 );
398 $options = []; // TODO: test *all* the options...
399 $updater->prepareUpdate( $rev, $options );
401 $this->assertTrue( $updater->isUpdatePrepared() );
402 $this->assertTrue( $updater->isContentPrepared() );
404 $this->assertSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord
::MAIN
) );
405 $this->assertSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
409 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
410 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
412 public function testPrepareUpdateOutputReset() {
413 $user = $this->getTestUser()->getUser();
414 $page = $this->getPage( __METHOD__
);
416 $mainContent1 = new WikitextContent( 'first --{{REVISIONID}}--' );
418 $update = new RevisionSlotsUpdate();
419 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
420 $updater = $this->getDerivedPageDataUpdater( $page );
421 $updater->prepareContent( $user, $update, false );
423 $mainOutput = $updater->getSlotParserOutput( SlotRecord
::MAIN
);
424 $canonicalOutput = $updater->getCanonicalParserOutput();
426 // prevent optimization on matching speculative ID
427 $mainOutput->setSpeculativeRevIdUsed( 0 );
428 $canonicalOutput->setSpeculativeRevIdUsed( 0 );
430 $rev = $this->createRevision( $page, 'first', $mainContent1 );
432 $options = []; // TODO: test *all* the options...
433 $updater->prepareUpdate( $rev, $options );
435 $this->assertTrue( $updater->isUpdatePrepared() );
436 $this->assertTrue( $updater->isContentPrepared() );
438 // ParserOutput objects should have been flushed.
439 $this->assertNotSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord
::MAIN
) );
440 $this->assertNotSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
442 $html = $updater->getCanonicalParserOutput()->getText();
443 $this->assertContains( '--' . $rev->getId() . '--', $html );
445 // TODO: MCR: ensure that when the main slot uses {{REVISIONID}} but another slot is
446 // updated, the main slot is still re-rendered!
449 // TODO: test failure of prepareUpdate() when called again with a different revision
450 // TODO: test failure of prepareUpdate() on inconsistency with prepareContent.
453 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
455 public function testGetPreparedEditAfterPrepareContent() {
456 $user = $this->getTestUser()->getUser();
458 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
459 $update = new RevisionSlotsUpdate();
460 $update->modifyContent( SlotRecord
::MAIN
, $mainContent );
462 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
463 $updater->prepareContent( $user, $update, false );
465 $canonicalOutput = $updater->getCanonicalParserOutput();
467 $preparedEdit = $updater->getPreparedEdit();
468 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp
);
469 $this->assertSame( $canonicalOutput, $preparedEdit->output
);
470 $this->assertSame( $mainContent, $preparedEdit->newContent
);
471 $this->assertSame( $updater->getRawContent( SlotRecord
::MAIN
), $preparedEdit->pstContent
);
472 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts
);
473 $this->assertSame( null, $preparedEdit->revid
);
477 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
479 public function testGetPreparedEditAfterPrepareUpdate() {
480 $clock = MWTimestamp
::convert( TS_UNIX
, '20100101000000' );
481 MWTimestamp
::setFakeTime( function () use ( &$clock ) {
485 $page = $this->getPage( __METHOD__
);
487 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
488 $update = new MutableRevisionSlots();
489 $update->setContent( SlotRecord
::MAIN
, $mainContent );
491 $rev = $this->createRevision( $page, __METHOD__
);
493 $updater = $this->getDerivedPageDataUpdater( $page );
494 $updater->prepareUpdate( $rev );
496 $canonicalOutput = $updater->getCanonicalParserOutput();
498 $preparedEdit = $updater->getPreparedEdit();
499 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp
);
500 $this->assertSame( $canonicalOutput, $preparedEdit->output
);
501 $this->assertSame( $updater->getRawContent( SlotRecord
::MAIN
), $preparedEdit->pstContent
);
502 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts
);
503 $this->assertSame( $rev->getId(), $preparedEdit->revid
);
506 public function testGetSecondaryDataUpdatesAfterPrepareContent() {
507 $user = $this->getTestUser()->getUser();
508 $page = $this->getPage( __METHOD__
);
509 $this->createRevision( $page, __METHOD__
);
511 $mainContent1 = new WikitextContent( 'first' );
513 $update = new RevisionSlotsUpdate();
514 $update->modifyContent( SlotRecord
::MAIN
, $mainContent1 );
515 $updater = $this->getDerivedPageDataUpdater( $page );
516 $updater->prepareContent( $user, $update, false );
518 $dataUpdates = $updater->getSecondaryDataUpdates();
520 $this->assertNotEmpty( $dataUpdates );
522 $linksUpdates = array_filter( $dataUpdates, function ( $du ) {
523 return $du instanceof LinksUpdate
;
525 $this->assertCount( 1, $linksUpdates );
529 * @param string $name
531 * @return ContentHandler
533 private function defineMockContentModelForUpdateTesting( $name ) {
534 /** @var ContentHandler|MockObject $handler */
535 $handler = $this->getMockBuilder( TextContentHandler
::class )
536 ->setConstructorArgs( [ $name ] )
538 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
542 $dataUpdate = new MWCallableUpdate( 'time' );
543 $dataUpdate->_name
= "$name data update";
545 $deletionUpdate = new MWCallableUpdate( 'time' );
546 $deletionUpdate->_name
= "$name deletion update";
548 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] );
549 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] );
550 $handler->method( 'unserializeContent' )->willReturnCallback(
551 function ( $text ) use ( $handler ) {
552 return $this->createMockContent( $handler, $text );
556 $this->mergeMwGlobalArrayValue(
557 'wgContentHandlers', [
558 $name => function () use ( $handler ){
568 * @param ContentHandler $handler
569 * @param string $text
573 private function createMockContent( ContentHandler
$handler, $text ) {
574 /** @var Content|MockObject $content */
575 $content = $this->getMockBuilder( TextContent
::class )
576 ->setConstructorArgs( [ $text ] )
577 ->setMethods( [ 'getModel', 'getContentHandler' ] )
580 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
581 $content->method( 'getContentHandler' )->willReturn( $handler );
586 public function testGetSecondaryDataUpdatesWithSlotRemoval() {
587 global $wgMultiContentRevisionSchemaMigrationStage;
589 if ( ! ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) ) {
590 $this->markTestSkipped( 'Slot removal cannot happen with MCR being enabled' );
593 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
594 $a1 = $this->defineMockContentModelForUpdateTesting( 'A1' );
595 $m2 = $this->defineMockContentModelForUpdateTesting( 'M2' );
597 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
598 $auxContent1 = $this->createMockContent( $a1, 'aux 1' );
599 $mainContent2 = $this->createMockContent( $m2, 'main 2' );
601 $user = $this->getTestUser()->getUser();
602 $page = $this->getPage( __METHOD__
);
603 $this->createRevision(
606 [ 'main' => $mainContent1, 'aux' => $auxContent1 ]
609 $update = new RevisionSlotsUpdate();
610 $update->modifyContent( SlotRecord
::MAIN
, $mainContent2 );
611 $update->removeSlot( 'aux' );
613 $page = $this->getPage( __METHOD__
);
614 $updater = $this->getDerivedPageDataUpdater( $page );
615 $updater->prepareContent( $user, $update, false );
617 $dataUpdates = $updater->getSecondaryDataUpdates();
619 $this->assertNotEmpty( $dataUpdates );
621 $updateNames = array_map( function ( $du ) {
622 return isset( $du->_name
) ?
$du->_name
: get_class( $du );
625 $this->assertContains( LinksUpdate
::class, $updateNames );
626 $this->assertContains( 'A1 deletion update', $updateNames );
627 $this->assertContains( 'M2 data update', $updateNames );
628 $this->assertNotContains( 'M1 data update', $updateNames );
632 * Creates a dummy revision object without touching the database.
634 * @param Title $title
635 * @param RevisionSlotsUpdate $update
637 * @param string $comment
639 * @param int $parentId
641 * @return MutableRevisionRecord
643 private function makeRevision(
645 RevisionSlotsUpdate
$update,
651 $rev = new MutableRevisionRecord( $title );
653 $rev->applyUpdate( $update );
654 $rev->setUser( $user );
655 $rev->setComment( CommentStoreComment
::newUnsavedComment( $comment ) );
657 $rev->setPageId( $title->getArticleID() );
658 $rev->setParentId( $parentId );
667 private function getMockTitle( $id = 23 ) {
668 $mock = $this->getMockBuilder( Title
::class )
669 ->disableOriginalConstructor()
671 $mock->expects( $this->any() )
672 ->method( 'getDBkey' )
673 ->will( $this->returnValue( __CLASS__
) );
674 $mock->expects( $this->any() )
675 ->method( 'getArticleID' )
676 ->will( $this->returnValue( $id ) );
681 public function provideIsReusableFor() {
682 $title = $this->getMockTitle();
684 $user1 = User
::newFromName( 'Alice' );
685 $user2 = User
::newFromName( 'Bob' );
687 $content1 = new WikitextContent( 'one' );
688 $content2 = new WikitextContent( 'two' );
690 $update1 = new RevisionSlotsUpdate();
691 $update1->modifyContent( SlotRecord
::MAIN
, $content1 );
693 $update1b = new RevisionSlotsUpdate();
694 $update1b->modifyContent( 'xyz', $content1 );
696 $update2 = new RevisionSlotsUpdate();
697 $update2->modifyContent( SlotRecord
::MAIN
, $content2 );
699 $rev1 = $this->makeRevision( $title, $update1, $user1, 'rev1', 11 );
700 $rev1b = $this->makeRevision( $title, $update1b, $user1, 'rev1', 11 );
702 $rev2 = $this->makeRevision( $title, $update2, $user1, 'rev2', 12 );
703 $rev2x = $this->makeRevision( $title, $update2, $user2, 'rev2', 12 );
704 $rev2y = $this->makeRevision( $title, $update2, $user1, 'rev2', 122 );
708 '$prepRevision' => null,
709 '$prepUpdate' => null,
711 '$forRevision' => null,
712 '$forUpdate' => null,
713 '$forParent' => null,
714 '$isReusable' => true,
717 '$prepUser' => $user1,
718 '$prepRevision' => $rev1,
719 '$prepUpdate' => $update1,
721 '$forRevision' => null,
722 '$forUpdate' => null,
723 '$forParent' => null,
724 '$isReusable' => true,
726 yield
'unprepared' => [
728 '$prepRevision' => null,
729 '$prepUpdate' => null,
730 '$forUser' => $user1,
731 '$forRevision' => $rev1,
732 '$forUpdate' => $update1,
734 '$isReusable' => true,
736 yield
'match prepareContent' => [
737 '$prepUser' => $user1,
738 '$prepRevision' => null,
739 '$prepUpdate' => $update1,
740 '$forUser' => $user1,
741 '$forRevision' => null,
742 '$forUpdate' => $update1,
744 '$isReusable' => true,
746 yield
'match prepareUpdate' => [
748 '$prepRevision' => $rev1,
749 '$prepUpdate' => null,
750 '$forUser' => $user1,
751 '$forRevision' => $rev1,
752 '$forUpdate' => null,
754 '$isReusable' => true,
756 yield
'match all' => [
757 '$prepUser' => $user1,
758 '$prepRevision' => $rev1,
759 '$prepUpdate' => $update1,
760 '$forUser' => $user1,
761 '$forRevision' => $rev1,
762 '$forUpdate' => $update1,
764 '$isReusable' => true,
766 yield
'mismatch prepareContent update' => [
767 '$prepUser' => $user1,
768 '$prepRevision' => null,
769 '$prepUpdate' => $update1,
770 '$forUser' => $user1,
771 '$forRevision' => null,
772 '$forUpdate' => $update1b,
774 '$isReusable' => false,
776 yield
'mismatch prepareContent user' => [
777 '$prepUser' => $user1,
778 '$prepRevision' => null,
779 '$prepUpdate' => $update1,
780 '$forUser' => $user2,
781 '$forRevision' => null,
782 '$forUpdate' => $update1,
784 '$isReusable' => false,
786 yield
'mismatch prepareContent parent' => [
787 '$prepUser' => $user1,
788 '$prepRevision' => null,
789 '$prepUpdate' => $update1,
790 '$forUser' => $user1,
791 '$forRevision' => null,
792 '$forUpdate' => $update1,
794 '$isReusable' => false,
796 yield
'mismatch prepareUpdate revision update' => [
798 '$prepRevision' => $rev1,
799 '$prepUpdate' => null,
801 '$forRevision' => $rev1b,
802 '$forUpdate' => null,
804 '$isReusable' => false,
806 yield
'mismatch prepareUpdate revision user' => [
808 '$prepRevision' => $rev2,
809 '$prepUpdate' => null,
811 '$forRevision' => $rev2x,
812 '$forUpdate' => null,
814 '$isReusable' => false,
816 yield
'mismatch prepareUpdate revision id' => [
818 '$prepRevision' => $rev2,
819 '$prepUpdate' => null,
821 '$forRevision' => $rev2y,
822 '$forUpdate' => null,
824 '$isReusable' => false,
829 * @dataProvider provideIsReusableFor
830 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isReusableFor()
832 * @param User|null $prepUser
833 * @param RevisionRecord|null $prepRevision
834 * @param RevisionSlotsUpdate|null $prepUpdate
835 * @param User|null $forUser
836 * @param RevisionRecord|null $forRevision
837 * @param RevisionSlotsUpdate|null $forUpdate
838 * @param int|null $forParent
839 * @param bool $isReusable
841 public function testIsReusableFor(
842 User
$prepUser = null,
843 RevisionRecord
$prepRevision = null,
844 RevisionSlotsUpdate
$prepUpdate = null,
845 User
$forUser = null,
846 RevisionRecord
$forRevision = null,
847 RevisionSlotsUpdate
$forUpdate = null,
851 $updater = $this->getDerivedPageDataUpdater( __METHOD__
);
854 $updater->prepareContent( $prepUser, $prepUpdate, false );
857 if ( $prepRevision ) {
858 $updater->prepareUpdate( $prepRevision );
863 $updater->isReusableFor( $forUser, $forRevision, $forUpdate, $forParent )
868 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
869 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
870 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
872 public function testDoUpdates() {
873 $page = $this->getPage( __METHOD__
);
875 $content = [ 'main' => new WikitextContent( 'first [[main]]' ) ];
877 if ( $this->hasMultiSlotSupport() ) {
878 $content['aux'] = new WikitextContent( 'Aux [[Nix]]' );
881 $rev = $this->createRevision( $page, 'first', $content );
882 $pageId = $page->getId();
884 $oldStats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
885 $this->db
->delete( 'pagelinks', '*' );
887 $pcache = MediaWikiServices
::getInstance()->getParserCache();
888 $pcache->deleteOptionsKey( $page );
890 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
891 $updater->setArticleCountMethod( 'link' );
893 $options = []; // TODO: test *all* the options...
894 $updater->prepareUpdate( $rev, $options );
896 $updater->doUpdates();
898 // links table update
899 $pageLinks = $this->db
->select(
902 [ 'pl_from' => $pageId ],
904 [ 'ORDER BY' => 'pl_namespace, pl_title' ]
907 $pageLinksRow = $pageLinks->fetchObject();
908 $this->assertInternalType( 'object', $pageLinksRow );
909 $this->assertSame( 'Main', $pageLinksRow->pl_title
);
911 if ( $this->hasMultiSlotSupport() ) {
912 $pageLinksRow = $pageLinks->fetchObject();
913 $this->assertInternalType( 'object', $pageLinksRow );
914 $this->assertSame( 'Nix', $pageLinksRow->pl_title
);
917 // parser cache update
918 $cached = $pcache->get( $page, $updater->getCanonicalParserOptions() );
919 $this->assertInternalType( 'object', $cached );
920 $this->assertSame( $updater->getCanonicalParserOutput(), $cached );
923 $stats = $this->db
->selectRow( 'site_stats', '*', '1=1' );
924 $this->assertSame( $oldStats->ss_total_pages +
1, (int)$stats->ss_total_pages
);
925 $this->assertSame( $oldStats->ss_total_edits +
1, (int)$stats->ss_total_edits
);
926 $this->assertSame( $oldStats->ss_good_articles +
1, (int)$stats->ss_good_articles
);
928 // TODO: MCR: test data updates for additional slots!
929 // TODO: test update for edit without page creation
930 // TODO: test message cache purge
931 // TODO: test module cache purge
932 // TODO: test CDN purge
933 // TODO: test newtalk update
934 // TODO: test search update
935 // TODO: test site stats good_articles while turning the page into (or back from) a redir.
936 // TODO: test category membership update (with setRcWatchCategoryMembership())
939 private function hasMultiSlotSupport() {
940 global $wgMultiContentRevisionSchemaMigrationStage;
942 return ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
)
943 && ( $wgMultiContentRevisionSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
);