3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
9 use MediaWiki\Revision\MutableRevisionRecord
;
10 use MediaWiki\Revision\MainSlotRoleHandler
;
11 use MediaWiki\Revision\RevisionRecord
;
12 use MediaWiki\Revision\RevisionRenderer
;
13 use MediaWiki\Revision\SlotRecord
;
14 use MediaWiki\Revision\SlotRoleRegistry
;
15 use MediaWiki\Storage\NameTableStore
;
16 use MediaWikiTestCase
;
17 use MediaWiki\User\UserIdentityValue
;
20 use PHPUnit\Framework\MockObject\MockObject
;
23 use Wikimedia\Rdbms\IDatabase
;
24 use Wikimedia\Rdbms\ILoadBalancer
;
28 * @covers \MediaWiki\Revision\RevisionRenderer
30 class RevisionRendererTest
extends MediaWikiTestCase
{
33 * @param int $articleId
34 * @param int $revisionId
37 private function getMockTitle( $articleId, $revisionId ) {
38 /** @var Title|MockObject $mock */
39 $mock = $this->getMockBuilder( Title
::class )
40 ->disableOriginalConstructor()
42 $mock->expects( $this->any() )
43 ->method( 'getNamespace' )
44 ->will( $this->returnValue( NS_MAIN
) );
45 $mock->expects( $this->any() )
47 ->will( $this->returnValue( __CLASS__
) );
48 $mock->expects( $this->any() )
49 ->method( 'getPrefixedText' )
50 ->will( $this->returnValue( __CLASS__
) );
51 $mock->expects( $this->any() )
52 ->method( 'getDBkey' )
53 ->will( $this->returnValue( __CLASS__
) );
54 $mock->expects( $this->any() )
55 ->method( 'getArticleID' )
56 ->will( $this->returnValue( $articleId ) );
57 $mock->expects( $this->any() )
58 ->method( 'getLatestRevId' )
59 ->will( $this->returnValue( $revisionId ) );
60 $mock->expects( $this->any() )
61 ->method( 'getContentModel' )
62 ->will( $this->returnValue( CONTENT_MODEL_WIKITEXT
) );
63 $mock->expects( $this->any() )
64 ->method( 'getPageLanguage' )
65 ->will( $this->returnValue( Language
::factory( 'en' ) ) );
66 $mock->expects( $this->any() )
67 ->method( 'isContentPage' )
68 ->will( $this->returnValue( true ) );
69 $mock->expects( $this->any() )
72 function ( Title
$other ) use ( $mock ) {
73 return $mock->getArticleID() === $other->getArticleID();
76 $mock->expects( $this->any() )
79 function ( $perm, User
$user ) use ( $mock ) {
80 return $user->isAllowed( $perm );
89 * @param int $linkCount
93 private function getMockDatabaseConnection( $maxRev = 100, $linkCount = 0 ) {
94 /** @var IDatabase|MockObject $db */
95 $db = $this->getMock( IDatabase
::class );
96 $db->method( 'selectField' )
98 function ( $table, $fields, $cond ) use ( $maxRev, $linkCount ) {
99 return $this->selectFieldCallback(
113 * @return RevisionRenderer
115 private function newRevisionRenderer( $maxRev = 100, $useMaster = false ) {
116 $dbIndex = $useMaster ? DB_MASTER
: DB_REPLICA
;
118 $db = $this->getMockDatabaseConnection( $maxRev );
120 /** @var ILoadBalancer|MockObject $lb */
121 $lb = $this->getMock( ILoadBalancer
::class );
122 $lb->method( 'getConnection' )
125 $lb->method( 'getConnectionRef' )
128 $lb->method( 'getLazyConnectionRef' )
132 /** @var NameTableStore|MockObject $slotRoles */
133 $slotRoles = $this->getMockBuilder( NameTableStore
::class )
134 ->disableOriginalConstructor()
136 $slotRoles->method( 'getMap' )
139 $roleReg = new SlotRoleRegistry( $slotRoles );
140 $roleReg->defineRole( 'main', function () {
141 return new MainSlotRoleHandler( [] );
143 $roleReg->defineRoleWithModel( 'aux', CONTENT_MODEL_WIKITEXT
);
145 return new RevisionRenderer( $lb, $roleReg );
148 private function selectFieldCallback( $table, $fields, $cond, $maxRev ) {
149 if ( [ $table, $fields, $cond ] === [ 'revision', 'MAX(rev_id)', [] ] ) {
153 $this->fail( 'Unexpected call to selectField' );
154 throw new LogicException( 'Ooops' ); // Can't happen, make analyzer happy
157 public function testGetRenderedRevision_new() {
158 $renderer = $this->newRevisionRenderer( 100 );
159 $title = $this->getMockTitle( 7, 21 );
161 $rev = new MutableRevisionRecord( $title );
162 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
163 $rev->setTimestamp( '20180101000003' );
164 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
167 $text .= "* page:{{PAGENAME}}\n";
168 $text .= "* rev:{{REVISIONID}}\n";
169 $text .= "* user:{{REVISIONUSER}}\n";
170 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
171 $text .= "* [[Link It]]\n";
173 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
175 $options = ParserOptions
::newCanonical( 'canonical' );
176 $rr = $renderer->getRenderedRevision( $rev, $options );
178 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
180 $this->assertSame( $rev, $rr->getRevision() );
181 $this->assertSame( $options, $rr->getOptions() );
183 $html = $rr->getRevisionParserOutput()->getText();
185 $this->assertContains( 'page:' . __CLASS__
, $html );
186 $this->assertContains( 'rev:101', $html ); // from speculativeRevIdCallback
187 $this->assertContains( 'user:Frank', $html );
188 $this->assertContains( 'time:20180101000003', $html );
190 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord
::MAIN
)->getText() );
193 public function testGetRenderedRevision_current() {
194 $renderer = $this->newRevisionRenderer( 100 );
195 $title = $this->getMockTitle( 7, 21 );
197 $rev = new MutableRevisionRecord( $title );
198 $rev->setId( 21 ); // current!
199 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
200 $rev->setTimestamp( '20180101000003' );
201 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
204 $text .= "* page:{{PAGENAME}}\n";
205 $text .= "* rev:{{REVISIONID}}\n";
206 $text .= "* user:{{REVISIONUSER}}\n";
207 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
209 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
211 $options = ParserOptions
::newCanonical( 'canonical' );
212 $rr = $renderer->getRenderedRevision( $rev, $options );
214 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
216 $this->assertSame( $rev, $rr->getRevision() );
217 $this->assertSame( $options, $rr->getOptions() );
219 $html = $rr->getRevisionParserOutput()->getText();
221 $this->assertContains( 'page:' . __CLASS__
, $html );
222 $this->assertContains( 'rev:21', $html );
223 $this->assertContains( 'user:Frank', $html );
224 $this->assertContains( 'time:20180101000003', $html );
226 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord
::MAIN
)->getText() );
229 public function testGetRenderedRevision_master() {
230 $renderer = $this->newRevisionRenderer( 100, true ); // use master
231 $title = $this->getMockTitle( 7, 21 );
233 $rev = new MutableRevisionRecord( $title );
234 $rev->setId( 21 ); // current!
235 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
236 $rev->setTimestamp( '20180101000003' );
237 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
240 $text .= "* page:{{PAGENAME}}\n";
241 $text .= "* rev:{{REVISIONID}}\n";
242 $text .= "* user:{{REVISIONUSER}}\n";
243 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
245 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
247 $options = ParserOptions
::newCanonical( 'canonical' );
248 $rr = $renderer->getRenderedRevision( $rev, $options, null, [ 'use-master' => true ] );
250 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
252 $html = $rr->getRevisionParserOutput()->getText();
254 $this->assertContains( 'rev:21', $html );
256 $this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord
::MAIN
)->getText() );
259 public function testGetRenderedRevision_known() {
260 $renderer = $this->newRevisionRenderer( 100, true ); // use master
261 $title = $this->getMockTitle( 7, 21 );
263 $rev = new MutableRevisionRecord( $title );
264 $rev->setId( 21 ); // current!
265 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
266 $rev->setTimestamp( '20180101000003' );
267 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
269 $text = "uncached text";
270 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
272 $output = new ParserOutput( 'cached text' );
274 $options = ParserOptions
::newCanonical( 'canonical' );
275 $rr = $renderer->getRenderedRevision(
279 [ 'known-revision-output' => $output ]
282 $this->assertSame( $output, $rr->getRevisionParserOutput() );
283 $this->assertSame( 'cached text', $rr->getRevisionParserOutput()->getText() );
284 $this->assertSame( 'cached text', $rr->getSlotParserOutput( SlotRecord
::MAIN
)->getText() );
287 public function testGetRenderedRevision_old() {
288 $renderer = $this->newRevisionRenderer( 100 );
289 $title = $this->getMockTitle( 7, 21 );
291 $rev = new MutableRevisionRecord( $title );
292 $rev->setId( 11 ); // old!
293 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
294 $rev->setTimestamp( '20180101000003' );
295 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
298 $text .= "* page:{{PAGENAME}}\n";
299 $text .= "* rev:{{REVISIONID}}\n";
300 $text .= "* user:{{REVISIONUSER}}\n";
301 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
303 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
305 $options = ParserOptions
::newCanonical( 'canonical' );
306 $rr = $renderer->getRenderedRevision( $rev, $options );
308 $this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
310 $this->assertSame( $rev, $rr->getRevision() );
311 $this->assertSame( $options, $rr->getOptions() );
313 $html = $rr->getRevisionParserOutput()->getText();
315 $this->assertContains( 'page:' . __CLASS__
, $html );
316 $this->assertContains( 'rev:11', $html );
317 $this->assertContains( 'user:Frank', $html );
318 $this->assertContains( 'time:20180101000003', $html );
320 $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
323 public function testGetRenderedRevision_suppressed() {
324 $renderer = $this->newRevisionRenderer( 100 );
325 $title = $this->getMockTitle( 7, 21 );
327 $rev = new MutableRevisionRecord( $title );
328 $rev->setId( 11 ); // old!
329 $rev->setVisibility( RevisionRecord
::DELETED_TEXT
); // suppressed!
330 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
331 $rev->setTimestamp( '20180101000003' );
332 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
335 $text .= "* page:{{PAGENAME}}\n";
336 $text .= "* rev:{{REVISIONID}}\n";
337 $text .= "* user:{{REVISIONUSER}}\n";
338 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
340 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
342 $options = ParserOptions
::newCanonical( 'canonical' );
343 $rr = $renderer->getRenderedRevision( $rev, $options );
345 $this->assertNull( $rr, 'getRenderedRevision' );
348 public function testGetRenderedRevision_privileged() {
349 $renderer = $this->newRevisionRenderer( 100 );
350 $title = $this->getMockTitle( 7, 21 );
352 $rev = new MutableRevisionRecord( $title );
353 $rev->setId( 11 ); // old!
354 $rev->setVisibility( RevisionRecord
::DELETED_TEXT
); // suppressed!
355 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
356 $rev->setTimestamp( '20180101000003' );
357 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
360 $text .= "* page:{{PAGENAME}}\n";
361 $text .= "* rev:{{REVISIONID}}\n";
362 $text .= "* user:{{REVISIONUSER}}\n";
363 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
365 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
367 $options = ParserOptions
::newCanonical( 'canonical' );
368 $sysop = $this->getTestUser( [ 'sysop' ] )->getUser(); // privileged!
369 $rr = $renderer->getRenderedRevision( $rev, $options, $sysop );
371 $this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
373 $this->assertSame( $rev, $rr->getRevision() );
374 $this->assertSame( $options, $rr->getOptions() );
376 $html = $rr->getRevisionParserOutput()->getText();
378 // Suppressed content should be visible for sysops
379 $this->assertContains( 'page:' . __CLASS__
, $html );
380 $this->assertContains( 'rev:11', $html );
381 $this->assertContains( 'user:Frank', $html );
382 $this->assertContains( 'time:20180101000003', $html );
384 $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
387 public function testGetRenderedRevision_raw() {
388 $renderer = $this->newRevisionRenderer( 100 );
389 $title = $this->getMockTitle( 7, 21 );
391 $rev = new MutableRevisionRecord( $title );
392 $rev->setId( 11 ); // old!
393 $rev->setVisibility( RevisionRecord
::DELETED_TEXT
); // suppressed!
394 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
395 $rev->setTimestamp( '20180101000003' );
396 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
399 $text .= "* page:{{PAGENAME}}\n";
400 $text .= "* rev:{{REVISIONID}}\n";
401 $text .= "* user:{{REVISIONUSER}}\n";
402 $text .= "* time:{{REVISIONTIMESTAMP}}\n";
404 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( $text ) );
406 $options = ParserOptions
::newCanonical( 'canonical' );
407 $rr = $renderer->getRenderedRevision(
411 [ 'audience' => RevisionRecord
::RAW
]
414 $this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
416 $this->assertSame( $rev, $rr->getRevision() );
417 $this->assertSame( $options, $rr->getOptions() );
419 $html = $rr->getRevisionParserOutput()->getText();
421 // Suppressed content should be visible in raw mode
422 $this->assertContains( 'page:' . __CLASS__
, $html );
423 $this->assertContains( 'rev:11', $html );
424 $this->assertContains( 'user:Frank', $html );
425 $this->assertContains( 'time:20180101000003', $html );
427 $this->assertSame( $html, $rr->getSlotParserOutput( 'main' )->getText() );
430 public function testGetRenderedRevision_multi() {
431 $renderer = $this->newRevisionRenderer();
432 $title = $this->getMockTitle( 7, 21 );
434 $rev = new MutableRevisionRecord( $title );
435 $rev->setUser( new UserIdentityValue( 9, 'Frank', 0 ) );
436 $rev->setTimestamp( '20180101000003' );
437 $rev->setComment( CommentStoreComment
::newUnsavedComment( '' ) );
439 $rev->setContent( SlotRecord
::MAIN
, new WikitextContent( '[[Kittens]]' ) );
440 $rev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) );
442 $rr = $renderer->getRenderedRevision( $rev );
444 $combinedOutput = $rr->getRevisionParserOutput();
445 $mainOutput = $rr->getSlotParserOutput( SlotRecord
::MAIN
);
446 $auxOutput = $rr->getSlotParserOutput( 'aux' );
448 $combinedHtml = $combinedOutput->getText();
449 $mainHtml = $mainOutput->getText();
450 $auxHtml = $auxOutput->getText();
452 $this->assertContains( 'Kittens', $mainHtml );
453 $this->assertContains( 'Goats', $auxHtml );
454 $this->assertNotContains( 'Goats', $mainHtml );
455 $this->assertNotContains( 'Kittens', $auxHtml );
456 $this->assertContains( 'Kittens', $combinedHtml );
457 $this->assertContains( 'Goats', $combinedHtml );
458 $this->assertContains( '>aux<', $combinedHtml, 'slot header' );
459 $this->assertNotContains( '<mw:slotheader', $combinedHtml, 'slot header placeholder' );
461 // make sure output wrapping works right
462 $this->assertContains( 'class="mw-parser-output"', $mainHtml );
463 $this->assertContains( 'class="mw-parser-output"', $auxHtml );
464 $this->assertContains( 'class="mw-parser-output"', $combinedHtml );
466 // there should be only one wrapper div
467 $this->assertSame( 1, preg_match_all( '#class="mw-parser-output"#', $combinedHtml ) );
468 $this->assertNotContains( 'class="mw-parser-output"', $combinedOutput->getRawText() );
470 $combinedLinks = $combinedOutput->getLinks();
471 $mainLinks = $mainOutput->getLinks();
472 $auxLinks = $auxOutput->getLinks();
473 $this->assertTrue( isset( $combinedLinks[NS_MAIN
]['Kittens'] ), 'links from main slot' );
474 $this->assertTrue( isset( $combinedLinks[NS_MAIN
]['Goats'] ), 'links from aux slot' );
475 $this->assertFalse( isset( $mainLinks[NS_MAIN
]['Goats'] ), 'no aux links in main' );
476 $this->assertFalse( isset( $auxLinks[NS_MAIN
]['Kittens'] ), 'no main links in aux' );
479 public function testGetRenderedRevision_noHtml() {
480 /** @var MockObject|Content $mockContent */
481 $mockContent = $this->getMockBuilder( WikitextContent
::class )
482 ->setMethods( [ 'getParserOutput' ] )
483 ->setConstructorArgs( [ 'Whatever' ] )
485 $mockContent->method( 'getParserOutput' )
486 ->willReturnCallback( function ( Title
$title, $revId = null,
487 ParserOptions
$options = null, $generateHtml = true
489 if ( !$generateHtml ) {
490 return new ParserOutput( null );
492 $this->fail( 'Should not be called with $generateHtml == true' );
493 return null; // never happens, make analyzer happy
497 $renderer = $this->newRevisionRenderer();
498 $title = $this->getMockTitle( 7, 21 );
500 $rev = new MutableRevisionRecord( $title );
501 $rev->setContent( SlotRecord
::MAIN
, $mockContent );
502 $rev->setContent( 'aux', $mockContent );
504 // NOTE: we are testing the private combineSlotOutput() callback here.
505 $rr = $renderer->getRenderedRevision( $rev );
507 $output = $rr->getSlotParserOutput( SlotRecord
::MAIN
, [ 'generate-html' => false ] );
508 $this->assertFalse( $output->hasText(), 'hasText' );
510 $output = $rr->getRevisionParserOutput( [ 'generate-html' => false ] );
511 $this->assertFalse( $output->hasText(), 'hasText' );