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