Debian Codegraph @

tests/phpunit/includes/Storage/DerivedPageDataUpdaterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Storage;
4
5 use DummyContentHandlerForTesting;
6 use MediaWiki\CommentStore\CommentStoreComment;
7 use MediaWiki\Config\ServiceOptions;
8 use MediaWiki\Content\Content;
9 use MediaWiki\Content\ContentHandler;
10 use MediaWiki\Content\JavaScriptContent;
11 use MediaWiki\Content\TextContent;
12 use MediaWiki\Content\TextContentHandler;
13 use MediaWiki\Content\WikitextContent;
14 use MediaWiki\Content\WikitextContentHandler;
15 use MediaWiki\Deferred\DeferredUpdates;
16 use MediaWiki\Deferred\LinksUpdate\LinksUpdate;
17 use MediaWiki\Deferred\MWCallableUpdate;
18 use MediaWiki\Edit\ParsoidRenderID;
19 use MediaWiki\MainConfigNames;
20 use MediaWiki\Message\Message;
21 use MediaWiki\Page\PageIdentity;
22 use MediaWiki\Page\PageIdentityValue;
23 use MediaWiki\Page\ParserOutputAccess;
24 use MediaWiki\Parser\ParserCacheFactory;
25 use MediaWiki\Parser\ParserOptions;
26 use MediaWiki\Revision\MutableRevisionRecord;
27 use MediaWiki\Revision\MutableRevisionSlots;
28 use MediaWiki\Revision\RevisionRecord;
29 use MediaWiki\Revision\SlotRecord;
30 use MediaWiki\Storage\DerivedPageDataUpdater;
31 use MediaWiki\Storage\EditResult;
32 use MediaWiki\Storage\EditResultCache;
33 use MediaWiki\Storage\RevisionSlotsUpdate;
34 use MediaWiki\Title\Title;
35 use MediaWiki\User\User;
36 use MediaWiki\User\UserIdentity;
37 use MediaWiki\User\UserIdentityValue;
38 use MediaWiki\Utils\MWTimestamp;
39 use MediaWikiIntegrationTestCase;
40 use MockTitleTrait;
41 use PHPUnit\Framework\MockObject\MockObject;
42 use Wikimedia\ObjectCache\BagOStuff;
43 use Wikimedia\Rdbms\Platform\ISQLPlatform;
44 use Wikimedia\TestingAccessWrapper;
45 use Wikimedia\Timestamp\ConvertibleTimestamp;
46 use WikiPage;
47
48 /**
49 * @group Database
50 *
51 * @covers \MediaWiki\Storage\DerivedPageDataUpdater
52 */
53 class DerivedPageDataUpdaterTest extends MediaWikiIntegrationTestCase {
54 use MockTitleTrait;
55
56 /**
57 * @param string $title
58 *
59 * @return Title
60 */
61 private function getTitle( $title ) {
62 return Title::makeTitleSafe( $this->getDefaultWikitextNS(), $title );
63 }
64
65 /**
66 * @param string|Title $title
67 *
68 * @return WikiPage
69 */
70 private function getPage( $title ) {
71 $title = ( $title instanceof Title ) ? $title : $this->getTitle( $title );
72
73 return $this->getServiceContainer()->getWikiPageFactory()->newFromTitle( $title );
74 }
75
76 /**
77 * @param string|Title|WikiPage $page
78 * @param RevisionRecord|null $rec
79 * @param User|null $user
80 *
81 * @return DerivedPageDataUpdater
82 */
83 private function getDerivedPageDataUpdater(
84 $page, ?RevisionRecord $rec = null, ?User $user = null
85 ) {
86 if ( is_string( $page ) || $page instanceof Title ) {
87 $page = $this->getPage( $page );
88 }
89
90 $page = TestingAccessWrapper::newFromObject( $page );
91 return $page->getDerivedDataUpdater( $user, $rec );
92 }
93
94 /**
95 * Creates a revision in the database.
96 *
97 * @param WikiPage $page
98 * @param string|Message|CommentStoreComment $summary
99 * @param null|string|Content|Content[] $content
100 * @param User|null $user
101 *
102 * @return RevisionRecord|null
103 */
104 private function createRevision( WikiPage $page, $summary, $content = null, $user = null ) {
105 $user ??= $this->getTestUser()->getUser();
106 $comment = CommentStoreComment::newUnsavedComment( $summary );
107
108 if ( $content === null || is_string( $content ) ) {
109 $content = new WikitextContent( $content ?? $summary );
110 }
111
112 if ( !is_array( $content ) ) {
113 $content = [ SlotRecord::MAIN => $content ];
114 }
115
116 $this->getDerivedPageDataUpdater( $page ); // flush cached instance before.
117
118 $updater = $page->newPageUpdater( $user );
119
120 foreach ( $content as $role => $c ) {
121 $updater->setContent( $role, $c );
122 }
123
124 $rev = $updater->saveRevision( $comment );
125 if ( !$updater->wasSuccessful() ) {
126 $this->fail( $updater->getStatus()->getWikiText() );
127 }
128
129 $this->getDerivedPageDataUpdater( $page ); // flush cached instance after.
130 return $rev;
131 }
132
133 // TODO: test setArticleCountMethod() and isCountable();
134 // TODO: test isRedirect() and wasRedirect()
135
136 /**
137 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOptions()
138 */
139 public function testGetCanonicalParserOptions() {
140 $user = $this->getTestUser()->getUser();
141 $page = $this->getPage( __METHOD__ );
142
143 $parentRev = $this->createRevision( $page, 'first' );
144
145 $mainContent = new WikitextContent( 'Lorem ipsum' );
146
147 $update = new RevisionSlotsUpdate();
148 $update->modifyContent( SlotRecord::MAIN, $mainContent );
149 $updater = $this->getDerivedPageDataUpdater( $page );
150 $updater->prepareContent( $user, $update, false );
151
152 $options1 = $updater->getCanonicalParserOptions();
153 $this->assertSame( $this->getServiceContainer()->getContentLanguage(),
154 $options1->getUserLangObj() );
155
156 $speculativeId = $options1->getSpeculativeRevId();
157 $this->assertSame( $parentRev->getId() + 1, $speculativeId );
158
159 $rev = $this->makeRevision(
160 $page->getTitle(),
161 $update,
162 $user,
163 $parentRev->getId() + 7,
164 $parentRev->getId()
165 );
166 $updater->prepareUpdate( $rev );
167
168 $options2 = $updater->getCanonicalParserOptions();
169
170 $currentRev = $options2->getCurrentRevisionRecordCallback()( $page->getTitle() );
171 $this->assertSame( $rev->getId(), $currentRev->getId() );
172 }
173
174 /**
175 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::grabCurrentRevision()
176 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
177 */
178 public function testGrabCurrentRevision() {
179 $page = $this->getPage( __METHOD__ );
180
181 $updater0 = $this->getDerivedPageDataUpdater( $page );
182 $this->assertNull( $updater0->grabCurrentRevision() );
183 $this->assertFalse( $updater0->pageExisted() );
184
185 $rev1 = $this->createRevision( $page, 'first' );
186 $updater1 = $this->getDerivedPageDataUpdater( $page );
187 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
188 $this->assertFalse( $updater0->pageExisted() );
189 $this->assertTrue( $updater1->pageExisted() );
190
191 $rev2 = $this->createRevision( $page, 'second' );
192 $updater2 = $this->getDerivedPageDataUpdater( $page );
193 $this->assertSame( $rev1->getId(), $updater1->grabCurrentRevision()->getId() );
194 $this->assertSame( $rev2->getId(), $updater2->grabCurrentRevision()->getId() );
195 }
196
197 /**
198 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
199 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isContentPrepared()
200 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
201 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
202 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
203 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
204 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
205 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
206 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
207 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
208 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
209 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
210 */
211 public function testPrepareContent() {
212 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
213 if ( !$slotRoleRegistry->isDefinedRole( 'aux' ) ) {
214 $slotRoleRegistry->defineRoleWithModel(
215 'aux',
216 CONTENT_MODEL_WIKITEXT
217 );
218 }
219
220 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
221 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
222
223 $this->assertFalse( $updater->isContentPrepared() );
224
225 // TODO: test stash
226 // TODO: MCR: Test multiple slots. Test slot removal.
227 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
228 $auxContent = new WikitextContent( 'inherited ~~~ content' );
229 $auxSlot = SlotRecord::newSaved(
230 10, 7, 'tt:7',
231 SlotRecord::newUnsaved( 'aux', $auxContent )
232 );
233
234 $update = new RevisionSlotsUpdate();
235 $update->modifyContent( SlotRecord::MAIN, $mainContent );
236 $update->modifySlot( SlotRecord::newInherited( $auxSlot ) );
237 // TODO: MCR: test removing slots!
238
239 $updater->prepareContent( $sysop, $update, false );
240
241 // second be ok to call again with the same params
242 $updater->prepareContent( $sysop, $update, false );
243
244 $this->assertNull( $updater->grabCurrentRevision() );
245 $this->assertTrue( $updater->isContentPrepared() );
246 $this->assertFalse( $updater->isUpdatePrepared() );
247 $this->assertFalse( $updater->pageExisted() );
248 $this->assertTrue( $updater->isCreation() );
249 $this->assertTrue( $updater->isChange() );
250 $this->assertFalse( $updater->isContentDeleted() );
251
252 $this->assertNotNull( $updater->getRevision() );
253 $this->assertNotNull( $updater->getRenderedRevision() );
254
255 $this->assertEquals( [ SlotRecord::MAIN, 'aux' ], $updater->getSlots()->getSlotRoles() );
256 $this->assertEquals( [ SlotRecord::MAIN ], array_keys( $updater->getSlots()->getOriginalSlots() ) );
257 $this->assertEquals( [ 'aux' ], array_keys( $updater->getSlots()->getInheritedSlots() ) );
258 $this->assertEquals( [ SlotRecord::MAIN, 'aux' ], $updater->getModifiedSlotRoles() );
259 $this->assertEquals( [ SlotRecord::MAIN, 'aux' ], $updater->getTouchedSlotRoles() );
260
261 $mainSlot = $updater->getRawSlot( SlotRecord::MAIN );
262 $this->assertInstanceOf( SlotRecord::class, $mainSlot );
263 $this->assertStringNotContainsString(
264 '~~~',
265 $mainSlot->getContent()->serialize(),
266 'PST should apply.'
267 );
268 $this->assertStringContainsString( $sysop->getName(), $mainSlot->getContent()->serialize() );
269
270 $auxSlot = $updater->getRawSlot( 'aux' );
271 $this->assertInstanceOf( SlotRecord::class, $auxSlot );
272 $this->assertStringContainsString(
273 '~~~',
274 $auxSlot->getContent()->serialize(),
275 'No PST should apply.'
276 );
277
278 $mainOutput = $updater->getSlotParserOutput( SlotRecord::MAIN );
279 $text = $mainOutput->getRawText();
280 $this->assertStringContainsString( 'first', $text );
281 $this->assertStringContainsString( '<a ', $text );
282 $this->assertNotEmpty( $mainOutput->getLinks() );
283
284 $canonicalOutput = $updater->getCanonicalParserOutput();
285 $text = $canonicalOutput->getRawText();
286 $this->assertStringContainsString( 'first', $text );
287 $this->assertStringContainsString( '<a ', $text );
288 $this->assertStringContainsString( 'inherited ', $text );
289 $this->assertNotEmpty( $canonicalOutput->getLinks() );
290 }
291
292 /**
293 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareContent()
294 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::pageExisted()
295 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
296 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isChange()
297 */
298 public function testPrepareContentInherit() {
299 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser();
300 $page = $this->getPage( __METHOD__ );
301
302 $mainContent1 = new WikitextContent( 'first [[main]] ({{REVISIONUSER}}) #~~~#' );
303 $mainContent2 = new WikitextContent( 'second ({{subst:REVISIONUSER}}) #~~~#' );
304
305 $rev = $this->createRevision( $page, 'first', $mainContent1 );
306 $mainContent1 = $rev->getContent( SlotRecord::MAIN ); // get post-pst content
307 $userName = $rev->getUser()->getName();
308 $sysopName = $sysop->getName();
309
310 $update = new RevisionSlotsUpdate();
311 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
312 $updater1 = $this->getDerivedPageDataUpdater( $page );
313 $updater1->prepareContent( $sysop, $update, false );
314
315 $this->assertNotNull( $updater1->grabCurrentRevision() );
316 $this->assertTrue( $updater1->isContentPrepared() );
317 $this->assertTrue( $updater1->pageExisted() );
318 $this->assertFalse( $updater1->isCreation() );
319 $this->assertFalse( $updater1->isChange() );
320
321 $this->assertNotNull( $updater1->getRevision() );
322 $this->assertNotNull( $updater1->getRenderedRevision() );
323
324 // parser-output for null-edit uses the original author's name
325 $html = $updater1->getRenderedRevision()->getRevisionParserOutput()->getRawText();
326 $this->assertStringNotContainsString( $sysopName, $html, '{{REVISIONUSER}}' );
327 $this->assertStringNotContainsString( '{{REVISIONUSER}}', $html, '{{REVISIONUSER}}' );
328 $this->assertStringNotContainsString( '~~~', $html, 'signature ~~~' );
329 $this->assertStringContainsString( '(' . $userName . ')', $html, '{{REVISIONUSER}}' );
330 $this->assertStringContainsString( '>' . $userName . '<', $html, 'signature ~~~' );
331
332 // prepare forced dummy revision
333 $emptyUpdate = new RevisionSlotsUpdate();
334 $updater1 = $this->getDerivedPageDataUpdater( $page );
335 $updater1->setForceEmptyRevision( true );
336 $updater1->prepareContent( $sysop, $emptyUpdate, false );
337
338 // dummy revision inherits slots, but not revision ID
339 $mainSlot0 = $rev->getSlot( SlotRecord::MAIN );
340 $dummyRev = $updater1->getRevision();
341 $dummySlot = $dummyRev->getSlot( SlotRecord::MAIN );
342 $this->assertSame( $mainSlot0->getAddress(), $dummySlot->getAddress() );
343 $this->assertNull( $dummyRev->getId() );
344 $this->assertSame( $rev->getId(), $dummyRev->getParentId() );
345
346 // prepare non-null
347 $update = new RevisionSlotsUpdate();
348 $update->modifyContent( SlotRecord::MAIN, $mainContent2 );
349 $updater2 = $this->getDerivedPageDataUpdater( $page );
350 $updater2->prepareContent( $sysop, $update, false );
351
352 // non-null edit use the new user name in PST
353 $pstText = $updater2->getSlots()->getContent( SlotRecord::MAIN )->serialize();
354 $this->assertStringNotContainsString(
355 '{{subst:REVISIONUSER}}',
356 $pstText,
357 '{{subst:REVISIONUSER}}'
358 );
359 $this->assertStringNotContainsString( '~~~', $pstText, 'signature ~~~' );
360 $this->assertStringContainsString( '(' . $sysopName . ')', $pstText, '{{subst:REVISIONUSER}}' );
361 $this->assertStringContainsString( ':' . $sysopName . '|', $pstText, 'signature ~~~' );
362
363 $this->assertFalse( $updater2->isCreation() );
364 $this->assertTrue( $updater2->isChange() );
365 }
366
367 // TODO: test failure of prepareContent() when called again...
368 // - with different user
369 // - with different update
370 // - after calling prepareUpdate()
371
372 /**
373 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
374 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isUpdatePrepared()
375 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCreation()
376 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlots()
377 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawSlot()
378 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getRawContent()
379 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getModifiedSlotRoles()
380 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getTouchedSlotRoles()
381 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
382 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getCanonicalParserOutput()
383 */
384 public function testPrepareUpdate() {
385 $page = $this->getPage( __METHOD__ );
386
387 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
388 $rev1 = $this->createRevision( $page, 'first', $mainContent1 );
389 $updater1 = $this->getDerivedPageDataUpdater( $page, $rev1 );
390
391 $options = []; // TODO: test *all* the options...
392 $updater1->prepareUpdate( $rev1, $options );
393
394 $this->assertTrue( $updater1->isUpdatePrepared() );
395 $this->assertTrue( $updater1->isContentPrepared() );
396 $this->assertTrue( $updater1->isCreation() );
397 $this->assertTrue( $updater1->isChange() );
398 $this->assertFalse( $updater1->isContentDeleted() );
399
400 $this->assertNotNull( $updater1->getRevision() );
401 $this->assertNotNull( $updater1->getRenderedRevision() );
402
403 $this->assertEquals( [ SlotRecord::MAIN ], $updater1->getSlots()->getSlotRoles() );
404 $this->assertEquals( [ SlotRecord::MAIN ], array_keys( $updater1->getSlots()->getOriginalSlots() ) );
405 $this->assertEquals( [], array_keys( $updater1->getSlots()->getInheritedSlots() ) );
406 $this->assertEquals( [ SlotRecord::MAIN ], $updater1->getModifiedSlotRoles() );
407 $this->assertEquals( [ SlotRecord::MAIN ], $updater1->getTouchedSlotRoles() );
408
409 // TODO: MCR: test multiple slots, test slot removal!
410
411 $this->assertInstanceOf( SlotRecord::class, $updater1->getRawSlot( SlotRecord::MAIN ) );
412 $this->assertStringNotContainsString(
413 '~~~~',
414 $updater1->getRawContent( SlotRecord::MAIN )->serialize()
415 );
416
417 $mainOutput = $updater1->getSlotParserOutput( SlotRecord::MAIN );
418 $mainText = $mainOutput->getRawText();
419 $this->assertStringContainsString( 'first', $mainText );
420 $this->assertStringContainsString( '<a ', $mainText );
421 $this->assertNotEmpty( $mainOutput->getLinks() );
422
423 $canonicalOutput = $updater1->getCanonicalParserOutput();
424 $canonicalText = $canonicalOutput->getRawText();
425 $this->assertStringContainsString( 'first', $canonicalText );
426 $this->assertStringContainsString( '<a ', $canonicalText );
427 $this->assertNotEmpty( $canonicalOutput->getLinks() );
428
429 $mainContent2 = new WikitextContent( 'second' );
430 $rev2 = $this->createRevision( $page, 'second', $mainContent2 );
431 $updater2 = $this->getDerivedPageDataUpdater( $page, $rev2 );
432
433 $options = []; // TODO: test *all* the options...
434 $updater2->prepareUpdate( $rev2, $options );
435
436 $this->assertFalse( $updater2->isCreation() );
437 $this->assertTrue( $updater2->isChange() );
438
439 $canonicalOutput = $updater2->getCanonicalParserOutput();
440 $this->assertStringContainsString( 'second', $canonicalOutput->getRawText() );
441 }
442
443 /**
444 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
445 */
446 public function testPrepareUpdateReusesParserOutput() {
447 $user = $this->getTestUser()->getUser();
448 $page = $this->getPage( __METHOD__ );
449
450 $mainContent1 = new WikitextContent( 'first [[main]] ~~~' );
451
452 $update = new RevisionSlotsUpdate();
453 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
454 $updater = $this->getDerivedPageDataUpdater( $page );
455 $updater->prepareContent( $user, $update, false );
456
457 $mainOutput = $updater->getSlotParserOutput( SlotRecord::MAIN );
458 $canonicalOutput = $updater->getCanonicalParserOutput();
459
460 $rev = $this->createRevision( $page, 'first', $mainContent1 );
461
462 $options = []; // TODO: test *all* the options...
463 $updater->prepareUpdate( $rev, $options );
464
465 $this->assertTrue( $updater->isUpdatePrepared() );
466 $this->assertTrue( $updater->isContentPrepared() );
467
468 $this->assertSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord::MAIN ) );
469 $this->assertSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
470 }
471
472 /**
473 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::prepareUpdate()
474 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getSlotParserOutput()
475 */
476 public function testPrepareUpdateOutputReset() {
477 $user = $this->getTestUser()->getUser();
478 $page = $this->getPage( __METHOD__ );
479
480 $mainContent1 = new WikitextContent( 'first --{{REVISIONID}}--' );
481
482 $update = new RevisionSlotsUpdate();
483 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
484 $updater = $this->getDerivedPageDataUpdater( $page );
485 $updater->prepareContent( $user, $update, false );
486
487 $mainOutput = $updater->getSlotParserOutput( SlotRecord::MAIN );
488 $canonicalOutput = $updater->getCanonicalParserOutput();
489
490 // prevent optimization on matching speculative ID
491 $mainOutput->setSpeculativeRevIdUsed( 0 );
492 $canonicalOutput->setSpeculativeRevIdUsed( 0 );
493
494 $rev = $this->createRevision( $page, 'first', $mainContent1 );
495
496 $options = []; // TODO: test *all* the options...
497 $updater->prepareUpdate( $rev, $options );
498
499 $this->assertTrue( $updater->isUpdatePrepared() );
500 $this->assertTrue( $updater->isContentPrepared() );
501
502 // ParserOutput objects should have been flushed.
503 $this->assertNotSame( $mainOutput, $updater->getSlotParserOutput( SlotRecord::MAIN ) );
504 $this->assertNotSame( $canonicalOutput, $updater->getCanonicalParserOutput() );
505
506 $html = $updater->getCanonicalParserOutput()->getRawText();
507 $this->assertStringContainsString( '--' . $rev->getId() . '--', $html );
508
509 // TODO: MCR: ensure that when the main slot uses {{REVISIONID}} but another slot is
510 // updated, the main slot is still re-rendered!
511 }
512
513 // TODO: test failure of prepareUpdate() when called again with a different revision
514 // TODO: test failure of prepareUpdate() on inconsistency with prepareContent.
515
516 /**
517 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
518 */
519 public function testGetPreparedEditAfterPrepareContent() {
520 $user = $this->getTestUser()->getUser();
521
522 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
523 $update = new RevisionSlotsUpdate();
524 $update->modifyContent( SlotRecord::MAIN, $mainContent );
525
526 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
527 $updater->prepareContent( $user, $update, false );
528
529 $canonicalOutput = $updater->getCanonicalParserOutput();
530
531 $preparedEdit = $updater->getPreparedEdit();
532 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp );
533 $this->assertSame( $canonicalOutput, $preparedEdit->output );
534 $this->assertSame( $mainContent, $preparedEdit->newContent );
535 $this->assertSame( $updater->getRawContent( SlotRecord::MAIN ), $preparedEdit->pstContent );
536 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts );
537 $this->assertSame( null, $preparedEdit->revid );
538 }
539
540 /**
541 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::getPreparedEdit()
542 */
543 public function testGetPreparedEditAfterPrepareUpdate() {
544 $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' );
545 MWTimestamp::setFakeTime( static function () use ( &$clock ) {
546 return $clock++;
547 } );
548
549 $page = $this->getPage( __METHOD__ );
550
551 $mainContent = new WikitextContent( 'first [[main]] ~~~' );
552 $update = new MutableRevisionSlots();
553 $update->setContent( SlotRecord::MAIN, $mainContent );
554
555 $rev = $this->createRevision( $page, __METHOD__ );
556
557 $updater = $this->getDerivedPageDataUpdater( $page );
558 $updater->prepareUpdate( $rev );
559
560 $canonicalOutput = $updater->getCanonicalParserOutput();
561
562 $preparedEdit = $updater->getPreparedEdit();
563 $this->assertSame( $canonicalOutput->getCacheTime(), $preparedEdit->timestamp );
564 $this->assertSame( $canonicalOutput, $preparedEdit->output );
565 $this->assertSame( $updater->getRawContent( SlotRecord::MAIN ), $preparedEdit->pstContent );
566 $this->assertSame( $updater->getCanonicalParserOptions(), $preparedEdit->popts );
567 $this->assertSame( $rev->getId(), $preparedEdit->revid );
568 }
569
570 public function testGetSecondaryDataUpdatesAfterPrepareContent() {
571 $user = $this->getTestUser()->getUser();
572 $page = $this->getPage( __METHOD__ );
573 $this->createRevision( $page, __METHOD__ );
574
575 $mainContent1 = new WikitextContent( 'first' );
576
577 $update = new RevisionSlotsUpdate();
578 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
579 $updater = $this->getDerivedPageDataUpdater( $page );
580 $updater->prepareContent( $user, $update, false );
581
582 $dataUpdates = $updater->getSecondaryDataUpdates();
583
584 $this->assertNotEmpty( $dataUpdates );
585
586 $linksUpdates = array_filter( $dataUpdates, static function ( $du ) {
587 return $du instanceof LinksUpdate;
588 } );
589 $this->assertCount( 1, $linksUpdates );
590 }
591
592 public function testAvoidSecondaryDataUpdatesOnNonHTMLContentHandlers() {
593 $this->overrideConfigValue(
594 MainConfigNames::ContentHandlers,
595 [
596 CONTENT_MODEL_WIKITEXT => [
597 'class' => WikitextContentHandler::class,
598 'services' => [
599 'TitleFactory',
600 'ParserFactory',
601 'GlobalIdGenerator',
602 'LanguageNameUtils',
603 'LinkRenderer',
604 'MagicWordFactory',
605 'ParsoidParserFactory',
606 ],
607 ],
608 'testing' => DummyContentHandlerForTesting::class,
609 ]
610 );
611
612 $user = $this->getTestUser()->getUser();
613 $page = $this->getPage( __METHOD__ );
614 $this->createRevision( $page, __METHOD__ );
615
616 $contentHandler = new DummyContentHandlerForTesting( 'testing' );
617 $mainContent1 = $contentHandler->unserializeContent( serialize( 'first' ) );
618 $update = new RevisionSlotsUpdate();
619 $pcache = $this->getServiceContainer()->getParserCache();
620 $pcache->deleteOptionsKey( $page );
621 $rev = $this->createRevision( $page, 'first', $mainContent1 );
622
623 // Run updates
624 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
625 $updater = $this->getDerivedPageDataUpdater( $page );
626 $updater->prepareContent( $user, $update, false );
627 $dataUpdates = $updater->getSecondaryDataUpdates();
628 $updater->prepareUpdate( $rev );
629 $updater->doUpdates();
630
631 // Links updates should be triggered
632 $this->assertNotEmpty( $dataUpdates );
633 $linksUpdates = array_filter( $dataUpdates, static function ( $du ) {
634 return $du instanceof LinksUpdate;
635 } );
636 $this->assertCount( 1, $linksUpdates );
637
638 // Parser cache should not be populated.
639 $cached = $pcache->get( $page, $updater->getCanonicalParserOptions() );
640 $this->assertFalse( $cached );
641 }
642
643 public function testGetSecondaryDataUpdatesDeleted() {
644 $user = $this->getTestUser()->getUser();
645 $page = $this->getPage( __METHOD__ );
646 $this->createRevision( $page, __METHOD__ );
647
648 $mainContent1 = new WikitextContent( 'first' );
649
650 $update = new RevisionSlotsUpdate();
651 $update->modifyContent( SlotRecord::MAIN, $mainContent1 );
652 $updater = $this->getDerivedPageDataUpdater( $page );
653 $updater->prepareContent( $user, $update, false );
654
655 // Test that nothing happens if the page was deleted in the meantime
656 // This can happen when started by the job queue
657 $this->deletePage( $page );
658
659 $dataUpdates = $updater->getSecondaryDataUpdates();
660
661 $this->assertSame( [], $dataUpdates );
662 }
663
664 /**
665 * @param string $name
666 *
667 * @return ContentHandler
668 */
669 private function defineMockContentModelForUpdateTesting( $name ) {
670 /** @var ContentHandler|MockObject $handler */
671 $handler = $this->getMockBuilder( TextContentHandler::class )
672 ->setConstructorArgs( [ $name ] )
673 ->onlyMethods(
674 [ 'getSecondaryDataUpdates', 'getDeletionUpdates', 'unserializeContent' ]
675 )
676 ->getMock();
677
678 $dataUpdate = new MWCallableUpdate( 'time', "$name data update" );
679
680 $deletionUpdate = new MWCallableUpdate( 'time', "$name deletion update" );
681
682 $handler->method( 'getSecondaryDataUpdates' )->willReturn( [ $dataUpdate ] );
683 $handler->method( 'getDeletionUpdates' )->willReturn( [ $deletionUpdate ] );
684 $handler->method( 'unserializeContent' )->willReturnCallback(
685 function ( $text ) use ( $handler ) {
686 return $this->createMockContent( $handler, $text );
687 }
688 );
689
690 $this->mergeMwGlobalArrayValue(
691 'wgContentHandlers', [
692 $name => static function () use ( $handler ){
693 return $handler;
694 }
695 ]
696 );
697
698 return $handler;
699 }
700
701 /**
702 * @param ContentHandler $handler
703 * @param string $text
704 *
705 * @return Content
706 */
707 private function createMockContent( ContentHandler $handler, $text ) {
708 /** @var Content|MockObject $content */
709 $content = $this->getMockBuilder( TextContent::class )
710 ->setConstructorArgs( [ $text ] )
711 ->onlyMethods( [ 'getModel', 'getContentHandler' ] )
712 ->getMock();
713
714 $content->method( 'getModel' )->willReturn( $handler->getModelID() );
715 $content->method( 'getContentHandler' )->willReturn( $handler );
716
717 return $content;
718 }
719
720 public function testGetSecondaryDataUpdatesWithSlotRemoval() {
721 $m1 = $this->defineMockContentModelForUpdateTesting( 'M1' );
722 $a1 = $this->defineMockContentModelForUpdateTesting( 'A1' );
723 $m2 = $this->defineMockContentModelForUpdateTesting( 'M2' );
724
725 $role = 'dpdu-test-a1';
726 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
727 $slotRoleRegistry->defineRoleWithModel(
728 $role,
729 $a1->getModelID()
730 );
731
732 // pin the service instance for this test
733 $this->setService( 'SlotRoleRegistry', $slotRoleRegistry );
734
735 $mainContent1 = $this->createMockContent( $m1, 'main 1' );
736 $auxContent1 = $this->createMockContent( $a1, 'aux 1' );
737 $mainContent2 = $this->createMockContent( $m2, 'main 2' );
738
739 $user = $this->getTestUser()->getUser();
740 $page = $this->getPage( __METHOD__ );
741 $this->createRevision(
742 $page,
743 __METHOD__,
744 [ SlotRecord::MAIN => $mainContent1, $role => $auxContent1 ]
745 );
746
747 $update = new RevisionSlotsUpdate();
748 $update->modifyContent( SlotRecord::MAIN, $mainContent2 );
749 $update->removeSlot( $role );
750
751 $page = $this->getPage( __METHOD__ );
752 $updater = $this->getDerivedPageDataUpdater( $page );
753 $updater->prepareContent( $user, $update, false );
754
755 $dataUpdates = $updater->getSecondaryDataUpdates();
756
757 $this->assertNotEmpty( $dataUpdates );
758
759 $updateNames = array_map( static function ( $du ) {
760 return $du instanceof MWCallableUpdate ? $du->getOrigin() : get_class( $du );
761 }, $dataUpdates );
762
763 $this->assertContains( LinksUpdate::class, $updateNames );
764 $this->assertContains( 'A1 deletion update', $updateNames );
765 $this->assertContains( 'M2 data update', $updateNames );
766 $this->assertNotContains( 'M1 data update', $updateNames );
767 }
768
769 /**
770 * Creates a dummy MutableRevisionRecord without touching the database.
771 *
772 * @param PageIdentity $title
773 * @param string|Content|Content[]|RevisionSlotsUpdate $update
774 * @param UserIdentity|null $user
775 * @param string $comment
776 * @param int $id
777 * @param int $parentId
778 *
779 * @return MutableRevisionRecord
780 */
781 private function makeRevision(
782 PageIdentity $title,
783 $update,
784 ?UserIdentity $user = null,
785 $comment = "testing",
786 $id = 0,
787 $parentId = 0
788 ) {
789 $rev = new MutableRevisionRecord( $title );
790
791 if ( $update instanceof RevisionSlotsUpdate ) {
792 $rev->applyUpdate( $update );
793 } else {
794 if ( is_string( $update ) ) {
795 $update = new WikitextContent( $update );
796 }
797
798 if ( !is_array( $update ) ) {
799 $update = [ SlotRecord::MAIN => $update ];
800 }
801
802 foreach ( $update as $role => $content ) {
803 $rev->setContent( $role, $content );
804 }
805 }
806
807 if ( !$user ) {
808 $user = $this->getTestUser()->getUser();
809 }
810
811 $rev->setUser( $user );
812 $rev->setComment( CommentStoreComment::newUnsavedComment( $comment ) );
813 $rev->setPageId( $title->getId() );
814 $rev->setParentId( $parentId );
815
816 if ( $id ) {
817 $rev->setId( $id );
818 }
819
820 return $rev;
821 }
822
823 public function provideIsReusableFor() {
824 $title = PageIdentityValue::localIdentity( 1234, NS_MAIN, __CLASS__ );
825
826 $user1 = new UserIdentityValue( 111, 'Alice' );
827 $user2 = new UserIdentityValue( 222, 'Bob' );
828
829 $content1 = new WikitextContent( 'one' );
830 $content2 = new WikitextContent( 'two' );
831
832 $update1 = new RevisionSlotsUpdate();
833 $update1->modifyContent( SlotRecord::MAIN, $content1 );
834
835 $update1b = new RevisionSlotsUpdate();
836 $update1b->modifyContent( 'xyz', $content1 );
837
838 $update2 = new RevisionSlotsUpdate();
839 $update2->modifyContent( SlotRecord::MAIN, $content2 );
840
841 $rev1 = $this->makeRevision( $title, $update1, $user1, 'rev1', 11 );
842 $rev1b = $this->makeRevision( $title, $update1b, $user1, 'rev1', 11 );
843
844 $rev2 = $this->makeRevision( $title, $update2, $user1, 'rev2', 12 );
845 $rev2x = $this->makeRevision( $title, $update2, $user2, 'rev2', 12 );
846 $rev2y = $this->makeRevision( $title, $update2, $user1, 'rev2', 122 );
847
848 yield 'any' => [
849 '$prepUser' => null,
850 '$prepRevision' => null,
851 '$prepUpdate' => null,
852 '$forUser' => null,
853 '$forRevision' => null,
854 '$forUpdate' => null,
855 '$forParent' => null,
856 '$isReusable' => true,
857 ];
858 yield 'for any' => [
859 '$prepUser' => $user1,
860 '$prepRevision' => $rev1,
861 '$prepUpdate' => $update1,
862 '$forUser' => null,
863 '$forRevision' => null,
864 '$forUpdate' => null,
865 '$forParent' => null,
866 '$isReusable' => true,
867 ];
868 yield 'unprepared' => [
869 '$prepUser' => null,
870 '$prepRevision' => null,
871 '$prepUpdate' => null,
872 '$forUser' => $user1,
873 '$forRevision' => $rev1,
874 '$forUpdate' => $update1,
875 '$forParent' => 0,
876 '$isReusable' => true,
877 ];
878 yield 'match prepareContent' => [
879 '$prepUser' => $user1,
880 '$prepRevision' => null,
881 '$prepUpdate' => $update1,
882 '$forUser' => $user1,
883 '$forRevision' => null,
884 '$forUpdate' => $update1,
885 '$forParent' => 0,
886 '$isReusable' => true,
887 ];
888 yield 'match prepareUpdate' => [
889 '$prepUser' => null,
890 '$prepRevision' => $rev1,
891 '$prepUpdate' => null,
892 '$forUser' => $user1,
893 '$forRevision' => $rev1,
894 '$forUpdate' => null,
895 '$forParent' => 0,
896 '$isReusable' => true,
897 ];
898 yield 'match all' => [
899 '$prepUser' => $user1,
900 '$prepRevision' => $rev1,
901 '$prepUpdate' => $update1,
902 '$forUser' => $user1,
903 '$forRevision' => $rev1,
904 '$forUpdate' => $update1,
905 '$forParent' => 0,
906 '$isReusable' => true,
907 ];
908 yield 'mismatch prepareContent update' => [
909 '$prepUser' => $user1,
910 '$prepRevision' => null,
911 '$prepUpdate' => $update1,
912 '$forUser' => $user1,
913 '$forRevision' => null,
914 '$forUpdate' => $update1b,
915 '$forParent' => 0,
916 '$isReusable' => false,
917 ];
918 yield 'mismatch prepareContent user' => [
919 '$prepUser' => $user1,
920 '$prepRevision' => null,
921 '$prepUpdate' => $update1,
922 '$forUser' => $user2,
923 '$forRevision' => null,
924 '$forUpdate' => $update1,
925 '$forParent' => 0,
926 '$isReusable' => false,
927 ];
928 yield 'mismatch prepareContent parent' => [
929 '$prepUser' => $user1,
930 '$prepRevision' => null,
931 '$prepUpdate' => $update1,
932 '$forUser' => $user1,
933 '$forRevision' => null,
934 '$forUpdate' => $update1,
935 '$forParent' => 7,
936 '$isReusable' => false,
937 ];
938 yield 'mismatch prepareUpdate revision update' => [
939 '$prepUser' => null,
940 '$prepRevision' => $rev1,
941 '$prepUpdate' => null,
942 '$forUser' => null,
943 '$forRevision' => $rev1b,
944 '$forUpdate' => null,
945 '$forParent' => 0,
946 '$isReusable' => false,
947 ];
948 yield 'mismatch prepareUpdate revision id' => [
949 '$prepUser' => null,
950 '$prepRevision' => $rev2,
951 '$prepUpdate' => null,
952 '$forUser' => null,
953 '$forRevision' => $rev2y,
954 '$forUpdate' => null,
955 '$forParent' => 0,
956 '$isReusable' => false,
957 ];
958 }
959
960 /**
961 * @dataProvider provideIsReusableFor
962 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isReusableFor()
963 */
964 public function testIsReusableFor(
965 ?UserIdentity $prepUser,
966 ?RevisionRecord $prepRevision,
967 ?RevisionSlotsUpdate $prepUpdate,
968 ?UserIdentity $forUser,
969 ?RevisionRecord $forRevision,
970 ?RevisionSlotsUpdate $forUpdate,
971 $forParent,
972 $isReusable
973 ) {
974 $updater = $this->getDerivedPageDataUpdater( __METHOD__ );
975
976 if ( $prepUpdate ) {
977 $updater->prepareContent( $prepUser, $prepUpdate, false );
978 }
979
980 if ( $prepRevision ) {
981 $updater->prepareUpdate( $prepRevision );
982 }
983
984 $this->assertSame(
985 $isReusable,
986 $updater->isReusableFor( $forUser, $forRevision, $forUpdate, $forParent )
987 );
988 }
989
990 /**
991 * * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCountable
992 */
993 public function testIsCountableNotContentPage() {
994 $updater = $this->getDerivedPageDataUpdater(
995 Title::makeTitle( NS_TALK, 'Main_Page' )
996 );
997 self::assertFalse( $updater->isCountable() );
998 }
999
1000 public static function provideIsCountable() {
1001 yield 'deleted revision' => [
1002 '$articleCountMethod' => 'any',
1003 '$wikitextContent' => 'Test',
1004 '$revisionVisibility' => RevisionRecord::SUPPRESSED_ALL,
1005 '$isCountable' => false
1006 ];
1007 yield 'redirect' => [
1008 '$articleCountMethod' => 'any',
1009 '$wikitextContent' => '#REDIRECT [[Main_Page]]',
1010 '$revisionVisibility' => 0,
1011 '$isCountable' => false
1012 ];
1013 yield 'no links count method any' => [
1014 '$articleCountMethod' => 'any',
1015 '$wikitextContent' => 'Test',
1016 '$revisionVisibility' => 0,
1017 '$isCountable' => true
1018 ];
1019 yield 'no links count method link' => [
1020 '$articleCountMethod' => 'link',
1021 '$wikitextContent' => 'Test',
1022 '$revisionVisibility' => 0,
1023 '$isCountable' => false
1024 ];
1025 yield 'with links count method link' => [
1026 '$articleCountMethod' => 'link',
1027 '$wikitextContent' => '[[Test]]',
1028 '$revisionVisibility' => 0,
1029 '$isCountable' => true
1030 ];
1031 }
1032
1033 /**
1034 * @dataProvider provideIsCountable
1035 *
1036 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCountable
1037 */
1038 public function testIsCountable(
1039 $articleCountMethod,
1040 $wikitextContent,
1041 $revisionVisibility,
1042 $isCountable
1043 ) {
1044 $this->overrideConfigValue( MainConfigNames::ArticleCountMethod, $articleCountMethod );
1045 $title = $this->getTitle( 'Main_Page' );
1046 $content = new WikitextContent( $wikitextContent );
1047 $update = new RevisionSlotsUpdate();
1048 $update->modifyContent( SlotRecord::MAIN, $content );
1049 $revision = $this->makeRevision( $title, $update, User::newFromName( 'Alice' ), 'rev1', 13 );
1050 $revision->setVisibility( $revisionVisibility );
1051 $updater = $this->getDerivedPageDataUpdater( $title );
1052 $updater->prepareUpdate( $revision );
1053 self::assertSame( $isCountable, $updater->isCountable() );
1054 }
1055
1056 /**
1057 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::isCountable
1058 */
1059 public function testIsCountableNoModifiedSlots() {
1060 $page = $this->getPage( __METHOD__ );
1061 $content = [ SlotRecord::MAIN => new WikitextContent( '[[Test]]' ) ];
1062 $rev = $this->createRevision( $page, 'first', $content );
1063 $nullRevision = MutableRevisionRecord::newFromParentRevision( $rev );
1064 $nullRevision->setId( 14 );
1065 $updater = $this->getDerivedPageDataUpdater( $page, $nullRevision );
1066 $updater->prepareUpdate( $nullRevision );
1067 $this->assertTrue( $updater->isCountable() );
1068 }
1069
1070 /**
1071 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
1072 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
1073 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
1074 */
1075 public function testDoUpdates() {
1076 $page = $this->getPage( __METHOD__ );
1077
1078 $content = [ SlotRecord::MAIN => new WikitextContent( 'first [[main]]' ) ];
1079
1080 $content['aux'] = new WikitextContent( 'Aux [[Nix]]' );
1081
1082 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
1083 if ( !$slotRoleRegistry->isDefinedRole( 'aux' ) ) {
1084 $slotRoleRegistry->defineRoleWithModel(
1085 'aux',
1086 CONTENT_MODEL_WIKITEXT
1087 );
1088 }
1089
1090 $rev = $this->createRevision( $page, 'first', $content );
1091 $pageId = $page->getId();
1092
1093 $oldStats = $this->getDb()->newSelectQueryBuilder()
1094 ->select( '*' )
1095 ->from( 'site_stats' )
1096 ->where( '1=1' )
1097 ->fetchRow();
1098 $this->getDb()->newDeleteQueryBuilder()
1099 ->deleteFrom( 'pagelinks' )
1100 ->where( ISQLPlatform::ALL_ROWS )
1101 ->caller( __METHOD__ )
1102 ->execute();
1103
1104 $pcache = $this->getServiceContainer()->getParserCache();
1105 $pcache->deleteOptionsKey( $page );
1106
1107 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
1108 $updater->setArticleCountMethod( 'link' );
1109
1110 $options = []; // TODO: test *all* the options...
1111 $updater->prepareUpdate( $rev, $options );
1112
1113 $updater->doUpdates();
1114
1115 // links table update
1116 $pageLinks = $this->getDb()->newSelectQueryBuilder()
1117 ->select( 'lt_title' )
1118 ->from( 'pagelinks' )
1119 ->join( 'linktarget', null, 'pl_target_id=lt_id' )
1120 ->where( [ 'pl_from' => $pageId ] )
1121 ->orderBy( [ 'lt_namespace', 'lt_title' ] )
1122 ->caller( __METHOD__ )->fetchResultSet();
1123
1124 $pageLinksRow = $pageLinks->fetchObject();
1125 $this->assertIsObject( $pageLinksRow );
1126 $this->assertSame( 'Main', $pageLinksRow->lt_title );
1127
1128 $pageLinksRow = $pageLinks->fetchObject();
1129 $this->assertIsObject( $pageLinksRow );
1130 $this->assertSame( 'Nix', $pageLinksRow->lt_title );
1131
1132 // parser cache update
1133 $cached = $pcache->get( $page, $updater->getCanonicalParserOptions() );
1134 $this->assertIsObject( $cached );
1135 $expected = $updater->getCanonicalParserOutput();
1136 $expected->clearParseStartTime();
1137 $this->assertEquals( $expected, $cached );
1138
1139 // site stats
1140 $stats = $this->getDb()->newSelectQueryBuilder()
1141 ->select( '*' )
1142 ->from( 'site_stats' )
1143 ->where( '1=1' )
1144 ->fetchRow();
1145 $this->assertSame( $oldStats->ss_total_pages + 1, (int)$stats->ss_total_pages );
1146 $this->assertSame( $oldStats->ss_total_edits + 1, (int)$stats->ss_total_edits );
1147 $this->assertSame( $oldStats->ss_good_articles + 1, (int)$stats->ss_good_articles );
1148
1149 // TODO: MCR: test data updates for additional slots!
1150 // TODO: test update for edit without page creation
1151 // TODO: test message cache purge
1152 // TODO: test module cache purge
1153 // TODO: test CDN purge
1154 // TODO: test newtalk update
1155 // TODO: test search update
1156 // TODO: test site stats good_articles while turning the page into (or back from) a redir.
1157 // TODO: test category membership update (with setRcWatchCategoryMembership())
1158 }
1159
1160 /**
1161 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
1162 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
1163 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
1164 */
1165 public function testDoUpdatesCacheSaveDeferral_canonical() {
1166 $page = $this->getPage( __METHOD__ );
1167
1168 // Case where user has canonical parser options
1169 $content = [ SlotRecord::MAIN => new WikitextContent( 'rev ID ver #1: {{REVISIONID}}' ) ];
1170 $rev = $this->createRevision( $page, 'first', $content );
1171 $pcache = $this->getServiceContainer()->getParserCache();
1172 $pcache->deleteOptionsKey( $page );
1173
1174 $this->getDb()->startAtomic( __METHOD__ ); // let deferred updates queue up
1175
1176 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
1177 $updater->prepareUpdate( $rev, [] );
1178 $updater->doUpdates();
1179
1180 $this->assertGreaterThan( 0, DeferredUpdates::pendingUpdatesCount(), 'Pending updates' );
1181 $this->assertNotFalse( $pcache->get( $page, $updater->getCanonicalParserOptions() ) );
1182
1183 $this->getDb()->endAtomic( __METHOD__ ); // run deferred updates
1184
1185 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount(), 'No pending updates' );
1186 }
1187
1188 /**
1189 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates()
1190 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doSecondaryDataUpdates()
1191 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
1192 */
1193 public function testDoUpdatesCacheSaveDeferral_noncanonical() {
1194 $page = $this->getPage( __METHOD__ );
1195
1196 // Case where user does not have canonical parser options
1197 $user = $this->getMutableTestUser()->getUser();
1198 $services = $this->getServiceContainer();
1199 $userOptionsManager = $services->getUserOptionsManager();
1200 $userOptionsManager->setOption(
1201 $user,
1202 'thumbsize',
1203 $userOptionsManager->getOption( $user, 'thumbsize' ) + 1
1204 );
1205 $content = [ SlotRecord::MAIN => new WikitextContent( 'rev ID ver #2: {{REVISIONID}}' ) ];
1206 $rev = $this->createRevision( $page, 'first', $content, $user );
1207 $pcache = $services->getParserCache();
1208 $pcache->deleteOptionsKey( $page );
1209
1210 $this->getDb()->startAtomic( __METHOD__ ); // let deferred updates queue up
1211
1212 $updater = $this->getDerivedPageDataUpdater( $page, $rev, $user );
1213 $updater->prepareUpdate( $rev, [] );
1214 $updater->doUpdates();
1215
1216 $this->assertGreaterThan( 1, DeferredUpdates::pendingUpdatesCount(), 'Pending updates' );
1217 $this->assertFalse( $pcache->get( $page, $updater->getCanonicalParserOptions() ) );
1218
1219 $this->getDb()->endAtomic( __METHOD__ ); // run deferred updates
1220
1221 $this->assertSame( 0, DeferredUpdates::pendingUpdatesCount(), 'No pending updates' );
1222 $this->assertNotFalse( $pcache->get( $page, $updater->getCanonicalParserOptions() ) );
1223 }
1224
1225 public static function provideEnqueueRevertedTagUpdateJob() {
1226 return [
1227 'approved' => [ true, 1 ],
1228 'not approved' => [ false, 0 ]
1229 ];
1230 }
1231
1232 /**
1233 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doUpdates
1234 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::maybeEnqueueRevertedTagUpdateJob
1235 * @dataProvider provideEnqueueRevertedTagUpdateJob
1236 */
1237 public function testEnqueueRevertedTagUpdateJob( bool $approved, int $queueSize ) {
1238 $page = $this->getPage( __METHOD__ );
1239
1240 $content = [ SlotRecord::MAIN => new WikitextContent( '1' ) ];
1241 $rev = $this->createRevision( $page, '', $content );
1242 $editResult = new EditResult(
1243 false,
1244 10,
1245 EditResult::REVERT_ROLLBACK,
1246 11,
1247 12,
1248 true,
1249 false,
1250 [ 'mw-rollback' ]
1251 );
1252
1253 $updater = $this->getDerivedPageDataUpdater( $page, $rev );
1254
1255 $updater->prepareUpdate( $rev, [
1256 'editResult' => $editResult,
1257 'approved' => $approved
1258 ] );
1259 $updater->doUpdates();
1260
1261 $services = $this->getServiceContainer();
1262 $editResultCache = new EditResultCache(
1263 $services->getMainObjectStash(),
1264 $services->getConnectionProvider(),
1265 new ServiceOptions(
1266 EditResultCache::CONSTRUCTOR_OPTIONS,
1267 [ MainConfigNames::RCMaxAge => BagOStuff::TTL_MONTH ]
1268 )
1269 );
1270
1271 if ( $approved ) {
1272 $this->assertNull(
1273 $editResultCache->get( $rev->getId() ),
1274 'EditResult should not be cached when the revert is approved'
1275 );
1276 } else {
1277 $this->assertEquals(
1278 $editResult,
1279 $editResultCache->get( $rev->getId() ),
1280 'EditResult should be cached when the revert is not approved'
1281 );
1282 }
1283
1284 $jobQueueGroup = $this->getServiceContainer()->getJobQueueGroup();
1285 $jobQueue = $jobQueueGroup->get( 'revertedTagUpdate' );
1286 $this->assertSame(
1287 $queueSize,
1288 $jobQueue->getSize()
1289 );
1290 }
1291
1292 /**
1293 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
1294 * @covers \ParsoidCachePrewarmJob::doParsoidCacheUpdate()
1295 */
1296 public function testDoParserCacheUpdate() {
1297 $this->overrideConfigValue(
1298 MainConfigNames::ParsoidCacheConfig,
1299 [
1300 'CacheThresholdTime' => 0.0,
1301 'WarmParsoidParserCache' => true, // enable caching
1302 ]
1303 );
1304
1305 $slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry();
1306 if ( !$slotRoleRegistry->isDefinedRole( 'aux' ) ) {
1307 $slotRoleRegistry->defineRoleWithModel(
1308 'aux',
1309 CONTENT_MODEL_WIKITEXT
1310 );
1311 }
1312
1313 // Create page
1314 $page = $this->getPage( __METHOD__ );
1315 ConvertibleTimestamp::setFakeTime( '2022-01-01T00:01:00Z' );
1316 $this->createRevision( $page, 'Dummy' );
1317
1318 // Assert cache update after edit ----------
1319 $parserCacheFactory = $this->getServiceContainer()->getParserCacheFactory();
1320 $parserCache = $parserCacheFactory->getParserCache( ParserCacheFactory::DEFAULT_NAME );
1321 $parsoidCache = $parserCacheFactory->getParserCache( "parsoid-" . ParserCacheFactory::DEFAULT_NAME );
1322
1323 $parserCache->deleteOptionsKey( $page );
1324 $parsoidCache->deleteOptionsKey( $page );
1325
1326 $user = $this->getTestUser()->getUser();
1327
1328 ConvertibleTimestamp::setFakeTime( '2022-01-01T00:02:00Z' );
1329 $updater = $page->newPageUpdater( $user );
1330 $updater->setContent( SlotRecord::MAIN, new WikitextContent( 'first [[Main]]' ) );
1331 $updater->setContent( 'aux', new WikitextContent( 'Aux [[Nix]]' ) );
1332 $rev = $updater->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) );
1333
1334 // run all the jobs
1335 ConvertibleTimestamp::setFakeTime( '2022-01-01T00:03:00Z' );
1336 $this->runJobs();
1337
1338 // Parsoid cache should have an entry
1339 $parserOptions = ParserOptions::newFromAnon();
1340 $parserOptions->setUseParsoid();
1341 $parsoidCached = $parsoidCache->get( $page, $parserOptions, true );
1342 $this->assertIsObject( $parsoidCached );
1343 $this->assertStringContainsString( 'first', $parsoidCached->getRawText() );
1344
1345 // The parsoid parser output is generated during runJobs(), after the last call to setFakeTime().
1346 $this->assertGreaterThan( $rev->getTimestamp(), $parsoidCached->getCacheTime() );
1347 $this->assertSame( $rev->getId(), $parsoidCached->getCacheRevisionId() );
1348
1349 // Check that ParsoidRenderID::newFromParserOutput() doesn't throw,
1350 // so we know that $parsoidCached is valid.
1351 ParsoidRenderID::newFromParserOutput( $parsoidCached );
1352
1353 // The cached ParserOutput should not use the revision timestamp
1354 // Create nwe ParserOptions object since we setUseParsoid() above
1355 $parserOptions = ParserOptions::newFromAnon();
1356 $cached = $parserCache->get( $page, $parserOptions, true );
1357 $this->assertIsObject( $cached );
1358 $this->assertNotSame( $parsoidCached, $cached );
1359 $this->assertStringContainsString( 'first', $cached->getRawText() );
1360
1361 // The regular parser output is generated immediately during saveRevision(),
1362 // so it uses the same timestamp as the revision.
1363 $this->assertSame( $rev->getTimestamp(), $cached->getCacheTime() );
1364 $this->assertSame( $rev->getId(), $cached->getCacheRevisionId() );
1365
1366 // Emulate forced update of an old revision ----------
1367 ConvertibleTimestamp::setFakeTime( '2022-01-01T00:04:00Z' );
1368 $parserCache->deleteOptionsKey( $page );
1369
1370 $updater = $this->getDerivedPageDataUpdater( $page );
1371 $updater->prepareUpdate( $rev );
1372
1373 // Force the page timestamp, so we notice whether ParserOutput::getTimestamp
1374 // or ParserOutput::getCacheTime are used.
1375 $page->setTimestamp( $rev->getTimestamp() );
1376 $updater->doParserCacheUpdate();
1377
1378 // The cached ParserOutput should not use the revision timestamp
1379 $cached = $parserCache->get( $page, $updater->getCanonicalParserOptions(), true );
1380 $this->assertIsObject( $cached );
1381 $expected = $updater->getCanonicalParserOutput();
1382 $expected->clearParseStartTime();
1383 $this->assertEquals( $expected, $cached );
1384
1385 $this->assertGreaterThan( $rev->getTimestamp(), $cached->getCacheTime() );
1386 $this->assertSame( $rev->getId(), $cached->getCacheRevisionId() );
1387 }
1388
1389 /**
1390 * @covers \MediaWiki\Storage\DerivedPageDataUpdater::doParserCacheUpdate()
1391 * @covers \ParsoidCachePrewarmJob::doParsoidCacheUpdate()
1392 */
1393 public function testDoParserCacheUpdateForJavaScriptContent() {
1394 $this->overrideConfigValue(
1395 MainConfigNames::ParsoidCacheConfig,
1396 [
1397 'CacheThresholdTime' => 0.0,
1398 'WarmParsoidParserCache' => true, // enable caching
1399 ]
1400 );
1401
1402 $page = $this->getPage( __METHOD__ );
1403 $this->createRevision( $page, 'Dummy' );
1404
1405 $user = $this->getTestUser()->getUser();
1406
1407 $update = new RevisionSlotsUpdate();
1408 $update->modifyContent( SlotRecord::MAIN, new JavaScriptContent( '{ first: "main"; }' ) );
1409
1410 // Emulate update after edit ----------
1411 $parserCacheFactory = $this->getServiceContainer()->getParserCacheFactory();
1412 $parserCache = $parserCacheFactory->getParserCache( ParserCacheFactory::DEFAULT_NAME );
1413 $parsoidCache = $parserCacheFactory->getParserCache( ParserOutputAccess::PARSOID_PCACHE_NAME );
1414
1415 $parserCache->deleteOptionsKey( $page );
1416 $parsoidCache->deleteOptionsKey( $page );
1417
1418 $rev = $this->makeRevision( $page->getTitle(), $update, $user, 'rev', null );
1419 $rev->setTimestamp( '20100101000000' );
1420 $rev->setParentId( $page->getLatest() );
1421
1422 $updater = $this->getDerivedPageDataUpdater( $page );
1423 $updater->prepareContent( $user, $update, false );
1424
1425 $rev->setId( 1107 );
1426 $updater->prepareUpdate( $rev );
1427
1428 // Force the page timestamp, so we notice whether ParserOutput::getTimestamp
1429 // or ParserOutput::getCacheTime are used.
1430 // Also ensure $page->getLatest() returns the correct revision ID, so the parser
1431 // cache doesn't get confused.
1432 TestingAccessWrapper::newFromObject( $page )->setLastEdit( $rev );
1433 $updater->doParserCacheUpdate();
1434
1435 // The cached ParserOutput should not use the revision timestamp
1436 $cached = $parserCache->get( $page, $updater->getCanonicalParserOptions(), true );
1437 $this->assertIsObject( $cached );
1438 }
1439
1440 /**
1441 * Helper for testTemplateUpdate
1442 *
1443 * @param WikiPage $page
1444 * @param string $content
1445 */
1446 private function editAndUpdate( $page, $content ) {
1447 $this->createRevision( $page, $content );
1448 $this->getServiceContainer()->resetServiceForTesting( 'BacklinkCacheFactory' );
1449 $this->runJobs();
1450 }
1451
1452 /**
1453 * Regression test for T368006
1454 */
1455 public function testTemplateUpdate() {
1456 $clock = MWTimestamp::convert( TS_UNIX, '20100101000000' );
1457 MWTimestamp::setFakeTime( static function () use ( &$clock ) {
1458 return $clock++;
1459 } );
1460
1461 $template = $this->getPage( 'Template:TestTemplateUpdate' );
1462 $page = $this->getPage( 'TestTemplateUpdate' );
1463 $this->editAndUpdate( $template, '1' );
1464 $this->editAndUpdate( $page, '{{TestTemplateUpdate}}' );
1465 $oldTouched = $page->getTouched();
1466 $page->clear();
1467
1468 $this->editAndUpdate( $template, '2' );
1469 $newTouched = $page->getTouched();
1470 $this->assertGreaterThan( $oldTouched, $newTouched );
1471 }
1472
1473 }