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)
28 use Wikimedia\Rdbms\IDatabase
;
29 use MediaWiki\MediaWikiServices
;
30 use Wikimedia\ScopedCallback
;
31 use Wikimedia\TestingAccessWrapper
;
36 class ParserTestRunner
{
38 * @var bool $useTemporaryTables Use temporary tables for the temporary database
40 private $useTemporaryTables = true;
43 * @var array $setupDone The status of each setup function
45 private $setupDone = [
46 'staticSetup' => false,
47 'perTestSetup' => false,
48 'setupDatabase' => false,
49 'setDatabase' => false,
50 'setupUploads' => false,
54 * Our connection to the database
60 * Database clone helper
73 private $tidyDriver = null;
81 * The upload directory, or null to not set up an upload directory
85 private $uploadDir = null;
88 * The name of the file backend to use, or null to use MockFileBackend.
91 private $fileBackendName;
94 * A complete regex for filtering tests.
100 * A list of normalization functions to apply to the expected and actual
104 private $normalizationFunctions = [];
107 * @param TestRecorder $recorder
108 * @param array $options
110 public function __construct( TestRecorder
$recorder, $options = [] ) {
111 $this->recorder
= $recorder;
113 if ( isset( $options['norm'] ) ) {
114 foreach ( $options['norm'] as $func ) {
115 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
116 $this->normalizationFunctions
[] = $func;
118 $this->recorder
->warning(
119 "Warning: unknown normalization option \"$func\"\n" );
124 if ( isset( $options['regex'] ) && $options['regex'] !== false ) {
125 $this->regex
= $options['regex'];
131 $this->keepUploads
= !empty( $options['keep-uploads'] );
133 $this->fileBackendName
= isset( $options['file-backend'] ) ?
134 $options['file-backend'] : false;
136 $this->runDisabled
= !empty( $options['run-disabled'] );
137 $this->runParsoid
= !empty( $options['run-parsoid'] );
139 $this->tidySupport
= new TidySupport( !empty( $options['use-tidy-config'] ) );
140 if ( !$this->tidySupport
->isEnabled() ) {
141 $this->recorder
->warning(
142 "Warning: tidy is not installed, skipping some tests\n" );
145 if ( isset( $options['upload-dir'] ) ) {
146 $this->uploadDir
= $options['upload-dir'];
150 public function getRecorder() {
151 return $this->recorder
;
155 * Do any setup which can be done once for all tests, independent of test
156 * options, except for database setup.
158 * Public setup functions in this class return a ScopedCallback object. When
159 * this object is destroyed by going out of scope, teardown of the
160 * corresponding test setup is performed.
162 * Teardown objects may be chained by passing a ScopedCallback from a
163 * previous setup stage as the $nextTeardown parameter. This enforces the
164 * convention that teardown actions are taken in reverse order to the
165 * corresponding setup actions. When $nextTeardown is specified, a
166 * ScopedCallback will be returned which first tears down the current
167 * setup stage, and then tears down the previous setup stage which was
168 * specified by $nextTeardown.
170 * @param ScopedCallback|null $nextTeardown
171 * @return ScopedCallback
173 public function staticSetup( $nextTeardown = null ) {
174 // A note on coding style:
176 // The general idea here is to keep setup code together with
177 // corresponding teardown code, in a fine-grained manner. We have two
178 // arrays: $setup and $teardown. The code snippets in the $setup array
179 // are executed at the end of the method, before it returns, and the
180 // code snippets in the $teardown array are executed in reverse order
181 // when the Wikimedia\ScopedCallback object is consumed.
183 // Because it is a common operation to save, set and restore global
184 // variables, we have an additional convention: when the array key of
185 // $setup is a string, the string is taken to be the name of the global
186 // variable, and the element value is taken to be the desired new value.
188 // It's acceptable to just do the setup immediately, instead of adding
189 // a closure to $setup, except when the setup action depends on global
190 // variable initialisation being done first. In this case, you have to
191 // append a closure to $setup after the global variable is appended.
193 // When you add to setup functions in this class, please keep associated
194 // setup and teardown actions together in the source code, and please
195 // add comments explaining why the setup action is necessary.
200 $teardown[] = $this->markSetupDone( 'staticSetup' );
202 // Some settings which influence HTML output
203 $setup['wgSitename'] = 'MediaWiki';
204 $setup['wgServer'] = 'http://example.org';
205 $setup['wgServerName'] = 'example.org';
206 $setup['wgScriptPath'] = '';
207 $setup['wgScript'] = '/index.php';
208 $setup['wgResourceBasePath'] = '';
209 $setup['wgStylePath'] = '/skins';
210 $setup['wgExtensionAssetsPath'] = '/extensions';
211 $setup['wgArticlePath'] = '/wiki/$1';
212 $setup['wgActionPaths'] = [];
213 $setup['wgVariantArticlePath'] = false;
214 $setup['wgUploadNavigationUrl'] = false;
215 $setup['wgCapitalLinks'] = true;
216 $setup['wgNoFollowLinks'] = true;
217 $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
218 $setup['wgExternalLinkTarget'] = false;
219 $setup['wgExperimentalHtmlIds'] = false;
220 $setup['wgLocaltimezone'] = 'UTC';
221 $setup['wgHtml5'] = true;
222 $setup['wgDisableLangConversion'] = false;
223 $setup['wgDisableTitleConversion'] = false;
225 // "extra language links"
226 // see https://gerrit.wikimedia.org/r/111390
227 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
229 // All FileRepo changes should be done here by injecting services,
230 // there should be no need to change global variables.
231 RepoGroup
::setSingleton( $this->createRepoGroup() );
232 $teardown[] = function () {
233 RepoGroup
::destroySingleton();
236 // Set up null lock managers
237 $setup['wgLockManagers'] = [ [
238 'name' => 'fsLockManager',
239 'class' => 'NullLockManager',
241 'name' => 'nullLockManager',
242 'class' => 'NullLockManager',
244 $reset = function () {
245 LockManagerGroup
::destroySingletons();
248 $teardown[] = $reset;
250 // This allows article insertion into the prefixed DB
251 $setup['wgDefaultExternalStore'] = false;
253 // This might slightly reduce memory usage
254 $setup['wgAdaptiveMessageCache'] = true;
256 // This is essential and overrides disabling of database messages in TestSetup
257 $setup['wgUseDatabaseMessages'] = true;
258 $reset = function () {
259 MessageCache
::destroyInstance();
262 $teardown[] = $reset;
264 // It's not necessary to actually convert any files
265 $setup['wgSVGConverter'] = 'null';
266 $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
268 // Fake constant timestamp
269 Hooks
::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) {
270 $ts = $this->getFakeTimestamp();
273 $teardown[] = function () {
274 Hooks
::clear( 'ParserGetVariableValueTs' );
277 $this->appendNamespaceSetup( $setup, $teardown );
279 // Set up interwikis and append teardown function
280 $teardown[] = $this->setupInterwikis();
282 // This affects title normalization in links. It invalidates
283 // MediaWikiTitleCodec objects.
284 $setup['wgLocalInterwikis'] = [ 'local', 'mi' ];
285 $reset = function () {
286 $this->resetTitleServices();
289 $teardown[] = $reset;
291 // Set up a mock MediaHandlerFactory
292 MediaWikiServices
::getInstance()->disableService( 'MediaHandlerFactory' );
293 MediaWikiServices
::getInstance()->redefineService(
294 'MediaHandlerFactory',
296 return new MockMediaHandlerFactory();
299 $teardown[] = function () {
300 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
303 // SqlBagOStuff broke when using temporary tables on r40209 (T17892).
304 // It seems to have been fixed since (r55079?), but regressed at some point before r85701.
305 // This works around it for now...
306 global $wgObjectCaches;
307 $setup['wgObjectCaches'] = [ CACHE_DB
=> $wgObjectCaches['hash'] ] +
$wgObjectCaches;
308 if ( isset( ObjectCache
::$instances[CACHE_DB
] ) ) {
309 $savedCache = ObjectCache
::$instances[CACHE_DB
];
310 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
311 $teardown[] = function () use ( $savedCache ) {
312 ObjectCache
::$instances[CACHE_DB
] = $savedCache;
316 $teardown[] = $this->executeSetupSnippets( $setup );
318 // Schedule teardown snippets in reverse order
319 return $this->createTeardownObject( $teardown, $nextTeardown );
322 private function appendNamespaceSetup( &$setup, &$teardown ) {
323 // Add a namespace shadowing a interwiki link, to test
324 // proper precedence when resolving links. (T53680)
325 $setup['wgExtraNamespaces'] = [
326 100 => 'MemoryAlpha',
327 101 => 'MemoryAlpha_talk'
329 // Changing wgExtraNamespaces invalidates caches in MWNamespace and
330 // any live Language object, both on setup and teardown
331 $reset = function () {
332 MWNamespace
::getCanonicalNamespaces( true );
333 $GLOBALS['wgContLang']->resetNamespaces();
336 $teardown[] = $reset;
340 * Create a RepoGroup object appropriate for the current configuration
343 protected function createRepoGroup() {
344 if ( $this->uploadDir
) {
345 if ( $this->fileBackendName
) {
346 throw new MWException( 'You cannot specify both use-filebackend and upload-dir' );
348 $backend = new FSFileBackend( [
349 'name' => 'local-backend',
350 'wikiId' => wfWikiID(),
351 'basePath' => $this->uploadDir
,
352 'tmpDirectory' => wfTempDir()
354 } elseif ( $this->fileBackendName
) {
355 global $wgFileBackends;
356 $name = $this->fileBackendName
;
358 foreach ( $wgFileBackends as $conf ) {
359 if ( $conf['name'] === $name ) {
363 if ( $useConfig === false ) {
364 throw new MWException( "Unable to find file backend \"$name\"" );
366 $useConfig['name'] = 'local-backend'; // swap name
367 unset( $useConfig['lockManager'] );
368 unset( $useConfig['fileJournal'] );
369 $class = $useConfig['class'];
370 $backend = new $class( $useConfig );
372 # Replace with a mock. We do not care about generating real
373 # files on the filesystem, just need to expose the file
375 $backend = new MockFileBackend( [
376 'name' => 'local-backend',
377 'wikiId' => wfWikiID()
381 return new RepoGroup(
383 'class' => 'MockLocalRepo',
385 'url' => 'http://example.com/images',
387 'transformVia404' => false,
388 'backend' => $backend
395 * Execute an array in which elements with integer keys are taken to be
396 * callable objects, and other elements are taken to be global variable
397 * set operations, with the key giving the variable name and the value
398 * giving the new global variable value. A closure is returned which, when
399 * executed, sets the global variables back to the values they had before
400 * this function was called.
404 * @param array $setup
407 protected function executeSetupSnippets( $setup ) {
409 foreach ( $setup as $name => $value ) {
410 if ( is_int( $name ) ) {
413 $saved[$name] = isset( $GLOBALS[$name] ) ?
$GLOBALS[$name] : null;
414 $GLOBALS[$name] = $value;
417 return function () use ( $saved ) {
418 $this->executeSetupSnippets( $saved );
423 * Take a setup array in the same format as the one given to
424 * executeSetupSnippets(), and return a ScopedCallback which, when consumed,
425 * executes the snippets in the setup array in reverse order. This is used
426 * to create "teardown objects" for the public API.
430 * @param array $teardown The snippet array
431 * @param ScopedCallback|null A ScopedCallback to consume
432 * @return ScopedCallback
434 protected function createTeardownObject( $teardown, $nextTeardown = null ) {
435 return new ScopedCallback( function () use ( $teardown, $nextTeardown ) {
436 // Schedule teardown snippets in reverse order
437 $teardown = array_reverse( $teardown );
439 $this->executeSetupSnippets( $teardown );
440 if ( $nextTeardown ) {
441 ScopedCallback
::consume( $nextTeardown );
447 * Set a setupDone flag to indicate that setup has been done, and return
448 * the teardown closure. If the flag was already set, throw an exception.
450 * @param string $funcName The setup function name
453 protected function markSetupDone( $funcName ) {
454 if ( $this->setupDone
[$funcName] ) {
455 throw new MWException( "$funcName is already done" );
457 $this->setupDone
[$funcName] = true;
458 return function () use ( $funcName ) {
459 $this->setupDone
[$funcName] = false;
464 * Ensure a given setup stage has been done, throw an exception if it has
467 protected function checkSetupDone( $funcName, $funcName2 = null ) {
468 if ( !$this->setupDone
[$funcName]
469 && ( $funcName === null ||
!$this->setupDone
[$funcName2] )
471 throw new MWException( "$funcName must be called before calling " .
477 * Determine whether a particular setup function has been run
479 * @param string $funcName
482 public function isSetupDone( $funcName ) {
483 return isset( $this->setupDone
[$funcName] ) ?
$this->setupDone
[$funcName] : false;
487 * Insert hardcoded interwiki in the lookup table.
489 * This function insert a set of well known interwikis that are used in
490 * the parser tests. They can be considered has fixtures are injected in
491 * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
492 * Since we are not interested in looking up interwikis in the database,
493 * the hook completely replace the existing mechanism (hook returns false).
495 * @return closure for teardown
497 private function setupInterwikis() {
498 # Hack: insert a few Wikipedia in-project interwiki prefixes,
499 # for testing inter-language links
500 Hooks
::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
501 static $testInterwikis = [
503 'iw_url' => 'http://doesnt.matter.org/$1',
508 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
513 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
518 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
523 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
528 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
533 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
538 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
543 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
548 'iw_url' => 'http://wikisource.org/wiki/$1',
553 if ( array_key_exists( $prefix, $testInterwikis ) ) {
554 $iwData = $testInterwikis[$prefix];
557 // We only want to rely on the above fixtures
559 } );// hooks::register
563 Hooks
::clear( 'InterwikiLoadPrefix' );
568 * Reset the Title-related services that need resetting
571 private function resetTitleServices() {
572 $services = MediaWikiServices
::getInstance();
573 $services->resetServiceForTesting( 'TitleFormatter' );
574 $services->resetServiceForTesting( 'TitleParser' );
575 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
576 $services->resetServiceForTesting( 'LinkRenderer' );
577 $services->resetServiceForTesting( 'LinkRendererFactory' );
581 * Remove last character if it is a newline
586 public static function chomp( $s ) {
587 if ( substr( $s, -1 ) === "\n" ) {
588 return substr( $s, 0, -1 );
595 * Run a series of tests listed in the given text files.
596 * Each test consists of a brief description, wikitext input,
597 * and the expected HTML output.
599 * Prints status updates on stdout and counts up the total
600 * number and percentage of passed tests.
602 * Handles all setup and teardown.
604 * @param array $filenames Array of strings
605 * @return bool True if passed all tests, false if any tests failed.
607 public function runTestsFromFiles( $filenames ) {
610 $teardownGuard = $this->staticSetup();
611 $teardownGuard = $this->setupDatabase( $teardownGuard );
612 $teardownGuard = $this->setupUploads( $teardownGuard );
614 $this->recorder
->start();
618 foreach ( $filenames as $filename ) {
619 $testFileInfo = TestFileReader
::read( $filename, [
620 'runDisabled' => $this->runDisabled
,
621 'runParsoid' => $this->runParsoid
,
622 'regex' => $this->regex
] );
624 // Don't start the suite if there are no enabled tests in the file
625 if ( !$testFileInfo['tests'] ) {
629 $this->recorder
->startSuite( $filename );
630 $ok = $this->runTests( $testFileInfo ) && $ok;
631 $this->recorder
->endSuite( $filename );
634 $this->recorder
->report();
635 } catch ( DBError
$e ) {
636 $this->recorder
->warning( $e->getMessage() );
638 $this->recorder
->end();
640 ScopedCallback
::consume( $teardownGuard );
646 * Determine whether the current parser has the hooks registered in it
647 * that are required by a file read by TestFileReader.
649 public function meetsRequirements( $requirements ) {
650 foreach ( $requirements as $requirement ) {
651 switch ( $requirement['type'] ) {
653 $ok = $this->requireHook( $requirement['name'] );
656 $ok = $this->requireFunctionHook( $requirement['name'] );
658 case 'transparentHook':
659 $ok = $this->requireTransparentHook( $requirement['name'] );
670 * Run the tests from a single file. staticSetup() and setupDatabase()
671 * must have been called already.
673 * @param array $testFileInfo Parsed file info returned by TestFileReader
674 * @return bool True if passed all tests, false if any tests failed.
676 public function runTests( $testFileInfo ) {
679 $this->checkSetupDone( 'staticSetup' );
681 // Don't add articles from the file if there are no enabled tests from the file
682 if ( !$testFileInfo['tests'] ) {
686 // If any requirements are not met, mark all tests from the file as skipped
687 if ( !$this->meetsRequirements( $testFileInfo['requirements'] ) ) {
688 foreach ( $testFileInfo['tests'] as $test ) {
689 $this->recorder
->startTest( $test );
690 $this->recorder
->skipped( $test, 'required extension not enabled' );
696 $this->addArticles( $testFileInfo['articles'] );
699 foreach ( $testFileInfo['tests'] as $test ) {
700 $this->recorder
->startTest( $test );
702 $this->runTest( $test );
703 if ( $result !== false ) {
704 $ok = $ok && $result->isSuccess();
705 $this->recorder
->record( $test, $result );
713 * Get a Parser object
715 * @param string $preprocessor
718 function getParser( $preprocessor = null ) {
719 global $wgParserConf;
721 $class = $wgParserConf['class'];
722 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] +
$wgParserConf );
723 ParserTestParserHook
::setup( $parser );
729 * Run a given wikitext input through a freshly-constructed wiki parser,
730 * and compare the output against the expected results.
731 * Prints status and explanatory messages to stdout.
733 * staticSetup() and setupWikiData() must be called before this function
736 * @param array $test The test parameters:
737 * - test: The test name
738 * - desc: The subtest description
739 * - input: Wikitext to try rendering
740 * - options: Array of test options
741 * - config: Overrides for global variables, one per line
743 * @return ParserTestResult or false if skipped
745 public function runTest( $test ) {
746 wfDebug( __METHOD__
.": running {$test['desc']}" );
747 $opts = $this->parseOptions( $test['options'] );
748 $teardownGuard = $this->perTestSetup( $test );
750 $context = RequestContext
::getMain();
751 $user = $context->getUser();
752 $options = ParserOptions
::newFromContext( $context );
753 $options->setTimestamp( $this->getFakeTimestamp() );
755 if ( !isset( $opts['wrap'] ) ) {
756 $options->setWrapOutputClass( false );
759 if ( isset( $opts['tidy'] ) ) {
760 if ( !$this->tidySupport
->isEnabled() ) {
761 $this->recorder
->skipped( $test, 'tidy extension is not installed' );
764 $options->setTidy( true );
768 if ( isset( $opts['title'] ) ) {
769 $titleText = $opts['title'];
771 $titleText = 'Parser test';
774 $local = isset( $opts['local'] );
775 $preprocessor = isset( $opts['preprocessor'] ) ?
$opts['preprocessor'] : null;
776 $parser = $this->getParser( $preprocessor );
777 $title = Title
::newFromText( $titleText );
779 if ( isset( $opts['pst'] ) ) {
780 $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
781 $output = $parser->getOutput();
782 } elseif ( isset( $opts['msg'] ) ) {
783 $out = $parser->transformMsg( $test['input'], $options, $title );
784 } elseif ( isset( $opts['section'] ) ) {
785 $section = $opts['section'];
786 $out = $parser->getSection( $test['input'], $section );
787 } elseif ( isset( $opts['replace'] ) ) {
788 $section = $opts['replace'][0];
789 $replace = $opts['replace'][1];
790 $out = $parser->replaceSection( $test['input'], $section, $replace );
791 } elseif ( isset( $opts['comment'] ) ) {
792 $out = Linker
::formatComment( $test['input'], $title, $local );
793 } elseif ( isset( $opts['preload'] ) ) {
794 $out = $parser->getPreloadText( $test['input'], $title, $options );
796 $output = $parser->parse( $test['input'], $title, $options, true, true, 1337 );
797 $output->setTOCEnabled( !isset( $opts['notoc'] ) );
798 $out = $output->getText();
799 if ( isset( $opts['tidy'] ) ) {
800 $out = preg_replace( '/\s+$/', '', $out );
803 if ( isset( $opts['showtitle'] ) ) {
804 if ( $output->getTitleText() ) {
805 $title = $output->getTitleText();
808 $out = "$title\n$out";
811 if ( isset( $opts['showindicators'] ) ) {
813 foreach ( $output->getIndicators() as $id => $content ) {
814 $indicators .= "$id=$content\n";
816 $out = $indicators . $out;
819 if ( isset( $opts['ill'] ) ) {
820 $out = implode( ' ', $output->getLanguageLinks() );
821 } elseif ( isset( $opts['cat'] ) ) {
823 foreach ( $output->getCategories() as $name => $sortkey ) {
827 $out .= "cat=$name sort=$sortkey";
832 if ( isset( $output ) && isset( $opts['showflags'] ) ) {
833 $actualFlags = array_keys( TestingAccessWrapper
::newFromObject( $output )->mFlags
);
834 sort( $actualFlags );
835 $out .= "\nflags=" . join( ', ', $actualFlags );
838 ScopedCallback
::consume( $teardownGuard );
840 $expected = $test['result'];
841 if ( count( $this->normalizationFunctions
) ) {
842 $expected = ParserTestResultNormalizer
::normalize(
843 $test['expected'], $this->normalizationFunctions
);
844 $out = ParserTestResultNormalizer
::normalize( $out, $this->normalizationFunctions
);
847 $testResult = new ParserTestResult( $test, $expected, $out );
852 * Use a regex to find out the value of an option
853 * @param string $key Name of option val to retrieve
854 * @param array $opts Options array to look in
855 * @param mixed $default Default value returned if not found
858 private static function getOptionValue( $key, $opts, $default ) {
859 $key = strtolower( $key );
861 if ( isset( $opts[$key] ) ) {
869 * Given the options string, return an associative array of options.
870 * @todo Move this to TestFileReader
872 * @param string $instring
875 private function parseOptions( $instring ) {
881 // foo=bar,"baz quux"
884 (?<qstr> # Quoted string
886 (?:[^\\\\"] | \\\\.)*
892 [^"{}] | # Not a quoted string or object, or
893 (?&qstr) | # A quoted string, or
894 (?&json) # A json object (recursively)
900 (?&qstr) # Quoted val
908 (?&json) # JSON object
912 $regex = '/' . $defs . '\b
928 $valueregex = '/' . $defs . '(?&value)/x';
930 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER
) ) {
931 foreach ( $matches as $bits ) {
932 $key = strtolower( $bits['k'] );
933 if ( !isset( $bits['v'] ) ) {
936 preg_match_all( $valueregex, $bits['v'], $vmatches );
937 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
938 if ( count( $opts[$key] ) == 1 ) {
939 $opts[$key] = $opts[$key][0];
947 private function cleanupOption( $opt ) {
948 if ( substr( $opt, 0, 1 ) == '"' ) {
949 return stripcslashes( substr( $opt, 1, -1 ) );
952 if ( substr( $opt, 0, 2 ) == '[[' ) {
953 return substr( $opt, 2, -2 );
956 if ( substr( $opt, 0, 1 ) == '{' ) {
957 return FormatJson
::decode( $opt, true );
963 * Do any required setup which is dependent on test options.
965 * @see staticSetup() for more information about setup/teardown
967 * @param array $test Test info supplied by TestFileReader
968 * @param callable|null $nextTeardown
969 * @return ScopedCallback
971 public function perTestSetup( $test, $nextTeardown = null ) {
974 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
975 $teardown[] = $this->markSetupDone( 'perTestSetup' );
977 $opts = $this->parseOptions( $test['options'] );
978 $config = $test['config'];
980 // Find out values for some special options.
982 self
::getOptionValue( 'language', $opts, 'en' );
984 self
::getOptionValue( 'variant', $opts, false );
986 self
::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
987 $linkHolderBatchSize =
988 self
::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
991 'wgEnableUploads' => self
::getOptionValue( 'wgEnableUploads', $opts, true ),
992 'wgLanguageCode' => $langCode,
993 'wgRawHtml' => self
::getOptionValue( 'wgRawHtml', $opts, false ),
994 'wgNamespacesWithSubpages' => array_fill_keys(
995 MWNamespace
::getValidNamespaces(), isset( $opts['subpage'] )
997 'wgMaxTocLevel' => $maxtoclevel,
998 'wgAllowExternalImages' => self
::getOptionValue( 'wgAllowExternalImages', $opts, true ),
999 'wgThumbLimits' => [ self
::getOptionValue( 'thumbsize', $opts, 180 ) ],
1000 'wgDefaultLanguageVariant' => $variant,
1001 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
1002 // Set as a JSON object like:
1003 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
1004 'wgEnableMagicLinks' => self
::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
1005 +
[ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
1009 $configLines = explode( "\n", $config );
1011 foreach ( $configLines as $line ) {
1012 list( $var, $value ) = explode( '=', $line, 2 );
1013 $setup[$var] = eval( "return $value;" );
1018 Hooks
::run( 'ParserTestGlobals', [ &$setup ] );
1020 // Create tidy driver
1021 if ( isset( $opts['tidy'] ) ) {
1022 // Cache a driver instance
1023 if ( $this->tidyDriver
=== null ) {
1024 $this->tidyDriver
= MWTidy
::factory( $this->tidySupport
->getConfig() );
1026 $tidy = $this->tidyDriver
;
1030 MWTidy
::setInstance( $tidy );
1031 $teardown[] = function () {
1032 MWTidy
::destroySingleton();
1035 // Set content language. This invalidates the magic word cache and title services
1036 $lang = Language
::factory( $langCode );
1037 $setup['wgContLang'] = $lang;
1038 $reset = function () {
1039 MagicWord
::clearCache();
1040 $this->resetTitleServices();
1043 $teardown[] = $reset;
1045 // Make a user object with the same language
1047 $user->setOption( 'language', $langCode );
1048 $setup['wgLang'] = $lang;
1050 // We (re)set $wgThumbLimits to a single-element array above.
1051 $user->setOption( 'thumbsize', 0 );
1053 $setup['wgUser'] = $user;
1055 // And put both user and language into the context
1056 $context = RequestContext
::getMain();
1057 $context->setUser( $user );
1058 $context->setLanguage( $lang );
1059 $teardown[] = function () use ( $context ) {
1060 // Clear language conversion tables
1061 $wrapper = TestingAccessWrapper
::newFromObject(
1062 $context->getLanguage()->getConverter()
1064 $wrapper->reloadTables();
1065 // Reset context to the restored globals
1066 $context->setUser( $GLOBALS['wgUser'] );
1067 $context->setLanguage( $GLOBALS['wgContLang'] );
1070 $teardown[] = $this->executeSetupSnippets( $setup );
1072 return $this->createTeardownObject( $teardown, $nextTeardown );
1076 * List of temporary tables to create, without prefix.
1077 * Some of these probably aren't necessary.
1080 private function listTables() {
1081 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1082 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
1083 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1084 'site_stats', 'ipblocks', 'image', 'oldimage',
1085 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1086 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1087 'archive', 'user_groups', 'page_props', 'category'
1090 if ( in_array( $this->db
->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1091 array_push( $tables, 'searchindex' );
1094 // Allow extensions to add to the list of tables to duplicate;
1095 // may be necessary if they hook into page save or other code
1096 // which will require them while running tests.
1097 Hooks
::run( 'ParserTestTables', [ &$tables ] );
1102 public function setDatabase( IDatabase
$db ) {
1104 $this->setupDone
['setDatabase'] = true;
1108 * Set up temporary DB tables.
1110 * For best performance, call this once only for all tests. However, it can
1111 * be called at the start of each test if more isolation is desired.
1113 * @todo: This is basically an unrefactored copy of
1114 * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.
1116 * Do not call this function from a MediaWikiTestCase subclass, since
1117 * MediaWikiTestCase does its own DB setup. Instead use setDatabase().
1119 * @see staticSetup() for more information about setup/teardown
1121 * @param ScopedCallback|null $nextTeardown The next teardown object
1122 * @return ScopedCallback The teardown object
1124 public function setupDatabase( $nextTeardown = null ) {
1127 $this->db
= wfGetDB( DB_MASTER
);
1128 $dbType = $this->db
->getType();
1130 if ( $dbType == 'oracle' ) {
1131 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase
::ORA_DB_PREFIX
];
1133 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase
::DB_PREFIX
];
1135 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1136 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1141 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1143 # CREATE TEMPORARY TABLE breaks if there is more than one server
1144 if ( wfGetLB()->getServerCount() != 1 ) {
1145 $this->useTemporaryTables
= false;
1148 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
1149 $prefix = $dbType != 'oracle' ?
'parsertest_' : 'pt_';
1151 $this->dbClone
= new CloneDatabase( $this->db
, $this->listTables(), $prefix );
1152 $this->dbClone
->useTemporaryTables( $temporary );
1153 $this->dbClone
->cloneTableStructure();
1155 if ( $dbType == 'oracle' ) {
1156 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1157 # Insert 0 user to prevent FK violations
1160 $this->db
->insert( 'user', [
1162 'user_name' => 'Anonymous' ] );
1165 $teardown[] = function () {
1166 $this->teardownDatabase();
1169 // Wipe some DB query result caches on setup and teardown
1170 $reset = function () {
1171 LinkCache
::singleton()->clear();
1173 // Clear the message cache
1174 MessageCache
::singleton()->clear();
1177 $teardown[] = $reset;
1178 return $this->createTeardownObject( $teardown, $nextTeardown );
1182 * Add data about uploads to the new test DB, and set up the upload
1183 * directory. This should be called after either setDatabase() or
1186 * @param ScopedCallback|null $nextTeardown The next teardown object
1187 * @return ScopedCallback The teardown object
1189 public function setupUploads( $nextTeardown = null ) {
1192 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1193 $teardown[] = $this->markSetupDone( 'setupUploads' );
1195 // Create the files in the upload directory (or pretend to create them
1196 // in a MockFileBackend). Append teardown callback.
1197 $teardown[] = $this->setupUploadBackend();
1200 $user = User
::createNew( 'WikiSysop' );
1202 // Register the uploads in the database
1204 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.jpg' ) );
1205 # note that the size/width/height/bits/etc of the file
1206 # are actually set by inspecting the file itself; the arguments
1207 # to recordUpload2 have no effect. That said, we try to make things
1208 # match up so it is less confusing to readers of the code & tests.
1209 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1214 'media_type' => MEDIATYPE_BITMAP
,
1215 'mime' => 'image/jpeg',
1216 'metadata' => serialize( [] ),
1217 'sha1' => Wikimedia\base_convert
( '1', 16, 36, 31 ),
1218 'fileExists' => true
1219 ], $this->db
->timestamp( '20010115123500' ), $user );
1221 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Thumb.png' ) );
1222 # again, note that size/width/height below are ignored; see above.
1223 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1228 'media_type' => MEDIATYPE_BITMAP
,
1229 'mime' => 'image/png',
1230 'metadata' => serialize( [] ),
1231 'sha1' => Wikimedia\base_convert
( '2', 16, 36, 31 ),
1232 'fileExists' => true
1233 ], $this->db
->timestamp( '20130225203040' ), $user );
1235 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.svg' ) );
1236 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1241 'media_type' => MEDIATYPE_DRAWING
,
1242 'mime' => 'image/svg+xml',
1243 'metadata' => serialize( [] ),
1244 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1245 'fileExists' => true
1246 ], $this->db
->timestamp( '20010115123500' ), $user );
1248 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1249 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Bad.jpg' ) );
1250 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1255 'media_type' => MEDIATYPE_BITMAP
,
1256 'mime' => 'image/jpeg',
1257 'metadata' => serialize( [] ),
1258 'sha1' => Wikimedia\base_convert
( '3', 16, 36, 31 ),
1259 'fileExists' => true
1260 ], $this->db
->timestamp( '20010115123500' ), $user );
1262 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Video.ogv' ) );
1263 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1268 'media_type' => MEDIATYPE_VIDEO
,
1269 'mime' => 'application/ogg',
1270 'metadata' => serialize( [] ),
1271 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1272 'fileExists' => true
1273 ], $this->db
->timestamp( '20010115123500' ), $user );
1275 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Audio.oga' ) );
1276 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1281 'media_type' => MEDIATYPE_AUDIO
,
1282 'mime' => 'application/ogg',
1283 'metadata' => serialize( [] ),
1284 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1285 'fileExists' => true
1286 ], $this->db
->timestamp( '20010115123500' ), $user );
1289 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'LoremIpsum.djvu' ) );
1290 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1295 'media_type' => MEDIATYPE_BITMAP
,
1296 'mime' => 'image/vnd.djvu',
1297 'metadata' => '<?xml version="1.0" ?>
1298 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1301 <BODY><OBJECT height="3508" width="2480">
1302 <PARAM name="DPI" value="300" />
1303 <PARAM name="GAMMA" value="2.2" />
1305 <OBJECT height="3508" width="2480">
1306 <PARAM name="DPI" value="300" />
1307 <PARAM name="GAMMA" value="2.2" />
1309 <OBJECT height="3508" width="2480">
1310 <PARAM name="DPI" value="300" />
1311 <PARAM name="GAMMA" value="2.2" />
1313 <OBJECT height="3508" width="2480">
1314 <PARAM name="DPI" value="300" />
1315 <PARAM name="GAMMA" value="2.2" />
1317 <OBJECT height="3508" width="2480">
1318 <PARAM name="DPI" value="300" />
1319 <PARAM name="GAMMA" value="2.2" />
1323 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1324 'fileExists' => true
1325 ], $this->db
->timestamp( '20010115123600' ), $user );
1327 return $this->createTeardownObject( $teardown, $nextTeardown );
1331 * Helper for database teardown, called from the teardown closure. Destroy
1332 * the database clone and fix up some things that CloneDatabase doesn't fix.
1334 * @todo Move most things here to CloneDatabase
1336 private function teardownDatabase() {
1337 $this->checkSetupDone( 'setupDatabase' );
1339 $this->dbClone
->destroy();
1340 $this->databaseSetupDone
= false;
1342 if ( $this->useTemporaryTables
) {
1343 if ( $this->db
->getType() == 'sqlite' ) {
1344 # Under SQLite the searchindex table is virtual and need
1345 # to be explicitly destroyed. See T31912
1346 # See also MediaWikiTestCase::destroyDB()
1347 wfDebug( __METHOD__
. " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1348 $this->db
->query( "DROP TABLE `parsertest_searchindex`" );
1350 # Don't need to do anything
1354 $tables = $this->listTables();
1356 foreach ( $tables as $table ) {
1357 if ( $this->db
->getType() == 'oracle' ) {
1358 $this->db
->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1360 $this->db
->query( "DROP TABLE `parsertest_$table`" );
1364 if ( $this->db
->getType() == 'oracle' ) {
1365 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1370 * Upload test files to the backend created by createRepoGroup().
1372 * @return callable The teardown callback
1374 private function setupUploadBackend() {
1377 $repo = RepoGroup
::singleton()->getLocalRepo();
1378 $base = $repo->getZonePath( 'public' );
1379 $backend = $repo->getBackend();
1380 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1382 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1383 'dst' => "$base/3/3a/Foobar.jpg"
1385 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1387 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1388 'dst' => "$base/e/ea/Thumb.png"
1390 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1392 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1393 'dst' => "$base/0/09/Bad.jpg"
1395 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1397 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1398 'dst' => "$base/5/5f/LoremIpsum.djvu"
1401 // No helpful SVG file to copy, so make one ourselves
1402 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1403 '<svg xmlns="http://www.w3.org/2000/svg"' .
1404 ' version="1.1" width="240" height="180"/>';
1406 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1407 $backend->quickCreate( [
1408 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1411 return function () use ( $backend ) {
1412 if ( $backend instanceof MockFileBackend
) {
1413 // In memory backend, so dont bother cleaning them up.
1416 $this->teardownUploadBackend();
1421 * Remove the dummy uploads directory
1423 private function teardownUploadBackend() {
1424 if ( $this->keepUploads
) {
1428 $repo = RepoGroup
::singleton()->getLocalRepo();
1429 $public = $repo->getZonePath( 'public' );
1433 "$public/3/3a/Foobar.jpg",
1434 "$public/e/ea/Thumb.png",
1435 "$public/0/09/Bad.jpg",
1436 "$public/5/5f/LoremIpsum.djvu",
1437 "$public/f/ff/Foobar.svg",
1438 "$public/0/00/Video.ogv",
1439 "$public/4/41/Audio.oga",
1445 * Delete the specified files and their parent directories
1446 * @param array $files File backend URIs mwstore://...
1448 private function deleteFiles( $files ) {
1450 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
1451 foreach ( $files as $file ) {
1452 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1455 // Delete the parent directories
1456 foreach ( $files as $file ) {
1457 $tmp = FileBackend
::parentStoragePath( $file );
1459 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1462 $tmp = FileBackend
::parentStoragePath( $tmp );
1468 * Add articles to the test DB.
1470 * @param $articles Article info array from TestFileReader
1472 public function addArticles( $articles ) {
1477 // Be sure ParserTestRunner::addArticle has correct language set,
1478 // so that system messages get into the right language cache
1479 if ( $wgContLang->getCode() !== 'en' ) {
1480 $setup['wgLanguageCode'] = 'en';
1481 $setup['wgContLang'] = Language
::factory( 'en' );
1484 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1485 $this->appendNamespaceSetup( $setup, $teardown );
1487 // wgCapitalLinks obviously needs initialisation
1488 $setup['wgCapitalLinks'] = true;
1490 $teardown[] = $this->executeSetupSnippets( $setup );
1492 foreach ( $articles as $info ) {
1493 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1496 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1498 ObjectCache
::getMainWANInstance()->clearProcessCache();
1500 $this->executeSetupSnippets( $teardown );
1504 * Insert a temporary test article
1505 * @param string $name The title, including any prefix
1506 * @param string $text The article text
1507 * @param string $file The input file name
1508 * @param int|string $line The input line number, for reporting errors
1510 * @throws MWException
1512 private function addArticle( $name, $text, $file, $line ) {
1513 $text = self
::chomp( $text );
1514 $name = self
::chomp( $name );
1516 $title = Title
::newFromText( $name );
1517 wfDebug( __METHOD__
. ": adding $name" );
1519 if ( is_null( $title ) ) {
1520 throw new MWException( "invalid title '$name' at $file:$line\n" );
1523 $page = WikiPage
::factory( $title );
1524 $page->loadPageData( 'fromdbmaster' );
1526 if ( $page->exists() ) {
1527 throw new MWException( "duplicate article '$name' at $file:$line\n" );
1530 // Use mock parser, to make debugging of actual parser tests simpler.
1531 // But initialise the MessageCache clone first, don't let MessageCache
1532 // get a reference to the mock object.
1533 MessageCache
::singleton()->getParser();
1534 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser
] );
1535 $status = $page->doEditContent(
1536 ContentHandler
::makeContent( $text, $title ),
1538 EDIT_NEW | EDIT_INTERNAL
1542 if ( !$status->isOK() ) {
1543 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1546 // The RepoGroup cache is invalidated by the creation of file redirects
1547 if ( $title->inNamespace( NS_FILE
) ) {
1548 RepoGroup
::singleton()->clearCache( $title );
1553 * Check if a hook is installed
1555 * @param string $name
1556 * @return bool True if tag hook is present
1558 public function requireHook( $name ) {
1561 $wgParser->firstCallInit(); // make sure hooks are loaded.
1562 if ( isset( $wgParser->mTagHooks
[$name] ) ) {
1565 $this->recorder
->warning( " This test suite requires the '$name' hook " .
1566 "extension, skipping." );
1572 * Check if a function hook is installed
1574 * @param string $name
1575 * @return bool True if function hook is present
1577 public function requireFunctionHook( $name ) {
1580 $wgParser->firstCallInit(); // make sure hooks are loaded.
1582 if ( isset( $wgParser->mFunctionHooks
[$name] ) ) {
1585 $this->recorder
->warning( " This test suite requires the '$name' function " .
1586 "hook extension, skipping." );
1592 * Check if a transparent tag hook is installed
1594 * @param string $name
1595 * @return bool True if function hook is present
1597 public function requireTransparentHook( $name ) {
1600 $wgParser->firstCallInit(); // make sure hooks are loaded.
1602 if ( isset( $wgParser->mTransparentTagHooks
[$name] ) ) {
1605 $this->recorder
->warning( " This test suite requires the '$name' transparent " .
1606 "hook extension, skipping.\n" );
1612 * Fake constant timestamp to make sure time-related parser
1613 * functions give a persistent value.
1615 * - Parser::getVariableValue (via ParserGetVariableValueTs hook)
1616 * - Parser::preSaveTransform (via ParserOptions)
1618 private function getFakeTimestamp() {
1619 // parsed as '1970-01-01T00:02:03Z'