wiki.techinc.nl/tests/phpunit/integration/includes/parser/Parsoid/LanguageVariantConverterTest.php
Tim Starling 5e30a927bc tests: Make some PHPUnit data providers static
Just methods where adding "static" to the declaration was enough, I
didn't do anything with providers that used $this.

Initially by search and replace. There were many mistakes which I
found mostly by running the PHPStorm inspection which searches for
$this usage in a static method. Later I used the PHPStorm "make static"
action which avoids the more obvious mistakes.

Bug: T332865
Change-Id: I47ed6692945607dfa5c139d42edbd934fa4f3a36
2023-03-24 02:53:57 +00:00

273 lines
8.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace MediaWiki\Parser\Parsoid;
use Language;
use MediaWiki\MainConfigNames;
use MediaWiki\MainConfigSchema;
use MediaWiki\Page\PageIdentity;
use MediaWikiIntegrationTestCase;
use ParserOutput;
use Wikimedia\Bcp47Code\Bcp47CodeValue;
use Wikimedia\Parsoid\Core\PageBundle;
use Wikimedia\Parsoid\Parsoid;
/**
* @group Database
* @covers MediaWiki\Parser\Parsoid\LanguageVariantConverter
*/
class LanguageVariantConverterTest extends MediaWikiIntegrationTestCase {
public function setUp(): void {
$this->overrideConfigValue( 'UsePigLatinVariant', true );
}
public static function provideConvertPageBundleVariant() {
yield 'No source or base, rely on page language (en)' => [
new PageBundle(
'<p>test language conversion</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
null,
'en-x-piglatin',
null,
'>esttay anguagelay onversioncay<'
];
yield 'Source variant is base language' => [
new PageBundle(
'<p>test language conversion</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[ 'content-language' => 'en' ]
),
null,
'en-x-piglatin',
'en',
'>esttay anguagelay onversioncay<'
];
yield 'Source language is null' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[ 'content-language' => 'crh' ]
),
null,
'crh-Latn',
null,
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'Source language is explicit' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[ 'content-language' => 'crh' ]
),
null,
'crh-Latn',
'crh-Cyrl',
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'Content language is provided via HTTP header' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[ 'content-language' => 'crh-Cyrl' ]
),
'crh',
'crh-Latn',
'crh-Cyrl',
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'Content language is variant' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
'crh-Cyrl',
'crh-Latn',
null,
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'No content-language, but source variant provided' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
null,
'crh-Latn',
'crh-Cyrl',
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'Source variant is a base language code' => [
new PageBundle(
'<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
null,
'crh-Latn',
'crh',
'>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
];
yield 'Base language does not support variants' => [
new PageBundle(
'<p>Hallo Wereld</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
'nl',
'nl-be',
null,
'>Hallo Wereld<',
false // The output language is currently not indicated. Should be expected to be 'nl' in the future.
];
yield 'Variant conversion with fallback to core LanguageConverter' => [
new PageBundle(
'<p>Siltemeniñ astın sız:</p>',
[ 'parsoid-data' ],
[ 'mw-data' ],
Parsoid::defaultHTMLVersion(),
[]
),
null,
'kk-cyrl',
'kk-latn',
'<p>Сілтеменің астын сыз:</p>',
'kk-cyrl|kk-Cyrl'
];
}
/**
* @dataProvider provideConvertPageBundleVariant
*/
public function testConvertPageBundleVariant(
PageBundle $pageBundle,
$contentLanguage,
$target,
$source,
$expected,
$expectedLanguage = null
) {
if ( $expectedLanguage === null ) {
$expectedLanguage = $target;
}
$page = $this->getExistingTestPage();
$languageVariantConverter = $this->getLanguageVariantConverter( $page );
if ( $contentLanguage ) {
$contentLanguage = $this->getLanguageBcp47( $contentLanguage );
$languageVariantConverter->setPageLanguageOverride( $contentLanguage );
}
$target = $this->getLanguageBcp47( $target );
if ( $source ) {
$source = $this->getLanguageBcp47( $source );
}
$outputPageBundle = $languageVariantConverter->convertPageBundleVariant( $pageBundle, $target, $source );
$html = $outputPageBundle->toHtml();
$stripped = preg_replace( ':</?span[^>]*>:', '', $html );
$this->assertStringContainsString( $expected, $stripped );
if ( $expectedLanguage !== false ) {
$this->assertMatchesRegularExpression( "@<meta http-equiv=\"content-language\" content=\"($expectedLanguage)\"/>@i", $html );
$this->assertMatchesRegularExpression( "@^$expectedLanguage@i", $outputPageBundle->headers['content-language'] );
}
$this->assertEquals( Parsoid::defaultHTMLVersion(), $outputPageBundle->version );
}
public function provideConvertParserOutputVariant() {
foreach ( $this->provideConvertPageBundleVariant() as $name => $case ) {
$case[0] = PageBundleParserOutputConverter::parserOutputFromPageBundle( $case[0] );
yield $name => $case;
}
}
/**
* @dataProvider provideConvertParserOutputVariant
*/
public function testConvertParserOutputVariant(
ParserOutput $parserOutput,
$contentLanguage,
$target,
$source,
$expected,
$expectedLanguage = null
) {
if ( $expectedLanguage === null ) {
$expectedLanguage = $target;
}
$page = $this->getExistingTestPage();
$languageVariantConverter = $this->getLanguageVariantConverter( $page );
if ( $contentLanguage ) {
$contentLanguage = $this->getLanguageBcp47( $contentLanguage );
$languageVariantConverter->setPageLanguageOverride( $contentLanguage );
}
$target = $this->getLanguageBcp47( $target );
if ( $source ) {
$source = $this->getLanguageBcp47( $source );
}
// Set some misc metadata in $parserOutput so we can verify it was
// preserved.
$parserOutput->setExtensionData( 'my-key', 'my-data' );
$modifiedParserOutput = $languageVariantConverter
->convertParserOutputVariant( $parserOutput, $target, $source );
$this->assertSame( 'my-data', $modifiedParserOutput->getExtensionData( 'my-key' ) );
$html = $modifiedParserOutput->getRawText();
$stripped = preg_replace( ':</?span[^>]*>:', '', $html );
$this->assertStringContainsString( $expected, $stripped );
if ( $expectedLanguage !== false ) {
$this->assertMatchesRegularExpression( "@<meta http-equiv=\"content-language\" content=\"($expectedLanguage)\"/>@i", $html );
}
$extensionData = $modifiedParserOutput
->getExtensionData( PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY );
$this->assertEquals( Parsoid::defaultHTMLVersion(), $extensionData['version'] );
if ( $expectedLanguage !== false ) {
$this->assertMatchesRegularExpression( "@^$expectedLanguage@i", $extensionData['headers']['content-language'] );
}
}
private function getLanguageBcp47( $bcp47Code ): Language {
$languageFactory = $this->getServiceContainer()->getLanguageFactory();
return $languageFactory->getLanguage( new Bcp47CodeValue( $bcp47Code ) );
}
private function getLanguageVariantConverter( PageIdentity $pageIdentity ): LanguageVariantConverter {
return new LanguageVariantConverter(
$pageIdentity,
$this->getServiceContainer()->getParsoidPageConfigFactory(),
$this->getServiceContainer()->getService( '_Parsoid' ),
MainConfigSchema::getDefaultValue( MainConfigNames::ParsoidSettings ),
$this->getServiceContainer()->getParsoidSiteConfig(),
$this->getServiceContainer()->getTitleFactory(),
$this->getServiceContainer()->getLanguageConverterFactory(),
$this->getServiceContainer()->getLanguageFactory()
);
}
}