This reverts Ib73841bcc6c101bbe8a76f76dc81553290726039 and re-applies I55a58f9824329893575a532cd10b9422ededb9ba with some changes: The source variant is passed in explicitly. More complete handling of the input language will be added in a follow-up. Original description: This class is used in ParsoidHandler::languageConversion It uses the Parsoid to perform the actual conversion of the content to a language variant. The source language is determined using the PageBundle or the page language from the Title. To encapsulate Parsoid related concepts, the class has the ability to create Parsoid\Config\PageConfig if not provided. Bug: T317019 Change-Id: Ida1a040628c26ac2ef108b0c90a3d3285a493b0e
101 lines
3.2 KiB
PHP
101 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace MediaWiki\Parser\Parsoid;
|
|
|
|
use MediaWikiUnitTestCase;
|
|
use ParserOutput;
|
|
use Wikimedia\Parsoid\Core\PageBundle;
|
|
|
|
/**
|
|
* @covers MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter
|
|
*/
|
|
class PageBundleParserOutputConverterTest extends MediaWikiUnitTestCase {
|
|
/** @dataProvider provideParserOutputFromPageBundle */
|
|
public function testParserOutputFromPageBundle( PageBundle $pageBundle ) {
|
|
$output = PageBundleParserOutputConverter::parserOutputFromPageBundle( $pageBundle );
|
|
$this->assertSame( $pageBundle->html, $output->getRawText() );
|
|
|
|
$extensionData = $output->getExtensionData( PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY );
|
|
$this->assertSame( $pageBundle->mw, $extensionData['mw'] );
|
|
$this->assertSame( $pageBundle->parsoid, $extensionData['parsoid'] );
|
|
$this->assertSame( $pageBundle->headers, $extensionData['headers'] );
|
|
$this->assertSame( $pageBundle->headers['content-language'], $extensionData['headers']['content-language'] );
|
|
$this->assertSame( $pageBundle->version, $extensionData['version'] );
|
|
}
|
|
|
|
public function provideParserOutputFromPageBundle() {
|
|
yield 'should convert PageBundle containing data-parsoid and data-mw' => [
|
|
new PageBundle(
|
|
'html content',
|
|
[ 'ids' => '1.33' ],
|
|
[ 'ids' => '1.33' ],
|
|
'1.x',
|
|
[ 'content-language' => 'abc' ]
|
|
)
|
|
];
|
|
|
|
yield 'should convert PageBundle that contains no data-parsoid or data-mw' => [
|
|
new PageBundle(
|
|
'html content',
|
|
[],
|
|
[],
|
|
'1.x',
|
|
[ 'content-language' => null ]
|
|
)
|
|
];
|
|
}
|
|
|
|
/** @dataProvider providePageBundleFromParserOutput */
|
|
public function testPageBundleFromParserOutput( ParserOutput $parserOutput ) {
|
|
$pageBundle = PageBundleParserOutputConverter::pageBundleFromParserOutput( $parserOutput );
|
|
|
|
$this->assertSame( $parserOutput->getRawText(), $pageBundle->html );
|
|
|
|
$extensionData = $parserOutput->getExtensionData(
|
|
PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY
|
|
);
|
|
$this->assertSame( $extensionData['parsoid'] ?? [], $pageBundle->parsoid );
|
|
$this->assertSame( $extensionData['mw'] ?? [], $pageBundle->mw );
|
|
$this->assertSame( $extensionData['version'] ?? null, $pageBundle->version );
|
|
$this->assertSame( $extensionData['headers'] ?? [], $pageBundle->headers );
|
|
$this->assertSame( $extensionData['headers']['content-language'], $pageBundle->headers['content-language'] );
|
|
}
|
|
|
|
public function providePageBundleFromParserOutput() {
|
|
yield 'should convert ParsoidOutput containing data-parsoid and data-mw' => [
|
|
$this->getParsoidOutput(
|
|
'hello world',
|
|
[
|
|
'parsoid' => [ 'ids' => '1.22' ],
|
|
'mw' => [],
|
|
'version' => '2.x',
|
|
'headers' => [ 'content-language' => 'xyz' ]
|
|
]
|
|
)
|
|
];
|
|
|
|
yield 'should convert ParsoidOutput that does not contain data-parsoid or data-mw' => [
|
|
$this->getParsoidOutput(
|
|
'hello world',
|
|
[
|
|
'parsoid' => null,
|
|
'mw' => null,
|
|
'version' => null,
|
|
'headers' => [ 'content-language' => null ]
|
|
]
|
|
)
|
|
];
|
|
}
|
|
|
|
private function getParsoidOutput(
|
|
string $rawText,
|
|
?array $pageBundleData
|
|
): ParserOutput {
|
|
$parserOutput = new ParserOutput( $rawText );
|
|
$parserOutput->setExtensionData(
|
|
PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY, $pageBundleData
|
|
);
|
|
|
|
return $parserOutput;
|
|
}
|
|
}
|