* https://www.mediawiki.org/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html * * @todo Make this more independent of the configuration (and if possible the database) * @file * @ingroup Testing */ use MediaWiki\Html\Html; use MediaWiki\Interwiki\ClassicInterwikiLookup; use MediaWiki\MainConfigNames; use MediaWiki\MediaWikiServices; use MediaWiki\Parser\ParserOutputFlags; use MediaWiki\Revision\MutableRevisionRecord; use MediaWiki\Revision\RevisionRecord; use MediaWiki\Revision\SlotRecord; use MediaWiki\StubObject\StubGlobalUser; use MediaWiki\WikiMap\WikiMap; use Psr\Log\NullLogger; use Wikimedia\Assert\Assert; use Wikimedia\Parsoid\Config\PageConfig; use Wikimedia\Parsoid\Config\SiteConfig; use Wikimedia\Parsoid\Core\SelserData; use Wikimedia\Parsoid\DOM\Document; use Wikimedia\Parsoid\Ext\ExtensionModule; use Wikimedia\Parsoid\ParserTests\Article as ParserTestArticle; use Wikimedia\Parsoid\ParserTests\ParserHook as ParsoidParserHook; use Wikimedia\Parsoid\ParserTests\RawHTML as ParsoidRawHTML; use Wikimedia\Parsoid\ParserTests\StyleTag as ParsoidStyleTag; use Wikimedia\Parsoid\ParserTests\Test as ParserTest; use Wikimedia\Parsoid\ParserTests\TestFileReader; use Wikimedia\Parsoid\ParserTests\TestMode as ParserTestMode; use Wikimedia\Parsoid\Parsoid; use Wikimedia\Parsoid\Utils\ContentUtils; use Wikimedia\Parsoid\Utils\DOMCompat; use Wikimedia\Parsoid\Utils\DOMDataUtils; use Wikimedia\Parsoid\Utils\DOMUtils; use Wikimedia\Rdbms\DBError; use Wikimedia\Rdbms\IDatabase; use Wikimedia\ScopedCallback; use Wikimedia\TestingAccessWrapper; /** * @ingroup Testing */ class ParserTestRunner { /** * @var array The status of each setup function */ private $setupDone = [ 'staticSetup' => false, 'perTestSetup' => false, 'setupDatabase' => false, 'setupUploads' => false, ]; /** * @var array (CLI/Config) Options for the test runner * See the constructor for documentation */ private $options; /** * @var array set of requested test modes */ private $requestedTestModes; /** * Our connection to the database * @var IDatabase */ private $db; /** * @var TestRecorder */ private $recorder; /** * The upload directory, or null to not set up an upload directory * * @var string|null */ private $uploadDir = null; /** * The name of the file backend to use, or false to use MockFileBackend. * @var string|false */ private $fileBackendName; /** * A complete regex for filtering tests. * @var string */ private $regex; /** * A list of normalization functions to apply to the expected and actual * output. * @var array */ private $normalizationFunctions = []; /** * Run disabled parser tests * @var bool */ private $runDisabled; /** * Disable parse on article insertion * @var bool */ private $disableSaveParse; /** * Reuse upload directory * @var bool */ private $keepUploads; /** @var Title */ private $defaultTitle; /** * Did some Parsoid test pass where it was expected to fail? * This can happen if the test failure is recorded in the -knownFailures.json file * but the test result changed, or functionality changed that causes tests to pass. * @var bool */ public $unexpectedTestPasses = false; /** * Table name prefix. */ public const DB_PREFIX = 'parsertest_'; /** * Compute the set of valid test runner modes * * @return array */ public function getRequestedTestModes(): array { return $this->requestedTestModes; } /** * @param TestRecorder $recorder * @param array $options * - parsoid (bool) if true, run Parsoid tests * - testFile (string) * If set, the (Parsoid) test file to run tests from. * Currently, only used for CLI PHPUnit test runs * to avoid running every single test file out there. * Legacy parser test runs ignore this option. * - wt2html (bool) If true, run Parsoid wt2html tests * - wt2wt (bool) If true, run Parsoid wt2wt tests * - html2wt (bool) If true, run Parsoid html2wt tests * - html2html (bool) If true, run Parsoid html2html tests * - selser (bool/"noauto") * If true, run Parsoid auto-generated selser tests * If "noauto", run Parsoid manual edit selser tests * - numchanges (int) number of selser edit tests to generate * - changetree (array|null) * If not null, run a Parsoid selser edit test with this changetree * - updateKnownFailures (bool) * If true, *knownFailures.json files are updated * - norm (array) * An array of normalization functions to run on test output * to use in legacy parser test runs * - regex (string) Regex for filtering tests * - run-disabled (bool) If true, run disabled tests * - keep-uploads (bool) If true, reuse upload directory * - file-backend (string|bool) * If false, use MockFileBackend * Else name of the file backend to use * - disable-save-parse (bool) if true, disable parse on article insertion * * NOTE: At this time, Parsoid-specific test options are only handled * in PHPUnit mode. A future patch will likely tweak some of this and * support these flags no matter how this test runner is instantiated. */ public function __construct( TestRecorder $recorder, $options = [] ) { $this->recorder = $recorder; $this->options = $options + [ 'keep-uploads' => false, 'file-backend' => false, 'run-disabled' => false, 'disable-save-parse' => false, 'upload-dir' => null, 'regex' => false, 'norm' => [], // Parsoid-specific options 'parsoid' => false, 'knownFailures' => true, 'updateKnownFailures' => false, 'changetree' => null, // Options can also match those in ParserTestModes::TEST_MODES // but we don't need to initialize those here; they will be // accessed via $this->requestedTestModes instead. ]; // Requested test modes are used for Parsoid tests and ignored for // legacy parser tests. $this->requestedTestModes = ParserTestMode::requestedTestModes( $this->options ); // @phan-suppress-next-line PhanEmptyForeach False positive foreach ( $this->options['norm'] as $func ) { if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) { $this->normalizationFunctions[] = $func; } else { $this->recorder->warning( "Warning: unknown normalization option \"$func\"\n" ); } } if ( $this->options['regex'] !== false ) { $this->regex = $this->options['regex']; } else { # Matches anything $this->regex = '//'; } $this->keepUploads = (bool)$this->options['keep-uploads']; $this->fileBackendName = $this->options['file-backend']; $this->runDisabled = (bool)$this->options['run-disabled']; $this->disableSaveParse = (bool)$this->options['disable-save-parse']; $this->uploadDir = $this->options['upload-dir']; $this->defaultTitle = Title::newFromText( 'Parser test' ); } /** * @return array */ public function getOptions(): array { return $this->options; } /** * Get list of filenames to extension and core parser tests * * @param array $dirs * @return array */ public static function getParserTestFiles( array $dirs = [] ): array { if ( $dirs ) { $ptDirs = []; foreach ( $dirs as $i => $dir ) { if ( !is_dir( $dir ) ) { echo "$dir is not a directory. Skipping it.\n"; continue; } $ptDirs["_CLI{$i}_"] = $dir; } } else { // Auto-discover core test files $ptDirs = [ 'core' => __DIR__ ]; // Auto-discover extension parser tests $registry = ExtensionRegistry::getInstance(); foreach ( $registry->getAllThings() as $info ) { $dir = dirname( $info['path'] ) . '/tests/parser'; if ( !is_dir( $dir ) ) { continue; } $ptDirs[ $info['name'] ] = $dir; } } $files = []; foreach ( $ptDirs as $extName => $dir ) { $counter = 1; $dirIterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir ) ); foreach ( $dirIterator as $fileInfo ) { /** @var SplFileInfo $fileInfo */ if ( str_ends_with( $fileInfo->getFilename(), '.txt' ) ) { $name = $extName . '_' . $counter; while ( isset( $files[$name] ) ) { $counter++; $name = $extName . '_' . $counter; } $files[$name] = $fileInfo->getPathname(); } } } return array_unique( $files ); } public function getRecorder() { return $this->recorder; } /** * Do any setup which can be done once for all tests, independent of test * options, except for database setup. * * Public setup functions in this class return a ScopedCallback object. When * this object is destroyed by going out of scope, teardown of the * corresponding test setup is performed. * * Teardown objects may be chained by passing a ScopedCallback from a * previous setup stage as the $nextTeardown parameter. This enforces the * convention that teardown actions are taken in reverse order to the * corresponding setup actions. When $nextTeardown is specified, a * ScopedCallback will be returned which first tears down the current * setup stage, and then tears down the previous setup stage which was * specified by $nextTeardown. * * @param ScopedCallback|null $nextTeardown * @return ScopedCallback */ public function staticSetup( $nextTeardown = null ) { // A note on coding style: // The general idea here is to keep setup code together with // corresponding teardown code, in a fine-grained manner. We have two // arrays: $setup and $teardown. The code snippets in the $setup array // are executed at the end of the method, before it returns, and the // code snippets in the $teardown array are executed in reverse order // when the Wikimedia\ScopedCallback object is consumed. // Because it is a common operation to save, set and restore global // variables, we have an additional convention: when the array key of // $setup is a string, the string is taken to be the name of the global // variable, and the element value is taken to be the desired new value. // It's acceptable to just do the setup immediately, instead of adding // a closure to $setup, except when the setup action depends on global // variable initialisation being done first. In this case, you have to // append a closure to $setup after the global variable is appended. // When you add to setup functions in this class, please keep associated // setup and teardown actions together in the source code, and please // add comments explaining why the setup action is necessary. $setup = []; $teardown = []; $teardown[] = $this->markSetupDone( 'staticSetup' ); // Some settings which influence HTML output $setup['wgSitename'] = 'MediaWiki'; $setup['wgMetaNamespace'] = "TestWiki"; $setup['wgServer'] = 'http://example.org'; $setup['wgServerName'] = 'example.org'; $setup['wgScriptPath'] = ''; $setup['wgScript'] = '/index.php'; $setup['wgResourceBasePath'] = ''; $setup['wgStylePath'] = '/skins'; $setup['wgExtensionAssetsPath'] = '/extensions'; $setup['wgArticlePath'] = '/wiki/$1'; $setup['wgActionPaths'] = []; $setup['wgVariantArticlePath'] = false; $setup['wgUploadNavigationUrl'] = false; $setup['wgCapitalLinks'] = true; $setup['wgNoFollowLinks'] = true; $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ]; $setup['wgExternalLinkTarget'] = false; $setup['wgLocaltimezone'] = 'UTC'; $setup['wgDisableLangConversion'] = false; $setup['wgDisableTitleConversion'] = false; $setup['wgUsePigLatinVariant'] = false; $reset = static function () { // Reset to follow changes to $wgDisable*Conversion MediaWikiServices::getInstance()->resetServiceForTesting( 'LanguageConverterFactory' ); }; $setup[] = $reset; $teardown[] = $reset; // "extra language links" // see https://gerrit.wikimedia.org/r/111390 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ]; // Parsoid settings for testing $setup['wgParsoidSettings'] = [ 'nativeGalleryEnabled' => true, ]; // All FileRepo changes should be done here by injecting services, // there should be no need to change global variables. MediaWikiServices::getInstance()->disableService( 'RepoGroup' ); MediaWikiServices::getInstance()->redefineService( 'RepoGroup', function () { return $this->createRepoGroup(); } ); $teardown[] = static function () { MediaWikiServices::getInstance()->resetServiceForTesting( 'RepoGroup' ); }; // Set up null lock managers $setup['wgLockManagers'] = [ [ 'name' => 'fsLockManager', 'class' => NullLockManager::class, ], [ 'name' => 'nullLockManager', 'class' => NullLockManager::class, ] ]; $reset = static function () { MediaWikiServices::getInstance()->resetServiceForTesting( 'LockManagerGroupFactory' ); }; $setup[] = $reset; $teardown[] = $reset; // This allows article insertion into the prefixed DB $setup['wgDefaultExternalStore'] = false; // This might slightly reduce memory usage $setup['wgAdaptiveMessageCache'] = true; // This is essential and overrides disabling of database messages in TestSetup $setup['wgUseDatabaseMessages'] = true; $reset = static function () { MediaWikiServices::getInstance()->resetServiceForTesting( 'MessageCache' ); }; $setup[] = $reset; $teardown[] = $reset; // It's not necessary to actually convert any files $setup['wgSVGConverter'] = 'null'; $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ]; // Fake constant timestamp MediaWikiServices::getInstance()->getHookContainer()->register( 'ParserGetVariableValueTs', function ( $parser, &$ts ) { $ts = $this->getFakeTimestamp(); return true; } ); $teardown[] = static function () { MediaWikiServices::getInstance()->getHookContainer()->clear( 'ParserGetVariableValueTs' ); }; $this->appendNamespaceSetup( $setup, $teardown ); // Set up interwikis and append teardown function $this->appendInterwikiSetup( $setup, $teardown ); // Set up a mock MediaHandlerFactory MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' ); MediaWikiServices::getInstance()->redefineService( 'MediaHandlerFactory', static function ( MediaWikiServices $services ) { $handlers = $services->getMainConfig()->get( MainConfigNames::ParserTestMediaHandlers ); return new MediaHandlerFactory( new NullLogger(), $handlers ); } ); $teardown[] = static function () { MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' ); }; // SqlBagOStuff broke when using temporary tables on r40209 (T17892). // It seems to have been fixed since (r55079?), but regressed at some point before r85701. // This works around it for now... global $wgObjectCaches; $setup['wgObjectCaches'] = [ CACHE_DB => $wgObjectCaches['hash'] ] + $wgObjectCaches; if ( isset( ObjectCache::$instances[CACHE_DB] ) ) { $savedCache = ObjectCache::$instances[CACHE_DB]; ObjectCache::$instances[CACHE_DB] = new HashBagOStuff; $teardown[] = static function () use ( $savedCache ) { ObjectCache::$instances[CACHE_DB] = $savedCache; }; } $teardown[] = $this->executeSetupSnippets( $setup ); // Schedule teardown snippets in reverse order return $this->createTeardownObject( $teardown, $nextTeardown ); } private function appendNamespaceSetup( &$setup, &$teardown ) { // Add a namespace shadowing a interwiki link, to test // proper precedence when resolving links. (T53680) $setup['wgExtraNamespaces'] = [ 100 => 'MemoryAlpha', 101 => 'MemoryAlpha_talk' ]; // Changing wgExtraNamespaces invalidates caches in NamespaceInfo and any live Language // object, both on setup and teardown $reset = static function () { MediaWikiServices::getInstance()->resetServiceForTesting( 'MainConfig' ); MediaWikiServices::getInstance()->resetServiceForTesting( 'NamespaceInfo' ); MediaWikiServices::getInstance()->resetServiceForTesting( 'LanguageFactory' ); MediaWikiServices::getInstance()->resetServiceForTesting( 'ContentLanguage' ); MediaWikiServices::getInstance()->resetServiceForTesting( 'LinkCache' ); MediaWikiServices::getInstance()->resetServiceForTesting( 'LanguageConverterFactory' ); }; $setup[] = $reset; $teardown[] = $reset; } /** * Create a RepoGroup object appropriate for the current configuration * @return RepoGroup */ protected function createRepoGroup() { if ( $this->uploadDir ) { if ( $this->fileBackendName ) { throw new MWException( 'You cannot specify both use-filebackend and upload-dir' ); } $backend = new FSFileBackend( [ 'name' => 'local-backend', 'wikiId' => WikiMap::getCurrentWikiId(), 'basePath' => $this->uploadDir, 'tmpDirectory' => wfTempDir() ] ); } elseif ( $this->fileBackendName ) { global $wgFileBackends; $name = $this->fileBackendName; $useConfig = false; foreach ( $wgFileBackends as $conf ) { if ( $conf['name'] === $name ) { $useConfig = $conf; } } if ( $useConfig === false ) { throw new MWException( "Unable to find file backend \"$name\"" ); } $useConfig['name'] = 'local-backend'; // swap name unset( $useConfig['lockManager'] ); $class = $useConfig['class']; // @phan-suppress-next-line PhanInvalidFQSENInClasslike $backend = new $class( $useConfig ); } else { # Replace with a mock. We do not care about generating real # files on the filesystem, just need to expose the file # informations. $backend = new MockFileBackend( [ 'name' => 'local-backend', 'wikiId' => WikiMap::getCurrentWikiId() ] ); } $services = MediaWikiServices::getInstance(); return new RepoGroup( [ 'class' => MockLocalRepo::class, 'name' => 'local', 'url' => 'http://example.com/images', 'hashLevels' => 2, 'transformVia404' => false, 'backend' => $backend ], [], $services->getMainWANObjectCache(), $services->getMimeAnalyzer() ); } /** * Execute an array in which elements with integer keys are taken to be * callable objects, and other elements are taken to be global variable * set operations, with the key giving the variable name and the value * giving the new global variable value. A closure is returned which, when * executed, sets the global variables back to the values they had before * this function was called. * * @see staticSetup * * @param array $setup * @return closure */ protected function executeSetupSnippets( $setup ) { $saved = []; foreach ( $setup as $name => $value ) { if ( is_int( $name ) ) { $value(); } else { $saved[$name] = $GLOBALS[$name] ?? null; $GLOBALS[$name] = $value; } } return function () use ( $saved ) { $this->executeSetupSnippets( $saved ); }; } /** * Take a setup array in the same format as the one given to * executeSetupSnippets(), and return a ScopedCallback which, when consumed, * executes the snippets in the setup array in reverse order. This is used * to create "teardown objects" for the public API. * * @see staticSetup * * @param array $teardown The snippet array * @param ScopedCallback|null $nextTeardown A ScopedCallback to consume * @return ScopedCallback */ protected function createTeardownObject( array $teardown, ?ScopedCallback $nextTeardown = null ) { return new ScopedCallback( function () use ( $teardown, $nextTeardown ) { // Schedule teardown snippets in reverse order $teardown = array_reverse( $teardown ); $this->executeSetupSnippets( $teardown ); if ( $nextTeardown ) { ScopedCallback::consume( $nextTeardown ); } } ); } /** * Set a setupDone flag to indicate that setup has been done, and return * the teardown closure. If the flag was already set, throw an exception. * * @param string $funcName The setup function name * @return closure */ protected function markSetupDone( $funcName ) { if ( $this->setupDone[$funcName] ) { throw new MWException( "$funcName is already done" ); } $this->setupDone[$funcName] = true; return function () use ( $funcName ) { $this->setupDone[$funcName] = false; }; } /** * Ensure one of the given setup stages has been done, throw an exception otherwise. * @param string $funcName */ protected function checkSetupDone( string $funcName ) { if ( !$this->setupDone[$funcName] ) { throw new MWException( "$funcName must be called before calling " . wfGetCaller() ); } } /** * Determine whether a particular setup function has been run * * @param string $funcName * @return bool */ public function isSetupDone( $funcName ) { return $this->setupDone[$funcName] ?? false; } /** * Insert hardcoded interwiki in the lookup table. * * This function insert a set of well known interwikis that are used in * the parser tests. We use the $wgInterwikiCache mechanism to completely * replace any other lookup. (Note that the InterwikiLoadPrefix hook * isn't used because it doesn't alter the result of * Interwiki::getAllPrefixes() and so is incompatible with some users, * including Parsoid.) * @param array &$setup * @param array &$teardown */ private function appendInterwikiSetup( &$setup, &$teardown ) { static $testInterwikis = [ [ 'iw_prefix' => 'local', // This is a "local interwiki" (see wgLocalInterwikis elsewhere in this file) 'iw_url' => 'http://example.org/wiki/$1', 'iw_local' => 1, ], // Local interwiki that matches a namespace name (T228616) [ 'iw_prefix' => 'project', // This is a "local interwiki" (see wgLocalInterwikis elsewhere in this file) 'iw_url' => 'http://example.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'wikipedia', 'iw_url' => 'http://en.wikipedia.org/wiki/$1', 'iw_local' => 0, ], [ 'iw_prefix' => 'meatball', // this has been updated in the live wikis, but the parser tests // expect the old value 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1', 'iw_local' => 0, ], [ 'iw_prefix' => 'memoryalpha', 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1', 'iw_local' => 0, ], [ 'iw_prefix' => 'zh', 'iw_url' => 'http://zh.wikipedia.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'es', 'iw_url' => 'http://es.wikipedia.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'fr', 'iw_url' => 'http://fr.wikipedia.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'ru', 'iw_url' => 'http://ru.wikipedia.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'mi', // This is a "local interwiki" (see wgLocalInterwikis elsewhere in this file) 'iw_url' => 'http://example.org/wiki/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'mul', 'iw_url' => 'http://wikisource.org/wiki/$1', 'iw_local' => 1, ], // Additions from Parsoid [ 'iw_prefix' => 'en', 'iw_url' => '//en.wikipedia.org/wiki/$1', 'iw_local' => 1 ], [ 'iw_prefix' => 'stats', 'iw_url' => 'https://stats.wikimedia.org/$1', 'iw_local' => 1, ], [ 'iw_prefix' => 'gerrit', 'iw_url' => 'https://gerrit.wikimedia.org/$1', 'iw_local' => 1, ], // Deliberately missing a $1 in the URL to exercise a common // misconfiguration. [ 'iw_prefix' => 'wikinvest', 'iw_url' => 'https://meta.wikimedia.org/wiki/Interwiki_map/discontinued#Wikinvest', 'iw_local' => 1, ], ]; // When running from parserTests.php, database setup happens *after* // interwiki setup, and that changes the wiki id. In order to avoid // breaking the interwiki cache, use 'global scope' for the interwiki // lookup. $GLOBAL_SCOPE = 2; // See docs for $wgInterwikiScopes $setup['wgInterwikiScopes'] = $GLOBAL_SCOPE; $setup['wgInterwikiCache'] = ClassicInterwikiLookup::buildCdbHash( $testInterwikis, $GLOBAL_SCOPE ); $reset = static function () { // Reset the service in case any other tests already cached some prefixes. MediaWikiServices::getInstance()->resetServiceForTesting( 'InterwikiLookup' ); }; $setup[] = $reset; $teardown[] = $reset; // This affects title normalization in links. It invalidates // MediaWikiTitleCodec objects. // These interwikis should have 'iw_url' that matches wgServer. $setup['wgLocalInterwikis'] = [ 'local', 'project', 'mi' ]; $reset = function () { $this->resetTitleServices(); }; $setup[] = $reset; $teardown[] = $reset; } /** * Reset the Title-related services that need resetting * for each test * * @todo We need to reset all services on every test */ private function resetTitleServices() { $services = MediaWikiServices::getInstance(); $services->resetServiceForTesting( 'TitleFormatter' ); $services->resetServiceForTesting( 'TitleParser' ); $services->resetServiceForTesting( '_MediaWikiTitleCodec' ); $services->resetServiceForTesting( 'LinkRenderer' ); $services->resetServiceForTesting( 'LinkRendererFactory' ); $services->resetServiceForTesting( 'NamespaceInfo' ); $services->resetServiceForTesting( 'SpecialPageFactory' ); } /** * Remove last character if it is a newline * @param string $s * @return string */ public static function chomp( $s ) { if ( substr( $s, -1 ) === "\n" ) { return substr( $s, 0, -1 ); } else { return $s; } } /** * Run a series of tests listed in the given text files. * Each test consists of a brief description, wikitext input, * and the expected HTML output. * * Prints status updates on stdout and counts up the total * number and percentage of passed tests. * * Handles all setup and teardown. * * @param array $filenames Array of strings * @return bool True if passed all tests, false if any tests failed. */ public function runTestsFromFiles( $filenames ) { $ok = false; $teardownGuard = null; $teardownGuard = $this->setupDatabase( $teardownGuard ); $teardownGuard = $this->staticSetup( $teardownGuard ); $teardownGuard = $this->setupUploads( $teardownGuard ); $this->recorder->start(); try { $ok = true; foreach ( $filenames as $filename ) { $this->recorder->startSuite( $filename ); if ( $this->options['parsoid'] ) { $ok = $this->runParsoidTests( $filename ) && $ok; } else { $ok = $this->runLegacyTests( $filename ) && $ok; } $this->recorder->endSuite( $filename ); } $this->recorder->report(); } catch ( DBError $e ) { $this->recorder->warning( $e->getMessage() ); } $this->recorder->end(); ScopedCallback::consume( $teardownGuard ); return $ok; } /** * Determine whether the current parser has the hooks registered in it * that are required by a file read by TestFileReader. * @param array $requirements * @return bool */ public function meetsRequirements( $requirements ) { foreach ( $requirements as $requirement ) { $ok = true; switch ( $requirement['type'] ) { case 'hook': $ok = $this->requireHook( $requirement['name'] ); break; case 'functionHook': $ok = $this->requireFunctionHook( $requirement['name'] ); break; } if ( !$ok ) { return false; } } return true; } /** * Run the legacy parser tests from a single file. staticSetup() and * setupDatabase() must have been called already. * * @param string $filename Test file name * @return bool True if passed all tests, false if any tests failed. */ public function runLegacyTests( string $filename ): bool { $mode = new ParserTestMode( 'legacy' ); $testFileInfo = TestFileReader::read( $filename, static function ( $msg ) { wfDeprecatedMsg( $msg, '1.35', false, false ); } ); $this->checkSetupDone( 'staticSetup' ); // If any requirements are not met, mark all tests from the file as skipped $skipMessage = $this->getFileSkipMessage( true, $testFileInfo->fileOptions, $filename ); if ( $skipMessage !== null ) { foreach ( $testFileInfo->testCases as $test ) { $this->recorder->startTest( $test, $mode ); $this->recorder->skipped( $test, $mode, $skipMessage ); } return true; } // Add articles $teardown = $this->addArticles( $testFileInfo->articles ); // Run tests $ok = true; foreach ( $testFileInfo->testCases as $test ) { $result = $this->runTest( $test, $mode ); $ok = $ok && $result->isSuccess(); } // Clean up ScopedCallback::consume( $teardown ); return $ok; } /** * @param bool $isLegacy * @param array $fileOptions * @param string $filename * @return string|null */ public function getFileSkipMessage( bool $isLegacy, array $fileOptions, string $filename ): ?string { $runnerOpts = $this->getOptions(); // Verify minimum version # $testFormat = intval( $fileOptions['version'] ?? '1' ); if ( $testFormat < 2 ) { throw new MWException( "$filename needs an update. Support for the parserTest v1 file format was removed in MediaWiki 1.36" ); } // If any requirements are not met, mark all tests from the file as skipped if ( !( $isLegacy || isset( $fileOptions['parsoid-compatible'] ) || ( $runnerOpts['parsoid'] ?? false ) ) ) { // Running files in Parsoid integrated mode is opt-in for now. return 'not compatible with Parsoid integrated mode'; } elseif ( !$this->meetsRequirements( $fileOptions['requirements'] ?? [] ) ) { return 'required extension not enabled'; } elseif ( ( $runnerOpts['testFile'] ?? $filename ) !== $filename ) { return 'Not the requested test file'; } else { return null; } } public function getTestSkipMessage( ParserTest $test, ParserTestMode $mode ) { if ( $test->wikitext === null ) { // Note that /in theory/ we could have pure html2html tests // with no wikitext section, but /in practice/ all tests // include a wikitext section. $test->error( "Test lacks wikitext section", $test->testName ); } // Skip disabled / filtered tests if ( isset( $test->options['disabled'] ) && !$this->runDisabled ) { return "Test disabled"; } $testFilter = [ 'regex' => $this->regex ]; if ( !$test->matchesFilter( $testFilter ) ) { return "Test doesn't match filter"; } // Skip parsoid-only tests if running in a legacy test mode if ( $test->legacyHtml === 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'] ) ) { if ( $mode->isLegacy() ) { // Not an error, just skip this test if we're in // legacy mode. 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 ); } } return null; } /** * Compute valid test modes based on requested modes and file-enabled modes * @param array $testModes * @param array $fileOptions * @return array */ public function computeValidTestModes( array $testModes, array $fileOptions ): array { $modeRestriction = $fileOptions['parsoid-compatible'] ?? false; if ( $modeRestriction !== false ) { if ( is_string( $modeRestriction ) ) { // shorthand $modeRestriction = [ $modeRestriction ]; } $testModes = array_values( array_intersect( $testModes, $modeRestriction ) ); } return $testModes; } /** * Run the tests from a single file. staticSetup() and setupDatabase() * must have been called already. * * @param string $filename Test file name * @return bool True if passed all tests, false if any tests failed. */ public function runParsoidTests( string $filename ): bool { $testFileInfo = TestFileReader::read( $filename, static function ( $msg ) { wfDeprecatedMsg( $msg, '1.35', false, false ); } ); // Intersect requested modes with test modes enabled in the file $testModes = $this->computeValidTestModes( $this->getRequestedTestModes(), $testFileInfo->fileOptions ); $this->checkSetupDone( 'staticSetup' ); // If any requirements are not met, mark all tests from the file as skipped if ( !$testModes ) { return true; } $skipMode = new ParserTestMode( $testModes[0] ); $skipMessage = $this->getFileSkipMessage( false, $testFileInfo->fileOptions, $filename ); if ( $skipMessage !== null ) { foreach ( $testFileInfo->testCases as $test ) { $this->recorder->startTest( $test, $skipMode ); $this->recorder->skipped( $test, $skipMode, $skipMessage ); } return true; } // Add articles $teardown = $this->addArticles( $testFileInfo->articles ); // Run tests $ok = true; $runner = $this; foreach ( $testFileInfo->testCases as $t ) { $t->testAllModes( $t->computeTestModes( $testModes ), $this->options, function ( ParserTest $test, string $modeStr, array $options ) use ( $runner, $t, &$ok ) { // $test could be a clone of $t // Ensure that updates to knownFailures in $test are reflected in $t $test->knownFailures = &$t->knownFailures; $mode = new ParserTestMode( $modeStr, $test->changetree ); if ( $modeStr === 'selser' && $test->changetree === null ) { // This is an auto-edit test with either a CLI changetree // or a change tree that should be generated $mode = new ParserTestMode( 'selser-auto', json_decode( $runner->options['changetree'] ) ); $result = $this->runTest( $test, $mode ); // FIXME: Test.php in Parsoid doesn't know which tests are being // skipped for what reason. For now, prevent crashers on skipped tests // by matching expectations of Test.php::isDuplicateChangeTree(..) if ( $result->expected === 'SKIP' ) { // Make sure change tree is not null for skipped selser tests $test->changetree = []; } } else { $result = $this->runTest( $test, $mode ); } $ok = $ok && $result->isSuccess(); } ); } if ( $this->options['updateKnownFailures'] ) { $this->updateKnownFailures( $testFileInfo ); } // Clean up ScopedCallback::consume( $teardown ); return $ok; } /** * Update known failures JSON file for the parser tests file * @param TestFileReader $testFileInfo */ public function updateKnownFailures( TestFileReader $testFileInfo ): void { $testKnownFailures = []; foreach ( $testFileInfo->testCases as $t ) { if ( $t->knownFailures && $t->testName ) { // @phan-suppress-next-line PhanTypeMismatchDimAssignment False positive $testKnownFailures[$t->testName] = $t->knownFailures; // FIXME: This reduces noise when updateKnownFailures is used // with a subset of test modes. But, this also mixes up the selser // test results with non-selser ones. // ksort( $testKnownFailures[$t->testName] ); } } // Sort, otherwise, titles get added above based on the first // failing mode, which can make diffs harder to verify when // failing modes change. ksort( $testKnownFailures ); // Cast to object to ensure that empty list is encoded as `{}` not `[]` $contents = FormatJson::encode( (object)$testKnownFailures, "\t", FormatJson::ALL_OK ) . "\n"; if ( file_exists( $testFileInfo->knownFailuresPath ) ) { $old = file_get_contents( $testFileInfo->knownFailuresPath ); } else { // If file doesn't exist, use the JSON representation of an // empty array, so it compares equal in the case that we // end up with an empty array of known failures below. $old = "{}"; } if ( $testFileInfo->knownFailuresPath && $old !== $contents ) { $this->recorder->warning( "Updating known failures file: {$testFileInfo->knownFailuresPath}" ); file_put_contents( $testFileInfo->knownFailuresPath, $contents ); } } /** * @param string $wikitext * @return array */ private function getRevRecordProperties( string $wikitext ): array { return [ 'pageid' => 187, // Some random fake page id 'revid' => 1337, // see Parser::getRevisionId() 'timestamp' => $this->getFakeTimestamp(), 'wikitext' => $wikitext ]; } /** * Create a mutable rev record for test use. * * @param Title $title * @param User $user * @param array $revProps * @return RevisionRecord */ private function createRevRecord( Title $title, User $user, array $revProps ): RevisionRecord { $content = new WikitextContent( $revProps['wikitext'] ); $title = Title::newFromRow( (object)[ 'page_id' => $revProps['pageid'], 'page_len' => $content->getSize(), 'page_latest' => $revProps['revid'], 'page_namespace' => $title->getNamespace(), 'page_title' => $title->getDBkey(), 'page_is_redirect' => 0 ] ); $revRecord = new MutableRevisionRecord( $title ); $revRecord->setContent( SlotRecord::MAIN, $content ) ->setUser( $user ) ->setTimestamp( strval( $revProps['timestamp'] ) ) ->setPageId( $title->getArticleID() ) ->setId( $title->getLatestRevID() ); return $revRecord; } /** * Shared code to initialize ParserOptions based on the $test object, * used by both the legacy Parser and the Parsoid parser. * @param ParserTest $test * @param callable $parserOptionsCallback A callback to create the * initial ParserOptions object. This allows for some minor * differences in how the legacy Parser and Parsoid create this. * @return array An array of Title, ParserOptions, and integer revId. */ private function setupParserOptions( ParserTest $test, callable $parserOptionsCallback ) { $opts = $test->options; $context = RequestContext::getMain(); $wikitext = $test->wikitext; '@phan-var string $wikitext'; // assert that this is not null $revProps = $this->getRevRecordProperties( $wikitext ); $user = $context->getUser(); $title = isset( $opts['title'] ) ? Title::newFromText( $opts['title'] ) : $this->defaultTitle; $revRecord = null; if ( isset( $opts['lastsavedrevision'] ) ) { $revRecord = $this->createRevRecord( $title, $user, $revProps ); $revProps['rev'] = $revRecord; // Increment timestamp so that parser tests can distinguish between // ParserOptions source and RevisionRecord $revProps['timestamp'] += 234; } $options = $parserOptionsCallback( $context, $title, $revProps ); $options->setTimestamp( MWTimestamp::convert( TS_MW, $revProps['timestamp'] ) ); $options->setUserLang( $context->getLanguage() ); if ( isset( $opts['lastsavedrevision'] ) ) { $oldCallback = $options->getCurrentRevisionRecordCallback(); $options->setCurrentRevisionRecordCallback( static function ( Title $t, $parser = null ) use ( $title, $revRecord, $oldCallback ) { if ( $t->equals( $title ) ) { return $revRecord; } else { return $oldCallback( $t, $parser ); } } ); } if ( isset( $opts['maxincludesize'] ) ) { $options->setMaxIncludeSize( $opts['maxincludesize'] ); } if ( isset( $opts['maxtemplatedepth'] ) ) { $options->setMaxTemplateDepth( $opts['maxtemplatedepth'] ); } return [ $title, $options, $revProps['revid'] ]; } /** * Get a Parser object * * @return Parser */ public function getParser() { $parserFactory = MediaWikiServices::getInstance()->getParserFactory(); $parser = $parserFactory->create(); // A fresh parser object. ParserTestParserHook::setup( $parser ); return $parser; } /** * Run a given wikitext input through either the legacy wiki parser * or Parsoid, depending on the given test mode, and compare the * output against the expected results. * * @param ParserTest $test The test parameters * @param ParserTestMode $mode The test mode * @return ParserTestResult The test results. */ public function runTest( ParserTest $test, ParserTestMode $mode ): ParserTestResult { if ( $this->getTestSkipMessage( $test, $mode ) ) { return new ParserTestResult( $test, $mode, 'SKIP', 'SKIP' ); } $this->recorder->startTest( $test, $mode ); $result = $mode->isLegacy() ? $this->runLegacyTest( $test, $mode ) : $this->runParsoidTest( $test, $mode ); if ( $result === false ) { $this->recorder->skipped( $test, $mode, 'SKIP' ); return new ParserTestResult( $test, $mode, 'SKIP', 'SKIP' ); } else { $this->recorder->record( $result ); return $result; } } /** * Run a given wikitext input through a freshly-constructed instance * of the legacy wiki parser, and compare the output against the expected * results. * * Prints status and explanatory messages to stdout. * * staticSetup() and setupWikiData() must be called before this function * is entered. * * @param ParserTest $test The test parameters * @param ParserTestMode $mode The test mode * @return ParserTestResult|false false if skipped */ public function runLegacyTest( ParserTest $test, ParserTestMode $mode ) { $desc = ( $test->comment ?? '' ) . $test->testName; wfDebug( __METHOD__ . ": running $desc" ); $opts = $test->options; if ( isset( $opts['preprocessor'] ) && $opts['preprocessor'] !== 'Preprocessor_Hash' ) { wfDeprecated( 'preprocessor=Preprocessor_DOM', '1.36' ); return false; // Skip test. } $teardownGuard = $this->perTestSetup( $test ); [ $title, $options, $revId ] = $this->setupParserOptions( $test, static function ( $context, $title, $revProps ) { return ParserOptions::newFromContext( $context ); } ); $local = isset( $opts['local'] ); $parser = $this->getParser(); if ( isset( $opts['styletag'] ) ) { // For testing the behavior of