diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index 160e652c90b..fd0960a670d 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -987,13 +987,17 @@ class ParserTestRunner { return "Test doesn't match filter"; } // Skip parsoid-only tests if running in a legacy test mode - if ( $test->legacyHtml === null ) { + if ( + $test->legacyHtml === null && + self::getLegacyMetadataSection( $test ) === null + ) { // A Parsoid-only test should have one of the following sections if ( isset( $test->sections['html/parsoid'] ) || isset( $test->sections['html/parsoid+integrated'] ) || isset( $test->sections['html/parsoid+standalone'] ) || - isset( $test->sections['wikitext/edited'] ) + isset( $test->sections['wikitext/edited'] ) || + self::getParsoidMetadataSection( $test ) !== null ) { if ( $mode->isLegacy() ) { // Not an error, just skip this test if we're in @@ -1001,14 +1005,39 @@ class ParserTestRunner { return "Parsoid-only test"; } } else { - // This test lacks both a legacy html section and also - // any parsoid-specific html or wikitext/edited section. - $test->error( "Test lacks html section", $test->testName ); + // This test lacks both a legacy html or metadata + // section and also any parsoid-specific html or + // metadata section or wikitext/edited section. + $test->error( "Test lacks html or metadata section", $test->testName ); } } return null; } + public static function getLegacyMetadataSection( ParserTest $test ) { + return // specific results for legacy parser + $test->sections['metadata/php'] ?? + // specific results for legacy parser and parsoid integrated mode + $test->sections['metadata/integrated'] ?? + // generic for all parsers (even standalone) + $test->sections['metadata'] ?? + // missing (== use legacy combined output format) + null; + } + + public static function getParsoidMetadataSection( ParserTest $test ) { + return // specific results for parsoid integrated mode + $test->sections['metadata/parsoid+integrated'] ?? + // specific results for parsoid + $test->sections['metadata/parsoid'] ?? + // specific results for legacy parser and parsoid integrated mode + $test->sections['metadata/integrated'] ?? + // generic for all parsers (even standalone) + $test->sections['metadata'] ?? + // missing (== use legacy combined output format) + null; + } + /** * Compute valid test modes based on requested modes and file-enabled modes * @param array $testModes @@ -1360,14 +1389,19 @@ class ParserTestRunner { $out = preg_replace( '/\s+$/', '', $out ); } } + + $metadataExpected = self::getLegacyMetadataSection( $test ); + $metadataActual = null; if ( $output ) { - $this->addParserOutputInfo( $out, $output, $opts, $title ); + $this->addParserOutputInfo( + $out, $output, $opts, $title, + $metadataExpected, $metadataActual + ); } ScopedCallback::consume( $teardownGuard ); - $expected = $test->legacyHtml; - '@phan-var string $expected'; // assert that this is not null + $expected = $test->legacyHtml ?? ''; if ( count( $this->normalizationFunctions ) ) { $expected = ParserTestResultNormalizer::normalize( $expected, $this->normalizationFunctions ); @@ -1375,56 +1409,59 @@ class ParserTestRunner { } $testResult = new ParserTestResult( $test, $mode, $expected, $out ); + if ( $testResult->isSuccess() && $metadataExpected !== null ) { + $testResult = new ParserTestResult( $test, $mode, $metadataExpected, $metadataActual ?? '' ); + } return $testResult; } /** * Add information from the parser output to the result string * - * @param string &$out - * @param ParserOutput $output - * @param array $opts + * @param string &$out The "actual" parser output + * @param ParserOutput $output The "actual" parser metadata + * @param array $opts Test options * @param Title $title + * @param ?string $metadataExpected The contents of the !!metadata section, + * or null if it is missing + * @param ?string &$metadataActual The "actual" metadata output */ - private function addParserOutputInfo( &$out, ParserOutput $output, array $opts, Title $title ) { + private function addParserOutputInfo( + &$out, ParserOutput $output, array $opts, Title $title, + ?string $metadataExpected, ?string &$metadataActual + ) { + $before = []; + $after = []; + // The "before" entries may contain HTML. if ( isset( $opts['showtitle'] ) ) { if ( $output->getTitleText() ) { $titleText = $output->getTitleText(); } else { $titleText = $title->getPrefixedText(); } - - $out = "$titleText\n$out"; + $before[] = $titleText; } if ( isset( $opts['showindicators'] ) ) { - $indicators = ''; foreach ( $output->getIndicators() as $id => $content ) { - $indicators .= "$id=$content\n"; + $before[] = "$id=$content"; } - $out = $indicators . $out; } if ( isset( $opts['ill'] ) ) { - if ( $out !== '' ) { - $out .= "\n"; - } - $out .= implode( ' ', $output->getLanguageLinks() ); - } elseif ( isset( $opts['cat'] ) ) { + $after[] = implode( ' ', $output->getLanguageLinks() ); + } + + if ( isset( $opts['cat'] ) ) { foreach ( $output->getCategories() as $name => $sortkey ) { - if ( $out !== '' ) { - $out .= "\n"; - } - $out .= "cat=$name sort=$sortkey"; + $after[] = "cat=$name sort=$sortkey"; } } if ( isset( $opts['extension'] ) ) { foreach ( explode( ',', $opts['extension'] ) as $ext ) { - if ( $out !== '' ) { - $out .= "\n"; - } - $out .= "extension[$ext]=" . + $after[] = "extension[$ext]=" . + // XXX should use JsonCodec json_encode( $output->getExtensionData( $ext ), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT @@ -1434,10 +1471,7 @@ class ParserTestRunner { if ( isset( $opts['property'] ) ) { foreach ( explode( ',', $opts['property'] ) as $prop ) { - if ( $out !== '' ) { - $out .= "\n"; - } - $out .= "property[$prop]=" . + $after[] = "property[$prop]=" . ( $output->getPageProperty( $prop ) ?? '' ); } } @@ -1457,10 +1491,7 @@ class ParserTestRunner { } } sort( $actualFlags ); - if ( $out !== '' ) { - $out .= "\n"; - } - $out .= "flags=" . implode( ', ', $actualFlags ); + $after[] = "flags=" . implode( ', ', $actualFlags ); # In 1.21 we deprecated the use of arbitrary keys for # ParserOutput::setFlag() by extensions; if we find anyone # still doing that complain about it. @@ -1476,14 +1507,24 @@ class ParserTestRunner { // FIXME: We probably want to update this to a different format $sections = $output->getTOCData() !== null ? $output->getTOCData()->getSections() : []; - $toc = []; foreach ( $sections as $s ) { - $toc[] = json_encode( $s->toLegacy() ); + $after[] = json_encode( $s->toLegacy() ); } - if ( $out !== '' ) { - $out .= "\n"; + } + if ( $metadataExpected === null ) { + // legacy format, add $before and $after to $out + if ( $before ) { + $before = implode( "\n", $before ); + $out = "$before\n$out"; } - $out .= implode( "\n", $toc ); + if ( $after ) { + if ( $out && !str_ends_with( $out, "\n" ) ) { + $out .= "\n"; + } + $out .= implode( "\n", $after ); + } + } else { + $metadataActual = implode( "\n", array_merge( $before, $after ) ); } } diff --git a/tests/parser/interlanguageLinks.txt b/tests/parser/interlanguageLinks.txt index 5757ec5aba5..0926b2d7a92 100644 --- a/tests/parser/interlanguageLinks.txt +++ b/tests/parser/interlanguageLinks.txt @@ -235,11 +235,12 @@ ill [[es:]] [[ko:]] +!! metadata +es: !! html/php
-es: !! html/parsoid diff --git a/tests/parser/magicWords.txt b/tests/parser/magicWords.txt index 34a25a37c8e..4474156ee27 100644 --- a/tests/parser/magicWords.txt +++ b/tests/parser/magicWords.txt @@ -625,10 +625,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONID}} +!! metadata/integrated +flags=vary-revision-id !! html/php1337
-flags=vary-revision-id !! html/parsoid+integrated1337
!! end @@ -640,10 +641,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONID}} +!! metadata/integrated +flags=vary-revision-id !! html/php1337
-flags=vary-revision-id !! html/parsoid+integrated1337
!! end @@ -656,10 +658,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONTIMESTAMP}} +!! metadata/integrated +flags= !! html/php19700101000203
-flags= !! html/parsoid+integrated19700101000203
!! end @@ -672,10 +675,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONTIMESTAMP:{{PAGENAME}}}} +!! metadata/integrated +flags= !! html/php19700101000203
-flags= !! html/parsoid+integrated19700101000203
!! end @@ -688,10 +692,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONTIMESTAMP}} +!! metadata/integrated +flags=vary-revision-timestamp !! html/php19700101000203
-flags=vary-revision-timestamp !! html/parsoid+integrated19700101000203
!! end @@ -704,10 +709,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONTIMESTAMP:{{PAGENAME}}}} +!! metadata/integrated +flags=vary-revision-timestamp !! html/php19700101000203
-flags=vary-revision-timestamp !! html/parsoid+integrated19700101000203
!! end @@ -719,8 +725,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONTIMESTAMP:This page does not exist}} -!! html/php +!! metadata/integrated flags= +!! html/php !! html/parsoid+integrated !! end @@ -733,10 +740,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONUSER}} +!! metadata/integrated +flags=vary-user !! html/php127.0.0.1
-flags=vary-user !! html/parsoid+integrated127.0.0.1
!! end @@ -749,8 +757,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONUSER}} -!! html/php +!! metadata/integrated flags=vary-user +!! html/php !! html/parsoid+integrated !! end @@ -763,8 +772,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONUSER:{{PAGENAME}}}} -!! html/php +!! metadata/integrated flags=vary-user +!! html/php !! html/parsoid+integrated !! end @@ -776,8 +786,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONUSER:This page does not exist}} -!! html/php +!! metadata/integrated flags= +!! html/php !! html/parsoid+integrated !! end @@ -789,8 +800,9 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONUSER}} -!! html/php +!! metadata/integrated flags=vary-user +!! html/php !! html/parsoid+integrated !! end @@ -803,10 +815,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONID:{{PAGENAME}}}} +!! metadata/integrated +flags=vary-revision-id !! html/php1337
-flags=vary-revision-id !! html/parsoid+integrated1337
!! end @@ -818,8 +831,9 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONID:{{PAGENAME}}}} -!! html/php +!! metadata/integrated flags=vary-revision-id +!! html/php !! html/parsoid+integrated !! end @@ -832,10 +846,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONDAY}} +!! metadata/integrated +flags= !! html/php1
-flags= !! html/parsoid+integrated1
!! end @@ -848,10 +863,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONDAY:{{PAGENAME}}}} +!! metadata/integrated +flags= !! html/php1
-flags= !! html/parsoid+integrated1
!! end @@ -864,10 +880,11 @@ parsoid={ "modes": ["wt2html","wt2wt"], "normalizePhp": true } showflags !! wikitext {{REVISIONMONTH}} +!! metadata/integrated +flags= !! html/php01
-flags= !! html/parsoid+integrated01
!! end @@ -880,10 +897,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONMONTH:{{PAGENAME}}}} +!! metadata/integrated +flags= !! html/php01
-flags= !! html/parsoid+integrated01
!! end @@ -896,10 +914,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{REVISIONYEAR:{{PAGENAME}}}} +!! metadata/integrated +flags= !! html/php1970
-flags= !! html/parsoid+integrated1970
!! end @@ -912,10 +931,11 @@ parsoid={ "modes": ["wt2html","wt2wt"] } showflags !! wikitext {{PAGESIZE:{{PAGENAME}}}} +!! metadata/integrated +flags=vary-revision-sha1 !! html/php25
-flags=vary-revision-sha1 !! html/parsoid+integrated25
!! end diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index 7945a9d1d95..b710f1f2321 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -4055,9 +4055,9 @@ wgAllowDisplayTitle=true wgRestrictDisplayTitle=false !! wikitext {{DISPLAYTITLE:''{{PAGENAME}}''}} -!! html/php +!! metadata Parser test - +!! html/php !! html/parsoid !! end @@ -4074,9 +4074,9 @@ showtitle !! config wgAllowDisplayTitle=true wgRestrictDisplayTitle=false -!! html/php +!! metadata Foo - +!! html/php !! html/parsoid !! end @@ -7876,12 +7876,13 @@ __NOTOC__ ==Section 0== {{sections}} ==Section 4== +!! metadata +flags= !! html/php