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 MediaWiki\Tidy\TidyDriverBase
;
31 use Wikimedia\ScopedCallback
;
32 use Wikimedia\TestingAccessWrapper
;
37 class ParserTestRunner
{
40 * MediaWiki core parser test files, paths
41 * will be prefixed with __DIR__ . '/'
45 private static $coreTestFiles = [
47 'extraParserTests.txt',
51 * @var bool $useTemporaryTables Use temporary tables for the temporary database
53 private $useTemporaryTables = true;
56 * @var array $setupDone The status of each setup function
58 private $setupDone = [
59 'staticSetup' => false,
60 'perTestSetup' => false,
61 'setupDatabase' => false,
62 'setDatabase' => false,
63 'setupUploads' => false,
67 * Our connection to the database
73 * Database clone helper
86 private $tidyDriver = null;
94 * The upload directory, or null to not set up an upload directory
98 private $uploadDir = null;
101 * The name of the file backend to use, or null to use MockFileBackend.
104 private $fileBackendName;
107 * A complete regex for filtering tests.
113 * A list of normalization functions to apply to the expected and actual
117 private $normalizationFunctions = [];
120 * @param TestRecorder $recorder
121 * @param array $options
123 public function __construct( TestRecorder
$recorder, $options = [] ) {
124 $this->recorder
= $recorder;
126 if ( isset( $options['norm'] ) ) {
127 foreach ( $options['norm'] as $func ) {
128 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
129 $this->normalizationFunctions
[] = $func;
131 $this->recorder
->warning(
132 "Warning: unknown normalization option \"$func\"\n" );
137 if ( isset( $options['regex'] ) && $options['regex'] !== false ) {
138 $this->regex
= $options['regex'];
144 $this->keepUploads
= !empty( $options['keep-uploads'] );
146 $this->fileBackendName
= isset( $options['file-backend'] ) ?
147 $options['file-backend'] : false;
149 $this->runDisabled
= !empty( $options['run-disabled'] );
150 $this->runParsoid
= !empty( $options['run-parsoid'] );
152 $this->tidySupport
= new TidySupport( !empty( $options['use-tidy-config'] ) );
153 if ( !$this->tidySupport
->isEnabled() ) {
154 $this->recorder
->warning(
155 "Warning: tidy is not installed, skipping some tests\n" );
158 if ( isset( $options['upload-dir'] ) ) {
159 $this->uploadDir
= $options['upload-dir'];
164 * Get list of filenames to extension and core parser tests
168 public static function getParserTestFiles() {
169 global $wgParserTestFiles;
171 // Add core test files
172 $files = array_map( function ( $item ) {
173 return __DIR__
. "/$item";
174 }, self
::$coreTestFiles );
176 // Plus legacy global files
177 $files = array_merge( $files, $wgParserTestFiles );
179 // Auto-discover extension parser tests
180 $registry = ExtensionRegistry
::getInstance();
181 foreach ( $registry->getAllThings() as $info ) {
182 $dir = dirname( $info['path'] ) . '/tests/parser';
183 if ( !file_exists( $dir ) ) {
187 $dirIterator = new RecursiveIteratorIterator(
188 new RecursiveDirectoryIterator( $dir )
190 foreach ( $dirIterator as $fileInfo ) {
191 /** @var SplFileInfo $fileInfo */
192 if ( substr( $fileInfo->getFilename(), -4 ) === '.txt' ) {
193 $name = $info['name'] . $counter;
194 while ( isset( $files[$name] ) ) {
195 $name = $info['name'] . '_' . $counter++
;
197 $files[$name] = $fileInfo->getPathname();
202 return array_unique( $files );
205 public function getRecorder() {
206 return $this->recorder
;
210 * Do any setup which can be done once for all tests, independent of test
211 * options, except for database setup.
213 * Public setup functions in this class return a ScopedCallback object. When
214 * this object is destroyed by going out of scope, teardown of the
215 * corresponding test setup is performed.
217 * Teardown objects may be chained by passing a ScopedCallback from a
218 * previous setup stage as the $nextTeardown parameter. This enforces the
219 * convention that teardown actions are taken in reverse order to the
220 * corresponding setup actions. When $nextTeardown is specified, a
221 * ScopedCallback will be returned which first tears down the current
222 * setup stage, and then tears down the previous setup stage which was
223 * specified by $nextTeardown.
225 * @param ScopedCallback|null $nextTeardown
226 * @return ScopedCallback
228 public function staticSetup( $nextTeardown = null ) {
229 // A note on coding style:
231 // The general idea here is to keep setup code together with
232 // corresponding teardown code, in a fine-grained manner. We have two
233 // arrays: $setup and $teardown. The code snippets in the $setup array
234 // are executed at the end of the method, before it returns, and the
235 // code snippets in the $teardown array are executed in reverse order
236 // when the Wikimedia\ScopedCallback object is consumed.
238 // Because it is a common operation to save, set and restore global
239 // variables, we have an additional convention: when the array key of
240 // $setup is a string, the string is taken to be the name of the global
241 // variable, and the element value is taken to be the desired new value.
243 // It's acceptable to just do the setup immediately, instead of adding
244 // a closure to $setup, except when the setup action depends on global
245 // variable initialisation being done first. In this case, you have to
246 // append a closure to $setup after the global variable is appended.
248 // When you add to setup functions in this class, please keep associated
249 // setup and teardown actions together in the source code, and please
250 // add comments explaining why the setup action is necessary.
255 $teardown[] = $this->markSetupDone( 'staticSetup' );
257 // Some settings which influence HTML output
258 $setup['wgSitename'] = 'MediaWiki';
259 $setup['wgServer'] = 'http://example.org';
260 $setup['wgServerName'] = 'example.org';
261 $setup['wgScriptPath'] = '';
262 $setup['wgScript'] = '/index.php';
263 $setup['wgResourceBasePath'] = '';
264 $setup['wgStylePath'] = '/skins';
265 $setup['wgExtensionAssetsPath'] = '/extensions';
266 $setup['wgArticlePath'] = '/wiki/$1';
267 $setup['wgActionPaths'] = [];
268 $setup['wgVariantArticlePath'] = false;
269 $setup['wgUploadNavigationUrl'] = false;
270 $setup['wgCapitalLinks'] = true;
271 $setup['wgNoFollowLinks'] = true;
272 $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
273 $setup['wgExternalLinkTarget'] = false;
274 $setup['wgExperimentalHtmlIds'] = false;
275 $setup['wgLocaltimezone'] = 'UTC';
276 $setup['wgHtml5'] = true;
277 $setup['wgDisableLangConversion'] = false;
278 $setup['wgDisableTitleConversion'] = false;
280 // "extra language links"
281 // see https://gerrit.wikimedia.org/r/111390
282 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
284 // All FileRepo changes should be done here by injecting services,
285 // there should be no need to change global variables.
286 RepoGroup
::setSingleton( $this->createRepoGroup() );
287 $teardown[] = function () {
288 RepoGroup
::destroySingleton();
291 // Set up null lock managers
292 $setup['wgLockManagers'] = [ [
293 'name' => 'fsLockManager',
294 'class' => NullLockManager
::class,
296 'name' => 'nullLockManager',
297 'class' => NullLockManager
::class,
299 $reset = function () {
300 LockManagerGroup
::destroySingletons();
303 $teardown[] = $reset;
305 // This allows article insertion into the prefixed DB
306 $setup['wgDefaultExternalStore'] = false;
308 // This might slightly reduce memory usage
309 $setup['wgAdaptiveMessageCache'] = true;
311 // This is essential and overrides disabling of database messages in TestSetup
312 $setup['wgUseDatabaseMessages'] = true;
313 $reset = function () {
314 MessageCache
::destroyInstance();
317 $teardown[] = $reset;
319 // It's not necessary to actually convert any files
320 $setup['wgSVGConverter'] = 'null';
321 $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
323 // Fake constant timestamp
324 Hooks
::register( 'ParserGetVariableValueTs', function ( &$parser, &$ts ) {
325 $ts = $this->getFakeTimestamp();
328 $teardown[] = function () {
329 Hooks
::clear( 'ParserGetVariableValueTs' );
332 $this->appendNamespaceSetup( $setup, $teardown );
334 // Set up interwikis and append teardown function
335 $teardown[] = $this->setupInterwikis();
337 // This affects title normalization in links. It invalidates
338 // MediaWikiTitleCodec objects.
339 $setup['wgLocalInterwikis'] = [ 'local', 'mi' ];
340 $reset = function () {
341 $this->resetTitleServices();
344 $teardown[] = $reset;
346 // Set up a mock MediaHandlerFactory
347 MediaWikiServices
::getInstance()->disableService( 'MediaHandlerFactory' );
348 MediaWikiServices
::getInstance()->redefineService(
349 'MediaHandlerFactory',
350 function ( MediaWikiServices
$services ) {
351 $handlers = $services->getMainConfig()->get( 'ParserTestMediaHandlers' );
352 return new MediaHandlerFactory( $handlers );
355 $teardown[] = function () {
356 MediaWikiServices
::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
359 // SqlBagOStuff broke when using temporary tables on r40209 (T17892).
360 // It seems to have been fixed since (r55079?), but regressed at some point before r85701.
361 // This works around it for now...
362 global $wgObjectCaches;
363 $setup['wgObjectCaches'] = [ CACHE_DB
=> $wgObjectCaches['hash'] ] +
$wgObjectCaches;
364 if ( isset( ObjectCache
::$instances[CACHE_DB
] ) ) {
365 $savedCache = ObjectCache
::$instances[CACHE_DB
];
366 ObjectCache
::$instances[CACHE_DB
] = new HashBagOStuff
;
367 $teardown[] = function () use ( $savedCache ) {
368 ObjectCache
::$instances[CACHE_DB
] = $savedCache;
372 $teardown[] = $this->executeSetupSnippets( $setup );
374 // Schedule teardown snippets in reverse order
375 return $this->createTeardownObject( $teardown, $nextTeardown );
378 private function appendNamespaceSetup( &$setup, &$teardown ) {
379 // Add a namespace shadowing a interwiki link, to test
380 // proper precedence when resolving links. (T53680)
381 $setup['wgExtraNamespaces'] = [
382 100 => 'MemoryAlpha',
383 101 => 'MemoryAlpha_talk'
385 // Changing wgExtraNamespaces invalidates caches in MWNamespace and
386 // any live Language object, both on setup and teardown
387 $reset = function () {
388 MWNamespace
::clearCaches();
389 $GLOBALS['wgContLang']->resetNamespaces();
392 $teardown[] = $reset;
396 * Create a RepoGroup object appropriate for the current configuration
399 protected function createRepoGroup() {
400 if ( $this->uploadDir
) {
401 if ( $this->fileBackendName
) {
402 throw new MWException( 'You cannot specify both use-filebackend and upload-dir' );
404 $backend = new FSFileBackend( [
405 'name' => 'local-backend',
406 'wikiId' => wfWikiID(),
407 'basePath' => $this->uploadDir
,
408 'tmpDirectory' => wfTempDir()
410 } elseif ( $this->fileBackendName
) {
411 global $wgFileBackends;
412 $name = $this->fileBackendName
;
414 foreach ( $wgFileBackends as $conf ) {
415 if ( $conf['name'] === $name ) {
419 if ( $useConfig === false ) {
420 throw new MWException( "Unable to find file backend \"$name\"" );
422 $useConfig['name'] = 'local-backend'; // swap name
423 unset( $useConfig['lockManager'] );
424 unset( $useConfig['fileJournal'] );
425 $class = $useConfig['class'];
426 $backend = new $class( $useConfig );
428 # Replace with a mock. We do not care about generating real
429 # files on the filesystem, just need to expose the file
431 $backend = new MockFileBackend( [
432 'name' => 'local-backend',
433 'wikiId' => wfWikiID()
437 return new RepoGroup(
439 'class' => MockLocalRepo
::class,
441 'url' => 'http://example.com/images',
443 'transformVia404' => false,
444 'backend' => $backend
451 * Execute an array in which elements with integer keys are taken to be
452 * callable objects, and other elements are taken to be global variable
453 * set operations, with the key giving the variable name and the value
454 * giving the new global variable value. A closure is returned which, when
455 * executed, sets the global variables back to the values they had before
456 * this function was called.
460 * @param array $setup
463 protected function executeSetupSnippets( $setup ) {
465 foreach ( $setup as $name => $value ) {
466 if ( is_int( $name ) ) {
469 $saved[$name] = isset( $GLOBALS[$name] ) ?
$GLOBALS[$name] : null;
470 $GLOBALS[$name] = $value;
473 return function () use ( $saved ) {
474 $this->executeSetupSnippets( $saved );
479 * Take a setup array in the same format as the one given to
480 * executeSetupSnippets(), and return a ScopedCallback which, when consumed,
481 * executes the snippets in the setup array in reverse order. This is used
482 * to create "teardown objects" for the public API.
486 * @param array $teardown The snippet array
487 * @param ScopedCallback|null $nextTeardown A ScopedCallback to consume
488 * @return ScopedCallback
490 protected function createTeardownObject( $teardown, $nextTeardown = null ) {
491 return new ScopedCallback( function () use ( $teardown, $nextTeardown ) {
492 // Schedule teardown snippets in reverse order
493 $teardown = array_reverse( $teardown );
495 $this->executeSetupSnippets( $teardown );
496 if ( $nextTeardown ) {
497 ScopedCallback
::consume( $nextTeardown );
503 * Set a setupDone flag to indicate that setup has been done, and return
504 * the teardown closure. If the flag was already set, throw an exception.
506 * @param string $funcName The setup function name
509 protected function markSetupDone( $funcName ) {
510 if ( $this->setupDone
[$funcName] ) {
511 throw new MWException( "$funcName is already done" );
513 $this->setupDone
[$funcName] = true;
514 return function () use ( $funcName ) {
515 $this->setupDone
[$funcName] = false;
520 * Ensure a given setup stage has been done, throw an exception if it has
522 * @param string $funcName
523 * @param string|null $funcName2
525 protected function checkSetupDone( $funcName, $funcName2 = null ) {
526 if ( !$this->setupDone
[$funcName]
527 && ( $funcName === null ||
!$this->setupDone
[$funcName2] )
529 throw new MWException( "$funcName must be called before calling " .
535 * Determine whether a particular setup function has been run
537 * @param string $funcName
540 public function isSetupDone( $funcName ) {
541 return isset( $this->setupDone
[$funcName] ) ?
$this->setupDone
[$funcName] : false;
545 * Insert hardcoded interwiki in the lookup table.
547 * This function insert a set of well known interwikis that are used in
548 * the parser tests. They can be considered has fixtures are injected in
549 * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
550 * Since we are not interested in looking up interwikis in the database,
551 * the hook completely replace the existing mechanism (hook returns false).
553 * @return closure for teardown
555 private function setupInterwikis() {
556 # Hack: insert a few Wikipedia in-project interwiki prefixes,
557 # for testing inter-language links
558 Hooks
::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
559 static $testInterwikis = [
561 'iw_url' => 'http://doesnt.matter.org/$1',
566 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
571 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
576 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
581 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
586 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
591 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
596 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
601 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
606 'iw_url' => 'http://wikisource.org/wiki/$1',
611 if ( array_key_exists( $prefix, $testInterwikis ) ) {
612 $iwData = $testInterwikis[$prefix];
615 // We only want to rely on the above fixtures
617 } );// hooks::register
619 // Reset the service in case any other tests already cached some prefixes.
620 MediaWikiServices
::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
624 Hooks
::clear( 'InterwikiLoadPrefix' );
625 MediaWikiServices
::getInstance()->resetServiceForTesting( 'InterwikiLookup' );
630 * Reset the Title-related services that need resetting
633 private function resetTitleServices() {
634 $services = MediaWikiServices
::getInstance();
635 $services->resetServiceForTesting( 'TitleFormatter' );
636 $services->resetServiceForTesting( 'TitleParser' );
637 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
638 $services->resetServiceForTesting( 'LinkRenderer' );
639 $services->resetServiceForTesting( 'LinkRendererFactory' );
643 * Remove last character if it is a newline
647 public static function chomp( $s ) {
648 if ( substr( $s, -1 ) === "\n" ) {
649 return substr( $s, 0, -1 );
656 * Run a series of tests listed in the given text files.
657 * Each test consists of a brief description, wikitext input,
658 * and the expected HTML output.
660 * Prints status updates on stdout and counts up the total
661 * number and percentage of passed tests.
663 * Handles all setup and teardown.
665 * @param array $filenames Array of strings
666 * @return bool True if passed all tests, false if any tests failed.
668 public function runTestsFromFiles( $filenames ) {
671 $teardownGuard = $this->staticSetup();
672 $teardownGuard = $this->setupDatabase( $teardownGuard );
673 $teardownGuard = $this->setupUploads( $teardownGuard );
675 $this->recorder
->start();
679 foreach ( $filenames as $filename ) {
680 $testFileInfo = TestFileReader
::read( $filename, [
681 'runDisabled' => $this->runDisabled
,
682 'runParsoid' => $this->runParsoid
,
683 'regex' => $this->regex
] );
685 // Don't start the suite if there are no enabled tests in the file
686 if ( !$testFileInfo['tests'] ) {
690 $this->recorder
->startSuite( $filename );
691 $ok = $this->runTests( $testFileInfo ) && $ok;
692 $this->recorder
->endSuite( $filename );
695 $this->recorder
->report();
696 } catch ( DBError
$e ) {
697 $this->recorder
->warning( $e->getMessage() );
699 $this->recorder
->end();
701 ScopedCallback
::consume( $teardownGuard );
707 * Determine whether the current parser has the hooks registered in it
708 * that are required by a file read by TestFileReader.
709 * @param array $requirements
712 public function meetsRequirements( $requirements ) {
713 foreach ( $requirements as $requirement ) {
714 switch ( $requirement['type'] ) {
716 $ok = $this->requireHook( $requirement['name'] );
719 $ok = $this->requireFunctionHook( $requirement['name'] );
721 case 'transparentHook':
722 $ok = $this->requireTransparentHook( $requirement['name'] );
733 * Run the tests from a single file. staticSetup() and setupDatabase()
734 * must have been called already.
736 * @param array $testFileInfo Parsed file info returned by TestFileReader
737 * @return bool True if passed all tests, false if any tests failed.
739 public function runTests( $testFileInfo ) {
742 $this->checkSetupDone( 'staticSetup' );
744 // Don't add articles from the file if there are no enabled tests from the file
745 if ( !$testFileInfo['tests'] ) {
749 // If any requirements are not met, mark all tests from the file as skipped
750 if ( !$this->meetsRequirements( $testFileInfo['requirements'] ) ) {
751 foreach ( $testFileInfo['tests'] as $test ) {
752 $this->recorder
->startTest( $test );
753 $this->recorder
->skipped( $test, 'required extension not enabled' );
759 $this->addArticles( $testFileInfo['articles'] );
762 foreach ( $testFileInfo['tests'] as $test ) {
763 $this->recorder
->startTest( $test );
765 $this->runTest( $test );
766 if ( $result !== false ) {
767 $ok = $ok && $result->isSuccess();
768 $this->recorder
->record( $test, $result );
776 * Get a Parser object
778 * @param string $preprocessor
781 function getParser( $preprocessor = null ) {
782 global $wgParserConf;
784 $class = $wgParserConf['class'];
785 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] +
$wgParserConf );
786 ParserTestParserHook
::setup( $parser );
792 * Run a given wikitext input through a freshly-constructed wiki parser,
793 * and compare the output against the expected results.
794 * Prints status and explanatory messages to stdout.
796 * staticSetup() and setupWikiData() must be called before this function
799 * @param array $test The test parameters:
800 * - test: The test name
801 * - desc: The subtest description
802 * - input: Wikitext to try rendering
803 * - options: Array of test options
804 * - config: Overrides for global variables, one per line
806 * @return ParserTestResult|false false if skipped
808 public function runTest( $test ) {
809 wfDebug( __METHOD__
.": running {$test['desc']}" );
810 $opts = $this->parseOptions( $test['options'] );
811 $teardownGuard = $this->perTestSetup( $test );
813 $context = RequestContext
::getMain();
814 $user = $context->getUser();
815 $options = ParserOptions
::newFromContext( $context );
816 $options->setTimestamp( $this->getFakeTimestamp() );
818 if ( isset( $opts['tidy'] ) ) {
819 if ( !$this->tidySupport
->isEnabled() ) {
820 $this->recorder
->skipped( $test, 'tidy extension is not installed' );
823 $options->setTidy( true );
827 if ( isset( $opts['title'] ) ) {
828 $titleText = $opts['title'];
830 $titleText = 'Parser test';
833 $local = isset( $opts['local'] );
834 $preprocessor = isset( $opts['preprocessor'] ) ?
$opts['preprocessor'] : null;
835 $parser = $this->getParser( $preprocessor );
836 $title = Title
::newFromText( $titleText );
838 if ( isset( $opts['styletag'] ) ) {
839 // For testing the behavior of <style> (including those deduplicated
840 // into <link> tags), add tag hooks to allow them to be generated.
841 $parser->setHook( 'style', function ( $content, $attributes, $parser ) {
842 $marker = Parser
::MARKER_PREFIX
. '-style-' . md5( $content ) . Parser
::MARKER_SUFFIX
;
843 $parser->mStripState
->addNoWiki( $marker, $content );
844 return Html
::inlineStyle( $marker, 'all', $attributes );
846 $parser->setHook( 'link', function ( $content, $attributes, $parser ) {
847 return Html
::element( 'link', $attributes );
851 if ( isset( $opts['pst'] ) ) {
852 $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
853 $output = $parser->getOutput();
854 } elseif ( isset( $opts['msg'] ) ) {
855 $out = $parser->transformMsg( $test['input'], $options, $title );
856 } elseif ( isset( $opts['section'] ) ) {
857 $section = $opts['section'];
858 $out = $parser->getSection( $test['input'], $section );
859 } elseif ( isset( $opts['replace'] ) ) {
860 $section = $opts['replace'][0];
861 $replace = $opts['replace'][1];
862 $out = $parser->replaceSection( $test['input'], $section, $replace );
863 } elseif ( isset( $opts['comment'] ) ) {
864 $out = Linker
::formatComment( $test['input'], $title, $local );
865 } elseif ( isset( $opts['preload'] ) ) {
866 $out = $parser->getPreloadText( $test['input'], $title, $options );
868 $output = $parser->parse( $test['input'], $title, $options, true, true, 1337 );
869 $out = $output->getText( [
870 'allowTOC' => !isset( $opts['notoc'] ),
871 'unwrap' => !isset( $opts['wrap'] ),
873 if ( isset( $opts['tidy'] ) ) {
874 $out = preg_replace( '/\s+$/', '', $out );
877 if ( isset( $opts['showtitle'] ) ) {
878 if ( $output->getTitleText() ) {
879 $title = $output->getTitleText();
882 $out = "$title\n$out";
885 if ( isset( $opts['showindicators'] ) ) {
887 foreach ( $output->getIndicators() as $id => $content ) {
888 $indicators .= "$id=$content\n";
890 $out = $indicators . $out;
893 if ( isset( $opts['ill'] ) ) {
894 $out = implode( ' ', $output->getLanguageLinks() );
895 } elseif ( isset( $opts['cat'] ) ) {
897 foreach ( $output->getCategories() as $name => $sortkey ) {
901 $out .= "cat=$name sort=$sortkey";
906 if ( isset( $output ) && isset( $opts['showflags'] ) ) {
907 $actualFlags = array_keys( TestingAccessWrapper
::newFromObject( $output )->mFlags
);
908 sort( $actualFlags );
909 $out .= "\nflags=" . implode( ', ', $actualFlags );
912 ScopedCallback
::consume( $teardownGuard );
914 $expected = $test['result'];
915 if ( count( $this->normalizationFunctions
) ) {
916 $expected = ParserTestResultNormalizer
::normalize(
917 $test['expected'], $this->normalizationFunctions
);
918 $out = ParserTestResultNormalizer
::normalize( $out, $this->normalizationFunctions
);
921 $testResult = new ParserTestResult( $test, $expected, $out );
926 * Use a regex to find out the value of an option
927 * @param string $key Name of option val to retrieve
928 * @param array $opts Options array to look in
929 * @param mixed $default Default value returned if not found
932 private static function getOptionValue( $key, $opts, $default ) {
933 $key = strtolower( $key );
935 if ( isset( $opts[$key] ) ) {
943 * Given the options string, return an associative array of options.
944 * @todo Move this to TestFileReader
946 * @param string $instring
949 private function parseOptions( $instring ) {
955 // foo=bar,"baz quux"
958 (?<qstr> # Quoted string
960 (?:[^\\\\"] | \\\\.)*
966 [^"{}] | # Not a quoted string or object, or
967 (?&qstr) | # A quoted string, or
968 (?&json) # A json object (recursively)
974 (?&qstr) # Quoted val
982 (?&json) # JSON object
986 $regex = '/' . $defs . '\b
1002 $valueregex = '/' . $defs . '(?&value)/x';
1004 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER
) ) {
1005 foreach ( $matches as $bits ) {
1006 $key = strtolower( $bits['k'] );
1007 if ( !isset( $bits['v'] ) ) {
1010 preg_match_all( $valueregex, $bits['v'], $vmatches );
1011 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
1012 if ( count( $opts[$key] ) == 1 ) {
1013 $opts[$key] = $opts[$key][0];
1021 private function cleanupOption( $opt ) {
1022 if ( substr( $opt, 0, 1 ) == '"' ) {
1023 return stripcslashes( substr( $opt, 1, -1 ) );
1026 if ( substr( $opt, 0, 2 ) == '[[' ) {
1027 return substr( $opt, 2, -2 );
1030 if ( substr( $opt, 0, 1 ) == '{' ) {
1031 return FormatJson
::decode( $opt, true );
1037 * Do any required setup which is dependent on test options.
1039 * @see staticSetup() for more information about setup/teardown
1041 * @param array $test Test info supplied by TestFileReader
1042 * @param callable|null $nextTeardown
1043 * @return ScopedCallback
1045 public function perTestSetup( $test, $nextTeardown = null ) {
1048 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1049 $teardown[] = $this->markSetupDone( 'perTestSetup' );
1051 $opts = $this->parseOptions( $test['options'] );
1052 $config = $test['config'];
1054 // Find out values for some special options.
1056 self
::getOptionValue( 'language', $opts, 'en' );
1058 self
::getOptionValue( 'variant', $opts, false );
1060 self
::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
1061 $linkHolderBatchSize =
1062 self
::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
1064 // Default to fallback skin, but allow it to be overridden
1065 $skin = self
::getOptionValue( 'skin', $opts, 'fallback' );
1068 'wgEnableUploads' => self
::getOptionValue( 'wgEnableUploads', $opts, true ),
1069 'wgLanguageCode' => $langCode,
1070 'wgRawHtml' => self
::getOptionValue( 'wgRawHtml', $opts, false ),
1071 'wgNamespacesWithSubpages' => array_fill_keys(
1072 MWNamespace
::getValidNamespaces(), isset( $opts['subpage'] )
1074 'wgMaxTocLevel' => $maxtoclevel,
1075 'wgAllowExternalImages' => self
::getOptionValue( 'wgAllowExternalImages', $opts, true ),
1076 'wgThumbLimits' => [ self
::getOptionValue( 'thumbsize', $opts, 180 ) ],
1077 'wgDefaultLanguageVariant' => $variant,
1078 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
1079 // Set as a JSON object like:
1080 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
1081 'wgEnableMagicLinks' => self
::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
1082 +
[ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
1083 // Test with legacy encoding by default until HTML5 is very stable and default
1084 'wgFragmentMode' => [ 'legacy' ],
1088 $configLines = explode( "\n", $config );
1090 foreach ( $configLines as $line ) {
1091 list( $var, $value ) = explode( '=', $line, 2 );
1092 $setup[$var] = eval( "return $value;" );
1097 Hooks
::run( 'ParserTestGlobals', [ &$setup ] );
1099 // Create tidy driver
1100 if ( isset( $opts['tidy'] ) ) {
1101 // Cache a driver instance
1102 if ( $this->tidyDriver
=== null ) {
1103 $this->tidyDriver
= MWTidy
::factory( $this->tidySupport
->getConfig() );
1105 $tidy = $this->tidyDriver
;
1109 MWTidy
::setInstance( $tidy );
1110 $teardown[] = function () {
1111 MWTidy
::destroySingleton();
1114 // Set content language. This invalidates the magic word cache and title services
1115 $lang = Language
::factory( $langCode );
1116 $lang->resetNamespaces();
1117 $setup['wgContLang'] = $lang;
1118 $reset = function () {
1119 MagicWord
::clearCache();
1120 $this->resetTitleServices();
1123 $teardown[] = $reset;
1125 // Make a user object with the same language
1127 $user->setOption( 'language', $langCode );
1128 $setup['wgLang'] = $lang;
1130 // We (re)set $wgThumbLimits to a single-element array above.
1131 $user->setOption( 'thumbsize', 0 );
1133 $setup['wgUser'] = $user;
1135 // And put both user and language into the context
1136 $context = RequestContext
::getMain();
1137 $context->setUser( $user );
1138 $context->setLanguage( $lang );
1140 $oldSkin = $context->getSkin();
1141 $skinFactory = MediaWikiServices
::getInstance()->getSkinFactory();
1142 $context->setSkin( $skinFactory->makeSkin( $skin ) );
1143 $context->setOutput( new OutputPage( $context ) );
1144 $setup['wgOut'] = $context->getOutput();
1145 $teardown[] = function () use ( $context, $oldSkin ) {
1146 // Clear language conversion tables
1147 $wrapper = TestingAccessWrapper
::newFromObject(
1148 $context->getLanguage()->getConverter()
1150 $wrapper->reloadTables();
1151 // Reset context to the restored globals
1152 $context->setUser( $GLOBALS['wgUser'] );
1153 $context->setLanguage( $GLOBALS['wgContLang'] );
1154 $context->setSkin( $oldSkin );
1155 $context->setOutput( $GLOBALS['wgOut'] );
1158 $teardown[] = $this->executeSetupSnippets( $setup );
1160 return $this->createTeardownObject( $teardown, $nextTeardown );
1164 * List of temporary tables to create, without prefix.
1165 * Some of these probably aren't necessary.
1168 private function listTables() {
1169 global $wgCommentTableSchemaMigrationStage, $wgActorTableSchemaMigrationStage;
1171 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1172 'protected_titles', 'revision', 'ip_changes', 'text', 'pagelinks', 'imagelinks',
1173 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1174 'site_stats', 'ipblocks', 'image', 'oldimage',
1175 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1176 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1177 'archive', 'user_groups', 'page_props', 'category'
1180 if ( $wgCommentTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
) {
1181 // The new tables for comments are in use
1182 $tables[] = 'comment';
1183 $tables[] = 'revision_comment_temp';
1184 $tables[] = 'image_comment_temp';
1187 if ( $wgActorTableSchemaMigrationStage >= MIGRATION_WRITE_BOTH
) {
1188 // The new tables for actors are in use
1189 $tables[] = 'actor';
1190 $tables[] = 'revision_actor_temp';
1193 if ( in_array( $this->db
->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1194 array_push( $tables, 'searchindex' );
1197 // Allow extensions to add to the list of tables to duplicate;
1198 // may be necessary if they hook into page save or other code
1199 // which will require them while running tests.
1200 Hooks
::run( 'ParserTestTables', [ &$tables ] );
1205 public function setDatabase( IDatabase
$db ) {
1207 $this->setupDone
['setDatabase'] = true;
1211 * Set up temporary DB tables.
1213 * For best performance, call this once only for all tests. However, it can
1214 * be called at the start of each test if more isolation is desired.
1216 * @todo: This is basically an unrefactored copy of
1217 * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.
1219 * Do not call this function from a MediaWikiTestCase subclass, since
1220 * MediaWikiTestCase does its own DB setup. Instead use setDatabase().
1222 * @see staticSetup() for more information about setup/teardown
1224 * @param ScopedCallback|null $nextTeardown The next teardown object
1225 * @return ScopedCallback The teardown object
1227 public function setupDatabase( $nextTeardown = null ) {
1230 $this->db
= wfGetDB( DB_MASTER
);
1231 $dbType = $this->db
->getType();
1233 if ( $dbType == 'oracle' ) {
1234 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase
::ORA_DB_PREFIX
];
1236 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase
::DB_PREFIX
];
1238 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1239 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1244 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1246 # CREATE TEMPORARY TABLE breaks if there is more than one server
1247 if ( wfGetLB()->getServerCount() != 1 ) {
1248 $this->useTemporaryTables
= false;
1251 $temporary = $this->useTemporaryTables ||
$dbType == 'postgres';
1252 $prefix = $dbType != 'oracle' ?
'parsertest_' : 'pt_';
1254 $this->dbClone
= new CloneDatabase( $this->db
, $this->listTables(), $prefix );
1255 $this->dbClone
->useTemporaryTables( $temporary );
1256 $this->dbClone
->cloneTableStructure();
1258 if ( $dbType == 'oracle' ) {
1259 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1260 # Insert 0 user to prevent FK violations
1263 $this->db
->insert( 'user', [
1265 'user_name' => 'Anonymous' ] );
1268 $teardown[] = function () {
1269 $this->teardownDatabase();
1272 // Wipe some DB query result caches on setup and teardown
1273 $reset = function () {
1274 LinkCache
::singleton()->clear();
1276 // Clear the message cache
1277 MessageCache
::singleton()->clear();
1280 $teardown[] = $reset;
1281 return $this->createTeardownObject( $teardown, $nextTeardown );
1285 * Add data about uploads to the new test DB, and set up the upload
1286 * directory. This should be called after either setDatabase() or
1289 * @param ScopedCallback|null $nextTeardown The next teardown object
1290 * @return ScopedCallback The teardown object
1292 public function setupUploads( $nextTeardown = null ) {
1295 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1296 $teardown[] = $this->markSetupDone( 'setupUploads' );
1298 // Create the files in the upload directory (or pretend to create them
1299 // in a MockFileBackend). Append teardown callback.
1300 $teardown[] = $this->setupUploadBackend();
1303 $user = User
::createNew( 'WikiSysop' );
1305 // Register the uploads in the database
1307 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.jpg' ) );
1308 # note that the size/width/height/bits/etc of the file
1309 # are actually set by inspecting the file itself; the arguments
1310 # to recordUpload2 have no effect. That said, we try to make things
1311 # match up so it is less confusing to readers of the code & tests.
1312 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1317 'media_type' => MEDIATYPE_BITMAP
,
1318 'mime' => 'image/jpeg',
1319 'metadata' => serialize( [] ),
1320 'sha1' => Wikimedia\base_convert
( '1', 16, 36, 31 ),
1321 'fileExists' => true
1322 ], $this->db
->timestamp( '20010115123500' ), $user );
1324 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Thumb.png' ) );
1325 # again, note that size/width/height below are ignored; see above.
1326 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1331 'media_type' => MEDIATYPE_BITMAP
,
1332 'mime' => 'image/png',
1333 'metadata' => serialize( [] ),
1334 'sha1' => Wikimedia\base_convert
( '2', 16, 36, 31 ),
1335 'fileExists' => true
1336 ], $this->db
->timestamp( '20130225203040' ), $user );
1338 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.svg' ) );
1339 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1344 'media_type' => MEDIATYPE_DRAWING
,
1345 'mime' => 'image/svg+xml',
1346 'metadata' => serialize( [] ),
1347 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1348 'fileExists' => true
1349 ], $this->db
->timestamp( '20010115123500' ), $user );
1351 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1352 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Bad.jpg' ) );
1353 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1358 'media_type' => MEDIATYPE_BITMAP
,
1359 'mime' => 'image/jpeg',
1360 'metadata' => serialize( [] ),
1361 'sha1' => Wikimedia\base_convert
( '3', 16, 36, 31 ),
1362 'fileExists' => true
1363 ], $this->db
->timestamp( '20010115123500' ), $user );
1365 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Video.ogv' ) );
1366 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1371 'media_type' => MEDIATYPE_VIDEO
,
1372 'mime' => 'application/ogg',
1373 'metadata' => serialize( [] ),
1374 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1375 'fileExists' => true
1376 ], $this->db
->timestamp( '20010115123500' ), $user );
1378 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Audio.oga' ) );
1379 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1384 'media_type' => MEDIATYPE_AUDIO
,
1385 'mime' => 'application/ogg',
1386 'metadata' => serialize( [] ),
1387 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1388 'fileExists' => true
1389 ], $this->db
->timestamp( '20010115123500' ), $user );
1392 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'LoremIpsum.djvu' ) );
1393 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1398 'media_type' => MEDIATYPE_BITMAP
,
1399 'mime' => 'image/vnd.djvu',
1400 'metadata' => '<?xml version="1.0" ?>
1401 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1404 <BODY><OBJECT height="3508" width="2480">
1405 <PARAM name="DPI" value="300" />
1406 <PARAM name="GAMMA" value="2.2" />
1408 <OBJECT height="3508" width="2480">
1409 <PARAM name="DPI" value="300" />
1410 <PARAM name="GAMMA" value="2.2" />
1412 <OBJECT height="3508" width="2480">
1413 <PARAM name="DPI" value="300" />
1414 <PARAM name="GAMMA" value="2.2" />
1416 <OBJECT height="3508" width="2480">
1417 <PARAM name="DPI" value="300" />
1418 <PARAM name="GAMMA" value="2.2" />
1420 <OBJECT height="3508" width="2480">
1421 <PARAM name="DPI" value="300" />
1422 <PARAM name="GAMMA" value="2.2" />
1426 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
1427 'fileExists' => true
1428 ], $this->db
->timestamp( '20010115123600' ), $user );
1430 return $this->createTeardownObject( $teardown, $nextTeardown );
1434 * Helper for database teardown, called from the teardown closure. Destroy
1435 * the database clone and fix up some things that CloneDatabase doesn't fix.
1437 * @todo Move most things here to CloneDatabase
1439 private function teardownDatabase() {
1440 $this->checkSetupDone( 'setupDatabase' );
1442 $this->dbClone
->destroy();
1443 $this->databaseSetupDone
= false;
1445 if ( $this->useTemporaryTables
) {
1446 if ( $this->db
->getType() == 'sqlite' ) {
1447 # Under SQLite the searchindex table is virtual and need
1448 # to be explicitly destroyed. See T31912
1449 # See also MediaWikiTestCase::destroyDB()
1450 wfDebug( __METHOD__
. " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1451 $this->db
->query( "DROP TABLE `parsertest_searchindex`" );
1453 # Don't need to do anything
1457 $tables = $this->listTables();
1459 foreach ( $tables as $table ) {
1460 if ( $this->db
->getType() == 'oracle' ) {
1461 $this->db
->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1463 $this->db
->query( "DROP TABLE `parsertest_$table`" );
1467 if ( $this->db
->getType() == 'oracle' ) {
1468 $this->db
->query( 'BEGIN FILL_WIKI_INFO; END;' );
1473 * Upload test files to the backend created by createRepoGroup().
1475 * @return callable The teardown callback
1477 private function setupUploadBackend() {
1480 $repo = RepoGroup
::singleton()->getLocalRepo();
1481 $base = $repo->getZonePath( 'public' );
1482 $backend = $repo->getBackend();
1483 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1485 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1486 'dst' => "$base/3/3a/Foobar.jpg"
1488 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1490 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1491 'dst' => "$base/e/ea/Thumb.png"
1493 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1495 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1496 'dst' => "$base/0/09/Bad.jpg"
1498 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1500 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1501 'dst' => "$base/5/5f/LoremIpsum.djvu"
1504 // No helpful SVG file to copy, so make one ourselves
1505 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1506 '<svg xmlns="http://www.w3.org/2000/svg"' .
1507 ' version="1.1" width="240" height="180"/>';
1509 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1510 $backend->quickCreate( [
1511 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1514 return function () use ( $backend ) {
1515 if ( $backend instanceof MockFileBackend
) {
1516 // In memory backend, so dont bother cleaning them up.
1519 $this->teardownUploadBackend();
1524 * Remove the dummy uploads directory
1526 private function teardownUploadBackend() {
1527 if ( $this->keepUploads
) {
1531 $repo = RepoGroup
::singleton()->getLocalRepo();
1532 $public = $repo->getZonePath( 'public' );
1536 "$public/3/3a/Foobar.jpg",
1537 "$public/e/ea/Thumb.png",
1538 "$public/0/09/Bad.jpg",
1539 "$public/5/5f/LoremIpsum.djvu",
1540 "$public/f/ff/Foobar.svg",
1541 "$public/0/00/Video.ogv",
1542 "$public/4/41/Audio.oga",
1548 * Delete the specified files and their parent directories
1549 * @param array $files File backend URIs mwstore://...
1551 private function deleteFiles( $files ) {
1553 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
1554 foreach ( $files as $file ) {
1555 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1558 // Delete the parent directories
1559 foreach ( $files as $file ) {
1560 $tmp = FileBackend
::parentStoragePath( $file );
1562 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1565 $tmp = FileBackend
::parentStoragePath( $tmp );
1571 * Add articles to the test DB.
1573 * @param array $articles Article info array from TestFileReader
1575 public function addArticles( $articles ) {
1580 // Be sure ParserTestRunner::addArticle has correct language set,
1581 // so that system messages get into the right language cache
1582 if ( $wgContLang->getCode() !== 'en' ) {
1583 $setup['wgLanguageCode'] = 'en';
1584 $setup['wgContLang'] = Language
::factory( 'en' );
1587 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1588 $this->appendNamespaceSetup( $setup, $teardown );
1590 // wgCapitalLinks obviously needs initialisation
1591 $setup['wgCapitalLinks'] = true;
1593 $teardown[] = $this->executeSetupSnippets( $setup );
1595 foreach ( $articles as $info ) {
1596 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1599 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1601 ObjectCache
::getMainWANInstance()->clearProcessCache();
1603 $this->executeSetupSnippets( $teardown );
1607 * Insert a temporary test article
1608 * @param string $name The title, including any prefix
1609 * @param string $text The article text
1610 * @param string $file The input file name
1611 * @param int|string $line The input line number, for reporting errors
1613 * @throws MWException
1615 private function addArticle( $name, $text, $file, $line ) {
1616 $text = self
::chomp( $text );
1617 $name = self
::chomp( $name );
1619 $title = Title
::newFromText( $name );
1620 wfDebug( __METHOD__
. ": adding $name" );
1622 if ( is_null( $title ) ) {
1623 throw new MWException( "invalid title '$name' at $file:$line\n" );
1626 $newContent = ContentHandler
::makeContent( $text, $title );
1628 $page = WikiPage
::factory( $title );
1629 $page->loadPageData( 'fromdbmaster' );
1631 if ( $page->exists() ) {
1632 $content = $page->getContent( Revision
::RAW
);
1633 // Only reject the title, if the content/content model is different.
1634 // This makes it easier to create Template:(( or Template:)) in different extensions
1635 if ( $newContent->equals( $content ) ) {
1638 throw new MWException(
1639 "duplicate article '$name' with different content at $file:$line\n"
1643 // Use mock parser, to make debugging of actual parser tests simpler.
1644 // But initialise the MessageCache clone first, don't let MessageCache
1645 // get a reference to the mock object.
1646 MessageCache
::singleton()->getParser();
1647 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser
] );
1649 $status = $page->doEditContent(
1652 EDIT_NEW | EDIT_INTERNAL
1658 if ( !$status->isOK() ) {
1659 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1662 // The RepoGroup cache is invalidated by the creation of file redirects
1663 if ( $title->inNamespace( NS_FILE
) ) {
1664 RepoGroup
::singleton()->clearCache( $title );
1669 * Check if a hook is installed
1671 * @param string $name
1672 * @return bool True if tag hook is present
1674 public function requireHook( $name ) {
1677 $wgParser->firstCallInit(); // make sure hooks are loaded.
1678 if ( isset( $wgParser->mTagHooks
[$name] ) ) {
1681 $this->recorder
->warning( " This test suite requires the '$name' hook " .
1682 "extension, skipping." );
1688 * Check if a function hook is installed
1690 * @param string $name
1691 * @return bool True if function hook is present
1693 public function requireFunctionHook( $name ) {
1696 $wgParser->firstCallInit(); // make sure hooks are loaded.
1698 if ( isset( $wgParser->mFunctionHooks
[$name] ) ) {
1701 $this->recorder
->warning( " This test suite requires the '$name' function " .
1702 "hook extension, skipping." );
1708 * Check if a transparent tag hook is installed
1710 * @param string $name
1711 * @return bool True if function hook is present
1713 public function requireTransparentHook( $name ) {
1716 $wgParser->firstCallInit(); // make sure hooks are loaded.
1718 if ( isset( $wgParser->mTransparentTagHooks
[$name] ) ) {
1721 $this->recorder
->warning( " This test suite requires the '$name' transparent " .
1722 "hook extension, skipping.\n" );
1728 * Fake constant timestamp to make sure time-related parser
1729 * functions give a persistent value.
1731 * - Parser::getVariableValue (via ParserGetVariableValueTs hook)
1732 * - Parser::preSaveTransform (via ParserOptions)
1734 private function getFakeTimestamp() {
1735 // parsed as '1970-01-01T00:02:03Z'