2018-08-07 16:52:40 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Tests\Revision;
|
|
|
|
|
|
2018-08-13 20:33:31 +00:00
|
|
|
use Content;
|
2018-08-07 16:52:40 +00:00
|
|
|
use LogicException;
|
2023-12-04 11:27:42 +00:00
|
|
|
use MediaWiki\CommentStore\CommentStoreComment;
|
2020-01-18 20:25:04 +00:00
|
|
|
use MediaWiki\Content\IContentHandlerFactory;
|
2021-10-21 12:04:01 +00:00
|
|
|
use MediaWiki\Content\Renderer\ContentRenderer;
|
2024-08-06 13:40:20 +00:00
|
|
|
use MediaWiki\Content\WikitextContent;
|
2021-03-11 20:24:22 +00:00
|
|
|
use MediaWiki\HookContainer\HookContainer;
|
2021-09-29 14:02:36 +00:00
|
|
|
use MediaWiki\Page\PageIdentityValue;
|
2021-10-21 12:04:01 +00:00
|
|
|
use MediaWiki\Page\PageReference;
|
2023-12-14 19:20:33 +00:00
|
|
|
use MediaWiki\Parser\ParserOutput;
|
2018-11-19 11:39:56 +00:00
|
|
|
use MediaWiki\Revision\MainSlotRoleHandler;
|
2020-01-10 00:00:51 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2018-09-20 17:29:04 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2018-08-07 16:52:40 +00:00
|
|
|
use MediaWiki\Revision\RevisionRenderer;
|
2018-09-20 17:29:04 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
2018-11-19 11:39:56 +00:00
|
|
|
use MediaWiki\Revision\SlotRoleRegistry;
|
|
|
|
|
use MediaWiki\Storage\NameTableStore;
|
2021-03-23 17:00:36 +00:00
|
|
|
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
|
2022-11-26 01:15:16 +00:00
|
|
|
use MediaWiki\Title\TitleFactory;
|
2018-09-20 17:29:04 +00:00
|
|
|
use MediaWiki\User\UserIdentityValue;
|
2020-06-30 15:09:24 +00:00
|
|
|
use MediaWikiIntegrationTestCase;
|
2018-08-07 16:52:40 +00:00
|
|
|
use ParserOptions;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Wikimedia\Rdbms\IDatabase;
|
|
|
|
|
use Wikimedia\Rdbms\ILoadBalancer;
|
2023-08-01 16:34:16 +00:00
|
|
|
use Wikimedia\Rdbms\SelectQueryBuilder;
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Revision\RevisionRenderer
|
2023-07-19 23:08:27 +00:00
|
|
|
* @group Database
|
2018-08-07 16:52:40 +00:00
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class RevisionRendererTest extends MediaWikiIntegrationTestCase {
|
2021-03-23 17:00:36 +00:00
|
|
|
use MockAuthorityTrait;
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
private $fakePage;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
$this->fakePage = PageIdentityValue::localIdentity( 7, NS_MAIN, __CLASS__ );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-30 00:00:34 +00:00
|
|
|
* @param IDatabase&MockObject $db
|
2018-08-07 16:52:40 +00:00
|
|
|
* @param int $maxRev
|
2021-08-30 00:00:34 +00:00
|
|
|
* @return IDatabase&MockObject
|
2018-08-07 16:52:40 +00:00
|
|
|
*/
|
2021-08-30 00:00:34 +00:00
|
|
|
private function mockDatabaseConnection( $db, $maxRev = 100 ) {
|
2018-08-07 16:52:40 +00:00
|
|
|
$db->method( 'selectField' )
|
|
|
|
|
->willReturnCallback(
|
2021-03-23 17:00:36 +00:00
|
|
|
function ( $table, $fields, $cond ) use ( $maxRev ) {
|
2018-08-07 16:52:40 +00:00
|
|
|
return $this->selectFieldCallback(
|
|
|
|
|
$table,
|
|
|
|
|
$fields,
|
|
|
|
|
$cond,
|
2021-03-23 17:00:36 +00:00
|
|
|
$maxRev
|
2018-08-07 16:52:40 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-09-27 13:22:44 +00:00
|
|
|
$db->method( 'newSelectQueryBuilder' )->willReturnCallback( static fn () => new SelectQueryBuilder( $db ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
return $db;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-16 19:44:17 +00:00
|
|
|
* @param int $maxRev
|
2021-05-14 20:04:02 +00:00
|
|
|
* @param bool $usePrimary
|
2021-10-21 12:04:01 +00:00
|
|
|
* @param ContentRenderer|null $contentRenderer
|
2018-08-07 16:52:40 +00:00
|
|
|
* @return RevisionRenderer
|
|
|
|
|
*/
|
2021-10-21 12:04:01 +00:00
|
|
|
private function newRevisionRenderer(
|
|
|
|
|
$maxRev = 100,
|
|
|
|
|
$usePrimary = false,
|
|
|
|
|
$contentRenderer = null
|
|
|
|
|
) {
|
2021-05-14 20:04:02 +00:00
|
|
|
$dbIndex = $usePrimary ? DB_PRIMARY : DB_REPLICA;
|
2021-10-21 12:04:01 +00:00
|
|
|
$cr = $contentRenderer ?? $this->getServiceContainer()->getContentRenderer();
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
/** @var ILoadBalancer|MockObject $lb */
|
2019-10-05 22:14:35 +00:00
|
|
|
$lb = $this->createMock( ILoadBalancer::class );
|
2018-08-07 16:52:40 +00:00
|
|
|
$lb->method( 'getConnection' )
|
|
|
|
|
->with( $dbIndex )
|
2024-05-02 20:51:01 +00:00
|
|
|
->willReturn( $this->mockDatabaseConnection( $this->createMock( IDatabase::class ), $maxRev ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2018-11-19 11:39:56 +00:00
|
|
|
/** @var NameTableStore|MockObject $slotRoles */
|
2022-07-14 12:42:07 +00:00
|
|
|
$slotRoles = $this->createMock( NameTableStore::class );
|
2018-11-19 11:39:56 +00:00
|
|
|
$slotRoles->method( 'getMap' )
|
|
|
|
|
->willReturn( [] );
|
|
|
|
|
|
|
|
|
|
$roleReg = new SlotRoleRegistry( $slotRoles );
|
2023-01-03 15:24:42 +00:00
|
|
|
$roleReg->defineRole( SlotRecord::MAIN, function () {
|
2020-01-18 20:25:04 +00:00
|
|
|
return new MainSlotRoleHandler(
|
|
|
|
|
[],
|
2021-03-11 20:24:22 +00:00
|
|
|
$this->createMock( IContentHandlerFactory::class ),
|
|
|
|
|
$this->createMock( HookContainer::class ),
|
|
|
|
|
$this->createMock( TitleFactory::class )
|
2020-01-18 20:25:04 +00:00
|
|
|
);
|
2018-11-19 11:39:56 +00:00
|
|
|
} );
|
|
|
|
|
$roleReg->defineRoleWithModel( 'aux', CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
|
2021-10-21 12:04:01 +00:00
|
|
|
return new RevisionRenderer( $lb, $roleReg, $cr );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function selectFieldCallback( $table, $fields, $cond, $maxRev ) {
|
2023-08-01 16:34:16 +00:00
|
|
|
if ( [ $table, $fields, $cond ] === [ [ 'revision' ], 'MAX(rev_id)', [] ] ) {
|
2018-08-07 16:52:40 +00:00
|
|
|
return $maxRev;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->fail( 'Unexpected call to selectField' );
|
|
|
|
|
throw new LogicException( 'Ooops' ); // Can't happen, make analyzer happy
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_new() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
$text .= "* [[Link It]]\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $rev, $rr->getRevision() );
|
|
|
|
|
$this->assertSame( $options, $rr->getOptions() );
|
|
|
|
|
|
|
|
|
|
$html = $rr->getRevisionParserOutput()->getText();
|
|
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'page:' . __CLASS__, $html );
|
|
|
|
|
$this->assertStringContainsString( 'rev:101', $html ); // from speculativeRevIdCallback
|
|
|
|
|
$this->assertStringContainsString( 'user:Frank', $html );
|
|
|
|
|
$this->assertStringContainsString( 'time:20180101000003', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_current() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 21 ); // current!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $rev, $rr->getRevision() );
|
|
|
|
|
$this->assertSame( $options, $rr->getOptions() );
|
|
|
|
|
|
|
|
|
|
$html = $rr->getRevisionParserOutput()->getText();
|
|
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'page:' . __CLASS__, $html );
|
|
|
|
|
$this->assertStringContainsString( 'rev:21', $html );
|
|
|
|
|
$this->assertStringContainsString( 'user:Frank', $html );
|
|
|
|
|
$this->assertStringContainsString( 'time:20180101000003', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_master() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100, true ); // use master
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 21 ); // current!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options, null, [ 'use-master' => true ] );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$html = $rr->getRevisionParserOutput()->getText();
|
|
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'rev:21', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-11-15 16:40:53 +00:00
|
|
|
public function testGetRenderedRevision_known() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100, true ); // use master
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-11-15 16:40:53 +00:00
|
|
|
$rev->setId( 21 ); // current!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-11-15 16:40:53 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
|
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
|
|
|
|
|
|
|
|
|
$text = "uncached text";
|
|
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
|
|
|
|
|
|
|
|
|
$output = new ParserOutput( 'cached text' );
|
|
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-11-15 16:40:53 +00:00
|
|
|
$rr = $renderer->getRenderedRevision(
|
|
|
|
|
$rev,
|
|
|
|
|
$options,
|
|
|
|
|
null,
|
|
|
|
|
[ 'known-revision-output' => $output ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $output, $rr->getRevisionParserOutput() );
|
|
|
|
|
$this->assertSame( 'cached text', $rr->getRevisionParserOutput()->getText() );
|
|
|
|
|
$this->assertSame( 'cached text', $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 16:52:40 +00:00
|
|
|
public function testGetRenderedRevision_old() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 11 ); // old!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options );
|
|
|
|
|
|
|
|
|
|
$this->assertFalse( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $rev, $rr->getRevision() );
|
|
|
|
|
$this->assertSame( $options, $rr->getOptions() );
|
|
|
|
|
|
|
|
|
|
$html = $rr->getRevisionParserOutput()->getText();
|
|
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'page:' . __CLASS__, $html );
|
|
|
|
|
$this->assertStringContainsString( 'rev:11', $html );
|
|
|
|
|
$this->assertStringContainsString( 'user:Frank', $html );
|
|
|
|
|
$this->assertStringContainsString( 'time:20180101000003', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2023-01-03 15:24:42 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_suppressed() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 11 ); // old!
|
|
|
|
|
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options );
|
|
|
|
|
|
|
|
|
|
$this->assertNull( $rr, 'getRenderedRevision' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_privileged() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 11 ); // old!
|
|
|
|
|
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2021-03-23 17:00:36 +00:00
|
|
|
$sysop = $this->mockRegisteredUltimateAuthority();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options, $sysop );
|
|
|
|
|
|
2019-04-09 06:58:04 +00:00
|
|
|
$this->assertNotNull( $rr, 'getRenderedRevision' );
|
2018-08-07 16:52:40 +00:00
|
|
|
$this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $rev, $rr->getRevision() );
|
|
|
|
|
$this->assertSame( $options, $rr->getOptions() );
|
|
|
|
|
|
|
|
|
|
$html = $rr->getRevisionParserOutput()->getText();
|
|
|
|
|
|
|
|
|
|
// Suppressed content should be visible for sysops
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'page:' . __CLASS__, $html );
|
|
|
|
|
$this->assertStringContainsString( 'rev:11', $html );
|
|
|
|
|
$this->assertStringContainsString( 'user:Frank', $html );
|
|
|
|
|
$this->assertStringContainsString( 'time:20180101000003', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2023-01-03 15:24:42 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_raw() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer( 100 );
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setId( 11 ); // old!
|
|
|
|
|
$rev->setVisibility( RevisionRecord::DELETED_TEXT ); // suppressed!
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$text = "";
|
|
|
|
|
$text .= "* page:{{PAGENAME}}\n";
|
|
|
|
|
$text .= "* rev:{{REVISIONID}}\n";
|
|
|
|
|
$text .= "* user:{{REVISIONUSER}}\n";
|
|
|
|
|
$text .= "* time:{{REVISIONTIMESTAMP}}\n";
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( $text ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2022-06-16 13:22:24 +00:00
|
|
|
$options = ParserOptions::newFromAnon();
|
2018-08-07 16:52:40 +00:00
|
|
|
$rr = $renderer->getRenderedRevision(
|
|
|
|
|
$rev,
|
|
|
|
|
$options,
|
|
|
|
|
null,
|
|
|
|
|
[ 'audience' => RevisionRecord::RAW ]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $rr->isContentDeleted(), 'isContentDeleted' );
|
|
|
|
|
|
|
|
|
|
$this->assertSame( $rev, $rr->getRevision() );
|
|
|
|
|
$this->assertSame( $options, $rr->getOptions() );
|
|
|
|
|
|
2021-03-24 13:19:38 +00:00
|
|
|
$parserOutput = $rr->getRevisionParserOutput();
|
|
|
|
|
// Assert parser output recorded timestamp and parsed rev_id
|
|
|
|
|
$this->assertSame( $rev->getId(), $parserOutput->getCacheRevisionId() );
|
2023-11-06 21:25:07 +00:00
|
|
|
$this->assertSame( $rev->getTimestamp(), $parserOutput->getRevisionTimestamp() );
|
2021-03-24 13:19:38 +00:00
|
|
|
|
|
|
|
|
$html = $parserOutput->getText();
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
// Suppressed content should be visible in raw mode
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'page:' . __CLASS__, $html );
|
|
|
|
|
$this->assertStringContainsString( 'rev:11', $html );
|
|
|
|
|
$this->assertStringContainsString( 'user:Frank', $html );
|
|
|
|
|
$this->assertStringContainsString( 'time:20180101000003', $html );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2023-01-03 15:24:42 +00:00
|
|
|
$this->assertSame( $html, $rr->getSlotParserOutput( SlotRecord::MAIN )->getText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetRenderedRevision_multi() {
|
|
|
|
|
$renderer = $this->newRevisionRenderer();
|
|
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2021-02-15 18:58:09 +00:00
|
|
|
$rev->setUser( new UserIdentityValue( 9, 'Frank' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setTimestamp( '20180101000003' );
|
2018-09-03 13:34:12 +00:00
|
|
|
$rev->setComment( CommentStoreComment::newUnsavedComment( '' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, new WikitextContent( '[[Kittens]]' ) );
|
2018-08-07 16:52:40 +00:00
|
|
|
$rev->setContent( 'aux', new WikitextContent( '[[Goats]]' ) );
|
|
|
|
|
|
|
|
|
|
$rr = $renderer->getRenderedRevision( $rev );
|
|
|
|
|
|
|
|
|
|
$combinedOutput = $rr->getRevisionParserOutput();
|
2018-09-24 21:10:08 +00:00
|
|
|
$mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN );
|
2018-08-07 16:52:40 +00:00
|
|
|
$auxOutput = $rr->getSlotParserOutput( 'aux' );
|
|
|
|
|
|
|
|
|
|
$combinedHtml = $combinedOutput->getText();
|
|
|
|
|
$mainHtml = $mainOutput->getText();
|
2023-11-13 15:54:00 +00:00
|
|
|
|
|
|
|
|
$this->assertNotSame( $combinedHtml, $mainHtml );
|
|
|
|
|
|
2018-08-07 16:52:40 +00:00
|
|
|
$auxHtml = $auxOutput->getText();
|
|
|
|
|
|
2019-12-14 10:27:56 +00:00
|
|
|
$this->assertStringContainsString( 'Kittens', $mainHtml );
|
|
|
|
|
$this->assertStringContainsString( 'Goats', $auxHtml );
|
|
|
|
|
$this->assertStringNotContainsString( 'Goats', $mainHtml );
|
|
|
|
|
$this->assertStringNotContainsString( 'Kittens', $auxHtml );
|
|
|
|
|
$this->assertStringContainsString( 'Kittens', $combinedHtml );
|
|
|
|
|
$this->assertStringContainsString( 'Goats', $combinedHtml );
|
|
|
|
|
$this->assertStringContainsString( '>aux<', $combinedHtml, 'slot header' );
|
|
|
|
|
$this->assertStringNotContainsString(
|
|
|
|
|
'<mw:slotheader',
|
|
|
|
|
$combinedHtml,
|
|
|
|
|
'slot header placeholder'
|
|
|
|
|
);
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
// make sure output wrapping works right
|
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText
== Skin::wrapHTML ==
Skin::wrapHTML no longer has to perform any guessing of the
ParserOutput language. Nor does it have to special wiki pages vs
special pages in this regard. Yay, code removal.
== ImagePage ==
On URLs like /wiki/File:Example.jpg, the main output handler is
ImagePage::view. This calls the parent Article::view to handle most of
its output. Article::view obtains the ParserOptions, and then fetches
ParserOutput, and then adds `<div class=mw-parser-output>` and its
metadata to OutputPage.
Before this change, ImagePage::view was creating a wrapper based
on "predicting" what language the ParserOutput will contain. It
couldn't call the new OutputPage::getContentLanguage or some
equivalent as Article::view wouldn't have populated that yet.
This leaky abstraction is fixed by this change as now the `<div>`
from ParserOutput no longer comes with a "please wrap it properly"
contract that Article subclasses couldn't possibly implement correctly
(it coudln't wrap it after the fact because Article::view writes to
OutputPage directly).
RECENT (T310445):
A special case was recently added for file pages about translated SVGs.
For those, we decide which language to use for the "fullMedia" thumb
atop the page. This was recently changed as part of T310445 from a
hardcoded $wgLanguageCode (site content lang) to new problematic
Title::getPageViewLanguage, which tries to guestimate the page
language of the rendered ParserOutput and then gets the preferred
variant for the current user. The motivation for this was to support
language variants but used Title::getPageViewLanguage as a kitchen
sink to achieve that minor side-effect. The only part of this
now-deprecated method that we actually need is
LanguageConverter::getPreferredVariant().
Test plan: Covered by ImagePageTest.
== Skin mainpage-title ==
RECENT (T331095, T298715):
A special case was added to Skin::getTemplateData that powers the
mainpage-title interface message feature. This is empty by default,
but when created via MediaWiki:mainpage-title allows interface admins
to replace the H1 with a custom and localised page heading.
A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was
applied here to support language variants. Replace with the same
fix as for ImagePage. Revert back to Message::inContentLanguage()
but refactor to inLanguage() via MediaWikiServices::getContentLanguage
so that LanguageConverter::getPreferredVariant can be applied.
== EditPage ==
This was doing similar "predicting" of the ParserOutput language to
create an empty preview placeholder for use by preview.js. Now that
ApiParse (via ParserOutput::getText) returns a usable element without
any secret "you magically know the right class, lang, and dir" contract,
this placeholder is no longer needed.
Test Plan:
* EditPage: Default preview
1. index.php?title=Main_Page&action=edit
2. Show preview
3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
* EditPage: JS preview
1. Preferences > Editing > Show preview without reload
2. index.php?title=Main_Page&action=edit
3. Show preview
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
5. Type something and 'Show preview' again
6. Assert old element gone, new text is shown, and new element
attributes are the same as the above.
== McrUndoAction ==
Same as EditPage basically, but without the JS preview use case.
== DifferenceEngine ==
Test:
1. Open /w/index.php?title=Main_Page&diff=0
(this shows the latest diff, can do manually by viewing
/wiki/Main_Page, click "View history", click "Compare selected revisions")
2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
3. Open /w/index.php?title=Main_Page&diff=0&action=render
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
== Special:ExpandTemplates ==
Test:
1. /wiki/Special:ExpandTemplates
2. Write "Hello".
3. "OK"
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
Bug: T341244
Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c
Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1
Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105
Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
|
|
|
$this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $mainHtml );
|
|
|
|
|
$this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $auxHtml );
|
|
|
|
|
$this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $combinedHtml );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
// there should be only one wrapper div
|
parser: Move lang/dir and mw-content-ltr to ParserOutput::getText
== Skin::wrapHTML ==
Skin::wrapHTML no longer has to perform any guessing of the
ParserOutput language. Nor does it have to special wiki pages vs
special pages in this regard. Yay, code removal.
== ImagePage ==
On URLs like /wiki/File:Example.jpg, the main output handler is
ImagePage::view. This calls the parent Article::view to handle most of
its output. Article::view obtains the ParserOptions, and then fetches
ParserOutput, and then adds `<div class=mw-parser-output>` and its
metadata to OutputPage.
Before this change, ImagePage::view was creating a wrapper based
on "predicting" what language the ParserOutput will contain. It
couldn't call the new OutputPage::getContentLanguage or some
equivalent as Article::view wouldn't have populated that yet.
This leaky abstraction is fixed by this change as now the `<div>`
from ParserOutput no longer comes with a "please wrap it properly"
contract that Article subclasses couldn't possibly implement correctly
(it coudln't wrap it after the fact because Article::view writes to
OutputPage directly).
RECENT (T310445):
A special case was recently added for file pages about translated SVGs.
For those, we decide which language to use for the "fullMedia" thumb
atop the page. This was recently changed as part of T310445 from a
hardcoded $wgLanguageCode (site content lang) to new problematic
Title::getPageViewLanguage, which tries to guestimate the page
language of the rendered ParserOutput and then gets the preferred
variant for the current user. The motivation for this was to support
language variants but used Title::getPageViewLanguage as a kitchen
sink to achieve that minor side-effect. The only part of this
now-deprecated method that we actually need is
LanguageConverter::getPreferredVariant().
Test plan: Covered by ImagePageTest.
== Skin mainpage-title ==
RECENT (T331095, T298715):
A special case was added to Skin::getTemplateData that powers the
mainpage-title interface message feature. This is empty by default,
but when created via MediaWiki:mainpage-title allows interface admins
to replace the H1 with a custom and localised page heading.
A few months ago, in Ifc9f0a7174, Title::getPageViewLanguage was
applied here to support language variants. Replace with the same
fix as for ImagePage. Revert back to Message::inContentLanguage()
but refactor to inLanguage() via MediaWikiServices::getContentLanguage
so that LanguageConverter::getPreferredVariant can be applied.
== EditPage ==
This was doing similar "predicting" of the ParserOutput language to
create an empty preview placeholder for use by preview.js. Now that
ApiParse (via ParserOutput::getText) returns a usable element without
any secret "you magically know the right class, lang, and dir" contract,
this placeholder is no longer needed.
Test Plan:
* EditPage: Default preview
1. index.php?title=Main_Page&action=edit
2. Show preview
3. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
* EditPage: JS preview
1. Preferences > Editing > Show preview without reload
2. index.php?title=Main_Page&action=edit
3. Show preview
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
5. Type something and 'Show preview' again
6. Assert old element gone, new text is shown, and new element
attributes are the same as the above.
== McrUndoAction ==
Same as EditPage basically, but without the JS preview use case.
== DifferenceEngine ==
Test:
1. Open /w/index.php?title=Main_Page&diff=0
(this shows the latest diff, can do manually by viewing
/wiki/Main_Page, click "View history", click "Compare selected revisions")
2. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
3. Open /w/index.php?title=Main_Page&diff=0&action=render
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
== Special:ExpandTemplates ==
Test:
1. /wiki/Special:ExpandTemplates
2. Write "Hello".
3. "OK"
4. Assert <div class="mw-content-ltr mw-parser-output" lang=en dir=ltr>
Bug: T341244
Depends-On: Icd9c079f5896ee83d86b9c2699636dc81d25a14c
Depends-On: I4e7484b3b94f1cb6062e7cef9f20626b650bb4b1
Depends-On: I90b88f3b3a3bbeba4f48d118f92f54864997e105
Change-Id: Ib130a055e46764544af0f1a46d2bc2b3a7ee85b7
2023-10-04 04:45:07 +00:00
|
|
|
$this->assertSame( 1, preg_match_all( '#class="[^"]*mw-parser-output"#', $combinedHtml ) );
|
|
|
|
|
$this->assertStringNotContainsString( 'mw-parser-output"', $combinedOutput->getRawText() );
|
2018-08-07 16:52:40 +00:00
|
|
|
|
|
|
|
|
$combinedLinks = $combinedOutput->getLinks();
|
|
|
|
|
$mainLinks = $mainOutput->getLinks();
|
|
|
|
|
$auxLinks = $auxOutput->getLinks();
|
|
|
|
|
$this->assertTrue( isset( $combinedLinks[NS_MAIN]['Kittens'] ), 'links from main slot' );
|
|
|
|
|
$this->assertTrue( isset( $combinedLinks[NS_MAIN]['Goats'] ), 'links from aux slot' );
|
|
|
|
|
$this->assertFalse( isset( $mainLinks[NS_MAIN]['Goats'] ), 'no aux links in main' );
|
|
|
|
|
$this->assertFalse( isset( $auxLinks[NS_MAIN]['Kittens'] ), 'no main links in aux' );
|
2023-11-13 15:54:00 +00:00
|
|
|
|
|
|
|
|
// Same tests with Parsoid
|
|
|
|
|
// T351026: We should get only main slot output in the combined output.
|
|
|
|
|
// T351113 will have to update this test.
|
|
|
|
|
$options = ParserOptions::newFromAnon();
|
|
|
|
|
$options->setUseParsoid();
|
|
|
|
|
$rr = $renderer->getRenderedRevision( $rev, $options );
|
|
|
|
|
|
|
|
|
|
$combinedOutput = $rr->getRevisionParserOutput();
|
|
|
|
|
$mainOutput = $rr->getSlotParserOutput( SlotRecord::MAIN );
|
|
|
|
|
$combinedHtml = $combinedOutput->getText();
|
|
|
|
|
$this->assertSame( $combinedHtml, $mainOutput->getText() );
|
|
|
|
|
$this->assertSame( $combinedOutput->getLinks(), $mainOutput->getLinks() );
|
|
|
|
|
$this->assertStringContainsString( 'class="mw-content-ltr mw-parser-output"', $combinedHtml );
|
|
|
|
|
$this->assertStringContainsString( 'Kittens', $combinedHtml );
|
|
|
|
|
$this->assertStringNotContainsString( 'Goats', $combinedHtml );
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-13 20:33:31 +00:00
|
|
|
public function testGetRenderedRevision_noHtml() {
|
2021-10-21 12:04:01 +00:00
|
|
|
$content = new WikitextContent( 'Whatever' );
|
|
|
|
|
|
|
|
|
|
/** @var MockObject|ContentRenderer $mockContentRenderer */
|
|
|
|
|
$mockContentRenderer = $this->getMockBuilder( ContentRenderer::class )
|
2021-03-20 15:18:58 +00:00
|
|
|
->onlyMethods( [ 'getParserOutput' ] )
|
2021-10-21 12:04:01 +00:00
|
|
|
->disableOriginalConstructor()
|
2018-08-13 20:33:31 +00:00
|
|
|
->getMock();
|
2021-10-21 12:04:01 +00:00
|
|
|
$mockContentRenderer->method( 'getParserOutput' )
|
|
|
|
|
->willReturnCallback( function ( Content $content, PageReference $page, $revId = null,
|
2018-08-13 20:33:31 +00:00
|
|
|
ParserOptions $options = null, $generateHtml = true
|
|
|
|
|
) {
|
|
|
|
|
if ( !$generateHtml ) {
|
|
|
|
|
return new ParserOutput( null );
|
|
|
|
|
} else {
|
|
|
|
|
$this->fail( 'Should not be called with $generateHtml == true' );
|
|
|
|
|
return null; // never happens, make analyzer happy
|
|
|
|
|
}
|
|
|
|
|
} );
|
|
|
|
|
|
2021-10-21 12:04:01 +00:00
|
|
|
$renderer = $this->newRevisionRenderer( 100, false, $mockContentRenderer );
|
2018-08-13 20:33:31 +00:00
|
|
|
|
2021-09-29 14:02:36 +00:00
|
|
|
$rev = new MutableRevisionRecord( $this->fakePage );
|
2021-10-21 12:04:01 +00:00
|
|
|
$rev->setContent( SlotRecord::MAIN, $content );
|
|
|
|
|
$rev->setContent( 'aux', $content );
|
2018-08-13 20:33:31 +00:00
|
|
|
|
|
|
|
|
// NOTE: we are testing the private combineSlotOutput() callback here.
|
|
|
|
|
$rr = $renderer->getRenderedRevision( $rev );
|
|
|
|
|
|
2018-09-24 21:10:08 +00:00
|
|
|
$output = $rr->getSlotParserOutput( SlotRecord::MAIN, [ 'generate-html' => false ] );
|
2018-08-13 20:33:31 +00:00
|
|
|
$this->assertFalse( $output->hasText(), 'hasText' );
|
|
|
|
|
|
|
|
|
|
$output = $rr->getRevisionParserOutput( [ 'generate-html' => false ] );
|
|
|
|
|
$this->assertFalse( $output->hasText(), 'hasText' );
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-07 16:52:40 +00:00
|
|
|
}
|