Debian Codegraph @

vendor/wikimedia/parsoid/tests/phpunit/Parsoid/ParsoidTest.php

raw
1 <?php
2
3 namespace Test\Parsoid;
4
5 use Wikimedia\Bcp47Code\Bcp47CodeValue;
6 use Wikimedia\Parsoid\Core\PageBundle;
7 use Wikimedia\Parsoid\Mocks\MockDataAccess;
8 use Wikimedia\Parsoid\Mocks\MockMetrics;
9 use Wikimedia\Parsoid\Mocks\MockPageConfig;
10 use Wikimedia\Parsoid\Mocks\MockPageContent;
11 use Wikimedia\Parsoid\Mocks\MockSiteConfig;
12 use Wikimedia\Parsoid\Parsoid;
13 use Wikimedia\Parsoid\Utils\PHPUtils;
14
15 /**
16 * Test the entrypoint to the library.
17 *
18 * @coversDefaultClass \Wikimedia\Parsoid\Parsoid
19 */
20 class ParsoidTest extends \PHPUnit\Framework\TestCase {
21
22 private static $defaultContentVersion = Parsoid::AVAILABLE_VERSIONS[0];
23
24 /**
25 * @covers ::wikitext2html
26 * @dataProvider provideWt2Html
27 */
28 public function testWt2Html( string $wt, string $expected, array $parserOpts = [] ) {
29 $opts = [];
30
31 $siteConfig = new MockSiteConfig( $opts );
32 $dataAccess = new MockDataAccess( $siteConfig, $opts );
33 $parsoid = new Parsoid( $siteConfig, $dataAccess );
34
35 $pageContent = new MockPageContent( [ 'main' => $wt ] );
36 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
37 $out = $parsoid->wikitext2html( $pageConfig, $parserOpts );
38 if ( !empty( $parserOpts['pageBundle'] ) ) {
39 $this->assertTrue( $out instanceof PageBundle );
40 $this->assertEquals( $expected, $out->html );
41 } else {
42 $this->assertEquals( $expected, $out );
43 }
44 }
45
46 public function provideWt2Html(): array {
47 return [
48 [
49 "'''hi ho'''",
50 "<p data-parsoid='{\"dsr\":[0,11,0,0]}'><b data-parsoid='{\"dsr\":[0,11,3,3]}'>hi ho</b></p>",
51 [
52 'body_only' => true,
53 'wrapSections' => false,
54 ]
55 ],
56 [
57 "'''hi ho'''",
58 "<p id=\"mwAQ\"><b id=\"mwAg\">hi ho</b></p>",
59 [
60 'body_only' => true,
61 'wrapSections' => false,
62 'pageBundle' => true,
63 ]
64 ],
65 ];
66 }
67
68 /**
69 * @covers ::wikitext2lint
70 * @dataProvider provideWt2Lint
71 */
72 public function testWt2Lint( $wt, $expected, $parserOpts = [] ) {
73 $opts = [
74 'linting' => true,
75 ];
76
77 $siteConfig = new MockSiteConfig( $opts );
78 $dataAccess = new MockDataAccess( $siteConfig, $opts );
79 $parsoid = new Parsoid( $siteConfig, $dataAccess );
80
81 $pageContent = new MockPageContent( [ 'main' => $wt ] );
82 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
83 $lint = $parsoid->wikitext2lint( $pageConfig, $parserOpts );
84 $this->assertEquals( $expected, $lint );
85 }
86
87 public function provideWt2Lint() {
88 return [
89 [
90 "[http://google.com This is [[Google]]'s search page]",
91 [
92 [
93 'type' => 'wikilink-in-extlink',
94 'dsr' => [ 0, 52, 19, 1 ],
95 'params' => [],
96 'templateInfo' => null,
97 ]
98 ]
99 ]
100 ];
101 }
102
103 /**
104 * @covers ::html2wikitext
105 * @dataProvider provideHtml2Wt
106 */
107 public function testHtml2Wt( $input, $expected, $parserOpts = [] ) {
108 $opts = [];
109
110 $siteConfig = new MockSiteConfig( $opts );
111 $dataAccess = new MockDataAccess( $siteConfig, $opts );
112 $parsoid = new Parsoid( $siteConfig, $dataAccess );
113
114 $pageContent = new MockPageContent( [ 'main' => '' ] );
115 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
116 $wt = $parsoid->html2wikitext( $pageConfig, $input, $parserOpts );
117 $this->assertEquals( $expected, $wt );
118 }
119
120 public function provideHtml2Wt() {
121 return [
122 [
123 "<pre>hi</pre>\n<div>ho</div>",
124 " hi\n<div>ho</div>"
125 ],
126 [
127 "<h2></h2>",
128 ''
129 ]
130 ];
131 }
132
133 /**
134 * @covers ::pb2pb
135 * @dataProvider providePb2Pb
136 */
137 public function testPb2Pb( $update, $input, $expected, $testOpts = [] ) {
138 $opts = [];
139
140 $siteConfig = new MockSiteConfig( $opts );
141 $dataAccess = new MockDataAccess( $siteConfig, $opts );
142 $parsoid = new Parsoid( $siteConfig, $dataAccess );
143
144 $pageContent = new MockPageContent( [ 'main' => $testOpts['pageContent'] ?? '' ] );
145 $pageConfig = new MockPageConfig( $siteConfig, [
146 'pageLanguage' => $testOpts['pageLanguage'] ?? 'en'
147 ], $pageContent );
148 $pb = new PageBundle(
149 $input['html'],
150 PHPUtils::jsonDecode( $input['parsoid'] ?? 'null' ),
151 PHPUtils::jsonDecode( $input['mw'] ?? 'null' ),
152 $input['version'] ?? null,
153 $input['headers'] ?? null,
154 $input['contentmodel'] ?? null
155 );
156 $out = $parsoid->pb2pb( $pageConfig, $update, $pb, $testOpts );
157 $this->assertTrue( $out instanceof PageBundle );
158 $this->assertEquals( $expected['html'], $out->html );
159 $this->assertEquals( $expected['parsoid'] ?? 'null', PHPUtils::jsonEncode( $out->parsoid ) );
160 $this->assertEquals( $expected['mw'] ?? 'null', PHPUtils::jsonEncode( $out->mw ) );
161 $this->assertEquals( $expected['version'] ?? null, $out->version );
162 if ( isset( $expected['headers'] ) ) {
163 $this->assertEquals( $expected['headers'] ?? null, $out->headers );
164 }
165 }
166
167 public function providePb2Pb() {
168 // phpcs:disable Generic.Files.LineLength.TooLong
169 return [
170 [
171 'convertoffsets',
172 [
173 'html' => '<p id="mwAA">La Luna è l\'unico satellite naturale della Terra</p><p id="mwAQ">La Luna è l\'unico satellite naturale della Terra</p>',
174 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,49,0,0]},"mwAQ":{"dsr":[51,100,0,0]}},"offsetType":"byte"}',
175 'mw' => null,
176 ],
177 [
178 'html' => '<p id="mwAA">La Luna è l\'unico satellite naturale della Terra</p><p id="mwAQ">La Luna è l\'unico satellite naturale della Terra</p>',
179 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,48,0,0]},"mwAQ":{"dsr":[50,98,0,0]}},"offsetType":"ucs2"}',
180 'mw' => '{"ids":[]}',
181 'version' => self::$defaultContentVersion,
182 ],
183 [
184 'inputOffsetType' => 'byte',
185 'outputOffsetType' => 'ucs2',
186 // Note the accented e char that is a multi-byte char.
187 // Without the pageContent property, offset conversion will not work.
188 'pageContent' => "La Luna è l'unico satellite naturale della Terra\n\nLa Luna è l'unico satellite naturale della Terra",
189 'body_only' => true
190 ]
191 ],
192 [
193 'convertoffsets',
194 [
195 'html' => '<p id="mwAA">La Luna è l\'unico satellite naturale della Terra</p><p id="mwAQ">La Luna è l\'unico satellite naturale della Terra</p>',
196 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,48,0,0]},"mwAQ":{"dsr":[50,98,0,0]}},"offsetType":"ucs2"}',
197 'mw' => null,
198 ],
199 [
200 'html' => '<p id="mwAA">La Luna è l\'unico satellite naturale della Terra</p><p id="mwAQ">La Luna è l\'unico satellite naturale della Terra</p>',
201 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,49,0,0]},"mwAQ":{"dsr":[51,100,0,0]}},"offsetType":"byte"}',
202 'mw' => '{"ids":[]}',
203 'version' => self::$defaultContentVersion,
204 ],
205 [
206 'inputOffsetType' => 'ucs2',
207 'outputOffsetType' => 'byte',
208 'pageContent' => "La Luna è l'unico satellite naturale della Terra\n\nLa Luna è l'unico satellite naturale della Terra",
209 'body_only' => true
210 ]
211 ],
212 [
213 'redlinks',
214 [
215 'html' => '<p><a rel="mw:WikiLink" href="./Special:Version" title="Special:Version">Special:Version</a> <a rel="mw:WikiLink" href="./Doesnotexist?action=edit&amp;redlink=1" typeof="mw:LocalizedAttrs" title="Doesnotexist" class="new" data-mw-i18n=\'{"title":{"lang":"x-page","key":"red-link-title","params":["Doesnotexist"]}}\'>Doesnotexist</a> <a rel="mw:WikiLink" href="./Redirected" title="Redirected">Redirected</a></p>',
216 'parsoid' => null,
217 'mw' => null,
218 ],
219 [
220 'html' => '<p><a rel="mw:WikiLink" href="./Special:Version" title="Special:Version">Special:Version</a> <a rel="mw:WikiLink" href="./Doesnotexist?action=edit&amp;redlink=1" typeof="mw:LocalizedAttrs" title="Doesnotexist" class="new" data-mw-i18n=\'{"title":{"lang":"x-page","key":"red-link-title","params":["Doesnotexist"]}}\'>Doesnotexist</a> <a rel="mw:WikiLink" href="./Redirected" title="Redirected" class="mw-redirect">Redirected</a></p>',
221 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}',
222 'mw' => '{"ids":[]}',
223 'version' => self::$defaultContentVersion,
224 ],
225 [
226 'body_only' => true,
227 ]
228 ],
229 [
230 'redlinks',
231 [
232 'html' => '<body id="mwAA" lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body-content parsoid-body mediawiki mw-parser-output" dir="ltr"><p id="mwAQ"><a rel="mw:WikiLink" href="./Not_an_article?action=edit&amp;redlink=1" id="mwAg" typeof="mw:LocalizedAttrs" class="new" title="Not an article" data-mw-i18n=\'{"title":{"lang":"x-page","key":"red-link-title","params":["Not an article"]}}\'>abcd</a></p>' . "\n" . '</body>',
233 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,24,0,0]},"mwAQ":{"dsr":[0,23,0,0]},"mwAg":{"stx":"piped","a":{"href":"./Not_an_article"},"sa":{"href":"Not an article"},"dsr":[0,23,17,2]}},"offsetType":"byte"}',
234 'mw' => '{"ids":[]}',
235 ],
236 [
237 'html' => '<p id="mwAQ"><a rel="mw:WikiLink" href="./Not_an_article?action=edit&amp;redlink=1" id="mwAg" typeof="mw:LocalizedAttrs" title="Not an article" class="new" data-mw-i18n=\'{"title":{"lang":"x-page","key":"red-link-title","params":["Not an article"]}}\'>abcd</a></p>' . "\n",
238 'parsoid' => '{"counter":-1,"ids":{"mwAA":{"dsr":[0,24,0,0]},"mwAQ":{"dsr":[0,23,0,0]},"mwAg":{"stx":"piped","a":{"href":"./Not_an_article"},"sa":{"href":"Not an article"},"dsr":[0,23,17,2]}},"offsetType":"byte"}',
239 'mw' => '{"ids":[]}',
240 'version' => self::$defaultContentVersion,
241 ],
242 [
243 'body_only' => true,
244 ],
245 ],
246 // Note that id attributes are preserved, even if no data-parsoid
247 // is provided.
248 [
249 'redlinks',
250 [
251 'html' => '<body id="mwAA" lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body-content parsoid-body mediawiki mw-parser-output" dir="ltr"><p id="mwAQ"><a id="mwAg" href="./Not_an_article?action=edit&amp;redlink=1" title="Not an article">abcd</a></p></body>',
252 'parsoid' => null,
253 'mw' => null,
254 ],
255 [
256 'html' => '<p id="mwAQ"><a id="mwAg" href="./Not_an_article?action=edit&amp;redlink=1" title="Not an article">abcd</a></p>',
257 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}',
258 'mw' => '{"ids":[]}',
259 'version' => self::$defaultContentVersion,
260 ],
261 [
262 'body_only' => true,
263 ],
264 ],
265 // Language Variant conversion endpoint
266 [
267 'variant',
268 [
269 'html' => '<p>абвг abcd x</p>',
270 'parsoid' => null,
271 'mw' => null,
272 ],
273 [
274 'html' => '<p data-mw-variant-lang="sr-ec">abvg <span typeof="mw:LanguageVariant" data-mw-variant=\'{"twoway":[{"l":"sr-ec","t":"abcd"},{"l":"sr-el","t":"abcd"}],"rt":true}\'>abcd</span> x</p>',
275 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}',
276 'mw' => '{"ids":[]}',
277 'version' => self::$defaultContentVersion,
278 ],
279 [
280 'body_only' => true,
281 'pageLanguage' => new Bcp47CodeValue( 'sr' ),
282 'variant' => [
283 'wikitext' => new Bcp47CodeValue( 'sr-Cyrl' ),
284 'html' => new Bcp47CodeValue( 'sr-Latn' ),
285 ]
286 ]
287 ],
288 [
289 'variant',
290 [
291 'html' => '<p>абвг abcd x</p>',
292 'parsoid' => null,
293 'mw' => null,
294 ],
295 [
296 'html' => '<p data-mw-variant-lang="sr-el"><span typeof="mw:LanguageVariant" data-mw-variant=\'{"twoway":[{"l":"sr-el","t":"абвг"},{"l":"sr-ec","t":"абвг"}],"rt":true}\'>абвг</span> абцд x</p>',
297 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}',
298 'mw' => '{"ids":[]}',
299 'version' => self::$defaultContentVersion,
300 ],
301 [
302 'body_only' => true,
303 'pageLanguage' => new Bcp47CodeValue( 'sr' ),
304 'variant' => [
305 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ),
306 'html' => new Bcp47CodeValue( 'sr-Cyrl' ),
307 ]
308 ]
309 ],
310 [
311 'variant',
312 [
313 'html' => '<body id="mwAA" lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body-content parsoid-body mediawiki mw-parser-output" dir="ltr"><p id="mwAQ"><b id="mwAg">abcd</b></p></body>',
314 'parsoid' => '{"counter":2,"ids":{"mwAA":{"dsr":[0,11,0,0]},"mwAQ":{"dsr":[0,10,0,0]},"mwAg":{"dsr":[0,10,3,3]}},"offsetType":"byte"}',
315 'mw' => '{"ids":[]}',
316 ],
317 [
318 'html' => '<p id="mwAQ" data-mw-variant-lang="sr-el"><b id="mwAg">абцд</b></p>',
319 'parsoid' => '{"counter":-1,"ids":{"mwAA":{"dsr":[0,11,0,0]},"mwAQ":{"dsr":[0,10,0,0]},"mwAg":{"dsr":[0,10,3,3]}},"offsetType":"byte"}',
320 'mw' => '{"ids":[]}',
321 'version' => self::$defaultContentVersion,
322 ],
323 [
324 'body_only' => true,
325 'pageLanguage' => new Bcp47CodeValue( 'sr' ),
326 'variant' => [
327 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ),
328 'html' => new Bcp47CodeValue( 'sr-Cyrl' ),
329 ]
330 ]
331 ],
332 // Note that id attributes are preserved, even if no data-parsoid
333 // is provided.
334 [
335 'variant',
336 [
337 'html' => '<body id="mwAA" lang="en" class="mw-content-ltr sitedir-ltr ltr mw-body-content parsoid-body mediawiki mw-parser-output" dir="ltr"><p id="mwAQ"><b id="mwAg">abcd</b></p></body>',
338 'parsoid' => null,
339 'mw' => null,
340 ],
341 [
342 'html' => '<p id="mwAQ" data-mw-variant-lang="sr-el"><b id="mwAg">абцд</b></p>',
343 'parsoid' => '{"counter":-1,"ids":[],"offsetType":"byte"}',
344 'mw' => '{"ids":[]}',
345 'version' => self::$defaultContentVersion,
346 ],
347 [
348 'body_only' => true,
349 'pageLanguage' => new Bcp47CodeValue( 'sr' ),
350 'variant' => [
351 'wikitext' => new Bcp47CodeValue( 'sr-Latn' ),
352 'html' => new Bcp47CodeValue( 'sr-Cyrl' ),
353 ]
354 ]
355 ],
356 ];
357 // phpcs:enable Generic.Files.LineLength.TooLong
358 }
359
360 /**
361 * @covers ::implementsLanguageConversionBcp47
362 * @dataProvider provideImplementsLanguageConversionBcp47
363 */
364 public function testImplementsLanguageConversionBcp47( string $htmlVariantCode, $expected ) {
365 $opts = [];
366
367 $siteConfig = new MockSiteConfig( $opts );
368 $dataAccess = new MockDataAccess( $siteConfig, $opts );
369 $parsoid = new Parsoid( $siteConfig, $dataAccess );
370
371 $pageContent = new MockPageContent( [ 'main' => '' ] );
372 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
373
374 $actual = $parsoid->implementsLanguageConversionBcp47( $pageConfig, new Bcp47CodeValue( $htmlVariantCode ) );
375 $this->assertEquals( $expected, $actual );
376 }
377
378 public function provideImplementsLanguageConversionBcp47() {
379 yield 'Variant conversion is implemented for en-x-piglatin' => [
380 'en-x-piglatin', true
381 ];
382
383 yield 'Variant conversion is not implemented for kk-latn' => [
384 'kk-latn', false
385 ];
386 }
387
388 /**
389 * @covers ::recordParseMetrics
390 */
391 public function testParseMetrics() {
392 $opts = [];
393 $wt = "testing '''123'''";
394
395 $siteConfig = new MockSiteConfig( $opts );
396 $dataAccess = new MockDataAccess( $siteConfig, $opts );
397 $parsoid = new Parsoid( $siteConfig, $dataAccess );
398
399 $pageContent = new MockPageContent( [ 'main' => $wt ] );
400 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
401 $parsoid->wikitext2html( $pageConfig );
402
403 $metrics = $siteConfig->metrics();
404 $this->assertInstanceOf( MockMetrics::class, $metrics );
405 $log = $metrics->log;
406 $this->assertContains(
407 [ 'timing', 'entry.wt2html.pageWithOldid.size.input', 17.0 ],
408 $log
409 );
410 }
411
412 /**
413 * @covers ::recordSerializationMetrics
414 */
415 public function testSerializationMetrics() {
416 $opts = [];
417 $html = "<p>hiho</p>";
418
419 $siteConfig = new MockSiteConfig( $opts );
420 $dataAccess = new MockDataAccess( $siteConfig, $opts );
421 $parsoid = new Parsoid( $siteConfig, $dataAccess );
422
423 $pageContent = new MockPageContent( [ 'main' => '' ] );
424 $pageConfig = new MockPageConfig( $siteConfig, $opts, $pageContent );
425
426 $wt = $parsoid->html2wikitext( $pageConfig, $html );
427 $this->assertEquals( 'hiho', $wt );
428
429 $metrics = $siteConfig->metrics();
430 $this->assertInstanceOf( MockMetrics::class, $metrics );
431 $log = $metrics->log;
432 $this->assertCount( 14, $log );
433
434 $this->assertEquals(
435 [ 'timing', 'entry.html2wt.size.input', 11.0 ],
436 $log[6]
437 );
438 $this->assertEquals(
439 [ 'timing', 'entry.html2wt.size.output', 4.0 ],
440 $log[10]
441 );
442 }
443
444 }