[ new PageBundle( '
test language conversion
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [] ), null, 'en-x-piglatin', null, '>esttay anguagelay onversioncay<' ]; yield 'Source variant is base language' => [ new PageBundle( 'test language conversion
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [ 'content-language' => 'en' ] ), null, 'en-x-piglatin', 'en', '>esttay anguagelay onversioncay<' ]; yield 'Source language is null' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [ 'content-language' => 'sr' ] ), null, 'sr-el', null, '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'Source language is explicit' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [ 'content-language' => 'sr' ] ), null, 'sr-el', 'sr-ec', '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'Content language is provided via HTTP header' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [ 'content-language' => 'sr-ec' ] ), 'sr', 'sr-el', 'sr-ec', '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'Content language is variant' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [] ), 'sr-ec', 'sr-el', null, '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'No content-language, but source variant provided' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [] ), null, 'sr-el', 'sr-ec', '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'Source variant is a base language code' => [ new PageBundle( 'Ово је тестна страница
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [] ), null, 'sr-el', 'sr', '>Ovo je testna stranica<', 'sr-el|sr-Latn' // sr-el is accepted for backwards compatibility for now ]; yield 'Base language does not support variants' => [ new PageBundle( 'Hallo Wereld
', [ '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( 'Siltemeniñ astın sız:
', [ 'parsoid-data' ], [ 'mw-data' ], Parsoid::defaultHTMLVersion(), [] ), null, 'kk-cyrl', 'kk-latn', 'Сілтеменің астын сыз:
', '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 ) { $languageVariantConverter->setPageLanguageOverride( $contentLanguage ); } $outputPageBundle = $languageVariantConverter->convertPageBundleVariant( $pageBundle, $target, $source ); $html = $outputPageBundle->toHtml(); $this->assertStringContainsString( $expected, $html ); if ( $expectedLanguage !== false ) { $this->assertMatchesRegularExpression( "@@", $html ); $this->assertMatchesRegularExpression( "@^$expectedLanguage@", $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 ) { $languageVariantConverter->setPageLanguageOverride( $contentLanguage ); } $modifiedParserOutput = $languageVariantConverter ->convertParserOutputVariant( $parserOutput, $target, $source ); $html = $modifiedParserOutput->getRawText(); $this->assertStringContainsString( $expected, $html ); if ( $expectedLanguage !== false ) { $this->assertMatchesRegularExpression( "@@", $html ); } $extensionData = $modifiedParserOutput ->getExtensionData( PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY ); $this->assertEquals( Parsoid::defaultHTMLVersion(), $extensionData['version'] ); if ( $expectedLanguage !== false ) { $this->assertMatchesRegularExpression( "@^$expectedLanguage@", $extensionData['headers']['content-language'] ); } } 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() ); } }