2022-06-17 14:00:27 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Config\ServiceOptions;
|
2022-12-12 21:04:31 +00:00
|
|
|
use MediaWiki\Hook\ParserLogLinterDataHook;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Json\JsonCodec;
|
2022-10-05 18:47:44 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
|
|
|
|
use MediaWiki\MainConfigSchema;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Parser\ParserCacheFactory;
|
2023-03-09 15:52:24 +00:00
|
|
|
use MediaWiki\Parser\ParserOutputFlags;
|
2022-09-26 06:18:43 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Parser\Parsoid\ParsoidOutputAccess;
|
2022-12-07 22:09:58 +00:00
|
|
|
use MediaWiki\Rest\HttpException;
|
2022-08-30 10:26:39 +00:00
|
|
|
use MediaWiki\Revision\MutableRevisionRecord;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Revision\RevisionAccessException;
|
2023-06-13 18:34:25 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2022-06-17 14:00:27 +00:00
|
|
|
use MediaWiki\Revision\SlotRecord;
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
use Wikimedia\Parsoid\Config\PageConfig;
|
2023-03-09 15:52:24 +00:00
|
|
|
use Wikimedia\Parsoid\Core\ContentMetadataCollector;
|
2022-06-17 14:00:27 +00:00
|
|
|
use Wikimedia\Parsoid\Core\PageBundle;
|
|
|
|
|
use Wikimedia\Parsoid\Parsoid;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class ParsoidOutputAccessTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
private const WIKITEXT = 'Hello \'\'\'Parsoid\'\'\'!';
|
|
|
|
|
private const MOCKED_HTML = 'mocked HTML';
|
2022-10-05 18:47:44 +00:00
|
|
|
private const ENV_OPTS = [ 'pageBundle' => true ];
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $expectedCalls
|
|
|
|
|
*
|
|
|
|
|
* @return MockObject|Parsoid
|
|
|
|
|
*/
|
|
|
|
|
private function newMockParsoid( $expectedCalls = 1 ) {
|
|
|
|
|
$parsoid = $this->createNoOpMock( Parsoid::class, [ 'wikitext2html' ] );
|
|
|
|
|
$parsoid->expects( $this->exactly( $expectedCalls ) )->method( 'wikitext2html' )->willReturnCallback(
|
2023-03-09 15:52:24 +00:00
|
|
|
static function ( PageConfig $pageConfig, $options, &$headers, ?ContentMetadataCollector $metadata = null ) {
|
2022-06-17 14:00:27 +00:00
|
|
|
$wikitext = $pageConfig->getRevisionContent()->getContent( SlotRecord::MAIN );
|
2023-03-09 15:52:24 +00:00
|
|
|
if ( $metadata !== null ) {
|
|
|
|
|
$metadata->setExtensionData( 'my-key', 'my-data' );
|
|
|
|
|
$metadata->setPageProperty( 'forcetoc', '' );
|
|
|
|
|
$metadata->setOutputFlag( ParserOutputFlags::NO_GALLERY );
|
|
|
|
|
}
|
2022-09-09 09:32:17 +00:00
|
|
|
|
|
|
|
|
return new PageBundle(
|
|
|
|
|
self::MOCKED_HTML . ' of ' . $wikitext,
|
|
|
|
|
[ 'parsoid-data' ],
|
|
|
|
|
[ 'mw-data' ],
|
|
|
|
|
Parsoid::defaultHTMLVersion(),
|
|
|
|
|
[ 'content-language' => 'en' ],
|
|
|
|
|
$pageConfig->getContentModel()
|
|
|
|
|
);
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return $parsoid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $expectedParses
|
|
|
|
|
* @param array $parsoidCacheConfig
|
2022-12-08 07:34:25 +00:00
|
|
|
* @param BagOStuff|null $parserCacheBag
|
2022-06-17 14:00:27 +00:00
|
|
|
*
|
|
|
|
|
* @return ParsoidOutputAccess
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2022-12-08 07:34:25 +00:00
|
|
|
private function getParsoidOutputAccessWithCache(
|
|
|
|
|
$expectedParses,
|
|
|
|
|
$parsoidCacheConfig = [],
|
|
|
|
|
?BagOStuff $parserCacheBag = null
|
|
|
|
|
) {
|
2022-06-17 14:00:27 +00:00
|
|
|
$stats = new NullStatsdDataFactory();
|
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
|
|
|
|
|
$parsoidCacheConfig += [
|
|
|
|
|
'CacheThresholdTime' => 0,
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
$parserCacheFactoryOptions = new ServiceOptions( ParserCacheFactory::CONSTRUCTOR_OPTIONS, [
|
|
|
|
|
'CacheEpoch' => '20200202112233',
|
|
|
|
|
'OldRevisionParserCacheExpireTime' => 60 * 60,
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$parserCacheFactory = new ParserCacheFactory(
|
2022-12-08 07:34:25 +00:00
|
|
|
$parserCacheBag ?: new HashBagOStuff(),
|
2022-06-17 14:00:27 +00:00
|
|
|
new WANObjectCache( [ 'cache' => new HashBagOStuff(), ] ),
|
|
|
|
|
$this->createHookContainer(),
|
|
|
|
|
new JsonCodec(),
|
|
|
|
|
new NullStatsdDataFactory(),
|
|
|
|
|
new NullLogger(),
|
|
|
|
|
$parserCacheFactoryOptions,
|
|
|
|
|
$services->getTitleFactory(),
|
|
|
|
|
$services->getWikiPageFactory()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return new ParsoidOutputAccess(
|
|
|
|
|
new ServiceOptions(
|
|
|
|
|
ParsoidOutputAccess::CONSTRUCTOR_OPTIONS,
|
2022-10-05 18:47:44 +00:00
|
|
|
[
|
|
|
|
|
'ParsoidCacheConfig' => $parsoidCacheConfig,
|
|
|
|
|
'ParsoidSettings' => MainConfigSchema::getDefaultValue(
|
|
|
|
|
MainConfigNames::ParsoidSettings
|
|
|
|
|
),
|
2022-10-07 14:52:50 +00:00
|
|
|
'ParsoidWikiID' => 'MyWiki'
|
2022-10-05 18:47:44 +00:00
|
|
|
]
|
2022-06-17 14:00:27 +00:00
|
|
|
),
|
|
|
|
|
$parserCacheFactory,
|
2022-09-13 15:52:44 +00:00
|
|
|
$services->getPageStore(),
|
2022-06-17 14:00:27 +00:00
|
|
|
$services->getRevisionLookup(),
|
|
|
|
|
$services->getGlobalIdGenerator(),
|
|
|
|
|
$stats,
|
|
|
|
|
$this->newMockParsoid( $expectedParses ),
|
2022-06-29 17:01:21 +00:00
|
|
|
$services->getParsoidSiteConfig(),
|
2022-12-08 22:01:49 +00:00
|
|
|
$services->getParsoidPageConfigFactory(),
|
|
|
|
|
$services->getContentHandlerFactory()
|
2022-06-17 14:00:27 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return ParserOptions
|
|
|
|
|
*/
|
|
|
|
|
private function getParserOptions() {
|
|
|
|
|
return ParserOptions::newFromAnon();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
private function getHtml( $value ) {
|
|
|
|
|
if ( $value instanceof StatusValue ) {
|
|
|
|
|
$value = $value->getValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $value instanceof ParserOutput ) {
|
|
|
|
|
$value = $value->getRawText();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$html = preg_replace( '/<!--.*?-->/s', '', $value );
|
2023-03-24 03:21:20 +00:00
|
|
|
$html = trim( preg_replace( '/[\r\n]{2,}/', "\n", $html ) );
|
|
|
|
|
$html = trim( preg_replace( '/\s{2,}/', ' ', $html ) );
|
2022-06-28 10:30:44 +00:00
|
|
|
return $html;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function assertContainsHtml( $needle, $actual, $msg = '' ) {
|
|
|
|
|
$this->assertNotNull( $actual );
|
|
|
|
|
|
|
|
|
|
if ( $actual instanceof StatusValue ) {
|
|
|
|
|
$this->assertStatusOK( $actual, 'isOK' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->assertStringContainsString( $needle, $this->getHtml( $actual ), $msg );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutputThrowsIfRevisionNotFound() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 0 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$this->expectException( RevisionAccessException::class );
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-29 17:01:21 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutputThrowsIfNotWikitext() {
|
2022-12-07 22:22:16 +00:00
|
|
|
$this->markTestSkipped( 'Broken by fix for T324711. Restore once we have T311728.' );
|
|
|
|
|
|
2022-06-29 17:01:21 +00:00
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 0 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$updater = $page->newPageUpdater( $this->getTestUser()->getUserIdentity() );
|
|
|
|
|
$updater->setContent( SlotRecord::MAIN, new JavaScriptContent( '{}' ) );
|
|
|
|
|
$updater->saveRevision( CommentStoreComment::newUnsavedComment( 'testing' ) );
|
|
|
|
|
|
2022-12-07 22:09:58 +00:00
|
|
|
// NOTE: The fact that we throw an HttpException here is a code smell.
|
|
|
|
|
// It should be a different exception which gets converted to an HttpException later.
|
|
|
|
|
$this->expectException( HttpException::class );
|
|
|
|
|
$this->expectExceptionCode( 400 );
|
2022-06-29 17:01:21 +00:00
|
|
|
$access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will return output.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParsoidRenderID
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutput() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 1 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
|
|
|
|
|
|
|
|
|
$output = $status->getValue();
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
// check that getParsoidRenderID() doesn't throw
|
|
|
|
|
$this->assertNotNull( $access->getParsoidRenderID( $output ) );
|
2022-06-28 09:49:36 +00:00
|
|
|
|
2022-09-26 06:18:43 +00:00
|
|
|
// Ensure that we can still create a valid instance of PageBundle from the ParserOutput
|
|
|
|
|
$pageBundle = PageBundleParserOutputConverter::pageBundleFromParserOutput( $output );
|
2022-06-28 09:49:36 +00:00
|
|
|
$this->assertSame( $output->getRawText(), $pageBundle->html );
|
|
|
|
|
|
2022-09-26 06:18:43 +00:00
|
|
|
// Ensure that the expected mw and parsoid fields are set in the PageBundle
|
2022-06-28 09:49:36 +00:00
|
|
|
$this->assertNotEmpty( $pageBundle->mw );
|
|
|
|
|
$this->assertNotEmpty( $pageBundle->parsoid );
|
2022-09-09 09:32:17 +00:00
|
|
|
$this->assertNotEmpty( $pageBundle->headers );
|
|
|
|
|
$this->assertNotEmpty( $pageBundle->version );
|
2023-03-09 15:52:24 +00:00
|
|
|
|
|
|
|
|
// Check that the metadata set by our mock parsoid is preserved
|
|
|
|
|
$this->checkMetadata( $output );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will place the generated output for the latest revision
|
|
|
|
|
* in the parsoid parser cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
2022-09-13 15:52:44 +00:00
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getCachedParserOutputInternal
|
2022-06-17 14:00:27 +00:00
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionIsCached() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 1 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2022-09-13 15:52:44 +00:00
|
|
|
// Get the ParserOutput from cache
|
|
|
|
|
$status = $access->getCachedParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput from cache, without supplying a PageRecord
|
|
|
|
|
$status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
// Get the ParserOutput again, this should not trigger a new parse.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will force a parse since we know that
|
|
|
|
|
* the revision is not in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionWithForceParse() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 2 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput again, this should trigger a new parse
|
|
|
|
|
// since we're forcing it to.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput(
|
2022-06-17 14:00:27 +00:00
|
|
|
$page,
|
|
|
|
|
$parserOptions,
|
|
|
|
|
null,
|
|
|
|
|
ParsoidOutputAccess::OPT_FORCE_PARSE
|
|
|
|
|
);
|
2022-06-28 10:30:44 +00:00
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-12 21:04:31 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that the ParserLogLinterData hook is triggered when the OPT_LOG_LINT_DATA
|
|
|
|
|
* flag is set.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionWithLogLint() {
|
|
|
|
|
$this->overrideConfigValue( MainConfigNames::ParsoidSettings, [
|
|
|
|
|
'linting' => true
|
|
|
|
|
] );
|
|
|
|
|
|
|
|
|
|
$mockHandler = $this->createMock( ParserLogLinterDataHook::class );
|
|
|
|
|
$mockHandler->expects( $this->once() ) // this is the critical assertion in this test case!
|
|
|
|
|
->method( 'onParserLogLinterData' );
|
|
|
|
|
|
|
|
|
|
$this->setTemporaryHook(
|
|
|
|
|
'ParserLogLinterData',
|
|
|
|
|
$mockHandler
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Use the real ParsoidOutputAccess, so we use the real hook container.
|
|
|
|
|
$access = $this->getServiceContainer()->getParsoidOutputAccess();
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions, null, ParsoidOutputAccess::OPT_LOG_LINT_DATA );
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-24 19:58:56 +00:00
|
|
|
/**
|
|
|
|
|
* Tests that getParserOutput() will force a parse since we know that
|
|
|
|
|
* the revision is not in the cache.
|
|
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public function testLatestRevisionWithNoUpdateCache() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 2 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
|
|
|
|
|
$status = $access->getParserOutput(
|
|
|
|
|
$page,
|
|
|
|
|
$parserOptions,
|
|
|
|
|
null,
|
|
|
|
|
ParsoidOutputAccess::OPT_NO_UPDATE_CACHE
|
|
|
|
|
);
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-11-24 19:58:56 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput again, this should trigger a new parse
|
|
|
|
|
// since we suppressed caching above.
|
|
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-11-24 19:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-08 07:01:30 +00:00
|
|
|
/**
|
2022-12-08 07:34:25 +00:00
|
|
|
* Tests that getParserOutput() will not call Parsoid and will not write to ParserCache
|
|
|
|
|
* for unsupported content models.
|
2022-12-08 07:01:30 +00:00
|
|
|
*
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::getParserOutput
|
|
|
|
|
*/
|
2022-12-08 07:34:25 +00:00
|
|
|
public function testDummyContentForBadModel() {
|
|
|
|
|
// Expect no cache writes!
|
|
|
|
|
$cacheBag = $this->getMockBuilder( HashBagOStuff::class )
|
|
|
|
|
->onlyMethods( [ 'set', 'setMulti' ] )
|
|
|
|
|
->getMock();
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'set' );
|
|
|
|
|
$cacheBag->expects( $this->never() )->method( 'setMulti' );
|
|
|
|
|
|
|
|
|
|
// Expect no calls to parsoid!
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 0, [], $cacheBag );
|
2022-12-08 07:01:30 +00:00
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$this->editPage( $page, new JavaScriptContent( '"not wikitext"' ) );
|
|
|
|
|
|
|
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
2022-12-08 07:34:25 +00:00
|
|
|
$this->assertContainsHtml( 'Dummy output', $status );
|
2022-12-08 07:01:30 +00:00
|
|
|
|
2023-01-09 10:45:25 +00:00
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
|
|
|
|
$this->assertSame( '0/dummy-output', $parserOutput->getExtensionData( 'parsoid-render-id' ) );
|
|
|
|
|
|
2023-01-10 11:40:39 +00:00
|
|
|
$expTime = $parserOutput->getCacheExpiry();
|
|
|
|
|
$this->assertSame( 0, $expTime );
|
|
|
|
|
|
2022-12-08 07:01:30 +00:00
|
|
|
// Get the ParserOutput again, this should trigger a new parse
|
|
|
|
|
// since we suppressed caching for non-wikitext content.
|
|
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
2022-12-08 07:34:25 +00:00
|
|
|
$this->assertContainsHtml( 'Dummy output', $status );
|
2022-12-08 07:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideCacheThresholdData() {
|
2022-06-17 14:00:27 +00:00
|
|
|
return [
|
|
|
|
|
yield "fast parse" => [ 1, 2 ], // high threshold, no caching
|
|
|
|
|
yield "slow parse" => [ 0, 1 ], // low threshold, caching
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideCacheThresholdData()
|
|
|
|
|
*/
|
|
|
|
|
public function testHtmlWithCacheThreshold(
|
|
|
|
|
$cacheThresholdTime,
|
|
|
|
|
$expectedCalls
|
|
|
|
|
) {
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
$parsoidCacheConfig = [
|
|
|
|
|
'CacheThresholdTime' => $cacheThresholdTime
|
|
|
|
|
];
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( $expectedCalls, $parsoidCacheConfig );
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOldRevisionIsCached() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 1 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$status1 = $this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
$rev = $status1->getValue()['revision-record'];
|
|
|
|
|
|
|
|
|
|
// Make an edit so that the revision we're getting output
|
|
|
|
|
// for below is not the current revision.
|
|
|
|
|
$this->editPage( $page, 'Second revision' );
|
|
|
|
|
|
|
|
|
|
$access->getParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
|
2022-09-13 15:52:44 +00:00
|
|
|
// Get the ParserOutput from cache, using revision object
|
|
|
|
|
$status = $access->getCachedParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Get the ParserOutput from cache, using revision ID
|
|
|
|
|
$status = $access->getCachedParserOutput( $page->getTitle(), $parserOptions, $rev->getId() );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
// Get the ParserOutput again, this should not trigger a new parse.
|
2022-06-28 10:30:44 +00:00
|
|
|
$status = $access->getParserOutput( $page, $parserOptions, $rev );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status );
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetParserOutputWithOldRevision() {
|
|
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 2 );
|
|
|
|
|
$parserOptions = $this->getParserOptions();
|
|
|
|
|
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$status1 = $this->editPage( $page, self::WIKITEXT );
|
|
|
|
|
$rev1 = $status1->getValue()['revision-record'];
|
|
|
|
|
|
|
|
|
|
$this->editPage( $page, 'Second revision' );
|
|
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status2 = $access->getParserOutput( $page, $parserOptions );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of Second revision', $status2 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status2 );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
2022-06-28 10:30:44 +00:00
|
|
|
$status1 = $access->getParserOutput( $page, $parserOptions, $rev1 );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status1 );
|
2022-09-13 15:52:44 +00:00
|
|
|
|
|
|
|
|
// Again, using just the revision ID
|
|
|
|
|
$status1 = $access->getParserOutput( $page, $parserOptions, $rev1->getId() );
|
|
|
|
|
$this->assertContainsHtml( self::MOCKED_HTML . ' of ' . self::WIKITEXT, $status1 );
|
2023-03-09 15:52:24 +00:00
|
|
|
$this->checkMetadata( $status1 );
|
2022-06-17 14:00:27 +00:00
|
|
|
|
|
|
|
|
// check that getParsoidRenderID() doesn't throw
|
2022-06-28 10:30:44 +00:00
|
|
|
$output1 = $status1->getValue();
|
2022-06-17 14:00:27 +00:00
|
|
|
$this->assertNotNull( $access->getParsoidRenderID( $output1 ) );
|
|
|
|
|
}
|
2022-06-29 17:01:21 +00:00
|
|
|
|
2023-03-23 11:36:19 +00:00
|
|
|
public static function provideSupportsContentModels() {
|
2022-06-29 17:01:21 +00:00
|
|
|
yield [ CONTENT_MODEL_WIKITEXT, true ];
|
|
|
|
|
yield [ CONTENT_MODEL_JSON, true ];
|
|
|
|
|
yield [ CONTENT_MODEL_JAVASCRIPT, false ];
|
2022-12-08 22:01:49 +00:00
|
|
|
yield [ 'with-text', true ];
|
2022-06-29 17:01:21 +00:00
|
|
|
yield [ 'xyzzy', false ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideSupportsContentModels
|
|
|
|
|
*/
|
|
|
|
|
public function testSupportsContentModel( $model, $expected ) {
|
2022-12-08 22:01:49 +00:00
|
|
|
$contentHandlers = $this->getConfVar( 'ContentHandlers' );
|
|
|
|
|
$this->overrideConfigValue( 'ContentHandlers', [
|
|
|
|
|
'with-text' => [ 'factory' => static function () {
|
|
|
|
|
return new TextContentHandler( 'with-text', [ CONTENT_FORMAT_WIKITEXT, 'plain/test' ] );
|
|
|
|
|
} ],
|
|
|
|
|
] + $contentHandlers );
|
|
|
|
|
|
2022-06-29 17:01:21 +00:00
|
|
|
$access = $this->getParsoidOutputAccessWithCache( 0 );
|
|
|
|
|
$this->assertSame( $expected, $access->supportsContentModel( $model ) );
|
|
|
|
|
}
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parse
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseInternal
|
|
|
|
|
*/
|
|
|
|
|
public function testParseWithPageRecordAndNoRevision() {
|
|
|
|
|
$pageRecord = $this->getExistingTestPage( __METHOD__ )->toPageRecord();
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2022-10-05 18:47:44 +00:00
|
|
|
$status = $parsoidOutputAccess->parse( $pageRecord, $pOpts, self::ENV_OPTS, null );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
2022-08-30 10:26:39 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getExtensionData( 'parsoid-render-id' ) );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-09 15:52:24 +00:00
|
|
|
private function checkMetadata( $output ) {
|
|
|
|
|
$parserOutput = $output instanceof StatusValue ? $output->getValue() : $output;
|
|
|
|
|
|
|
|
|
|
// Check the metadata added by ::newMockParsoid() is preserved
|
|
|
|
|
$this->assertSame( 'my-data', $parserOutput->getExtensionData( 'my-key' ) );
|
|
|
|
|
$this->assertSame( '', $parserOutput->getPageProperty( 'forcetoc' ) );
|
|
|
|
|
$this->assertSame( true, $parserOutput->getOutputFlag( ParserOutputFlags::NO_GALLERY ) );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 10:26:39 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parse
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseInternal
|
|
|
|
|
*/
|
|
|
|
|
public function testParseWithPageRecordAndRevision() {
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
$pageRecord = $page->toPageRecord();
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
$revRecord = $page->getRevisionRecord();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2022-10-05 18:47:44 +00:00
|
|
|
$status = $parsoidOutputAccess->parse( $pageRecord, $pOpts, self::ENV_OPTS, $revRecord );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
2022-08-30 10:26:39 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getExtensionData( 'parsoid-render-id' ) );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 14:27:01 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parse
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseInternal
|
|
|
|
|
*/
|
|
|
|
|
public function testParseWithPageIdentityAndRevisionId() {
|
|
|
|
|
$page = $this->getExistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
$revId = $page->getLatest();
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
|
|
|
|
$status = $parsoidOutputAccess->parse( $page->getTitle(), $pOpts, self::ENV_OPTS, $revId );
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
2022-10-07 14:27:01 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getExtensionData( 'parsoid-render-id' ) );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 10:26:39 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parse
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parseInternal
|
|
|
|
|
*/
|
|
|
|
|
public function testParseWithNonExistingPageAndFakeRevision() {
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
// Create a fake revision record
|
|
|
|
|
$revRecord = new MutableRevisionRecord( $page->getTitle() );
|
|
|
|
|
$revRecord->setId( 0 );
|
|
|
|
|
$revRecord->setPageId( $page->getId() );
|
|
|
|
|
$revRecord->setContent(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
new WikitextContent( 'test' )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
2022-10-05 18:47:44 +00:00
|
|
|
$status = $parsoidOutputAccess->parse( $page->getTitle(), $pOpts, self::ENV_OPTS, $revRecord );
|
2022-08-30 10:26:39 +00:00
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
$this->assertInstanceOf( ParserOutput::class, $status->getValue() );
|
|
|
|
|
|
|
|
|
|
/** @var ParserOutput $parserOutput */
|
|
|
|
|
$parserOutput = $status->getValue();
|
2022-05-27 16:38:32 +00:00
|
|
|
$this->assertStringContainsString( __METHOD__, $parserOutput->getRawText() );
|
2022-08-30 10:26:39 +00:00
|
|
|
$this->assertNotEmpty( $parserOutput->getExtensionData( 'parsoid-render-id' ) );
|
|
|
|
|
// The revision ID is set to 0, so that's what is in the cache.
|
|
|
|
|
$this->assertSame( 0, $parserOutput->getCacheRevisionId() );
|
|
|
|
|
$this->assertNotEmpty( $parserOutput->getCacheTime() );
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 18:34:25 +00:00
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Parser\Parsoid\ParsoidOutputAccess::parse
|
|
|
|
|
*/
|
|
|
|
|
public function testParseDeletedRevision() {
|
|
|
|
|
$page = $this->getNonexistingTestPage( __METHOD__ );
|
|
|
|
|
$pOpts = ParserOptions::newFromAnon();
|
|
|
|
|
|
|
|
|
|
// Create a fake revision record
|
|
|
|
|
$revRecord = new MutableRevisionRecord( $page->getTitle() );
|
|
|
|
|
$revRecord->setId( 0 );
|
|
|
|
|
$revRecord->setPageId( $page->getId() );
|
|
|
|
|
$revRecord->setContent(
|
|
|
|
|
SlotRecord::MAIN,
|
|
|
|
|
new WikitextContent( 'test' )
|
|
|
|
|
);
|
|
|
|
|
// Induce a RevisionAccessException
|
|
|
|
|
$revRecord->setVisibility( RevisionRecord::DELETED_TEXT );
|
|
|
|
|
|
|
|
|
|
$parsoidOutputAccess = $this->getServiceContainer()->getParsoidOutputAccess();
|
|
|
|
|
$status = $parsoidOutputAccess->parse( $page->getTitle(), $pOpts, self::ENV_OPTS, $revRecord );
|
|
|
|
|
|
|
|
|
|
$this->assertInstanceOf( Status::class, $status );
|
|
|
|
|
$this->assertTrue( !$status->isOK() );
|
|
|
|
|
$this->assertSame(
|
|
|
|
|
[ 'parsoid-revision-access', 'Not an available content version.' ],
|
|
|
|
|
$status->getErrorsArray()[0] ?? []
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-07 14:27:01 +00:00
|
|
|
/**
|
|
|
|
|
* Mock the language class based on a language code.
|
|
|
|
|
*
|
|
|
|
|
* @param string $langCode
|
|
|
|
|
*
|
|
|
|
|
* @return Language|Language&MockObject|MockObject
|
|
|
|
|
*/
|
|
|
|
|
private function getLanguageMock( string $langCode ) {
|
|
|
|
|
$language = $this->createMock( Language::class );
|
|
|
|
|
$language->method( 'getCode' )->willReturn( $langCode );
|
Use Bcp47Code when interfacing with Parsoid
It is very easy for developers and maintainers to mix up "internal
MediaWiki language codes" and "BCP-47 language codes"; the latter are
standards-compliant and used in web protocols like HTTP, HTML, and
SVG; but much of WMF production is very dependent on historical codes
used by MediaWiki which in some cases predate the IANA standardized
name for the language in question.
Phan and other static checking tools aren't much help distinguishing
BCP-47 from internal codes when both are represented with the PHP
string type, so the wikimedia/bcp-47-code package introduced a very
lightweight wrapper type in order to uniquely identify BCP-47 codes.
Language implements Bcp47Code, and LanguageFactory::getLanguage() is
an easy way to convert (or downcast) between Bcp47Code and Language
objects.
This patch updates the Parsoid integration code and the associated
REST handlers to use Bcp47Code in APIs so that the standalone Parsoid
library does not need to know anything about MediaWiki-internal codes.
The principle has been, first, to try to convert a string to a
Bcp47Code as soon as possible and as close to the original input as
possible, so it is easy to see *why* a given string is a BCP-47 code
(usually, because it is coming from HTTP/HTML/etc) and we're not stuck
deep inside some method trying to figure out where a string we're
given is coming from and therefore what sort of string code it might
be. Second, we've added explicit compatibility code to accept
MediaWiki internal codes and convert them to Bcp47Code for backward
compatibility with existing clients, using the @internal
LanguageCode::normalizeNonstandardCodeAndWarn() method. The intention
is to gradually remove these backward compatibility thunks and replace
them with HTTP 400 errors or wfDeprecated messages in order to
identify and repair callers who are incorrectly using
non-standard-compliant language codes in web standards
(HTTP/HTML/SVG/etc).
Finally, maintaining a code as a Bcp47Code and not immediately
converting to Language helps us delay or even avoid full loading of a
Language object in some cases, which is another reason to occasionally
push Bcp47Code (instead of Language) down the call stack.
Bug: T327379
Depends-On: I830867d58f8962d6a57be16ce3735e8384f9ac1c
Change-Id: I982e0df706a633b05dcc02b5220b737c19adc401
2022-11-04 17:29:23 +00:00
|
|
|
$language->method( 'getDir' )->willReturn( 'ltr' );
|
|
|
|
|
$bcp47 = LanguageCode::bcp47( $langCode );
|
|
|
|
|
$language
|
|
|
|
|
->method( 'getHtmlCode' )
|
|
|
|
|
->willReturn( $bcp47 );
|
|
|
|
|
$language
|
|
|
|
|
->method( 'toBcp47Code' )
|
|
|
|
|
->willReturn( $bcp47 );
|
2022-10-07 14:27:01 +00:00
|
|
|
return $language;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @return Generator */
|
|
|
|
|
public function provideParserOptionsWithLanguageOverride() {
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( null );
|
|
|
|
|
yield 'ParserOptions with no language' => [ $parserOptions, 'en' ];
|
|
|
|
|
|
|
|
|
|
$langCode = 'de';
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( $this->getLanguageMock( $langCode ) );
|
|
|
|
|
yield 'ParserOptions for "de" language' => [ $parserOptions, $langCode ];
|
|
|
|
|
|
|
|
|
|
$langCode = 'ar';
|
|
|
|
|
$parserOptions = $this->createMock( ParserOptions::class );
|
|
|
|
|
$parserOptions->method( 'getTargetLanguage' )
|
|
|
|
|
->willReturn( $this->getLanguageMock( $langCode ) );
|
|
|
|
|
yield 'ParserOptions for "ar" language' => [ $parserOptions, $langCode ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-13 21:30:21 +00:00
|
|
|
* @covers \MediaWiki\Rest\Handler\Helper\HtmlOutputRendererHelper::getParserOutput
|
2022-10-07 14:27:01 +00:00
|
|
|
* @dataProvider provideParserOptionsWithLanguageOverride
|
|
|
|
|
*/
|
|
|
|
|
public function testGetParserOutputWithLanguageOverride( $parserOptions, $expectedLangCode ) {
|
|
|
|
|
$services = $this->getServiceContainer();
|
|
|
|
|
$parserOutputAccess = $services->getParsoidOutputAccess();
|
|
|
|
|
|
2023-08-09 15:43:50 +00:00
|
|
|
$content = 'Test content for ' . __METHOD__;
|
|
|
|
|
$page = Title::makeTitle( NS_MAIN, 'TestGetParserOutputWithLanguageOverride' );
|
|
|
|
|
$this->editPage( $page, $content );
|
2022-10-07 14:27:01 +00:00
|
|
|
|
|
|
|
|
$status = $parserOutputAccess->getParserOutput( $page, $parserOptions );
|
|
|
|
|
|
|
|
|
|
$this->assertTrue( $status->isOK() );
|
|
|
|
|
|
|
|
|
|
// assert dummy content in parsoid output HTML
|
2022-05-27 16:38:32 +00:00
|
|
|
$html = $status->getValue()->getRawText();
|
2023-08-09 15:43:50 +00:00
|
|
|
$this->assertStringContainsString( $content, $html );
|
2022-10-07 14:27:01 +00:00
|
|
|
|
|
|
|
|
if ( $parserOptions->getTargetLanguage() !== null ) {
|
|
|
|
|
$targetLanguage = $parserOptions->getTargetLanguage()->getCode();
|
|
|
|
|
$this->assertSame( $expectedLangCode, $targetLanguage );
|
|
|
|
|
$this->assertInstanceOf( Language::class, $parserOptions->getTargetLanguage() );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertNull( $parserOptions->getTargetLanguage() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assert the page language in parsoid output HTML
|
|
|
|
|
$this->assertStringContainsString( 'lang="' . $expectedLangCode . '"', $html );
|
|
|
|
|
$this->assertStringContainsString( 'content="' . $expectedLangCode . '"', $html );
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-17 14:00:27 +00:00
|
|
|
}
|