Debian Codegraph @

tests/phpunit/includes/Revision/RevisionRendererTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Revision;
4
5 use LogicException;
6 use MediaWiki\CommentStore\CommentStoreComment;
7 use MediaWiki\Content\Content;
8 use MediaWiki\Content\IContentHandlerFactory;
9 use MediaWiki\Content\Renderer\ContentRenderer;
10 use MediaWiki\Content\WikitextContent;
11 use MediaWiki\HookContainer\HookContainer;
12 use MediaWiki\MediaWikiServices;
13 use MediaWiki\Page\PageIdentity;
14 use MediaWiki\Page\PageIdentityValue;
15 use MediaWiki\Page\PageReference;
16 use MediaWiki\Parser\ParserOptions;
17 use MediaWiki\Parser\ParserOutput;
18 use MediaWiki\Revision\MainSlotRoleHandler;
19 use MediaWiki\Revision\MutableRevisionRecord;
20 use MediaWiki\Revision\RevisionRecord;
21 use MediaWiki\Revision\RevisionRenderer;
22 use MediaWiki\Revision\SlotRecord;
23 use MediaWiki\Revision\SlotRoleRegistry;
24 use MediaWiki\Storage\NameTableStore;
25 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
26 use MediaWiki\Title\TitleFactory;
27 use MediaWiki\User\UserIdentityValue;
28 use MediaWikiIntegrationTestCase;
29 use PHPUnit\Framework\MockObject\MockObject;
30 use Wikimedia\Rdbms\IDatabase;
31 use Wikimedia\Rdbms\ILoadBalancer;
32 use Wikimedia\Rdbms\SelectQueryBuilder;
33
34 /**
35 * @covers \MediaWiki\Revision\RevisionRenderer
36 * @group Database
37 */
38 class RevisionRendererTest extends MediaWikiIntegrationTestCase {
39 use MockAuthorityTrait;
40
41 /** @var PageIdentity */
42 private $fakePage;
43
44 protected function setUp(): void {
45 parent::setUp();
46 $this->fakePage = PageIdentityValue::localIdentity( 7, NS_MAIN, __CLASS__ );
47 }
48
49 /**
50 * @param IDatabase&MockObject $db
51 * @param int $maxRev
52 * @return IDatabase&MockObject
53 */
54 private function mockDatabaseConnection( $db, $maxRev = 100 ) {
55 $db->method( 'selectField' )
56 ->willReturnCallback(
57 function ( $table, $fields, $cond ) use ( $maxRev ) {
58 return $this->selectFieldCallback(
59 $table,
60 $fields,
61 $cond,
62 $maxRev
63 );
64 }
65 );
66 $db->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $db ) );
67
68 return $db;
69 }
70
71 /**
72 * @param int $maxRev
73 * @param bool $usePrimary
74 * @param ContentRenderer|null $contentRenderer
75 * @return RevisionRenderer
76 */
77 private function newRevisionRenderer(
78 $maxRev = 100,
79 $usePrimary = false,
80 $contentRenderer = null
81 ) {
82 $dbIndex = $usePrimary ? DB_PRIMARY : DB_REPLICA;
83 $cr = $contentRenderer ?? $this->getServiceContainer()->getContentRenderer();
84
85 /** @var ILoadBalancer|MockObject $lb */
86 $lb = $this->createMock( ILoadBalancer::class );
87 $lb->method( 'getConnection' )
88 ->with( $dbIndex )
89 ->willReturn( $this->mockDatabaseConnection( $this->createMock( IDatabase::class ), $maxRev ) );
90
91 /** @var NameTableStore|MockObject $slotRoles */
92 $slotRoles = $this->createMock( NameTableStore::class );
93 $slotRoles->method( 'getMap' )
94 ->willReturn( [] );
95
96 $roleReg = new SlotRoleRegistry( $slotRoles );
97 $roleReg->defineRole( SlotRecord::MAIN, function () {
98 return new MainSlotRoleHandler(
99 [],
100 $this->createMock( IContentHandlerFactory::class ),
101 $this->createMock( HookContainer::class ),
102 $this->createMock( TitleFactory::class )
103 );
104 } );
105 $roleReg->defineRoleWithModel( 'aux', CONTENT_MODEL_WIKITEXT );
106
107 return new RevisionRenderer( $lb, $roleReg, $cr );
108 }
109
110 private function selectFieldCallback( $table, $fields, $cond, $maxRev ) {
111 if ( [ $table, $fields, $cond ] === [ [ 'revision' ], 'MAX(rev_id)', [] ] ) {
112 return $maxRev;
113 }
114
115 $this->fail( 'Unexpected call to selectField' );
116 throw new LogicException( 'Ooops' ); // Can't happen, make analyzer happy
117 }
118
119 public function testGetRenderedRevision_new() {
120 $renderer = $this->newRevisionRenderer( 100 );
121
122 $rev = new MutableRevisionRecord( $this->fakePage );
123 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
124 $rev->setTimestamp( '20180101000003' );
125 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
126
127 $text = "";
128 $text .= "* page:{{PAGENAME}}\n";
129 $text .= "* rev:{{REVISIONID}}\n";
130 $text .= "* user:{{REVISIONUSER}}\n";
131 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
132 $text .= "* [[Link It]]\n";
133
134 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
135
136 $options = ParserOptions::newFromAnon();
137 $rr = $renderer->getRenderedRevision( $rev, $options );
138
139 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
140
141 $this->assertSame( $rev, $rr->getRevision() );
142 $this->assertSame( $options, $rr->getOptions() );
143
144 $html = $rr->getRevisionParserOutput()->getRawText();
145
146 $this->assertStringContainsString( 'page:' . __CLASS__, $html );
147 $this->assertStringContainsString( 'rev:101', $html ); // from speculativeRevIdCallback
148 $this->assertStringContainsString( 'user:Frank', $html );
149 $this->assertStringContainsString( 'time:20180101000003', $html );
150
151 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
152 }
153
154 public function testGetRenderedRevision_current() {
155 $renderer = $this->newRevisionRenderer( 100 );
156
157 $rev = new MutableRevisionRecord( $this->fakePage );
158 $rev->setId( 21 ); // current!
159 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
160 $rev->setTimestamp( '20180101000003' );
161 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
162
163 $text = "";
164 $text .= "* page:{{PAGENAME}}\n";
165 $text .= "* rev:{{REVISIONID}}\n";
166 $text .= "* user:{{REVISIONUSER}}\n";
167 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
168
169 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
170
171 $options = ParserOptions::newFromAnon();
172 $rr = $renderer->getRenderedRevision( $rev, $options );
173
174 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
175
176 $this->assertSame( $rev, $rr->getRevision() );
177 $this->assertSame( $options, $rr->getOptions() );
178
179 $html = $rr->getRevisionParserOutput()->getRawText();
180
181 $this->assertStringContainsString( 'page:' . __CLASS__, $html );
182 $this->assertStringContainsString( 'rev:21', $html );
183 $this->assertStringContainsString( 'user:Frank', $html );
184 $this->assertStringContainsString( 'time:20180101000003', $html );
185
186 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
187 }
188
189 public function testGetRenderedRevision_master() {
190 $renderer = $this->newRevisionRenderer( 100, true ); // use master
191
192 $rev = new MutableRevisionRecord( $this->fakePage );
193 $rev->setId( 21 ); // current!
194 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
195 $rev->setTimestamp( '20180101000003' );
196 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
197
198 $text = "";
199 $text .= "* page:{{PAGENAME}}\n";
200 $text .= "* rev:{{REVISIONID}}\n";
201 $text .= "* user:{{REVISIONUSER}}\n";
202 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
203
204 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
205
206 $options = ParserOptions::newFromAnon();
207 $rr = $renderer->getRenderedRevision( $rev, $options, null, [ 'use-master' => true ] );
208
209 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
210
211 $html = $rr->getRevisionParserOutput()->getRawText();
212
213 $this->assertStringContainsString( 'rev:21', $html );
214
215 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
216 }
217
218 public function testGetRenderedRevision_known() {
219 $renderer = $this->newRevisionRenderer( 100, true ); // use master
220
221 $rev = new MutableRevisionRecord( $this->fakePage );
222 $rev->setId( 21 ); // current!
223 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
224 $rev->setTimestamp( '20180101000003' );
225 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
226
227 $text = "uncached text";
228 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
229
230 $output = new ParserOutput( 'cached text' );
231
232 $options = ParserOptions::newFromAnon();
233 $rr = $renderer->getRenderedRevision(
234 $rev,
235 $options,
236 null,
237 [ 'known-revision-output' => $output ]
238 );
239
240 $this->assertSame( $output, $rr->getRevisionParserOutput() );
241 $this->assertSame( 'cached text', $rr->getRevisionParserOutput()->getRawText() );
242 $this->assertSame( 'cached text', $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
243 }
244
245 public function testGetRenderedRevision_old() {
246 $renderer = $this->newRevisionRenderer( 100 );
247
248 $rev = new MutableRevisionRecord( $this->fakePage );
249 $rev->setId( 11 ); // old!
250 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
251 $rev->setTimestamp( '20180101000003' );
252 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
253
254 $text = "";
255 $text .= "* page:{{PAGENAME}}\n";
256 $text .= "* rev:{{REVISIONID}}\n";
257 $text .= "* user:{{REVISIONUSER}}\n";
258 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
259
260 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
261
262 $options = ParserOptions::newFromAnon();
263 $rr = $renderer->getRenderedRevision( $rev, $options );
264
265 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
266
267 $this->assertSame( $rev, $rr->getRevision() );
268 $this->assertSame( $options, $rr->getOptions() );
269
270 $html = $rr->getRevisionParserOutput()->getRawText();
271
272 $this->assertStringContainsString( 'page:' . __CLASS__, $html );
273 $this->assertStringContainsString( 'rev:11', $html );
274 $this->assertStringContainsString( 'user:Frank', $html );
275 $this->assertStringContainsString( 'time:20180101000003', $html );
276
277 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
278 }
279
280 public function testGetRenderedRevision_suppressed() {
281 $renderer = $this->newRevisionRenderer( 100 );
282
283 $rev = new MutableRevisionRecord( $this->fakePage );
284 $rev->setId( 11 ); // old!
285 $rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
286 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
287 $rev->setTimestamp( '20180101000003' );
288 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
289
290 $text = "";
291 $text .= "* page:{{PAGENAME}}\n";
292 $text .= "* rev:{{REVISIONID}}\n";
293 $text .= "* user:{{REVISIONUSER}}\n";
294 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
295
296 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
297
298 $options = ParserOptions::newFromAnon();
299 $rr = $renderer->getRenderedRevision( $rev, $options );
300
301 $this->assertNull( $rr, 'getRenderedRevision' );
302 }
303
304 public function testGetRenderedRevision_privileged() {
305 $renderer = $this->newRevisionRenderer( 100 );
306
307 $rev = new MutableRevisionRecord( $this->fakePage );
308 $rev->setId( 11 ); // old!
309 $rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
310 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
311 $rev->setTimestamp( '20180101000003' );
312 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
313
314 $text = "";
315 $text .= "* page:{{PAGENAME}}\n";
316 $text .= "* rev:{{REVISIONID}}\n";
317 $text .= "* user:{{REVISIONUSER}}\n";
318 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
319
320 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
321
322 $options = ParserOptions::newFromAnon();
323 $sysop = $this->mockRegisteredUltimateAuthority();
324 $rr = $renderer->getRenderedRevision( $rev, $options, $sysop );
325
326 $this->assertNotNull( $rr, 'getRenderedRevision' );
327 $this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
328
329 $this->assertSame( $rev, $rr->getRevision() );
330 $this->assertSame( $options, $rr->getOptions() );
331
332 $html = $rr->getRevisionParserOutput()->getRawText();
333
334 // Suppressed content should be visible for sysops
335 $this->assertStringContainsString( 'page:' . __CLASS__, $html );
336 $this->assertStringContainsString( 'rev:11', $html );
337 $this->assertStringContainsString( 'user:Frank', $html );
338 $this->assertStringContainsString( 'time:20180101000003', $html );
339
340 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
341 }
342
343 public function testGetRenderedRevision_raw() {
344 $renderer = $this->newRevisionRenderer( 100 );
345
346 $rev = new MutableRevisionRecord( $this->fakePage );
347 $rev->setId( 11 ); // old!
348 $rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
349 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
350 $rev->setTimestamp( '20180101000003' );
351 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
352
353 $text = "";
354 $text .= "* page:{{PAGENAME}}\n";
355 $text .= "* rev:{{REVISIONID}}\n";
356 $text .= "* user:{{REVISIONUSER}}\n";
357 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
358
359 $rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
360
361 $options = ParserOptions::newFromAnon();
362 $rr = $renderer->getRenderedRevision(
363 $rev,
364 $options,
365 null,
366 [ 'audience' => RevisionRecord::RAW ]
367 );
368
369 $this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
370
371 $this->assertSame( $rev, $rr->getRevision() );
372 $this->assertSame( $options, $rr->getOptions() );
373
374 $parserOutput = $rr->getRevisionParserOutput();
375 // Assert parser output recorded timestamp and parsed rev_id
376 $this->assertSame( $rev->getId(), $parserOutput->getCacheRevisionId() );
377 $this->assertSame( $rev->getTimestamp(), $parserOutput->getRevisionTimestamp() );
378
379 $html = $parserOutput->getRawText();
380
381 // Suppressed content should be visible in raw mode
382 $this->assertStringContainsString( 'page:' . __CLASS__, $html );
383 $this->assertStringContainsString( 'rev:11', $html );
384 $this->assertStringContainsString( 'user:Frank', $html );
385 $this->assertStringContainsString( 'time:20180101000003', $html );
386
387 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getRawText() );
388 }
389
390 public function testGetRenderedRevision_multi() {
391 $renderer = $this->newRevisionRenderer();
392
393 $rev = new MutableRevisionRecord( $this->fakePage );
394 $rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
395 $rev->setTimestamp( '20180101000003' );
396 $rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
397
398 $rev->setContent( SlotRecord::MAIN, new WikitextContent( '[[Kittens]]' ) );
399 $rev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) );
400
401 $options = ParserOptions::newFromAnon();
402 $rr = $renderer->getRenderedRevision( $rev, $options );
403
404 $combinedOutput = $rr->getRevisionParserOutput();
405 $mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN );
406 $auxOutput = $rr->getSlotParserOutput( 'aux' );
407
408 $pipeline = MediaWikiServices::getInstance()->getDefaultOutputPipeline();
409 $combinedHtml = $pipeline->run( $combinedOutput, $options, [] )->getContentHolderText();
410 $mainHtml = $pipeline->run( $mainOutput, $options, [] )->getContentHolderText();
411
412 $this->assertNotSame( $combinedHtml, $mainHtml );
413
414 $auxHtml = $pipeline->run( $auxOutput, $options, [] )->getContentHolderText();
415
416 $this->assertStringContainsString( 'Kittens', $mainHtml );
417 $this->assertStringContainsString( 'Goats', $auxHtml );
418 $this->assertStringNotContainsString( 'Goats', $mainHtml );
419 $this->assertStringNotContainsString( 'Kittens', $auxHtml );
420 $this->assertStringContainsString( 'Kittens', $combinedHtml );
421 $this->assertStringContainsString( 'Goats', $combinedHtml );
422 $this->assertStringContainsString( '>aux<', $combinedHtml, 'slot header' );
423 $this->assertStringNotContainsString(
424 '<mw:slotheader',
425 $combinedHtml,
426 'slot header placeholder'
427 );
428
429 // make sure output wrapping works right
430 $this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $mainHtml );
431 $this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $auxHtml );
432 $this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $combinedHtml );
433
434 // there should be only one wrapper div
435 $this->assertSame( 1, preg_match_all( '#class="[^"]*mw-parser-output"#', $combinedHtml ) );
436 $this->assertStringNotContainsString( 'mw-parser-output"', $combinedOutput->getRawText() );
437
438 $combinedLinks = $combinedOutput->getLinks();
439 $mainLinks = $mainOutput->getLinks();
440 $auxLinks = $auxOutput->getLinks();
441 $this->assertTrue( isset( $combinedLinks[NS_MAIN]['Kittens'] ), 'links from main slot' );
442 $this->assertTrue( isset( $combinedLinks[NS_MAIN]['Goats'] ), 'links from aux slot' );
443 $this->assertFalse( isset( $mainLinks[NS_MAIN]['Goats'] ), 'no aux links in main' );
444 $this->assertFalse( isset( $auxLinks[NS_MAIN]['Kittens'] ), 'no main links in aux' );
445
446 // Same tests with Parsoid
447 // T351026: We should get only main slot output in the combined output.
448 // T351113 will have to update this test.
449 $options = ParserOptions::newFromAnon();
450 $options->setUseParsoid();
451 $rr = $renderer->getRenderedRevision( $rev, $options );
452
453 $combinedOutput = $rr->getRevisionParserOutput();
454 $mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN );
455
456 $combinedHtml = $pipeline->run( $combinedOutput, $options, [] )->getContentHolderText();
457 $mainHtml = $pipeline->run( $mainOutput, $options, [] )->getContentHolderText();
458 $this->assertSame( $combinedHtml, $mainHtml );
459 $this->assertSame( $combinedOutput->getLinks(), $mainOutput->getLinks() );
460 $this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $mainHtml );
461 $this->assertStringContainsString( 'Kittens', $combinedHtml );
462 $this->assertStringNotContainsString( 'Goats', $combinedHtml );
463 }
464
465 public function testGetRenderedRevision_noHtml() {
466 $content = new WikitextContent( 'Whatever' );
467
468 /** @var MockObject|ContentRenderer $mockContentRenderer */
469 $mockContentRenderer = $this->getMockBuilder( ContentRenderer::class )
470 ->onlyMethods( [ 'getParserOutput' ] )
471 ->disableOriginalConstructor()
472 ->getMock();
473 $mockContentRenderer->method( 'getParserOutput' )
474 ->willReturnCallback( function ( Content $content, PageReference $page, $revId = null,
475 ?ParserOptions $options = null, $hints = []
476 ) {
477 if ( is_bool( $hints ) ) {
478 $hints = [ 'generate-html' => $hints ];
479 }
480 $generateHtml = $hints['generate-html'] ?? true;
481 if ( !$generateHtml ) {
482 return new ParserOutput( null );
483 } else {
484 $this->fail( 'Should not be called with $generateHtml == true' );
485 return null; // never happens, make analyzer happy
486 }
487 } );
488
489 $renderer = $this->newRevisionRenderer( 100, false, $mockContentRenderer );
490
491 $rev = new MutableRevisionRecord( $this->fakePage );
492 $rev->setContent( SlotRecord::MAIN, $content );
493 $rev->setContent( 'aux', $content );
494
495 // NOTE: we are testing the private combineSlotOutput() callback here.
496 $rr = $renderer->getRenderedRevision( $rev );
497
498 $output = $rr->getSlotParserOutput( SlotRecord::MAIN, [ 'generate-html' => false ] );
499 $this->assertFalse( $output->hasText(), 'hasText' );
500
501 $output = $rr->getRevisionParserOutput( [ 'generate-html' => false ] );
502 $this->assertFalse( $output->hasText(), 'hasText' );
503 }
504
505 }