X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fparser%2FParserTestRunner.php;h=9255733e7498e3d38b0f03d2c685a4affb332856;hp=f100411f253c06d66a2e7cb8b019599bfdfa1717;hb=9081dd1217c574f77a43beab36b436ba43f4188e;hpb=6fbafe5494440f5c37686b250ed759f6a96271b9 diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index f100411f25..9255733e74 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -28,11 +28,24 @@ use Wikimedia\Rdbms\IDatabase; use MediaWiki\MediaWikiServices; use Wikimedia\ScopedCallback; +use Wikimedia\TestingAccessWrapper; /** * @ingroup Testing */ class ParserTestRunner { + + /** + * MediaWiki core parser test files, paths + * will be prefixed with __DIR__ . '/' + * + * @var array + */ + private static $coreTestFiles = [ + 'parserTests.txt', + 'extraParserTests.txt', + ]; + /** * @var bool $useTemporaryTables Use temporary tables for the temporary database */ @@ -146,6 +159,43 @@ class ParserTestRunner { } } + /** + * Get list of filenames to extension and core parser tests + * + * @return array + */ + public static function getParserTestFiles() { + global $wgParserTestFiles; + + // Add core test files + $files = array_map( function( $item ) { + return __DIR__ . "/$item"; + }, self::$coreTestFiles ); + + // Plus legacy global files + $files = array_merge( $files, $wgParserTestFiles ); + + // Auto-discover extension parser tests + $registry = ExtensionRegistry::getInstance(); + foreach ( $registry->getAllThings() as $info ) { + $dir = dirname( $info['path'] ) . '/tests/parser'; + if ( !file_exists( $dir ) ) { + continue; + } + $dirIterator = new RecursiveIteratorIterator( + new RecursiveDirectoryIterator( $dir ) + ); + foreach ( $dirIterator as $fileInfo ) { + /** @var SplFileInfo $fileInfo */ + if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) { + $files[] = $fileInfo->getPathname(); + } + } + } + + return array_unique( $files ); + } + public function getRecorder() { return $this->recorder; } @@ -240,7 +290,7 @@ class ParserTestRunner { 'name' => 'nullLockManager', 'class' => 'NullLockManager', ] ]; - $reset = function() { + $reset = function () { LockManagerGroup::destroySingletons(); }; $setup[] = $reset; @@ -265,7 +315,10 @@ class ParserTestRunner { $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ]; // Fake constant timestamp - Hooks::register( 'ParserGetVariableValueTs', 'ParserTestRunner::getFakeTimestamp' ); + Hooks::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) { + $ts = $this->getFakeTimestamp(); + return true; + } ); $teardown[] = function () { Hooks::clear( 'ParserGetVariableValueTs' ); }; @@ -288,7 +341,7 @@ class ParserTestRunner { MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' ); MediaWikiServices::getInstance()->redefineService( 'MediaHandlerFactory', - function() { + function () { return new MockMediaHandlerFactory(); } ); @@ -428,7 +481,7 @@ class ParserTestRunner { * @return ScopedCallback */ protected function createTeardownObject( $teardown, $nextTeardown = null ) { - return new ScopedCallback( function() use ( $teardown, $nextTeardown ) { + return new ScopedCallback( function () use ( $teardown, $nextTeardown ) { // Schedule teardown snippets in reverse order $teardown = array_reverse( $teardown ); @@ -746,6 +799,11 @@ class ParserTestRunner { $context = RequestContext::getMain(); $user = $context->getUser(); $options = ParserOptions::newFromContext( $context ); + $options->setTimestamp( $this->getFakeTimestamp() ); + + if ( !isset( $opts['wrap'] ) ) { + $options->setWrapOutputClass( false ); + } if ( isset( $opts['tidy'] ) ) { if ( !$this->tidySupport->isEnabled() ) { @@ -769,6 +827,7 @@ class ParserTestRunner { if ( isset( $opts['pst'] ) ) { $out = $parser->preSaveTransform( $test['input'], $title, $user, $options ); + $output = $parser->getOutput(); } elseif ( isset( $opts['msg'] ) ) { $out = $parser->transformMsg( $test['input'], $options, $title ); } elseif ( isset( $opts['section'] ) ) { @@ -819,6 +878,12 @@ class ParserTestRunner { } } + if ( isset( $output ) && isset( $opts['showflags'] ) ) { + $actualFlags = array_keys( TestingAccessWrapper::newFromObject( $output )->mFlags ); + sort( $actualFlags ); + $out .= "\nflags=" . join( ', ', $actualFlags ); + } + ScopedCallback::consume( $teardownGuard ); $expected = $test['result']; @@ -1041,6 +1106,11 @@ class ParserTestRunner { $context->setUser( $user ); $context->setLanguage( $lang ); $teardown[] = function () use ( $context ) { + // Clear language conversion tables + $wrapper = TestingAccessWrapper::newFromObject( + $context->getLanguage()->getConverter() + ); + $wrapper->reloadTables(); // Reset context to the restored globals $context->setUser( $GLOBALS['wgUser'] ); $context->setLanguage( $GLOBALS['wgContLang'] ); @@ -1588,11 +1658,14 @@ class ParserTestRunner { } /** - * The ParserGetVariableValueTs hook, used to make sure time-related parser + * Fake constant timestamp to make sure time-related parser * functions give a persistent value. + * + * - Parser::getVariableValue (via ParserGetVariableValueTs hook) + * - Parser::preSaveTransform (via ParserOptions) */ - static function getFakeTimestamp( &$parser, &$ts ) { - $ts = 123; // parsed as '1970-01-01T00:02:03Z' - return true; + private function getFakeTimestamp() { + // parsed as '1970-01-01T00:02:03Z' + return 123; } }