Debian Codegraph @

vendor/wikimedia/parsoid/src/ParserTests/TestRunner.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\ParserTests;
5
6 use Closure;
7 use Psr\Log\LoggerInterface;
8 use Wikimedia\Assert\Assert;
9 use Wikimedia\Bcp47Code\Bcp47CodeValue;
10 use Wikimedia\Parsoid\Config\Api\DataAccess;
11 use Wikimedia\Parsoid\Config\Api\PageConfig;
12 use Wikimedia\Parsoid\Config\Env;
13 use Wikimedia\Parsoid\Config\StubMetadataCollector;
14 use Wikimedia\Parsoid\Core\SelectiveUpdateData;
15 use Wikimedia\Parsoid\DOM\Document;
16 use Wikimedia\Parsoid\DOM\Element;
17 use Wikimedia\Parsoid\Ext\ParsoidExtensionAPI;
18 use Wikimedia\Parsoid\Mocks\MockPageConfig;
19 use Wikimedia\Parsoid\Mocks\MockPageContent;
20 use Wikimedia\Parsoid\Utils\ContentUtils;
21 use Wikimedia\Parsoid\Utils\DOMCompat;
22 use Wikimedia\Parsoid\Utils\DOMDataUtils;
23 use Wikimedia\Parsoid\Utils\ScriptUtils;
24 use Wikimedia\Parsoid\Utils\Title;
25 use Wikimedia\Parsoid\Utils\Utils;
26 use Wikimedia\Parsoid\Wt2Html\PageConfigFrame;
27
28 /**
29 * Test runner for parser tests
30 */
31 class TestRunner {
32 // Hard-code some interwiki prefixes, as is done
33 // in ParserTestRunner::appendInterwikiSetup() in core
34 // Note that ApiQuerySiteInfo will always expand the URL to include a
35 // protocol, but will set 'protorel' to indicate whether its internal
36 // form included a protocol or not. So in this file 'url' will always
37 // have a protocol and we'll include an explicit 'protorel' field; but
38 // in core there is no 'protorel' field and 'url' will not always have
39 // a protocol.
40 private const PARSER_TESTS_IWPS = [
41 [
42 'prefix' => 'wikinvest',
43 'local' => true,
44 // This url doesn't have a $1 to exercise the fix in
45 // ConfigUtils::computeInterwikiMap
46 'url' => 'https://meta.wikimedia.org/wiki/Interwiki_map/discontinued#Wikinvest',
47 'protorel' => false
48 ],
49 [
50 'prefix' => 'local',
51 'url' => 'http://example.org/wiki/$1',
52 'local' => true,
53 'localinterwiki' => true
54 ],
55 [
56 // Local interwiki that matches a namespace name (T228616)
57 'prefix' => 'project',
58 'url' => 'http://example.org/wiki/$1',
59 'local' => true,
60 'localinterwiki' => true
61 ],
62 [
63 'prefix' => 'wikipedia',
64 'url' => 'http://en.wikipedia.org/wiki/$1'
65 ],
66 [
67 'prefix' => 'meatball',
68 // this has been updated in the live wikis, but the parser tests
69 // expect the old value (as set in parserTest.inc:setupInterwikis())
70 'url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1'
71 ],
72 [
73 'prefix' => 'memoryalpha',
74 'url' => 'http://www.memory-alpha.org/en/index.php/$1'
75 ],
76 [
77 'prefix' => 'zh',
78 'url' => 'http://zh.wikipedia.org/wiki/$1',
79 'language' => "中文",
80 'local' => true
81 ],
82 [
83 'prefix' => 'es',
84 'url' => 'http://es.wikipedia.org/wiki/$1',
85 'language' => "español",
86 'local' => true
87 ],
88 [
89 'prefix' => 'fr',
90 'url' => 'http://fr.wikipedia.org/wiki/$1',
91 'language' => "français",
92 'local' => true
93 ],
94 [
95 'prefix' => 'ru',
96 'url' => 'http://ru.wikipedia.org/wiki/$1',
97 'language' => "русский",
98 'local' => true
99 ],
100 [
101 'prefix' => 'mi',
102 'url' => 'http://example.org/wiki/$1',
103 // better for testing if one of the
104 // localinterwiki prefixes is also a language
105 'language' => 'Test',
106 'local' => true,
107 'localinterwiki' => true
108 ],
109 [
110 'prefix' => 'mul',
111 'url' => 'http://wikisource.org/wiki/$1',
112 'extralanglink' => true,
113 'linktext' => 'Multilingual',
114 'sitename' => 'WikiSource',
115 'local' => true
116 ],
117 // added to core's ParserTestRunner::appendInterwikiSetup() to support
118 // Parsoid tests [T254181]
119 [
120 'prefix' => 'en',
121 'url' => 'http://en.wikipedia.org/wiki/$1',
122 'language' => 'English',
123 'local' => true,
124 'protorel' => true
125 ],
126 [
127 'prefix' => 'stats',
128 'local' => true,
129 'url' => 'https://stats.wikimedia.org/$1'
130 ],
131 [
132 'prefix' => 'gerrit',
133 'local' => true,
134 'url' => 'https://gerrit.wikimedia.org/$1'
135 ],
136 ];
137
138 /** @var bool */
139 private $runDisabled;
140
141 /** @var bool */
142 private $runPHP;
143
144 /** @var string */
145 private $offsetType;
146
147 /** @var string */
148 private $testFileName;
149
150 /** @var string */
151 private $testFilePath;
152
153 /** @var ?string */
154 private $knownFailuresInfix;
155
156 /** @var string */
157 private $knownFailuresPath;
158
159 /** @var array */
160 private $articles;
161
162 /** @var LoggerInterface */
163 private $defaultLogger;
164
165 /**
166 * Sets one of 'regex' or 'string' properties
167 * - $testFilter['raw'] is the value of the filter
168 * - if $testFilter['regex'] is true, $testFilter['raw'] is used as a regex filter.
169 * - If $testFilter['string'] is true, $testFilter['raw'] is used as a plain string filter.
170 * @var ?array
171 */
172 private $testFilter;
173
174 /** @var Test[] */
175 private $testCases;
176
177 /** @var Stats */
178 private $stats;
179
180 /** @var MockApiHelper */
181 private $mockApi;
182
183 /** @var SiteConfig */
184 private $siteConfig;
185
186 /** @var DataAccess */
187 private $dataAccess;
188
189 /**
190 * Global cross-test env object only to be used for title processing while
191 * reading the parserTests file.
192 *
193 * Every test constructs its own private $env object.
194 *
195 * @var Env
196 */
197 private $dummyEnv;
198
199 /**
200 * Options needed to construct the per-test private $env object
201 * @var array
202 */
203 private $envOptions;
204
205 /**
206 * @param string $testFilePath
207 * @param ?string $knownFailuresInfix
208 * @param string[] $modes
209 */
210 public function __construct( string $testFilePath, ?string $knownFailuresInfix, array $modes ) {
211 $this->testFilePath = $testFilePath;
212 $this->knownFailuresInfix = $knownFailuresInfix;
213
214 $testFilePathInfo = pathinfo( $testFilePath );
215 $this->testFileName = $testFilePathInfo['basename'];
216
217 $newModes = [];
218 $modes[] = 'metadata';
219 foreach ( $modes as $mode ) {
220 $newModes[$mode] = new Stats();
221 $newModes[$mode]->failList = [];
222 $newModes[$mode]->result = ''; // XML reporter uses this.
223 }
224
225 $this->stats = new Stats();
226 $this->stats->modes = $newModes;
227
228 $this->mockApi = new MockApiHelper( null, fn ( $title )=>$this->normalizeTitleKey( $title ) );
229 $this->siteConfig = new SiteConfig( $this->mockApi, [] );
230 $this->dataAccess = new DataAccess( $this->mockApi, $this->siteConfig, [ 'stripProto' => false ] );
231 $this->dummyEnv = new Env(
232 $this->siteConfig,
233 // Unused; needed to satisfy Env signature requirements
234 new MockPageConfig( $this->siteConfig, [], new MockPageContent( [ 'main' => '' ] ) ),
235 // Unused; needed to satisfy Env signature requirements
236 $this->dataAccess,
237 // Unused; needed to satisfy Env signature requirements
238 new StubMetadataCollector( $this->siteConfig )
239 );
240
241 // Init interwiki map to parser tests info.
242 // This suppresses interwiki info from cached configs.
243 $this->siteConfig->setupInterwikiMap( self::PARSER_TESTS_IWPS );
244 $this->siteConfig->reset();
245 }
246
247 private function newEnv( Test $test, string $wikitext ): Env {
248 $title = $this->dummyEnv->makeTitleFromURLDecodedStr(
249 $test->pageName()
250 );
251
252 $opts = [
253 'title' => $title,
254 'pageContent' => $wikitext,
255 'pageLanguage' => $this->siteConfig->langBcp47(),
256 'pageLanguagedir' => $this->siteConfig->rtl() ? 'rtl' : 'ltr'
257 ];
258
259 $pageConfig = new PageConfig( null, $this->siteConfig, $opts );
260
261 $env = new Env(
262 $this->siteConfig,
263 $pageConfig,
264 $this->dataAccess,
265 new StubMetadataCollector( $this->siteConfig ),
266 $this->envOptions
267 );
268 $env->pageCache = $this->articles;
269
270 // Set parsing resource limits.
271 // $env->setResourceLimits();
272
273 return $env;
274 }
275
276 private function normalizeTitleKey( string $title ): string {
277 return $this->dummyEnv->normalizedTitleKey( $title, false, true );
278 }
279
280 private function addArticle( Article $art ): array {
281 $key = $this->normalizeTitleKey( $art->title );
282 $oldVal = $this->articles[$key] ?? null;
283 $this->articles[$key] = $art->text;
284 $teardown = [
285 function () use ( $key, $oldVal ) {
286 $this->articles[$key] = $oldVal;
287 },
288 $this->mockApi->addArticle( $key, $art ),
289 ];
290 return $teardown;
291 }
292
293 /**
294 * Parse the test file and set up articles and test cases
295 * @param array $options
296 */
297 private function buildTests( array $options ): void {
298 // Startup by loading .txt test file
299 $warnFunc = static function ( string $warnMsg ): void {
300 error_log( $warnMsg );
301 };
302 $normFunc = function ( string $title ): string {
303 return $this->normalizeTitleKey( $title );
304 };
305 $testReader = TestFileReader::read(
306 $this->testFilePath, $warnFunc, $normFunc, $this->knownFailuresInfix
307 );
308 $this->knownFailuresPath = $testReader->knownFailuresPath;
309 $this->testCases = $testReader->testCases;
310 $this->articles = [];
311 foreach ( $testReader->articles as $art ) {
312 $this->addArticle( $art );
313 }
314 if ( !ScriptUtils::booleanOption( $options['quieter'] ?? '' ) ) {
315 if ( $this->knownFailuresPath ) {
316 error_log( 'Loaded known failures from ' . $this->knownFailuresPath );
317 } else {
318 error_log( 'No known failures found.' );
319 }
320 }
321 }
322
323 /**
324 * Convert a wikitext string to an HTML Node
325 *
326 * @param Env $env
327 * @param Test $test
328 * @param string $mode
329 * @param string $wikitext
330 * @return Document
331 */
332 private function convertWt2Html(
333 Env $env, Test $test, string $mode, string $wikitext
334 ): Document {
335 // FIXME: Ugly! Maybe we should switch to using the entrypoint to
336 // the library for parserTests instead of reusing the environment
337 // and touching these internals.
338 $content = $env->getPageConfig()->getRevisionContent();
339 // @phan-suppress-next-line PhanUndeclaredProperty
340 $content->data['main']['content'] = $wikitext;
341 $env->topFrame = new PageConfigFrame(
342 $env, $env->getPageConfig(), $env->getSiteConfig()
343 );
344 if ( $mode === 'html2html' ) {
345 // Since this was set when serializing we need to setup a new doc
346 $env->setupTopLevelDoc();
347 }
348 $handler = $env->getContentHandler();
349 $extApi = new ParsoidExtensionAPI( $env );
350 $doc = $handler->toDOM( $extApi );
351 return $doc;
352 }
353
354 /**
355 * Convert a DOM to Wikitext.
356 *
357 * @param Env $env
358 * @param Test $test
359 * @param string $mode
360 * @param Document $doc
361 * @return string
362 */
363 private function convertHtml2Wt( Env $env, Test $test, string $mode, Document $doc ): string {
364 $startsAtWikitext = $mode === 'wt2wt' || $mode === 'wt2html' || $mode === 'selser';
365 if ( $mode === 'selser' ) {
366 $selserData = new SelectiveUpdateData( $test->wikitext, $test->cachedBODYstr );
367 } else {
368 $selserData = null;
369 }
370 $env->topLevelDoc = $doc;
371 $extApi = new ParsoidExtensionAPI( $env );
372 return $env->getContentHandler()->fromDOM( $extApi, $selserData );
373 }
374
375 /**
376 * Run test in the requested mode
377 * @param Test $test
378 * @param string $mode
379 * @param array $options
380 */
381 private function runTest( Test $test, string $mode, array $options ): void {
382 $test->time = [];
383 $testOpts = $test->options;
384
385 // These changes are for environment options that change between runs of
386 // different modes. See `processTest` for changes per test.
387
388 // Page language matches "wiki language" (which is set by
389 // the item 'language' option).
390
391 // Variant conversion is disabled by default
392 $this->envOptions['wtVariantLanguage'] = null;
393 $this->envOptions['htmlVariantLanguage'] = null;
394 // The test can explicitly opt-in to variant conversion with the
395 // 'langconv' option.
396 if ( $testOpts['langconv'] ?? null ) {
397 // These test option names are deprecated:
398 // (Note that test options names are lowercased by the reader.)
399 if ( $testOpts['sourcevariant'] ?? false ) {
400 $this->envOptions['wtVariantLanguage'] = Utils::mwCodeToBcp47(
401 $testOpts['sourcevariant'], true, $this->siteConfig->getLogger()
402 );
403 }
404 if ( $testOpts['variant'] ?? false ) {
405 $this->envOptions['htmlVariantLanguage'] = Utils::mwCodeToBcp47(
406 $testOpts['variant'], true, $this->siteConfig->getLogger()
407 );
408 }
409 // Preferred option names, which are also specified in bcp-47 codes
410 // (Note that test options names are lowercased by the reader.)
411 if ( $testOpts['wtvariantlanguage'] ?? false ) {
412 $this->envOptions['wtVariantLanguage'] =
413 new Bcp47CodeValue( $testOpts['wtvariantlanguage'] );
414 }
415 if ( $testOpts['htmlvariantlanguage'] ?? false ) {
416 $this->envOptions['htmlVariantLanguage'] =
417 new Bcp47CodeValue( $testOpts['htmlvariantlanguage'] );
418 }
419 }
420
421 $env = $this->newEnv( $test, $test->wikitext ?? '' );
422
423 // Some useful booleans
424 $startsAtHtml = $mode === 'html2html' || $mode === 'html2wt';
425 $endsAtHtml = $mode === 'wt2html' || $mode === 'html2html';
426
427 $parsoidOnly = isset( $test->sections['html/parsoid'] ) ||
428 isset( $test->sections['html/parsoid+standalone'] ) || (
429 !empty( $testOpts['parsoid'] ) &&
430 !isset( $testOpts['parsoid']['normalizePhp'] )
431 );
432 $test->time['start'] = microtime( true );
433 $doc = null;
434 $wt = null;
435
436 if ( isset( $test->sections['html/parsoid+standalone'] ) ) {
437 $test->parsoidHtml = $test->sections['html/parsoid+standalone'];
438 }
439
440 // Source preparation
441 if ( $startsAtHtml ) {
442 $html = $test->parsoidHtml ?? '';
443 if ( !$parsoidOnly ) {
444 // Strip some php output that has no wikitext representation
445 // (like .mw-editsection) and won't html2html roundtrip and
446 // therefore causes false failures.
447 $html = TestUtils::normalizePhpOutput( $html );
448 }
449 $doc = ContentUtils::createDocument( $html );
450 $wt = $this->convertHtml2Wt( $env, $test, $mode, $doc );
451 } else { // startsAtWikitext
452 // Always serialize DOM to string and reparse before passing to wt2wt
453 if ( $test->cachedBODYstr === null ) {
454 $doc = $this->convertWt2Html( $env, $test, $mode, $test->wikitext );
455
456 // Cache parsed HTML
457 $test->cachedBODYstr = ContentUtils::toXML( DOMCompat::getBody( $doc ) );
458
459 // - In wt2html mode, pass through original DOM
460 // so that it is serialized just once.
461 // - In wt2wt and selser modes, pass through serialized and
462 // reparsed DOM so that fostering/normalization effects
463 // are reproduced.
464 if ( $mode === 'wt2html' ) {
465 // no-op
466 } else {
467 $doc = ContentUtils::createDocument( $test->cachedBODYstr );
468 }
469 } else {
470 $doc = ContentUtils::createDocument( $test->cachedBODYstr );
471 }
472 }
473
474 // Generate and make changes for the selser test mode
475 $testManualChanges = $testOpts['parsoid']['changes'] ?? null;
476 if ( $mode === 'selser' ) {
477 if ( $testManualChanges && $test->changetree === [ 'manual' ] ) {
478 $test->applyManualChanges( $doc );
479 } else {
480 $changetree = isset( $options['changetree'] ) ?
481 json_decode( $options['changetree'] ) : $test->changetree;
482 if ( !$changetree ) {
483 $changetree = $test->generateChanges( $doc );
484 }
485 $dumpOpts = [
486 'dom:post-changes' => $env->hasDumpFlag( 'dom:post-changes' ),
487 'logger' => $env->getSiteConfig()->getLogger()
488 ];
489 $test->applyChanges( $dumpOpts, $doc, $changetree );
490 }
491 // Save the modified DOM so we can re-test it later.
492 // Always serialize to string and reparse before passing to selser/wt2wt.
493 $test->changedHTMLStr = ContentUtils::toXML( DOMCompat::getBody( $doc ) );
494 $doc = ContentUtils::createDocument( $test->changedHTMLStr );
495 } elseif ( $mode === 'wt2wt' ) {
496 // Handle a 'changes' option if present.
497 if ( $testManualChanges ) {
498 $test->applyManualChanges( $doc );
499 }
500 }
501
502 // Roundtrip stage
503 if ( $mode === 'wt2wt' || $mode === 'selser' ) {
504 $wt = $this->convertHtml2Wt( $env, $test, $mode, $doc );
505 } elseif ( $mode === 'html2html' ) {
506 $doc = $this->convertWt2Html( $env, $test, $mode, $wt );
507 }
508
509 // Result verification stage
510 if ( $endsAtHtml ) {
511 $this->processParsedHTML( $env, $test, $options, $mode, $doc );
512 } else {
513 $this->processSerializedWT( $env, $test, $options, $mode, $wt );
514 }
515 }
516
517 /**
518 * Process test options that impact output.
519 * These are almost always only pertinent in wt2html test modes.
520 * Returns:
521 * - null if there are no applicable output options.
522 * - true if the output matches expected output for the requested option(s).
523 * - false otherwise
524 *
525 * @param Env $env
526 * @param Test $test
527 * @param array $options
528 * @param string $mode
529 * @param Document $doc
530 * @param ?string $metadataExpected A metadata section from the test,
531 * or null if none present. If a metadata section is not present,
532 * the metadata output is added to $doc, otherwise it is returned
533 * in $metadataActual
534 * @param ?string &$metadataActual The "actual" metadata output for
535 * this test.
536 */
537 private function addParserOutputInfo(
538 Env $env, Test $test, array $options, string $mode, Document $doc,
539 ?string $metadataExpected, ?string &$metadataActual
540 ): void {
541 $output = $env->getMetadata();
542 $opts = $test->options;
543 '@phan-var StubMetadataCollector $output'; // @var StubMetadataCollector $metadata
544 // See ParserTestRunner::addParserOutputInfo() in core.
545 $before = [];
546 $after = [];
547
548 // 'showtitle' not yet supported
549
550 // unlike other link types, this dumps the 'sort' property as well
551 if ( isset( $opts['cat'] ) ) {
552 $defaultSortKey = $output->getPageProperty( 'defaultsort' ) ?? '';
553 foreach (
554 $output->getLinkList( StubMetadataCollector::LINKTYPE_CATEGORY )
555 as [ 'link' => $link, 'sort' => $sort ]
556 ) {
557 $sortkey = $sort ?: $defaultSortKey;
558 $name = $link->getDBkey();
559 $after[] = "cat=$name sort=$sortkey";
560 }
561 }
562
563 if ( isset( $opts['extlinks'] ) ) {
564 foreach ( $output->getExternalLinks() as $url => $ignore ) {
565 $after[] = "extlink=$url";
566 }
567 }
568
569 // Unlike other link types, this is stored as text, not dbkey
570 if ( isset( $opts['ill'] ) ) {
571 foreach (
572 $output->getLinkList( StubMetadataCollector::LINKTYPE_LANGUAGE )
573 as [ 'link' => $ll ]
574 ) {
575 $after[] = "ill=" . Title::newFromLinkTarget( $ll, $this->siteConfig )->getFullText();
576 }
577 }
578
579 $linkoptions = [
580 [ 'iwl', 'iwl=', StubMetadataCollector::LINKTYPE_INTERWIKI ],
581 [ 'links', 'link=', StubMetadataCollector::LINKTYPE_LOCAL ],
582 [ 'special', 'special=', StubMetadataCollector::LINKTYPE_SPECIAL ],
583 [ 'templates', 'template=', StubMetadataCollector::LINKTYPE_TEMPLATE ],
584 ];
585 foreach ( $linkoptions as [ $optName, $prefix, $type ] ) {
586 if ( isset( $opts[$optName] ) ) {
587 foreach ( $output->getLinkList( $type ) as [ 'link' => $ll ] ) {
588 $after[] = $prefix . Title::newFromLinkTarget( $ll, $this->siteConfig )->getPrefixedDBkey();
589 }
590 }
591 }
592
593 if ( isset( $opts['extension'] ) ) {
594 $extList = $opts['extension'];
595 if ( !is_array( $extList ) ) {
596 $extList = [ $extList ];
597 }
598 foreach ( $extList as $ext ) {
599 $after[] = "extension[$ext]=" .
600 // XXX should use JsonCodec
601 json_encode(
602 $output->getExtensionData( $ext ),
603 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
604 );
605 }
606 }
607 if ( isset( $opts['property'] ) ) {
608 $propList = $opts['property'];
609 if ( !is_array( $propList ) ) {
610 $propList = [ $propList ];
611 }
612 foreach ( $propList as $prop ) {
613 $after[] = "property[$prop]=" .
614 ( $output->getPageProperty( $prop ) ?? '' );
615 }
616 }
617 if ( isset( $opts['showflags'] ) ) {
618 $actualFlags = $output->getOutputFlags();
619 sort( $actualFlags );
620 $after[] = "flags=" . implode( ', ', $actualFlags );
621 }
622 if ( isset( $opts['showtocdata'] ) ) {
623 $tocData = $output->getTOCData();
624 if ( $tocData !== null ) {
625 $after[] = $tocData->prettyPrint();
626 }
627 }
628 if ( isset( $opts['showindicators'] ) ) {
629 foreach ( $output->getIndicators() as $name => $content ) {
630 $after[] = "$name=$content";
631 }
632 }
633 if ( isset( $opts['showmedia'] ) ) {
634 $images = array_map(
635 fn ( $item ) => $item['link']->getDBkey(),
636 $output->getLinkList( StubMetadataCollector::LINKTYPE_MEDIA )
637 );
638 $after[] = 'images=' . implode( ', ', $images );
639 }
640 if ( $metadataExpected === null ) {
641 // legacy format, add $before and $after to $doc
642 $body = DOMCompat::getBody( $doc );
643 if ( count( $before ) ) {
644 $before = $doc->createTextNode( implode( "\n", $before ) );
645 $body->insertBefore( $before, $body->firstChild );
646 }
647 if ( count( $after ) ) {
648 $after = $doc->createTextNode( implode( "\n", $after ) );
649 $body->appendChild( $after );
650 }
651 } else {
652 $metadataActual = implode( "\n", array_merge( $before, $after ) );
653 }
654 }
655
656 /**
657 * Return the appropriate metadata section for this test, given that
658 * we are running in parsoid "standalone" mode, or 'null' if none is
659 * present.
660 * @param Test $test
661 * @return ?string The expected metadata for this test
662 */
663 public static function getStandaloneMetadataSection( Test $test ): ?string {
664 return // specific results for parsoid standalone mode
665 $test->sections['metadata/parsoid+standalone'] ??
666 // specific results for parsoid
667 $test->sections['metadata/parsoid'] ??
668 // generic for all parsers (even standalone)
669 $test->sections['metadata'] ??
670 // missing (== use legacy combined output format)
671 null;
672 }
673
674 /**
675 * Check the given HTML result against the expected result,
676 * and throw an exception if necessary.
677 *
678 * @param Env $env
679 * @param Test $test
680 * @param array $options
681 * @param string $mode
682 * @param Document $doc
683 */
684 private function processParsedHTML(
685 Env $env, Test $test, array $options, string $mode, Document $doc
686 ): void {
687 $modeObj = new TestMode( $mode );
688 $test->time['end'] = microtime( true );
689 $metadataExpected = self::getStandaloneMetadataSection( $test );
690 $metadataActual = null;
691 if ( isset( $test->options['nohtml'] ) ) {
692 $body = DOMCompat::getBody( $doc );
693 while ( $body->hasChildNodes() ) {
694 $body->removeChild( $body->firstChild );
695 }
696 }
697 $this->addParserOutputInfo(
698 $env, $test, $options, $mode, $doc,
699 $metadataExpected, $metadataActual
700 );
701 if ( $test->parsoidHtml !== null ) {
702 $checkPassed = $this->checkHTML( $test, DOMCompat::getBody( $doc ), $options, $mode );
703 } else {
704 // Running the test for metadata, presumably.
705 $checkPassed = true;
706 }
707
708 // We could also check metadata in the html2html or wt2wt
709 // modes, but (a) we'd need a separate key for known failures
710 // to avoid overwriting the wt2html metadata results, and (b)
711 // any failures would probably be redundant with html2wt
712 // failures and not indicative of a "real" root cause bug.
713 if ( $metadataExpected !== null && !$modeObj->isCachingMode() && $mode === 'wt2html' ) {
714 $metadataResult = $this->checkMetadata( $test, $metadataExpected, $metadataActual ?? '', $options );
715 $checkPassed = $checkPassed && $metadataResult;
716 }
717
718 // Only throw an error if --exit-unexpected was set and there was an error
719 // Otherwise, continue running tests
720 if ( $options['exit-unexpected'] && !$checkPassed ) {
721 throw new UnexpectedException;
722 }
723 }
724
725 /**
726 * Check the given wikitext result against the expected result,
727 * and throw an exception if necessary.
728 *
729 * @param Env $env
730 * @param Test $test
731 * @param array $options
732 * @param string $mode
733 * @param string $wikitext
734 */
735 private function processSerializedWT(
736 Env $env, Test $test, array $options, string $mode, string $wikitext
737 ): void {
738 $test->time['end'] = microtime( true );
739
740 if ( $mode === 'selser' && $options['selser'] !== 'noauto' ) {
741 if ( $test->changetree === [ 5 ] ) {
742 $test->resultWT = $test->wikitext;
743 } else {
744 $doc = ContentUtils::createDocument( $test->changedHTMLStr );
745 $test->resultWT = $this->convertHtml2Wt( $env, $test, 'wt2wt', $doc );
746 }
747 }
748
749 $checkPassed = $this->checkWikitext( $test, $wikitext, $options, $mode );
750
751 // Only throw an error if --exit-unexpected was set and there was an error
752 // Otherwise, continue running tests
753 if ( $options['exit-unexpected'] && !$checkPassed ) {
754 throw new UnexpectedException;
755 }
756 }
757
758 private function checkHTML(
759 Test $test, Element $out, array $options, string $mode
760 ): bool {
761 [ $normOut, $normExpected ] = $test->normalizeHTML( $out, $test->cachedNormalizedHTML );
762 $expected = [ 'normal' => $normExpected, 'raw' => $test->parsoidHtml ];
763 $actual = [
764 'normal' => $normOut,
765 'raw' => ContentUtils::toXML( $out, [ 'innerXML' => true ] ),
766 'input' => ( $mode === 'html2html' ) ? $test->parsoidHtml : $test->wikitext
767 ];
768
769 return $options['reportResult'](
770 $this->stats, $test, $options, $mode, $expected, $actual
771 );
772 }
773
774 private function checkMetadata(
775 Test $test, string $metadataExpected, string $metadataActual, array $options
776 ): bool {
777 $expected = [ 'normal' => $metadataExpected, 'raw' => $metadataExpected ];
778 $actual = [
779 'normal' => $metadataActual,
780 'raw' => $metadataActual,
781 'input' => $test->wikitext,
782 ];
783 $mode = 'metadata';
784
785 return $options['reportResult'](
786 $this->stats, $test, $options, $mode, $expected, $actual
787 );
788 }
789
790 /**
791 * Removes DSR from data-parsoid for test normalization of a complet document. If
792 * data-parsoid gets subsequently empty, removes it too.
793 * @param string $raw
794 * @return string
795 */
796 private function filterDsr( string $raw ): string {
797 $doc = ContentUtils::createAndLoadDocument( $raw );
798 foreach ( $doc->childNodes as $child ) {
799 if ( $child instanceof Element ) {
800 $this->filterNodeDsr( $child );
801 }
802 }
803 DOMDataUtils::visitAndStoreDataAttribs( $doc );
804 $ret = ContentUtils::toXML( DOMCompat::getBody( $doc ), [ 'innerXML' => true ] );
805 $ret = preg_replace( '/\sdata-parsoid="{}"/', '', $ret );
806 return $ret;
807 }
808
809 /**
810 * Removes DSR from data-parsoid for test normalization of an element.
811 */
812 private function filterNodeDsr( Element $el ) {
813 $dp = DOMDataUtils::getDataParsoid( $el );
814 unset( $dp->dsr );
815 foreach ( $el->childNodes as $child ) {
816 if ( $child instanceof Element ) {
817 $this->filterNodeDsr( $child );
818 }
819 }
820 }
821
822 private function checkWikitext(
823 Test $test, string $out, array $options, string $mode
824 ): bool {
825 if ( $mode === 'html2wt' ) {
826 $input = $test->parsoidHtml;
827 $testWikitext = $test->wikitext;
828 } elseif ( $mode === 'wt2wt' ) {
829 if ( isset( $test->options['parsoid']['changes'] ) ) {
830 $input = $test->wikitext;
831 $testWikitext = $test->sections['wikitext/edited'];
832 } else {
833 $input = $testWikitext = $test->wikitext;
834 }
835 } else { /* selser */
836 if ( $test->changetree === [ 5 ] ) { /* selser with oracle */
837 $input = $test->changedHTMLStr;
838 $testWikitext = $test->wikitext;
839 $out = preg_replace( '/<!--' . Test::STATIC_RANDOM_STRING . '-->/', '', $out );
840 } elseif ( $test->changetree === [ 'manual' ] &&
841 isset( $test->options['parsoid']['changes'] )
842 ) { /* manual changes */
843 $input = $test->wikitext;
844 $testWikitext = $test->sections['wikitext/edited'];
845 } else { /* automated selser changes, no oracle */
846 $input = $test->changedHTMLStr;
847 $testWikitext = $test->resultWT;
848 }
849 }
850
851 [ $normalizedOut, $normalizedExpected ] = $test->normalizeWT( $out, $testWikitext );
852
853 $expected = [ 'normal' => $normalizedExpected, 'raw' => $testWikitext ];
854 $actual = [ 'normal' => $normalizedOut, 'raw' => $out, 'input' => $input ];
855
856 return $options['reportResult'](
857 $this->stats, $test, $options, $mode, $expected, $actual );
858 }
859
860 private function updateKnownFailures( array $options ): array {
861 // Check in case any tests were removed but we didn't update
862 // the knownFailures
863 $knownFailuresChanged = false;
864 $allModes = $options['wt2html'] && $options['wt2wt'] &&
865 $options['html2wt'] && $options['html2html'] &&
866 isset( $options['selser'] ) && !(
867 isset( $options['filter'] ) ||
868 isset( $options['regex'] ) ||
869 isset( $options['maxtests'] )
870 );
871 $offsetType = $options['offsetType'] ?? 'byte';
872
873 // Update knownFailures, if requested
874 if ( $allModes ||
875 ScriptUtils::booleanOption( $options['updateKnownFailures'] ?? null )
876 ) {
877 if ( $this->knownFailuresPath !== null ) {
878 $old = file_get_contents( $this->knownFailuresPath );
879 } else {
880 // If file doesn't exist, use the JSON representation of an
881 // empty array, so it compares equal in the case that we
882 // end up with an empty array of known failures below.
883 $old = '{}';
884 }
885 $testKnownFailures = [];
886 $kfModes = array_merge( $options['modes'], [ 'metadata' ] );
887 foreach ( $kfModes as $mode ) {
888 foreach ( $this->stats->modes[$mode]->failList as $fail ) {
889 $testKnownFailures[$fail['testName']] ??= [];
890 Assert::invariant(
891 !isset( $testKnownFailures[$fail['testName']][$mode . $fail['suffix']] ),
892 "Overwriting known failures result for " . $fail['testName'] . " " . $mode . $fail['suffix']
893 );
894 $testKnownFailures[$fail['testName']][$mode . $fail['suffix']] = $fail['raw'];
895 }
896 }
897 // Sort, otherwise, titles get added above based on the first
898 // failing mode, which can make diffs harder to verify when
899 // failing modes change.
900 ksort( $testKnownFailures );
901 $contents = json_encode(
902 $testKnownFailures,
903 JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES |
904 JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE
905 ) . "\n";
906 if ( ScriptUtils::booleanOption( $options['updateKnownFailures'] ?? null ) ) {
907 if ( $this->knownFailuresPath !== null ) {
908 file_put_contents( $this->knownFailuresPath, $contents );
909 } else {
910 // To be safe, we don't try to write a file that doesn't
911 // (yet) exist. Create an empty file if you need to, and
912 // then we'll happily update it for you.
913 throw new \RuntimeException(
914 "Known failures file for {$this->testFileName} does not exist, " .
915 "and so won't be updated."
916 );
917 }
918 } elseif ( $allModes && $offsetType === 'byte' ) {
919 $knownFailuresChanged = $contents !== $old;
920 }
921 }
922 // Write updated tests from failed ones
923 if ( ScriptUtils::booleanOption( $options['update-tests'] ?? null ) ||
924 ScriptUtils::booleanOption( $options['update-unexpected'] ?? null )
925 ) {
926 $updateFormat = $options['update-format'];
927 if ( $updateFormat !== 'raw' && $updateFormat !== 'actualNormalized' ) {
928 $updateFormat = 'noDsr';
929 }
930
931 $fileContent = file_get_contents( $this->testFilePath );
932 foreach ( [ 'wt2html', 'metadata' ] as $mode ) {
933 foreach ( $this->stats->modes[$mode]->failList as $fail ) {
934 if ( $options['update-tests'] || $fail['unexpected'] ) {
935 $exp = '/(!!\s*test\s*' .
936 preg_quote( $fail['testName'], '/' ) .
937 '(?:(?!!!\s*end)[\s\S])*' .
938 ')(' . preg_quote( $fail['expected'], '/' ) .
939 ')/m';
940 $fail['noDsr'] = $fail['raw'];
941 if ( $updateFormat === 'noDsr' && $mode !== 'metadata' ) {
942 $fail['noDsr'] = $this->filterDsr( $fail['noDsr'] );
943 }
944 $fileContent = preg_replace_callback(
945 $exp,
946 static function ( array $matches ) use ( $fail, $updateFormat ) {
947 return $matches[1] . $fail[$updateFormat];
948 },
949 $fileContent
950 );
951 }
952 }
953 }
954 file_put_contents( $this->testFilePath, $fileContent );
955 }
956
957 // print out the summary
958 $options['reportSummary'](
959 $options['modes'], $this->stats, $this->testFileName,
960 $this->testFilter, $knownFailuresChanged, $options
961 );
962
963 // we're done!
964 // exit status 1 == uncaught exception
965 $failures = $this->stats->allFailures();
966 $exitCode = ( $failures > 0 || $knownFailuresChanged ) ? 2 : 0;
967 if ( ScriptUtils::booleanOption( $options['exit-zero'] ?? null ) ) {
968 $exitCode = 0;
969 }
970
971 return [
972 'exitCode' => $exitCode,
973 'stats' => $this->stats,
974 'file' => $this->testFileName,
975 'knownFailuresChanged' => $knownFailuresChanged
976 ];
977 }
978
979 /**
980 * Run the test in all requested modes.
981 *
982 * @param Test $test
983 * @param array $options
984 */
985 private function processTest( Test $test, array $options ): void {
986 if ( !$test->options ) {
987 $test->options = [];
988 }
989
990 $testOpts = $test->options;
991
992 // ensure that test is not skipped if it has a wikitext/edited or
993 // html/parsoid+langconv section (but not a parsoid html section)
994 $haveHtml = ( $test->parsoidHtml !== null ) ||
995 isset( $test->sections['wikitext/edited'] ) ||
996 isset( $test->sections['html/parsoid+standalone'] ) ||
997 isset( $test->sections['html/parsoid+langconv'] ) ||
998 self::getStandaloneMetadataSection( $test ) !== null;
999 $hasHtmlParsoid =
1000 isset( $test->sections['html/parsoid'] ) ||
1001 isset( $test->sections['html/parsoid+standalone'] );
1002
1003 // Skip test whose title does not match --filter
1004 // or which is disabled or php-only
1005 if ( $test->wikitext === null ||
1006 !$haveHtml ||
1007 ( isset( $testOpts['disabled'] ) && !$this->runDisabled ) ||
1008 ( isset( $testOpts['php'] ) && !(
1009 $hasHtmlParsoid || $this->runPHP )
1010 ) ||
1011 !$test->matchesFilter( $this->testFilter )
1012 ) {
1013 return;
1014 }
1015
1016 $suppressErrors = !empty( $testOpts['parsoid']['suppressErrors'] );
1017 $this->siteConfig->setLogger( $suppressErrors ?
1018 $this->siteConfig->suppressLogger : $this->defaultLogger );
1019
1020 $targetModes = $test->computeTestModes( $options['modes'] );
1021
1022 // Filter out html2* tests if we don't have an HTML section
1023 // (Most likely there's either a metadata section or a html/php
1024 // section but not html/parsoid section.)
1025 if ( $test->parsoidHtml === null && !isset( $test->sections['html/parsoid+standalone'] ) ) {
1026 $targetModes = array_diff( $targetModes, [ 'html2wt', 'html2html' ] );
1027 }
1028
1029 if ( !count( $targetModes ) ) {
1030 return;
1031 }
1032
1033 // Honor language option
1034 $prefix = $testOpts['language'] ?? 'enwiki';
1035 if ( !str_contains( $prefix, 'wiki' ) ) {
1036 // Convert to our enwiki.. format
1037 $prefix .= 'wiki';
1038 }
1039
1040 // Switch to requested wiki
1041 $this->mockApi->setApiPrefix( $prefix );
1042 $this->siteConfig->reset();
1043
1044 // Add the title associated with the current test as a known title to
1045 // be consistent with the test runner in the core repo.
1046 $teardown = $this->addArticle( new Article( [
1047 'title' => $test->pageName(),
1048 'text' => $test->wikitext ?? '',
1049 // Fake it
1050 'type' => 'article',
1051 'filename' => 'fake',
1052 'lineNumStart' => 0,
1053 'lineNumEnd' => 0,
1054 ] ) );
1055
1056 // We don't do any sanity checking or type casting on $test->config
1057 // values here: if you set a bogus value in a parser test it *should*
1058 // blow things up, so that you fix your test case.
1059
1060 // Update $wgInterwikiMagic flag
1061 // default (undefined) setting is true
1062 $this->siteConfig->setInterwikiMagic(
1063 $test->config['wgInterwikiMagic'] ?? true
1064 );
1065
1066 // Update $wgEnableMagicLinks flag
1067 // default (undefined) setting is true for all types
1068 foreach ( [ "RFC", "ISBN", "PMID" ] as $v ) {
1069 $this->siteConfig->setMagicLinkEnabled(
1070 $v,
1071 ( $test->config['wgEnableMagicLinks'] ?? [] )[$v] ?? true
1072 );
1073 }
1074 if ( isset( $testOpts['pmid-interwiki'] ) ) {
1075 $this->siteConfig->setupInterwikiMap( array_merge( self::PARSER_TESTS_IWPS, [
1076 // Added to support T145590#8608455
1077 [
1078 'prefix' => 'pmid',
1079 'local' => true,
1080 'url' => '//www.ncbi.nlm.nih.gov/pubmed/$1?dopt=Abstract',
1081 ]
1082 ] ) );
1083 $teardown[] = fn () => $this->siteConfig->setupInterwikiMap( self::PARSER_TESTS_IWPS );
1084 }
1085
1086 // FIXME: Cite-specific hack
1087 $this->siteConfig->responsiveReferences = [
1088 'enabled' => $test->config['wgCiteResponsiveReferences'] ??
1089 $this->siteConfig->responsiveReferences['enabled'],
1090 'threshold' => $test->config['wgCiteResponsiveReferencesThreshold'] ??
1091 $this->siteConfig->responsiveReferences['threshold'],
1092 ];
1093
1094 if ( isset( $test->config['wgNoFollowLinks'] ) ) {
1095 $this->siteConfig->setNoFollowConfig(
1096 'nofollow', $test->config['wgNoFollowLinks']
1097 );
1098 }
1099
1100 if ( isset( $test->config['wgNoFollowDomainExceptions'] ) ) {
1101 $this->siteConfig->setNoFollowConfig(
1102 'domainexceptions',
1103 $test->config['wgNoFollowDomainExceptions']
1104 );
1105 }
1106
1107 // FIXME: Redundant with $testOpts['externallinktarget'] below
1108 if ( isset( $test->config['wgExternalLinkTarget'] ) ) {
1109 $this->siteConfig->setExternalLinkTarget(
1110 $test->config['wgExternalLinkTarget']
1111 );
1112 }
1113
1114 // Process test-specific options
1115 if ( $testOpts ) {
1116 Assert::invariant( !isset( $testOpts['extensions'] ),
1117 'Cannot configure extensions in tests' );
1118
1119 $availableParsoidTestOpts = [ 'wrapSections' ];
1120 foreach ( $availableParsoidTestOpts as $opt ) {
1121 if ( isset( $testOpts['parsoid'][$opt] ) ) {
1122 $this->envOptions[$opt] = $testOpts['parsoid'][$opt];
1123 }
1124 }
1125
1126 $this->siteConfig->disableSubpagesForNS( 0 );
1127 if ( isset( $testOpts['subpage'] ) ) {
1128 $this->siteConfig->enableSubpagesForNS( 0 );
1129 }
1130
1131 $allowedPrefixes = [ '' ]; // all allowed
1132 if ( isset( $testOpts['wgallowexternalimages'] ) &&
1133 !preg_match( '/^(1|true|)$/D', $testOpts['wgallowexternalimages'] )
1134 ) {
1135 $allowedPrefixes = [];
1136 }
1137 $this->siteConfig->allowedExternalImagePrefixes = $allowedPrefixes;
1138
1139 // Emulate PHP parser's tag hook to tunnel content past the sanitizer
1140 if ( isset( $testOpts['styletag'] ) ) {
1141 $this->siteConfig->registerParserTestExtension( new StyleTag() );
1142 }
1143
1144 if ( ( $testOpts['wgrawhtml'] ?? null ) === '1' ) {
1145 $this->siteConfig->registerParserTestExtension( new RawHTML() );
1146 }
1147
1148 if ( isset( $testOpts['thumbsize'] ) ) {
1149 $this->siteConfig->thumbsize = (int)$testOpts['thumbsize'];
1150 }
1151 if ( isset( $testOpts['annotations'] ) ) {
1152 $this->siteConfig->registerParserTestExtension( new DummyAnnotation() );
1153 }
1154 if ( isset( $testOpts['i18next'] ) ) {
1155 $this->siteConfig->registerParserTestExtension( new I18nTag() );
1156 }
1157 if ( isset( $testOpts['externallinktarget'] ) ) {
1158 $this->siteConfig->setExternalLinkTarget( $testOpts['externallinktarget'] );
1159 }
1160 }
1161
1162 // Ensure ParserHook is always registered!
1163 $this->siteConfig->registerParserTestExtension( new ParserHook() );
1164
1165 $runner = $this;
1166 $test->testAllModes( $targetModes, $options, Closure::fromCallable( [ $this, 'runTest' ] ) );
1167
1168 foreach ( $teardown as $t ) {
1169 $t();
1170 }
1171 }
1172
1173 /**
1174 * Run parser tests for the file with the provided options
1175 *
1176 * @param array $options
1177 * @return array
1178 */
1179 public function run( array $options ): array {
1180 $this->runDisabled = ScriptUtils::booleanOption( $options['run-disabled'] ?? null );
1181 $this->runPHP = ScriptUtils::booleanOption( $options['run-php'] ?? null );
1182 $this->offsetType = $options['offsetType'] ?? 'byte';
1183
1184 // Test case filtering
1185 $this->testFilter = null;
1186 if ( isset( $options['filter'] ) || isset( $options['regex'] ) ) {
1187 $this->testFilter = [
1188 'raw' => $options['regex'] ?? $options['filter'],
1189 'regex' => isset( $options['regex'] ),
1190 'string' => isset( $options['filter'] )
1191 ];
1192 }
1193
1194 $this->buildTests( $options );
1195
1196 // Trim test cases to the desired amount
1197 if ( isset( $options['maxtests'] ) ) {
1198 $n = $options['maxtests'];
1199 if ( $n > 0 ) {
1200 $this->testCases = array_slice( $this->testCases, 0, $n );
1201 }
1202 }
1203
1204 $defaultOpts = [
1205 'wrapSections' => false,
1206 'nativeTemplateExpansion' => true,
1207 'offsetType' => $this->offsetType,
1208 ];
1209 ScriptUtils::setDebuggingFlags( $defaultOpts, $options );
1210 ScriptUtils::setTemplatingAndProcessingFlags( $defaultOpts, $options );
1211
1212 if (
1213 ScriptUtils::booleanOption( $options['quiet'] ?? null ) ||
1214 ScriptUtils::booleanOption( $options['quieter'] ?? null )
1215 ) {
1216 $defaultOpts['logLevels'] = [ 'fatal', 'error' ];
1217 }
1218
1219 // Save default logger so we can be reset it after temporarily
1220 // switching to the suppressLogger to suppress expected error messages.
1221 $this->defaultLogger = $this->siteConfig->getLogger();
1222
1223 /**
1224 * PORT-FIXME(T238722)
1225 * // Enable sampling to assert it's working while testing.
1226 * $parsoidConfig->loggerSampling = [ [ '/^warn(\/|$)/', 100 ] ];
1227 *
1228 * // Override env's `setLogger` to record if we see `fatal` or `error`
1229 * // while running parser tests. (Keep it clean, folks! Use
1230 * // "suppressError" option on the test if error is expected.)
1231 * $env->setLogger = ( ( function ( $parserTests, $superSetLogger ) {
1232 * return function ( $_logger ) use ( &$parserTests ) {
1233 * call_user_func( 'superSetLogger', $_logger );
1234 * $this->log = function ( $level ) use ( &$_logger, &$parserTests ) {
1235 * if ( $_logger !== $parserTests->suppressLogger &&
1236 * preg_match( '/^(fatal|error)\b/', $level )
1237 * ) {
1238 * $parserTests->stats->loggedErrorCount++;
1239 * }
1240 * return call_user_func_array( [ $_logger, 'log' ], $arguments );
1241 * };
1242 * };
1243 * } ) );
1244 */
1245
1246 $options['reportStart']();
1247
1248 // Run tests
1249 foreach ( $this->testCases as $test ) {
1250 try {
1251 $this->envOptions = $defaultOpts;
1252 $this->processTest( $test, $options );
1253 } catch ( UnexpectedException $e ) {
1254 // Exit unexpected
1255 break;
1256 }
1257 }
1258
1259 // Update knownFailures
1260 return $this->updateKnownFailures( $options );
1261 }
1262 }