3 * Generic backend for the MediaWiki parser test suite, used by both the
4 * standalone parserTests.php and the PHPUnit "parsertests" suite.
6 * Copyright © 2004, 2010 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @todo Make this more independent of the configuration (and if possible the database)
29 use Wikimedia\Rdbms\IDatabase
;
30 use MediaWiki\MediaWikiServices
;
31 use MediaWiki\Tidy\TidyDriverBase
;
32 use Wikimedia\ScopedCallback
;
33 use Wikimedia\TestingAccessWrapper
;
38 class ParserTestRunner
{
41 * MediaWiki core parser test files, paths
42 * will be prefixed with __DIR__ . '/'
46 private static $coreTestFiles = [
48 'extraParserTests.txt',
52 * @var bool $useTemporaryTables Use temporary tables for the temporary database
54 private $useTemporaryTables = true;
57 * @var array $setupDone The status of each setup function
59 private $setupDone = [
60 'staticSetup' => false,
61 'perTestSetup' => false,
62 'setupDatabase' => false,
63 'setDatabase' => false,
64 'setupUploads' => false,
68 * Our connection to the database
74 * Database clone helper
82 private $tidyDriver = null;
90 * The upload directory, or null to not set up an upload directory
94 private $uploadDir = null;
97 * The name of the file backend to use, or null to use MockFileBackend.
100 private $fileBackendName;
103 * A complete regex for filtering tests.
109 * A list of normalization functions to apply to the expected and actual
113 private $normalizationFunctions = [];
116 * Run disabled parser tests
119 private $runDisabled;
122 * Disable parse on article insertion
125 private $disableSaveParse;
128 * Reuse upload directory
131 private $keepUploads;
134 private $defaultTitle;
137 * @param TestRecorder $recorder
138 * @param array $options
140 public function __construct( TestRecorder
$recorder, $options = [] ) {
141 $this->recorder
= $recorder;
143 if ( isset( $options['norm'] ) ) {
144 foreach ( $options['norm'] as $func ) {
145 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
146 $this->normalizationFunctions
[] = $func;
148 $this->recorder
->warning(
149 "Warning: unknown normalization option \"$func\"\n" );
154 if ( isset( $options['regex'] ) && $options['regex'] !== false ) {
155 $this->regex
= $options['regex'];
161 $this->keepUploads
= !empty( $options['keep-uploads'] );
163 $this->fileBackendName
= $options['file-backend'] ??
false;
165 $this->runDisabled
= !empty( $options['run-disabled'] );
167 $this->disableSaveParse
= !empty( $options['disable-save-parse'] );
169 if ( isset( $options['upload-dir'] ) ) {
170 $this->uploadDir
= $options['upload-dir'];
173 $this->defaultTitle
= Title
::newFromText( 'Parser test' );
177 * Get list of filenames to extension and core parser tests
181 public static function getParserTestFiles() {
182 global $wgParserTestFiles;
184 // Add core test files
185 $files = array_map( function ( $item ) {
186 return __DIR__
. "/$item";
187 }, self
::$coreTestFiles );
189 // Plus legacy global files
190 $files = array_merge( $files, $wgParserTestFiles );
192 // Auto-discover extension parser tests
193 $registry = ExtensionRegistry
::getInstance();
194 foreach ( $registry->getAllThings() as $info ) {
195 $dir = dirname( $info['path'] ) . '/tests/parser';
196 if ( !file_exists( $dir ) ) {
200 $dirIterator = new RecursiveIteratorIterator(
201 new RecursiveDirectoryIterator( $dir )
203 foreach ( $dirIterator as $fileInfo ) {
204 /** @var SplFileInfo $fileInfo */
205 if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) {
206 $name = $info['name'] . $counter;
207 while ( isset( $files[$name] ) ) {
208 $name = $info['name'] . '_' . $counter++
;
210 $files[$name] = $fileInfo->getPathname();
215 return array_unique( $files );
218 public function getRecorder() {
219 return $this->recorder
;
223 * Do any setup which can be done once for all tests, independent of test
224 * options, except for database setup.
226 * Public setup functions in this class return a ScopedCallback object. When
227 * this object is destroyed by going out of scope, teardown of the
228 * corresponding test setup is performed.
230 * Teardown objects may be chained by passing a ScopedCallback from a
231 * previous setup stage as the $nextTeardown parameter. This enforces the
232 * convention that teardown actions are taken in reverse order to the
233 * corresponding setup actions. When $nextTeardown is specified, a
234 * ScopedCallback will be returned which first tears down the current
235 * setup stage, and then tears down the previous setup stage which was
236 * specified by $nextTeardown.
238 * @param ScopedCallback|null $nextTeardown
239 * @return ScopedCallback
241 public function staticSetup( $nextTeardown = null ) {
242 // A note on coding style:
244 // The general idea here is to keep setup code together with
245 // corresponding teardown code, in a fine-grained manner. We have two
246 // arrays: $setup and $teardown. The code snippets in the $setup array
247 // are executed at the end of the method, before it returns, and the
248 // code snippets in the $teardown array are executed in reverse order
249 // when the Wikimedia\ScopedCallback object is consumed.
251 // Because it is a common operation to save, set and restore global
252 // variables, we have an additional convention: when the array key of
253 // $setup is a string, the string is taken to be the name of the global
254 // variable, and the element value is taken to be the desired new value.
256 // It's acceptable to just do the setup immediately, instead of adding
257 // a closure to $setup, except when the setup action depends on global
258 // variable initialisation being done first. In this case, you have to
259 // append a closure to $setup after the global variable is appended.
261 // When you add to setup functions in this class, please keep associated
262 // setup and teardown actions together in the source code, and please
263 // add comments explaining why the setup action is necessary.
268 $teardown[] = $this->markSetupDone( 'staticSetup' );
270 // Some settings which influence HTML output
271 $setup['wgSitename'] = 'MediaWiki';
272 $setup['wgServer'] = 'http://example.org';
273 $setup['wgServerName'] = 'example.org';
274 $setup['wgScriptPath'] = '';
275 $setup['wgScript'] = '/index.php';
276 $setup['wgResourceBasePath'] = '';
277 $setup['wgStylePath'] = '/skins';
278 $setup['wgExtensionAssetsPath'] = '/extensions';
279 $setup['wgArticlePath'] = '/wiki/$1';
280 $setup['wgActionPaths'] = [];
281 $setup['wgVariantArticlePath'] = false;
282 $setup['wgUploadNavigationUrl'] = false;
283 $setup['wgCapitalLinks'] = true;
284 $setup['wgNoFollowLinks'] = true;
285 $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
286 $setup['wgExternalLinkTarget'] = false;
287 $setup['wgLocaltimezone'] = 'UTC';
288 $setup['wgHtml5'] = true;
289 $setup['wgDisableLangConversion'] = false;
290 $setup['wgDisableTitleConversion'] = false;
292 // "extra language links"
293 // see https://gerrit.wikimedia.org/r/111390
294 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
296 // All FileRepo changes should be done here by injecting services,
297 // there should be no need to change global variables.
298 MediaWikiServices
::getInstance()->disableService( 'RepoGroup' );
299 MediaWikiServices
::getInstance()->redefineService( 'RepoGroup',
301 return $this->createRepoGroup();
304 $teardown[] = function () {
305 MediaWikiServices
::getInstance()->resetServiceForTesting( 'RepoGroup' );
308 // Set up null lock managers
309 $setup['wgLockManagers'] = [ [
310 'name' => 'fsLockManager',
311 'class' => NullLockManager
::class,
313 'name' => 'nullLockManager',
314 'class' => NullLockManager
::class,
316 $reset = function () {
317 LockManagerGroup
::destroySingletons();
320 $teardown[] = $reset;
322 // This allows article insertion into the prefixed DB
323 $setup['wgDefaultExternalStore'] = false;
325 // This might slightly reduce memory usage
326 $setup['wgAdaptiveMessageCache'] = true;
328 // This is essential and overrides disabling of database messages in TestSetup
329 $setup['wgUseDatabaseMessages'] = true;
330 $reset = function () {
331 MessageCache
::destroyInstance();
334 $teardown[] = $reset;
336 // It's not necessary to actually convert any files
337 $setup['wgSVGConverter'] = 'null';
338 $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
340 // Fake constant timestamp
341 Hooks
::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) {
342 $ts = $this->getFakeTimestamp();
345 $teardown[] = function () {
346 Hooks
::clear( 'ParserGetVariableValueTs' );
349 $this->appendNamespaceSetup( $setup, $teardown );
351 // Set up interwikis and append teardown function
352 $teardown[] = $this->setupInterwikis();
354 // This affects title normalization in links. It invalidates
355 // MediaWikiTitleCodec objects.
356 $setup['wgLocalInterwikis'] = [ 'local', 'mi' ];
357 $reset = function () {
358 $this->resetTitleServices();
361 $teardown[] = $reset;
363 // Set up a mock MediaHandlerFactory
364 MediaWikiServices
::getInstance()->disableService( 'MediaHandlerFactory' );
365 MediaWikiServices
::getInstance()->redefineService(
366 'MediaHandlerFactory',
367 function ( MediaWikiServices
$services ) {
368 $handlers = $services->getMainConfig()->get( 'ParserTestMediaHandlers' );
369 return new MediaHandlerFactory( $handlers );
372 $teardown[] = function () {
373 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
376 // SqlBagOStuff broke when using temporary tables on r40209 (T17892).
377 // It seems to have been fixed since (r55079?), but regressed at some point before r85701.
378 // This works around it for now...
379 global $wgObjectCaches;
380 $setup['wgObjectCaches'] = [ CACHE_DB
=> $wgObjectCaches['hash'] ] +
$wgObjectCaches;
381 if ( isset( ObjectCache
::$instances[CACHE_DB
] ) ) {
382 $savedCache = ObjectCache
::$instances[CACHE_DB
];
383 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
384 $teardown[] = function () use ( $savedCache ) {
385 ObjectCache
::$instances[CACHE_DB
] = $savedCache;
389 $teardown[] = $this->executeSetupSnippets( $setup );
391 // Schedule teardown snippets in reverse order
392 return $this->createTeardownObject( $teardown, $nextTeardown );
395 private function appendNamespaceSetup( &$setup, &$teardown ) {
396 // Add a namespace shadowing a interwiki link, to test
397 // proper precedence when resolving links. (T53680)
398 $setup['wgExtraNamespaces'] = [
399 100 => 'MemoryAlpha',
400 101 => 'MemoryAlpha_talk'
402 // Changing wgExtraNamespaces invalidates caches in NamespaceInfo and any live Language
403 // object, both on setup and teardown
404 $reset = function () {
405 MediaWikiServices
::getInstance()->resetServiceForTesting( 'NamespaceInfo' );
406 MediaWikiServices
::getInstance()->getContentLanguage()->resetNamespaces();
409 $teardown[] = $reset;
413 * Create a RepoGroup object appropriate for the current configuration
416 protected function createRepoGroup() {
417 if ( $this->uploadDir
) {
418 if ( $this->fileBackendName
) {
419 throw new MWException( 'You cannot specify both use-filebackend and upload-dir' );
421 $backend = new FSFileBackend( [
422 'name' => 'local-backend',
423 'wikiId' => wfWikiID(),
424 'basePath' => $this->uploadDir
,
425 'tmpDirectory' => wfTempDir()
427 } elseif ( $this->fileBackendName
) {
428 global $wgFileBackends;
429 $name = $this->fileBackendName
;
431 foreach ( $wgFileBackends as $conf ) {
432 if ( $conf['name'] === $name ) {
436 if ( $useConfig === false ) {
437 throw new MWException( "Unable to find file backend \"$name\"" );
439 $useConfig['name'] = 'local-backend'; // swap name
440 unset( $useConfig['lockManager'] );
441 unset( $useConfig['fileJournal'] );
442 $class = $useConfig['class'];
443 $backend = new $class( $useConfig );
445 # Replace with a mock. We do not care about generating real
446 # files on the filesystem, just need to expose the file
448 $backend = new MockFileBackend( [
449 'name' => 'local-backend',
450 'wikiId' => wfWikiID()
454 return new RepoGroup(
456 'class' => MockLocalRepo
::class,
458 'url' => 'http://example.com/images',
460 'transformVia404' => false,
461 'backend' => $backend
464 MediaWikiServices
::getInstance()->getMainWANObjectCache()
469 * Execute an array in which elements with integer keys are taken to be
470 * callable objects, and other elements are taken to be global variable
471 * set operations, with the key giving the variable name and the value
472 * giving the new global variable value. A closure is returned which, when
473 * executed, sets the global variables back to the values they had before
474 * this function was called.
478 * @param array $setup
481 protected function executeSetupSnippets( $setup ) {
483 foreach ( $setup as $name => $value ) {
484 if ( is_int( $name ) ) {
487 $saved[$name] = $GLOBALS[$name] ??
null;
488 $GLOBALS[$name] = $value;
491 return function () use ( $saved ) {
492 $this->executeSetupSnippets( $saved );
497 * Take a setup array in the same format as the one given to
498 * executeSetupSnippets(), and return a ScopedCallback which, when consumed,
499 * executes the snippets in the setup array in reverse order. This is used
500 * to create "teardown objects" for the public API.
504 * @param array $teardown The snippet array
505 * @param ScopedCallback|null $nextTeardown A ScopedCallback to consume
506 * @return ScopedCallback
508 protected function createTeardownObject( $teardown, $nextTeardown = null ) {
509 return new ScopedCallback( function () use ( $teardown, $nextTeardown ) {
510 // Schedule teardown snippets in reverse order
511 $teardown = array_reverse( $teardown );
513 $this->executeSetupSnippets( $teardown );
514 if ( $nextTeardown ) {
515 ScopedCallback
::consume( $nextTeardown );
521 * Set a setupDone flag to indicate that setup has been done, and return
522 * the teardown closure. If the flag was already set, throw an exception.
524 * @param string $funcName The setup function name
527 protected function markSetupDone( $funcName ) {
528 if ( $this->setupDone
[$funcName] ) {
529 throw new MWException( "$funcName is already done" );
531 $this->setupDone
[$funcName] = true;
532 return function () use ( $funcName ) {
533 $this->setupDone
[$funcName] = false;
538 * Ensure a given setup stage has been done, throw an exception if it has
540 * @param string $funcName
541 * @param string|null $funcName2
543 protected function checkSetupDone( $funcName, $funcName2 = null ) {
544 if ( !$this->setupDone
[$funcName]
545 && ( $funcName === null ||
!$this->setupDone
[$funcName2] )
547 throw new MWException( "$funcName must be called before calling " .
553 * Determine whether a particular setup function has been run
555 * @param string $funcName
558 public function isSetupDone( $funcName ) {
559 return $this->setupDone
[$funcName] ??
false;
563 * Insert hardcoded interwiki in the lookup table.
565 * This function insert a set of well known interwikis that are used in
566 * the parser tests. They can be considered has fixtures are injected in
567 * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
568 * Since we are not interested in looking up interwikis in the database,
569 * the hook completely replace the existing mechanism (hook returns false).
571 * @return closure for teardown
573 private function setupInterwikis() {
574 # Hack: insert a few Wikipedia in-project interwiki prefixes,
575 # for testing inter-language links
576 Hooks
::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
577 static $testInterwikis = [
579 'iw_url' => 'http://doesnt.matter.org/$1',
584 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
589 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
594 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
599 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
604 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
609 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
614 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
619 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
624 'iw_url' => 'http://wikisource.org/wiki/$1',
629 if ( array_key_exists( $prefix, $testInterwikis ) ) {
630 $iwData = $testInterwikis[$prefix];
633 // We only want to rely on the above fixtures
635 } );// hooks::register
637 // Reset the service in case any other tests already cached some prefixes.
638 MediaWikiServices
::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
642 Hooks
::clear( 'InterwikiLoadPrefix' );
643 MediaWikiServices
::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
648 * Reset the Title-related services that need resetting
651 * @todo We need to reset all services on every test
653 private function resetTitleServices() {
654 $services = MediaWikiServices
::getInstance();
655 $services->resetServiceForTesting( 'TitleFormatter' );
656 $services->resetServiceForTesting( 'TitleParser' );
657 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
658 $services->resetServiceForTesting( 'LinkRenderer' );
659 $services->resetServiceForTesting( 'LinkRendererFactory' );
660 $services->resetServiceForTesting( 'NamespaceInfo' );
664 * Remove last character if it is a newline
668 public static function chomp( $s ) {
669 if ( substr( $s, -1 ) === "\n" ) {
670 return substr( $s, 0, -1 );
677 * Run a series of tests listed in the given text files.
678 * Each test consists of a brief description, wikitext input,
679 * and the expected HTML output.
681 * Prints status updates on stdout and counts up the total
682 * number and percentage of passed tests.
684 * Handles all setup and teardown.
686 * @param array $filenames Array of strings
687 * @return bool True if passed all tests, false if any tests failed.
689 public function runTestsFromFiles( $filenames ) {
692 $teardownGuard = $this->staticSetup();
693 $teardownGuard = $this->setupDatabase( $teardownGuard );
694 $teardownGuard = $this->setupUploads( $teardownGuard );
696 $this->recorder
->start();
700 foreach ( $filenames as $filename ) {
701 $testFileInfo = TestFileReader
::read( $filename, [
702 'runDisabled' => $this->runDisabled
,
703 'regex' => $this->regex
] );
705 // Don't start the suite if there are no enabled tests in the file
706 if ( !$testFileInfo['tests'] ) {
710 $this->recorder
->startSuite( $filename );
711 $ok = $this->runTests( $testFileInfo ) && $ok;
712 $this->recorder
->endSuite( $filename );
715 $this->recorder
->report();
716 } catch ( DBError
$e ) {
717 $this->recorder
->warning( $e->getMessage() );
719 $this->recorder
->end();
721 ScopedCallback
::consume( $teardownGuard );
727 * Determine whether the current parser has the hooks registered in it
728 * that are required by a file read by TestFileReader.
729 * @param array $requirements
732 public function meetsRequirements( $requirements ) {
733 foreach ( $requirements as $requirement ) {
734 switch ( $requirement['type'] ) {
736 $ok = $this->requireHook( $requirement['name'] );
739 $ok = $this->requireFunctionHook( $requirement['name'] );
741 case 'transparentHook':
742 $ok = $this->requireTransparentHook( $requirement['name'] );
753 * Run the tests from a single file. staticSetup() and setupDatabase()
754 * must have been called already.
756 * @param array $testFileInfo Parsed file info returned by TestFileReader
757 * @return bool True if passed all tests, false if any tests failed.
759 public function runTests( $testFileInfo ) {
762 $this->checkSetupDone( 'staticSetup' );
764 // Don't add articles from the file if there are no enabled tests from the file
765 if ( !$testFileInfo['tests'] ) {
769 // If any requirements are not met, mark all tests from the file as skipped
770 if ( !$this->meetsRequirements( $testFileInfo['requirements'] ) ) {
771 foreach ( $testFileInfo['tests'] as $test ) {
772 $this->recorder
->startTest( $test );
773 $this->recorder
->skipped( $test, 'required extension not enabled' );
779 $this->addArticles( $testFileInfo['articles'] );
782 foreach ( $testFileInfo['tests'] as $test ) {
783 $this->recorder
->startTest( $test );
785 $this->runTest( $test );
786 if ( $result !== false ) {
787 $ok = $ok && $result->isSuccess();
788 $this->recorder
->record( $test, $result );
796 * Get a Parser object
798 * @param string|null $preprocessor
801 function getParser( $preprocessor = null ) {
802 global $wgParserConf;
804 $class = $wgParserConf['class'];
805 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] +
$wgParserConf );
806 if ( $preprocessor ) {
807 # Suppress deprecation warning for Preprocessor_DOM while testing
808 Wikimedia\
suppressWarnings();
809 wfDeprecated( 'Preprocessor_DOM::__construct' );
810 Wikimedia\restoreWarnings
();
811 $parser->getPreprocessor();
813 ParserTestParserHook
::setup( $parser );
819 * Run a given wikitext input through a freshly-constructed wiki parser,
820 * and compare the output against the expected results.
821 * Prints status and explanatory messages to stdout.
823 * staticSetup() and setupWikiData() must be called before this function
826 * @param array $test The test parameters:
827 * - test: The test name
828 * - desc: The subtest description
829 * - input: Wikitext to try rendering
830 * - options: Array of test options
831 * - config: Overrides for global variables, one per line
833 * @return ParserTestResult|false false if skipped
835 public function runTest( $test ) {
836 wfDebug( __METHOD__
. ": running {$test['desc']}" );
837 $opts = $this->parseOptions( $test['options'] );
838 $teardownGuard = $this->perTestSetup( $test );
840 $context = RequestContext
::getMain();
841 $user = $context->getUser();
842 $options = ParserOptions
::newFromContext( $context );
843 $options->setTimestamp( $this->getFakeTimestamp() );
845 if ( isset( $opts['tidy'] ) ) {
846 $options->setTidy( true );
849 $revId = 1337; // see Parser::getRevisionId()
850 $title = isset( $opts['title'] )
851 ? Title
::newFromText( $opts['title'] )
852 : $this->defaultTitle
;
854 if ( isset( $opts['lastsavedrevision'] ) ) {
855 $content = new WikitextContent( $test['input'] );
856 $title = Title
::newFromRow( (object)[
858 'page_len' => $content->getSize(),
859 'page_latest' => 1337,
860 'page_namespace' => $title->getNamespace(),
861 'page_title' => $title->getDBkey(),
862 'page_is_redirect' => 0
866 'id' => $title->getLatestRevID(),
867 'page' => $title->getArticleID(),
869 'content' => $content,
870 'timestamp' => $this->getFakeTimestamp(),
873 Revision
::READ_LATEST
,
876 $oldCallback = $options->getCurrentRevisionCallback();
877 $options->setCurrentRevisionCallback(
878 function ( Title
$t, $parser ) use ( $title, $rev, $oldCallback ) {
879 if ( $t->equals( $title ) ) {
882 return call_user_func( $oldCallback, $t, $parser );
888 if ( isset( $opts['maxincludesize'] ) ) {
889 $options->setMaxIncludeSize( $opts['maxincludesize'] );
891 if ( isset( $opts['maxtemplatedepth'] ) ) {
892 $options->setMaxTemplateDepth( $opts['maxtemplatedepth'] );
895 $local = isset( $opts['local'] );
896 $preprocessor = $opts['preprocessor'] ??
null;
897 $parser = $this->getParser( $preprocessor );
899 if ( isset( $opts['styletag'] ) ) {
900 // For testing the behavior of <style> (including those deduplicated
901 // into <link> tags), add tag hooks to allow them to be generated.
902 $parser->setHook( 'style', function ( $content, $attributes, $parser ) {
903 $marker = Parser
::MARKER_PREFIX
. '-style-' . md5( $content ) . Parser
::MARKER_SUFFIX
;
904 $parser->mStripState
->addNoWiki( $marker, $content );
905 return Html
::inlineStyle( $marker, 'all', $attributes );
907 $parser->setHook( 'link', function ( $content, $attributes, $parser ) {
908 return Html
::element( 'link', $attributes );
912 if ( isset( $opts['pst'] ) ) {
913 $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
914 $output = $parser->getOutput();
915 } elseif ( isset( $opts['msg'] ) ) {
916 $out = $parser->transformMsg( $test['input'], $options, $title );
917 } elseif ( isset( $opts['section'] ) ) {
918 $section = $opts['section'];
919 $out = $parser->getSection( $test['input'], $section );
920 } elseif ( isset( $opts['replace'] ) ) {
921 $section = $opts['replace'][0];
922 $replace = $opts['replace'][1];
923 $out = $parser->replaceSection( $test['input'], $section, $replace );
924 } elseif ( isset( $opts['comment'] ) ) {
925 $out = Linker
::formatComment( $test['input'], $title, $local );
926 } elseif ( isset( $opts['preload'] ) ) {
927 $out = $parser->getPreloadText( $test['input'], $title, $options );
929 $output = $parser->parse( $test['input'], $title, $options, true, true, $revId );
930 $out = $output->getText( [
931 'allowTOC' => !isset( $opts['notoc'] ),
932 'unwrap' => !isset( $opts['wrap'] ),
934 if ( isset( $opts['tidy'] ) ) {
935 $out = preg_replace( '/\s+$/', '', $out );
938 if ( isset( $opts['showtitle'] ) ) {
939 if ( $output->getTitleText() ) {
940 $title = $output->getTitleText();
943 $out = "$title\n$out";
946 if ( isset( $opts['showindicators'] ) ) {
948 foreach ( $output->getIndicators() as $id => $content ) {
949 $indicators .= "$id=$content\n";
951 $out = $indicators . $out;
954 if ( isset( $opts['ill'] ) ) {
955 $out = implode( ' ', $output->getLanguageLinks() );
956 } elseif ( isset( $opts['cat'] ) ) {
958 foreach ( $output->getCategories() as $name => $sortkey ) {
962 $out .= "cat=$name sort=$sortkey";
967 if ( isset( $output ) && isset( $opts['showflags'] ) ) {
968 $actualFlags = array_keys( TestingAccessWrapper
::newFromObject( $output )->mFlags
);
969 sort( $actualFlags );
970 $out .= "\nflags=" . implode( ', ', $actualFlags );
973 ScopedCallback
::consume( $teardownGuard );
975 $expected = $test['result'];
976 if ( count( $this->normalizationFunctions
) ) {
977 $expected = ParserTestResultNormalizer
::normalize(
978 $test['expected'], $this->normalizationFunctions
);
979 $out = ParserTestResultNormalizer
::normalize( $out, $this->normalizationFunctions
);
982 $testResult = new ParserTestResult( $test, $expected, $out );
987 * Use a regex to find out the value of an option
988 * @param string $key Name of option val to retrieve
989 * @param array $opts Options array to look in
990 * @param mixed $default Default value returned if not found
993 private static function getOptionValue( $key, $opts, $default ) {
994 $key = strtolower( $key );
995 return $opts[$key] ??
$default;
999 * Given the options string, return an associative array of options.
1000 * @todo Move this to TestFileReader
1002 * @param string $instring
1005 private function parseOptions( $instring ) {
1011 // foo=bar,"baz quux"
1014 (?<qstr> # Quoted string
1016 (?:[^\\\\"] | \\\\.)*
1022 [^"{}] | # Not a quoted string or object, or
1023 (?&qstr) | # A quoted string, or
1024 (?&json) # A json object (recursively)
1030 (?&qstr) # Quoted val
1038 (?&json) # JSON object
1042 $regex = '/' . $defs . '\b
1058 $valueregex = '/' . $defs . '(?&value)/x';
1060 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER
) ) {
1061 foreach ( $matches as $bits ) {
1062 $key = strtolower( $bits['k'] );
1063 if ( !isset( $bits['v'] ) ) {
1066 preg_match_all( $valueregex, $bits['v'], $vmatches );
1067 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
1068 if ( count( $opts[$key] ) == 1 ) {
1069 $opts[$key] = $opts[$key][0];
1077 private function cleanupOption( $opt ) {
1078 if ( substr( $opt, 0, 1 ) == '"' ) {
1079 return stripcslashes( substr( $opt, 1, -1 ) );
1082 if ( substr( $opt, 0, 2 ) == '[[' ) {
1083 return substr( $opt, 2, -2 );
1086 if ( substr( $opt, 0, 1 ) == '{' ) {
1087 return FormatJson
::decode( $opt, true );
1093 * Do any required setup which is dependent on test options.
1095 * @see staticSetup() for more information about setup/teardown
1097 * @param array $test Test info supplied by TestFileReader
1098 * @param callable|null $nextTeardown
1099 * @return ScopedCallback
1101 public function perTestSetup( $test, $nextTeardown = null ) {
1104 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1105 $teardown[] = $this->markSetupDone( 'perTestSetup' );
1107 $opts = $this->parseOptions( $test['options'] );
1108 $config = $test['config'];
1110 // Find out values for some special options.
1112 self
::getOptionValue( 'language', $opts, 'en' );
1114 self
::getOptionValue( 'variant', $opts, false );
1116 self
::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
1117 $linkHolderBatchSize =
1118 self
::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
1120 // Default to fallback skin, but allow it to be overridden
1121 $skin = self
::getOptionValue( 'skin', $opts, 'fallback' );
1124 'wgEnableUploads' => self
::getOptionValue( 'wgEnableUploads', $opts, true ),
1125 'wgLanguageCode' => $langCode,
1126 'wgRawHtml' => self
::getOptionValue( 'wgRawHtml', $opts, false ),
1127 'wgNamespacesWithSubpages' => array_fill_keys(
1128 MediaWikiServices
::getInstance()->getNamespaceInfo()->getValidNamespaces(),
1129 isset( $opts['subpage'] )
1131 'wgMaxTocLevel' => $maxtoclevel,
1132 'wgAllowExternalImages' => self
::getOptionValue( 'wgAllowExternalImages', $opts, true ),
1133 'wgThumbLimits' => [ self
::getOptionValue( 'thumbsize', $opts, 180 ) ],
1134 'wgDefaultLanguageVariant' => $variant,
1135 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
1136 // Set as a JSON object like:
1137 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
1138 'wgEnableMagicLinks' => self
::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
1139 +
[ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
1140 // Test with legacy encoding by default until HTML5 is very stable and default
1141 'wgFragmentMode' => [ 'legacy' ],
1144 $nonIncludable = self
::getOptionValue( 'wgNonincludableNamespaces', $opts, false );
1145 if ( $nonIncludable !== false ) {
1146 $setup['wgNonincludableNamespaces'] = [ $nonIncludable ];
1150 $configLines = explode( "\n", $config );
1152 foreach ( $configLines as $line ) {
1153 list( $var, $value ) = explode( '=', $line, 2 );
1154 $setup[$var] = eval( "return $value;" );
1159 Hooks
::run( 'ParserTestGlobals', [ &$setup ] );
1161 // Create tidy driver
1162 if ( isset( $opts['tidy'] ) ) {
1163 // Cache a driver instance
1164 if ( $this->tidyDriver
=== null ) {
1165 $this->tidyDriver
= MWTidy
::factory();
1167 $tidy = $this->tidyDriver
;
1172 # Suppress warnings about running tests without tidy
1173 Wikimedia\
suppressWarnings();
1174 wfDeprecated( 'disabling tidy' );
1175 wfDeprecated( 'MWTidy::setInstance' );
1176 Wikimedia\restoreWarnings
();
1178 MWTidy
::setInstance( $tidy );
1179 $teardown[] = function () {
1180 MWTidy
::destroySingleton();
1183 // Set content language. This invalidates the magic word cache and title services
1184 $lang = Language
::factory( $langCode );
1185 $lang->resetNamespaces();
1186 $setup['wgContLang'] = $lang;
1187 $setup[] = function () use ( $lang ) {
1188 MediaWikiServices
::getInstance()->disableService( 'ContentLanguage' );
1189 MediaWikiServices
::getInstance()->redefineService(
1191 function () use ( $lang ) {
1196 $teardown[] = function () {
1197 MediaWikiServices
::getInstance()->resetServiceForTesting( 'ContentLanguage' );
1199 $reset = function () {
1200 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MagicWordFactory' );
1201 $this->resetTitleServices();
1204 $teardown[] = $reset;
1206 // Make a user object with the same language
1208 $user->setOption( 'language', $langCode );
1209 $setup['wgLang'] = $lang;
1211 // We (re)set $wgThumbLimits to a single-element array above.
1212 $user->setOption( 'thumbsize', 0 );
1214 $setup['wgUser'] = $user;
1216 // And put both user and language into the context
1217 $context = RequestContext
::getMain();
1218 $context->setUser( $user );
1219 $context->setLanguage( $lang );
1221 $oldSkin = $context->getSkin();
1222 $skinFactory = MediaWikiServices
::getInstance()->getSkinFactory();
1223 $context->setSkin( $skinFactory->makeSkin( $skin ) );
1224 $context->setOutput( new OutputPage( $context ) );
1225 $setup['wgOut'] = $context->getOutput();
1226 $teardown[] = function () use ( $context, $oldSkin ) {
1227 // Clear language conversion tables
1228 $wrapper = TestingAccessWrapper
::newFromObject(
1229 $context->getLanguage()->getConverter()
1231 $wrapper->reloadTables();
1232 // Reset context to the restored globals
1233 $context->setUser( $GLOBALS['wgUser'] );
1234 $context->setLanguage( $GLOBALS['wgContLang'] );
1235 $context->setSkin( $oldSkin );
1236 $context->setOutput( $GLOBALS['wgOut'] );
1239 $teardown[] = $this->executeSetupSnippets( $setup );
1241 return $this->createTeardownObject( $teardown, $nextTeardown );
1245 * List of temporary tables to create, without prefix.
1246 * Some of these probably aren't necessary.
1249 private function listTables() {
1250 global $wgActorTableSchemaMigrationStage;
1252 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1253 'protected_titles', 'revision', 'ip_changes', 'text', 'pagelinks', 'imagelinks',
1254 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1255 'site_stats', 'ipblocks', 'image', 'oldimage',
1256 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1257 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1258 'archive', 'user_groups', 'page_props', 'category',
1259 'slots', 'content', 'slot_roles', 'content_models',
1260 'comment', 'revision_comment_temp',
1263 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_NEW
) {
1264 // The new tables for actors are in use
1265 $tables[] = 'actor';
1266 $tables[] = 'revision_actor_temp';
1269 if ( in_array( $this->db
->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1270 array_push( $tables, 'searchindex' );
1273 // Allow extensions to add to the list of tables to duplicate;
1274 // may be necessary if they hook into page save or other code
1275 // which will require them while running tests.
1276 Hooks
::run( 'ParserTestTables', [ &$tables ] );
1281 public function setDatabase( IDatabase
$db ) {
1283 $this->setupDone
['setDatabase'] = true;
1287 * Set up temporary DB tables.
1289 * For best performance, call this once only for all tests. However, it can
1290 * be called at the start of each test if more isolation is desired.
1292 * @todo This is basically an unrefactored copy of
1293 * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.
1295 * Do not call this function from a MediaWikiTestCase subclass, since
1296 * MediaWikiTestCase does its own DB setup. Instead use setDatabase().
1298 * @see staticSetup() for more information about setup/teardown
1300 * @param ScopedCallback|null $nextTeardown The next teardown object
1301 * @return ScopedCallback The teardown object
1303 public function setupDatabase( $nextTeardown = null ) {
1306 $this->db
= wfGetDB( DB_MASTER
);
1307 $dbType = $this->db
->getType();
1309 if ( $dbType == 'oracle' ) {
1310 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase
::ORA_DB_PREFIX
];
1312 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase
::DB_PREFIX
];
1314 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1315 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1320 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1322 # CREATE TEMPORARY TABLE breaks if there is more than one server
1323 if ( MediaWikiServices
::getInstance()->getDBLoadBalancer()->getServerCount() != 1 ) {
1324 $this->useTemporaryTables
= false;
1327 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
1328 $prefix = $dbType != 'oracle' ?
'parsertest_' : 'pt_';
1330 $this->dbClone
= new CloneDatabase( $this->db
, $this->listTables(), $prefix );
1331 $this->dbClone
->useTemporaryTables( $temporary );
1332 $this->dbClone
->cloneTableStructure();
1333 CloneDatabase
::changePrefix( $prefix );
1335 if ( $dbType == 'oracle' ) {
1336 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1337 # Insert 0 user to prevent FK violations
1340 $this->db
->insert( 'user', [
1342 'user_name' => 'Anonymous' ] );
1345 $teardown[] = function () {
1346 $this->teardownDatabase();
1349 // Wipe some DB query result caches on setup and teardown
1350 $reset = function () {
1351 MediaWikiServices
::getInstance()->getLinkCache()->clear();
1353 // Clear the message cache
1354 MessageCache
::singleton()->clear();
1357 $teardown[] = $reset;
1358 return $this->createTeardownObject( $teardown, $nextTeardown );
1362 * Add data about uploads to the new test DB, and set up the upload
1363 * directory. This should be called after either setDatabase() or
1366 * @param ScopedCallback|null $nextTeardown The next teardown object
1367 * @return ScopedCallback The teardown object
1369 public function setupUploads( $nextTeardown = null ) {
1372 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1373 $teardown[] = $this->markSetupDone( 'setupUploads' );
1375 // Create the files in the upload directory (or pretend to create them
1376 // in a MockFileBackend). Append teardown callback.
1377 $teardown[] = $this->setupUploadBackend();
1380 $user = User
::createNew( 'WikiSysop' );
1382 // Register the uploads in the database
1384 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.jpg' ) );
1385 # note that the size/width/height/bits/etc of the file
1386 # are actually set by inspecting the file itself; the arguments
1387 # to recordUpload2 have no effect. That said, we try to make things
1388 # match up so it is less confusing to readers of the code & tests.
1389 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1394 'media_type' => MEDIATYPE_BITMAP
,
1395 'mime' => 'image/jpeg',
1396 'metadata' => serialize( [] ),
1397 'sha1' => Wikimedia\base_convert
( '1', 16, 36, 31 ),
1398 'fileExists' => true
1399 ], $this->db
->timestamp( '20010115123500' ), $user );
1401 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Thumb.png' ) );
1402 # again, note that size/width/height below are ignored; see above.
1403 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1408 'media_type' => MEDIATYPE_BITMAP
,
1409 'mime' => 'image/png',
1410 'metadata' => serialize( [] ),
1411 'sha1' => Wikimedia\base_convert
( '2', 16, 36, 31 ),
1412 'fileExists' => true
1413 ], $this->db
->timestamp( '20130225203040' ), $user );
1415 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.svg' ) );
1416 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1421 'media_type' => MEDIATYPE_DRAWING
,
1422 'mime' => 'image/svg+xml',
1423 'metadata' => serialize( [
1424 'version' => SvgHandler
::SVG_METADATA_VERSION
,
1427 'originalWidth' => '100%',
1428 'originalHeight' => '100%',
1430 'en' => SVGReader
::LANG_FULL_MATCH
,
1431 'ru' => SVGReader
::LANG_FULL_MATCH
,
1434 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1435 'fileExists' => true
1436 ], $this->db
->timestamp( '20010115123500' ), $user );
1438 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1439 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Bad.jpg' ) );
1440 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1445 'media_type' => MEDIATYPE_BITMAP
,
1446 'mime' => 'image/jpeg',
1447 'metadata' => serialize( [] ),
1448 'sha1' => Wikimedia\base_convert
( '3', 16, 36, 31 ),
1449 'fileExists' => true
1450 ], $this->db
->timestamp( '20010115123500' ), $user );
1452 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Video.ogv' ) );
1453 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1458 'media_type' => MEDIATYPE_VIDEO
,
1459 'mime' => 'application/ogg',
1460 'metadata' => serialize( [] ),
1461 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1462 'fileExists' => true
1463 ], $this->db
->timestamp( '20010115123500' ), $user );
1465 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Audio.oga' ) );
1466 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1471 'media_type' => MEDIATYPE_AUDIO
,
1472 'mime' => 'application/ogg',
1473 'metadata' => serialize( [] ),
1474 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1475 'fileExists' => true
1476 ], $this->db
->timestamp( '20010115123500' ), $user );
1479 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'LoremIpsum.djvu' ) );
1480 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1485 'media_type' => MEDIATYPE_BITMAP
,
1486 'mime' => 'image/vnd.djvu',
1487 'metadata' => '<?xml version="1.0" ?>
1488 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1491 <BODY><OBJECT height="3508" width="2480">
1492 <PARAM name="DPI" value="300" />
1493 <PARAM name="GAMMA" value="2.2" />
1495 <OBJECT height="3508" width="2480">
1496 <PARAM name="DPI" value="300" />
1497 <PARAM name="GAMMA" value="2.2" />
1499 <OBJECT height="3508" width="2480">
1500 <PARAM name="DPI" value="300" />
1501 <PARAM name="GAMMA" value="2.2" />
1503 <OBJECT height="3508" width="2480">
1504 <PARAM name="DPI" value="300" />
1505 <PARAM name="GAMMA" value="2.2" />
1507 <OBJECT height="3508" width="2480">
1508 <PARAM name="DPI" value="300" />
1509 <PARAM name="GAMMA" value="2.2" />
1513 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1514 'fileExists' => true
1515 ], $this->db
->timestamp( '20010115123600' ), $user );
1517 return $this->createTeardownObject( $teardown, $nextTeardown );
1521 * Helper for database teardown, called from the teardown closure. Destroy
1522 * the database clone and fix up some things that CloneDatabase doesn't fix.
1524 * @todo Move most things here to CloneDatabase
1526 private function teardownDatabase() {
1527 $this->checkSetupDone( 'setupDatabase' );
1529 $this->dbClone
->destroy();
1531 if ( $this->useTemporaryTables
) {
1532 if ( $this->db
->getType() == 'sqlite' ) {
1533 # Under SQLite the searchindex table is virtual and need
1534 # to be explicitly destroyed. See T31912
1535 # See also MediaWikiTestCase::destroyDB()
1536 wfDebug( __METHOD__
. " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1537 $this->db
->query( "DROP TABLE `parsertest_searchindex`" );
1539 # Don't need to do anything
1543 $tables = $this->listTables();
1545 foreach ( $tables as $table ) {
1546 if ( $this->db
->getType() == 'oracle' ) {
1547 $this->db
->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1549 $this->db
->query( "DROP TABLE `parsertest_$table`" );
1553 if ( $this->db
->getType() == 'oracle' ) {
1554 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1559 * Upload test files to the backend created by createRepoGroup().
1561 * @return callable The teardown callback
1563 private function setupUploadBackend() {
1566 $repo = RepoGroup
::singleton()->getLocalRepo();
1567 $base = $repo->getZonePath( 'public' );
1568 $backend = $repo->getBackend();
1569 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1571 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1572 'dst' => "$base/3/3a/Foobar.jpg"
1574 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1576 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1577 'dst' => "$base/e/ea/Thumb.png"
1579 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1581 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1582 'dst' => "$base/0/09/Bad.jpg"
1584 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1586 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1587 'dst' => "$base/5/5f/LoremIpsum.djvu"
1590 // No helpful SVG file to copy, so make one ourselves
1591 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1592 '<svg xmlns="http://www.w3.org/2000/svg"' .
1593 ' version="1.1" width="240" height="180"/>';
1595 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1596 $backend->quickCreate( [
1597 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1600 return function () use ( $backend ) {
1601 if ( $backend instanceof MockFileBackend
) {
1602 // In memory backend, so dont bother cleaning them up.
1605 $this->teardownUploadBackend();
1610 * Remove the dummy uploads directory
1612 private function teardownUploadBackend() {
1613 if ( $this->keepUploads
) {
1617 $repo = RepoGroup
::singleton()->getLocalRepo();
1618 $public = $repo->getZonePath( 'public' );
1622 "$public/3/3a/Foobar.jpg",
1623 "$public/e/ea/Thumb.png",
1624 "$public/0/09/Bad.jpg",
1625 "$public/5/5f/LoremIpsum.djvu",
1626 "$public/f/ff/Foobar.svg",
1627 "$public/0/00/Video.ogv",
1628 "$public/4/41/Audio.oga",
1634 * Delete the specified files and their parent directories
1635 * @param array $files File backend URIs mwstore://...
1637 private function deleteFiles( $files ) {
1639 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
1640 foreach ( $files as $file ) {
1641 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1644 // Delete the parent directories
1645 foreach ( $files as $file ) {
1646 $tmp = FileBackend
::parentStoragePath( $file );
1648 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1651 $tmp = FileBackend
::parentStoragePath( $tmp );
1657 * Add articles to the test DB.
1659 * @param array $articles Article info array from TestFileReader
1661 public function addArticles( $articles ) {
1665 // Be sure ParserTestRunner::addArticle has correct language set,
1666 // so that system messages get into the right language cache
1667 if ( MediaWikiServices
::getInstance()->getContentLanguage()->getCode() !== 'en' ) {
1668 $setup['wgLanguageCode'] = 'en';
1669 $lang = Language
::factory( 'en' );
1670 $setup['wgContLang'] = $lang;
1671 $setup[] = function () use ( $lang ) {
1672 $services = MediaWikiServices
::getInstance();
1673 $services->disableService( 'ContentLanguage' );
1674 $services->redefineService( 'ContentLanguage', function () use ( $lang ) {
1678 $teardown[] = function () {
1679 MediaWikiServices
::getInstance()->resetServiceForTesting( 'ContentLanguage' );
1683 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1684 $this->appendNamespaceSetup( $setup, $teardown );
1686 // wgCapitalLinks obviously needs initialisation
1687 $setup['wgCapitalLinks'] = true;
1689 $teardown[] = $this->executeSetupSnippets( $setup );
1691 foreach ( $articles as $info ) {
1692 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1695 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1697 ObjectCache
::getMainWANInstance()->clearProcessCache();
1699 $this->executeSetupSnippets( $teardown );
1703 * Insert a temporary test article
1704 * @param string $name The title, including any prefix
1705 * @param string $text The article text
1706 * @param string $file The input file name
1707 * @param int|string $line The input line number, for reporting errors
1709 * @throws MWException
1711 private function addArticle( $name, $text, $file, $line ) {
1712 $text = self
::chomp( $text );
1713 $name = self
::chomp( $name );
1715 $title = Title
::newFromText( $name );
1716 wfDebug( __METHOD__
. ": adding $name" );
1718 if ( is_null( $title ) ) {
1719 throw new MWException( "invalid title '$name' at $file:$line\n" );
1722 $newContent = ContentHandler
::makeContent( $text, $title );
1724 $page = WikiPage
::factory( $title );
1725 $page->loadPageData( 'fromdbmaster' );
1727 if ( $page->exists() ) {
1728 $content = $page->getContent( Revision
::RAW
);
1729 // Only reject the title, if the content/content model is different.
1730 // This makes it easier to create Template:(( or Template:)) in different extensions
1731 if ( $newContent->equals( $content ) ) {
1734 throw new MWException(
1735 "duplicate article '$name' with different content at $file:$line\n"
1739 // Optionally use mock parser, to make debugging of actual parser tests simpler.
1740 // But initialise the MessageCache clone first, don't let MessageCache
1741 // get a reference to the mock object.
1742 if ( $this->disableSaveParse
) {
1743 MessageCache
::singleton()->getParser();
1744 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser
] );
1749 $status = $page->doEditContent(
1752 EDIT_NEW | EDIT_INTERNAL
1760 if ( !$status->isOK() ) {
1761 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1764 // The RepoGroup cache is invalidated by the creation of file redirects
1765 if ( $title->inNamespace( NS_FILE
) ) {
1766 RepoGroup
::singleton()->clearCache( $title );
1771 * Check if a hook is installed
1773 * @param string $name
1774 * @return bool True if tag hook is present
1776 public function requireHook( $name ) {
1777 $parser = MediaWikiServices
::getInstance()->getParser();
1779 $parser->firstCallInit(); // make sure hooks are loaded.
1780 if ( isset( $parser->mTagHooks
[$name] ) ) {
1783 $this->recorder
->warning( " This test suite requires the '$name' hook " .
1784 "extension, skipping." );
1790 * Check if a function hook is installed
1792 * @param string $name
1793 * @return bool True if function hook is present
1795 public function requireFunctionHook( $name ) {
1796 $parser = MediaWikiServices
::getInstance()->getParser();
1798 $parser->firstCallInit(); // make sure hooks are loaded.
1800 if ( isset( $parser->mFunctionHooks
[$name] ) ) {
1803 $this->recorder
->warning( " This test suite requires the '$name' function " .
1804 "hook extension, skipping." );
1810 * Check if a transparent tag hook is installed
1812 * @param string $name
1813 * @return bool True if function hook is present
1815 public function requireTransparentHook( $name ) {
1816 $parser = MediaWikiServices
::getInstance()->getParser();
1818 $parser->firstCallInit(); // make sure hooks are loaded.
1820 if ( isset( $parser->mTransparentTagHooks
[$name] ) ) {
1823 $this->recorder
->warning( " This test suite requires the '$name' transparent " .
1824 "hook extension, skipping.\n" );
1830 * Fake constant timestamp to make sure time-related parser
1831 * functions give a persistent value.
1833 * - Parser::getVariableValue (via ParserGetVariableValueTs hook)
1834 * - Parser::preSaveTransform (via ParserOptions)
1836 private function getFakeTimestamp() {
1837 // parsed as '1970-01-01T00:02:03Z'