Debian Codegraph @

tests/phpunit/integration/includes/parser/Parsoid/LanguageVariantConverterTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Parser\Parsoid;
4
5 use MediaWiki\Language\Language;
6 use MediaWiki\MainConfigNames;
7 use MediaWiki\Page\PageIdentity;
8 use MediaWiki\Parser\ParserOutput;
9 use MediaWiki\Parser\Parsoid\LanguageVariantConverter;
10 use MediaWiki\Parser\Parsoid\PageBundleParserOutputConverter;
11 use MediaWikiIntegrationTestCase;
12 use Wikimedia\Bcp47Code\Bcp47CodeValue;
13 use Wikimedia\Parsoid\Core\PageBundle;
14 use Wikimedia\Parsoid\Parsoid;
15
16 /**
17 * @group Database
18 * @covers \MediaWiki\Parser\Parsoid\LanguageVariantConverter
19 */
20 class LanguageVariantConverterTest extends MediaWikiIntegrationTestCase {
21 public function setUp(): void {
22 $this->overrideConfigValue( MainConfigNames::UsePigLatinVariant, true );
23 }
24
25 public static function provideConvertPageBundleVariant() {
26 yield 'No source or base, rely on page language (en)' => [
27 new PageBundle(
28 '<p>test language conversion</p>',
29 [ 'parsoid-data' ],
30 [ 'mw-data' ],
31 Parsoid::defaultHTMLVersion(),
32 []
33 ),
34 null,
35 'en-x-piglatin',
36 null,
37 '>esttay anguagelay onversioncay<'
38 ];
39 yield 'Source variant is base language' => [
40 new PageBundle(
41 '<p>test language conversion</p>',
42 [ 'parsoid-data' ],
43 [ 'mw-data' ],
44 Parsoid::defaultHTMLVersion(),
45 [ 'content-language' => 'en' ]
46 ),
47 null,
48 'en-x-piglatin',
49 'en',
50 '>esttay anguagelay onversioncay<'
51 ];
52 yield 'Source language is null' => [
53 new PageBundle(
54 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
55 [ 'parsoid-data' ],
56 [ 'mw-data' ],
57 Parsoid::defaultHTMLVersion(),
58 [ 'content-language' => 'crh' ]
59 ),
60 null,
61 'crh-Latn',
62 null,
63 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
64 ];
65 yield 'Source language is explicit' => [
66 new PageBundle(
67 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
68 [ 'parsoid-data' ],
69 [ 'mw-data' ],
70 Parsoid::defaultHTMLVersion(),
71 [ 'content-language' => 'crh' ]
72 ),
73 null,
74 'crh-Latn',
75 'crh-Cyrl',
76 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
77 ];
78 yield 'Content language is provided via HTTP header' => [
79 new PageBundle(
80 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
81 [ 'parsoid-data' ],
82 [ 'mw-data' ],
83 Parsoid::defaultHTMLVersion(),
84 [ 'content-language' => 'crh-Cyrl' ]
85 ),
86 'crh',
87 'crh-Latn',
88 'crh-Cyrl',
89 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
90 ];
91 yield 'Content language is variant' => [
92 new PageBundle(
93 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
94 [ 'parsoid-data' ],
95 [ 'mw-data' ],
96 Parsoid::defaultHTMLVersion(),
97 []
98 ),
99 'crh-Cyrl',
100 'crh-Latn',
101 null,
102 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
103 ];
104 yield 'No content-language, but source variant provided' => [
105 new PageBundle(
106 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
107 [ 'parsoid-data' ],
108 [ 'mw-data' ],
109 Parsoid::defaultHTMLVersion(),
110 []
111 ),
112 null,
113 'crh-Latn',
114 'crh-Cyrl',
115 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
116 ];
117 yield 'Source variant is a base language code' => [
118 new PageBundle(
119 '<p>Бутун инсанлар сербестлик, менлик ве укъукъларда мусавий олып дунйагъа келелер.</p>',
120 [ 'parsoid-data' ],
121 [ 'mw-data' ],
122 Parsoid::defaultHTMLVersion(),
123 []
124 ),
125 null,
126 'crh-Latn',
127 'crh',
128 '>Butun insanlar serbestlik, menlik ve uquqlarda musaviy olıp dunyağa keleler.</'
129 ];
130 yield 'Base language does not support variants' => [
131 new PageBundle(
132 '<p>Hallo Wereld</p>',
133 [ 'parsoid-data' ],
134 [ 'mw-data' ],
135 Parsoid::defaultHTMLVersion(),
136 []
137 ),
138 'nl',
139 'nl-be',
140 null,
141 '>Hallo Wereld<',
142 false // The output language is currently not indicated. Should be expected to be 'nl' in the future.
143 ];
144 }
145
146 /**
147 * @dataProvider provideConvertPageBundleVariant
148 */
149 public function testConvertPageBundleVariant(
150 PageBundle $pageBundle,
151 $contentLanguage,
152 $target,
153 $source,
154 $expected,
155 $expectedLanguage = null
156 ) {
157 $expectedLanguage ??= $target;
158
159 $page = $this->getExistingTestPage();
160 $languageVariantConverter = $this->getLanguageVariantConverter( $page );
161 if ( $contentLanguage ) {
162 $contentLanguage = $this->getLanguageBcp47( $contentLanguage );
163 $languageVariantConverter->setPageLanguageOverride( $contentLanguage );
164 }
165 $target = $this->getLanguageBcp47( $target );
166 if ( $source ) {
167 $source = $this->getLanguageBcp47( $source );
168 }
169
170 $outputPageBundle = $languageVariantConverter->convertPageBundleVariant( $pageBundle, $target, $source );
171
172 $html = $outputPageBundle->toHtml();
173 $stripped = preg_replace( ':</?span[^>]*>:', '', $html );
174 $this->assertStringContainsString( $expected, $stripped );
175
176 if ( $expectedLanguage !== false ) {
177 $this->assertMatchesRegularExpression( "@<meta http-equiv=\"content-language\" content=\"($expectedLanguage)\"/>@i", $html );
178 $this->assertMatchesRegularExpression( "@^$expectedLanguage@i", $outputPageBundle->headers['content-language'] );
179 }
180 $this->assertEquals( Parsoid::defaultHTMLVersion(), $outputPageBundle->version );
181 }
182
183 public function provideConvertParserOutputVariant() {
184 foreach ( $this->provideConvertPageBundleVariant() as $name => $case ) {
185 $case[0] = PageBundleParserOutputConverter::parserOutputFromPageBundle( $case[0] );
186 yield $name => $case;
187 }
188 }
189
190 /**
191 * @dataProvider provideConvertParserOutputVariant
192 */
193 public function testConvertParserOutputVariant(
194 ParserOutput $parserOutput,
195 $contentLanguage,
196 $target,
197 $source,
198 $expected,
199 $expectedLanguage = null
200 ) {
201 $expectedLanguage ??= $target;
202
203 $page = $this->getExistingTestPage();
204 $languageVariantConverter = $this->getLanguageVariantConverter( $page );
205 if ( $contentLanguage ) {
206 $contentLanguage = $this->getLanguageBcp47( $contentLanguage );
207 $languageVariantConverter->setPageLanguageOverride( $contentLanguage );
208 }
209 $target = $this->getLanguageBcp47( $target );
210 if ( $source ) {
211 $source = $this->getLanguageBcp47( $source );
212 }
213
214 // Set some misc metadata in $parserOutput so we can verify it was
215 // preserved.
216 $parserOutput->setExtensionData( 'my-key', 'my-data' );
217
218 $modifiedParserOutput = $languageVariantConverter
219 ->convertParserOutputVariant( $parserOutput, $target, $source );
220
221 $this->assertSame( 'my-data', $modifiedParserOutput->getExtensionData( 'my-key' ) );
222
223 $html = $modifiedParserOutput->getRawText();
224 $stripped = preg_replace( ':</?span[^>]*>:', '', $html );
225 $this->assertStringContainsString( $expected, $stripped );
226 if ( $expectedLanguage !== false ) {
227 $this->assertMatchesRegularExpression( "@<meta http-equiv=\"content-language\" content=\"($expectedLanguage)\"/>@i", $html );
228 }
229
230 $extensionData = $modifiedParserOutput
231 ->getExtensionData( PageBundleParserOutputConverter::PARSOID_PAGE_BUNDLE_KEY );
232 $this->assertEquals( Parsoid::defaultHTMLVersion(), $extensionData['version'] );
233
234 if ( $expectedLanguage !== false ) {
235 $this->assertMatchesRegularExpression( "@^$expectedLanguage@i", $extensionData['headers']['content-language'] );
236 $this->assertSame( $expectedLanguage, (string)$modifiedParserOutput->getLanguage() );
237 }
238 }
239
240 private function getLanguageBcp47( $bcp47Code ): Language {
241 $languageFactory = $this->getServiceContainer()->getLanguageFactory();
242 return $languageFactory->getLanguage( new Bcp47CodeValue( $bcp47Code ) );
243 }
244
245 private function getLanguageVariantConverter( PageIdentity $pageIdentity ): LanguageVariantConverter {
246 return new LanguageVariantConverter(
247 $pageIdentity,
248 $this->getServiceContainer()->getParsoidPageConfigFactory(),
249 $this->getServiceContainer()->getService( '_Parsoid' ),
250 $this->getServiceContainer()->getParsoidSiteConfig(),
251 $this->getServiceContainer()->getTitleFactory(),
252 $this->getServiceContainer()->getLanguageConverterFactory(),
253 $this->getServiceContainer()->getLanguageFactory()
254 );
255 }
256 }