Add 'ApiMakeParserOptions' hook
[mediawiki.git] / tests / parser / parserTest.inc
blobe519f59d0aa9fb0cac2d4ada1d58cf7377ec47a4
1 <?php
2 /**
3  * Helper code for the MediaWiki parser test suite. Some code is duplicated
4  * in PHPUnit's NewParserTests.php, so you'll probably want to update both
5  * at the same time.
6  *
7  * Copyright © 2004, 2010 Brion Vibber <brion@pobox.com>
8  * https://www.mediawiki.org/
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  * http://www.gnu.org/copyleft/gpl.html
24  *
25  * @todo Make this more independent of the configuration (and if possible the database)
26  * @todo document
27  * @file
28  * @ingroup Testing
29  */
30 use MediaWiki\MediaWikiServices;
32 /**
33  * @ingroup Testing
34  */
35 class ParserTest {
36         /**
37          * @var bool $color whereas output should be colorized
38          */
39         private $color;
41         /**
42          * @var bool $showOutput Show test output
43          */
44         private $showOutput;
46         /**
47          * @var bool $useTemporaryTables Use temporary tables for the temporary database
48          */
49         private $useTemporaryTables = true;
51         /**
52          * @var bool $databaseSetupDone True if the database has been set up
53          */
54         private $databaseSetupDone = false;
56         /**
57          * Our connection to the database
58          * @var DatabaseBase
59          */
60         private $db;
62         /**
63          * Database clone helper
64          * @var CloneDatabase
65          */
66         private $dbClone;
68         /**
69          * @var DjVuSupport
70          */
71         private $djVuSupport;
73         /**
74          * @var TidySupport
75          */
76         private $tidySupport;
78         private $maxFuzzTestLength = 300;
79         private $fuzzSeed = 0;
80         private $memoryLimit = 50;
81         private $uploadDir = null;
83         public $regex = "";
84         private $savedGlobals = [];
86         /**
87          * Sets terminal colorization and diff/quick modes depending on OS and
88          * command-line options (--color and --quick).
89          * @param array $options
90          */
91         public function __construct( $options = [] ) {
92                 # Only colorize output if stdout is a terminal.
93                 $this->color = !wfIsWindows() && Maintenance::posix_isatty( 1 );
95                 if ( isset( $options['color'] ) ) {
96                         switch ( $options['color'] ) {
97                                 case 'no':
98                                         $this->color = false;
99                                         break;
100                                 case 'yes':
101                                 default:
102                                         $this->color = true;
103                                         break;
104                         }
105                 }
107                 $this->term = $this->color
108                         ? new AnsiTermColorer()
109                         : new DummyTermColorer();
111                 $this->showDiffs = !isset( $options['quick'] );
112                 $this->showProgress = !isset( $options['quiet'] );
113                 $this->showFailure = !(
114                         isset( $options['quiet'] )
115                                 && ( isset( $options['record'] )
116                                 || isset( $options['compare'] ) ) ); // redundant output
118                 $this->showOutput = isset( $options['show-output'] );
120                 if ( isset( $options['filter'] ) ) {
121                         $options['regex'] = $options['filter'];
122                 }
124                 if ( isset( $options['regex'] ) ) {
125                         if ( isset( $options['record'] ) ) {
126                                 echo "Warning: --record cannot be used with --regex, disabling --record\n";
127                                 unset( $options['record'] );
128                         }
129                         $this->regex = $options['regex'];
130                 } else {
131                         # Matches anything
132                         $this->regex = '';
133                 }
135                 $this->setupRecorder( $options );
136                 $this->keepUploads = isset( $options['keep-uploads'] );
138                 if ( $this->keepUploads ) {
139                         $this->uploadDir = wfTempDir() . '/mwParser-images';
140                 } else {
141                         $this->uploadDir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
142                 }
144                 if ( isset( $options['seed'] ) ) {
145                         $this->fuzzSeed = intval( $options['seed'] ) - 1;
146                 }
148                 $this->runDisabled = isset( $options['run-disabled'] );
149                 $this->runParsoid = isset( $options['run-parsoid'] );
151                 $this->djVuSupport = new DjVuSupport();
152                 $this->tidySupport = new TidySupport();
153                 if ( !$this->tidySupport->isEnabled() ) {
154                         echo "Warning: tidy is not installed, skipping some tests\n";
155                 }
157                 if ( !extension_loaded( 'gd' ) ) {
158                         echo "Warning: GD extension is not present, thumbnailing tests will probably fail\n";
159                 }
161                 $this->hooks = [];
162                 $this->functionHooks = [];
163                 $this->transparentHooks = [];
164                 $this->setUp();
165         }
167         function setUp() {
168                 global $wgParser, $wgParserConf, $IP, $messageMemc, $wgMemc,
169                         $wgUser, $wgLang, $wgOut, $wgRequest, $wgStyleDirectory,
170                         $wgExtraNamespaces, $wgNamespaceAliases, $wgNamespaceProtection, $wgLocalFileRepo,
171                         $wgExtraInterlanguageLinkPrefixes, $wgLocalInterwikis,
172                         $parserMemc, $wgThumbnailScriptPath, $wgScriptPath, $wgResourceBasePath,
173                         $wgArticlePath, $wgScript, $wgStylePath, $wgExtensionAssetsPath,
174                         $wgMainCacheType, $wgMessageCacheType, $wgParserCacheType, $wgLockManagers;
176                 $wgScriptPath = '';
177                 $wgScript = '/index.php';
178                 $wgStylePath = '/skins';
179                 $wgResourceBasePath = '';
180                 $wgExtensionAssetsPath = '/extensions';
181                 $wgArticlePath = '/wiki/$1';
182                 $wgThumbnailScriptPath = false;
183                 $wgLockManagers = [ [
184                         'name' => 'fsLockManager',
185                         'class' => 'FSLockManager',
186                         'lockDirectory' => $this->uploadDir . '/lockdir',
187                 ], [
188                         'name' => 'nullLockManager',
189                         'class' => 'NullLockManager',
190                 ] ];
191                 $wgLocalFileRepo = [
192                         'class' => 'LocalRepo',
193                         'name' => 'local',
194                         'url' => 'http://example.com/images',
195                         'hashLevels' => 2,
196                         'transformVia404' => false,
197                         'backend' => new FSFileBackend( [
198                                 'name' => 'local-backend',
199                                 'wikiId' => wfWikiID(),
200                                 'containerPaths' => [
201                                         'local-public' => $this->uploadDir . '/public',
202                                         'local-thumb' => $this->uploadDir . '/thumb',
203                                         'local-temp' => $this->uploadDir . '/temp',
204                                         'local-deleted' => $this->uploadDir . '/deleted',
205                                 ]
206                         ] )
207                 ];
208                 $wgNamespaceProtection[NS_MEDIAWIKI] = 'editinterface';
209                 $wgNamespaceAliases['Image'] = NS_FILE;
210                 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
211                 # add a namespace shadowing a interwiki link, to test
212                 # proper precedence when resolving links. (bug 51680)
213                 $wgExtraNamespaces[100] = 'MemoryAlpha';
214                 $wgExtraNamespaces[101] = 'MemoryAlpha talk';
216                 // XXX: tests won't run without this (for CACHE_DB)
217                 if ( $wgMainCacheType === CACHE_DB ) {
218                         $wgMainCacheType = CACHE_NONE;
219                 }
220                 if ( $wgMessageCacheType === CACHE_DB ) {
221                         $wgMessageCacheType = CACHE_NONE;
222                 }
223                 if ( $wgParserCacheType === CACHE_DB ) {
224                         $wgParserCacheType = CACHE_NONE;
225                 }
227                 DeferredUpdates::clearPendingUpdates();
228                 $wgMemc = wfGetMainCache(); // checks $wgMainCacheType
229                 $messageMemc = wfGetMessageCacheStorage();
230                 $parserMemc = wfGetParserCacheStorage();
232                 RequestContext::resetMain();
233                 $context = new RequestContext;
234                 $wgUser = new User;
235                 $wgLang = $context->getLanguage();
236                 $wgOut = $context->getOutput();
237                 $wgRequest = $context->getRequest();
238                 $wgParser = new StubObject( 'wgParser', $wgParserConf['class'], [ $wgParserConf ] );
240                 if ( $wgStyleDirectory === false ) {
241                         $wgStyleDirectory = "$IP/skins";
242                 }
244                 self::setupInterwikis();
245                 $wgLocalInterwikis = [ 'local', 'mi' ];
246                 // "extra language links"
247                 // see https://gerrit.wikimedia.org/r/111390
248                 array_push( $wgExtraInterlanguageLinkPrefixes, 'mul' );
250                 // Reset namespace cache
251                 MWNamespace::getCanonicalNamespaces( true );
252                 Language::factory( 'en' )->resetNamespaces();
253         }
255         /**
256          * Insert hardcoded interwiki in the lookup table.
257          *
258          * This function insert a set of well known interwikis that are used in
259          * the parser tests. They can be considered has fixtures are injected in
260          * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
261          * Since we are not interested in looking up interwikis in the database,
262          * the hook completely replace the existing mechanism (hook returns false).
263          */
264         public static function setupInterwikis() {
265                 # Hack: insert a few Wikipedia in-project interwiki prefixes,
266                 # for testing inter-language links
267                 Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
268                         static $testInterwikis = [
269                                 'local' => [
270                                         'iw_url' => 'http://doesnt.matter.org/$1',
271                                         'iw_api' => '',
272                                         'iw_wikiid' => '',
273                                         'iw_local' => 0 ],
274                                 'wikipedia' => [
275                                         'iw_url' => 'http://en.wikipedia.org/wiki/$1',
276                                         'iw_api' => '',
277                                         'iw_wikiid' => '',
278                                         'iw_local' => 0 ],
279                                 'meatball' => [
280                                         'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
281                                         'iw_api' => '',
282                                         'iw_wikiid' => '',
283                                         'iw_local' => 0 ],
284                                 'memoryalpha' => [
285                                         'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
286                                         'iw_api' => '',
287                                         'iw_wikiid' => '',
288                                         'iw_local' => 0 ],
289                                 'zh' => [
290                                         'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
291                                         'iw_api' => '',
292                                         'iw_wikiid' => '',
293                                         'iw_local' => 1 ],
294                                 'es' => [
295                                         'iw_url' => 'http://es.wikipedia.org/wiki/$1',
296                                         'iw_api' => '',
297                                         'iw_wikiid' => '',
298                                         'iw_local' => 1 ],
299                                 'fr' => [
300                                         'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
301                                         'iw_api' => '',
302                                         'iw_wikiid' => '',
303                                         'iw_local' => 1 ],
304                                 'ru' => [
305                                         'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
306                                         'iw_api' => '',
307                                         'iw_wikiid' => '',
308                                         'iw_local' => 1 ],
309                                 'mi' => [
310                                         'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
311                                         'iw_api' => '',
312                                         'iw_wikiid' => '',
313                                         'iw_local' => 1 ],
314                                 'mul' => [
315                                         'iw_url' => 'http://wikisource.org/wiki/$1',
316                                         'iw_api' => '',
317                                         'iw_wikiid' => '',
318                                         'iw_local' => 1 ],
319                         ];
320                         if ( array_key_exists( $prefix, $testInterwikis ) ) {
321                                 $iwData = $testInterwikis[$prefix];
322                         }
324                         // We only want to rely on the above fixtures
325                         return false;
326                 } );// hooks::register
327         }
329         /**
330          * Remove the hardcoded interwiki lookup table.
331          */
332         public static function tearDownInterwikis() {
333                 Hooks::clear( 'InterwikiLoadPrefix' );
334         }
336         /**
337          * Reset the Title-related services that need resetting
338          * for each test
339          */
340         public static function resetTitleServices() {
341                 $services = MediaWikiServices::getInstance();
342                 $services->resetServiceForTesting( 'TitleFormatter' );
343                 $services->resetServiceForTesting( 'TitleParser' );
344                 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
345                 $services->resetServiceForTesting( 'LinkRenderer' );
346                 $services->resetServiceForTesting( 'LinkRendererFactory' );
347         }
349         public function setupRecorder( $options ) {
350                 if ( isset( $options['record'] ) ) {
351                         $this->recorder = new DbTestRecorder( $this );
352                         $this->recorder->version = isset( $options['setversion'] ) ?
353                                 $options['setversion'] : SpecialVersion::getVersion();
354                 } elseif ( isset( $options['compare'] ) ) {
355                         $this->recorder = new DbTestPreviewer( $this );
356                 } else {
357                         $this->recorder = new TestRecorder( $this );
358                 }
359         }
361         /**
362          * Remove last character if it is a newline
363          * @group utility
364          * @param string $s
365          * @return string
366          */
367         public static function chomp( $s ) {
368                 if ( substr( $s, -1 ) === "\n" ) {
369                         return substr( $s, 0, -1 );
370                 } else {
371                         return $s;
372                 }
373         }
375         /**
376          * Run a fuzz test series
377          * Draw input from a set of test files
378          * @param array $filenames
379          */
380         function fuzzTest( $filenames ) {
381                 $GLOBALS['wgContLang'] = Language::factory( 'en' );
382                 $dict = $this->getFuzzInput( $filenames );
383                 $dictSize = strlen( $dict );
384                 $logMaxLength = log( $this->maxFuzzTestLength );
385                 $this->setupDatabase();
386                 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
388                 $numTotal = 0;
389                 $numSuccess = 0;
390                 $user = new User;
391                 $opts = ParserOptions::newFromUser( $user );
392                 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
394                 while ( true ) {
395                         // Generate test input
396                         mt_srand( ++$this->fuzzSeed );
397                         $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
398                         $input = '';
400                         while ( strlen( $input ) < $totalLength ) {
401                                 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
402                                 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
403                                 $offset = mt_rand( 0, $dictSize - $hairLength );
404                                 $input .= substr( $dict, $offset, $hairLength );
405                         }
407                         $this->setupGlobals();
408                         $parser = $this->getParser();
410                         // Run the test
411                         try {
412                                 $parser->parse( $input, $title, $opts );
413                                 $fail = false;
414                         } catch ( Exception $exception ) {
415                                 $fail = true;
416                         }
418                         if ( $fail ) {
419                                 echo "Test failed with seed {$this->fuzzSeed}\n";
420                                 echo "Input:\n";
421                                 printf( "string(%d) \"%s\"\n\n", strlen( $input ), $input );
422                                 echo "$exception\n";
423                         } else {
424                                 $numSuccess++;
425                         }
427                         $numTotal++;
428                         $this->teardownGlobals();
429                         $parser->__destruct();
431                         if ( $numTotal % 100 == 0 ) {
432                                 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
433                                 echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
434                                 if ( $usage > 90 ) {
435                                         echo "Out of memory:\n";
436                                         $memStats = $this->getMemoryBreakdown();
438                                         foreach ( $memStats as $name => $usage ) {
439                                                 echo "$name: $usage\n";
440                                         }
441                                         $this->abort();
442                                 }
443                         }
444                 }
445         }
447         /**
448          * Get an input dictionary from a set of parser test files
449          * @param array $filenames
450          * @return string
451          */
452         function getFuzzInput( $filenames ) {
453                 $dict = '';
455                 foreach ( $filenames as $filename ) {
456                         $contents = file_get_contents( $filename );
457                         preg_match_all(
458                                 '/!!\s*(input|wikitext)\n(.*?)\n!!\s*(result|html|html\/\*|html\/php)/s',
459                                 $contents,
460                                 $matches
461                         );
463                         foreach ( $matches[1] as $match ) {
464                                 $dict .= $match . "\n";
465                         }
466                 }
468                 return $dict;
469         }
471         /**
472          * Get a memory usage breakdown
473          * @return array
474          */
475         function getMemoryBreakdown() {
476                 $memStats = [];
478                 foreach ( $GLOBALS as $name => $value ) {
479                         $memStats['$' . $name] = strlen( serialize( $value ) );
480                 }
482                 $classes = get_declared_classes();
484                 foreach ( $classes as $class ) {
485                         $rc = new ReflectionClass( $class );
486                         $props = $rc->getStaticProperties();
487                         $memStats[$class] = strlen( serialize( $props ) );
488                         $methods = $rc->getMethods();
490                         foreach ( $methods as $method ) {
491                                 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
492                         }
493                 }
495                 $functions = get_defined_functions();
497                 foreach ( $functions['user'] as $function ) {
498                         $rf = new ReflectionFunction( $function );
499                         $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
500                 }
502                 asort( $memStats );
504                 return $memStats;
505         }
507         function abort() {
508                 $this->abort();
509         }
511         /**
512          * Run a series of tests listed in the given text files.
513          * Each test consists of a brief description, wikitext input,
514          * and the expected HTML output.
515          *
516          * Prints status updates on stdout and counts up the total
517          * number and percentage of passed tests.
518          *
519          * @param array $filenames Array of strings
520          * @return bool True if passed all tests, false if any tests failed.
521          */
522         public function runTestsFromFiles( $filenames ) {
523                 $ok = false;
525                 // be sure, ParserTest::addArticle has correct language set,
526                 // so that system messages gets into the right language cache
527                 $GLOBALS['wgLanguageCode'] = 'en';
528                 $GLOBALS['wgContLang'] = Language::factory( 'en' );
530                 $this->recorder->start();
531                 try {
532                         $this->setupDatabase();
533                         $ok = true;
535                         foreach ( $filenames as $filename ) {
536                                 echo "Running parser tests from: $filename\n";
537                                 $tests = new TestFileIterator( $filename, $this );
538                                 $ok = $this->runTests( $tests ) && $ok;
539                         }
541                         $this->teardownDatabase();
542                         $this->recorder->report();
543                 } catch ( DBError $e ) {
544                         echo $e->getMessage();
545                 }
546                 $this->recorder->end();
548                 return $ok;
549         }
551         function runTests( $tests ) {
552                 $ok = true;
554                 foreach ( $tests as $t ) {
555                         $result =
556                                 $this->runTest( $t['test'], $t['input'], $t['result'], $t['options'], $t['config'] );
557                         $ok = $ok && $result;
558                         $this->recorder->record( $t['test'], $t['subtest'], $result );
559                 }
561                 if ( $this->showProgress ) {
562                         print "\n";
563                 }
565                 return $ok;
566         }
568         /**
569          * Get a Parser object
570          *
571          * @param string $preprocessor
572          * @return Parser
573          */
574         function getParser( $preprocessor = null ) {
575                 global $wgParserConf;
577                 $class = $wgParserConf['class'];
578                 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] + $wgParserConf );
580                 foreach ( $this->hooks as $tag => $callback ) {
581                         $parser->setHook( $tag, $callback );
582                 }
584                 foreach ( $this->functionHooks as $tag => $bits ) {
585                         list( $callback, $flags ) = $bits;
586                         $parser->setFunctionHook( $tag, $callback, $flags );
587                 }
589                 foreach ( $this->transparentHooks as $tag => $callback ) {
590                         $parser->setTransparentTagHook( $tag, $callback );
591                 }
593                 Hooks::run( 'ParserTestParser', [ &$parser ] );
595                 return $parser;
596         }
598         /**
599          * Run a given wikitext input through a freshly-constructed wiki parser,
600          * and compare the output against the expected results.
601          * Prints status and explanatory messages to stdout.
602          *
603          * @param string $desc Test's description
604          * @param string $input Wikitext to try rendering
605          * @param string $result Result to output
606          * @param array $opts Test's options
607          * @param string $config Overrides for global variables, one per line
608          * @return bool
609          */
610         public function runTest( $desc, $input, $result, $opts, $config ) {
611                 if ( $this->showProgress ) {
612                         $this->showTesting( $desc );
613                 }
615                 $opts = $this->parseOptions( $opts );
616                 $context = $this->setupGlobals( $opts, $config );
618                 $user = $context->getUser();
619                 $options = ParserOptions::newFromContext( $context );
621                 if ( isset( $opts['djvu'] ) ) {
622                         if ( !$this->djVuSupport->isEnabled() ) {
623                                 return $this->showSkipped();
624                         }
625                 }
627                 if ( isset( $opts['tidy'] ) ) {
628                         if ( !$this->tidySupport->isEnabled() ) {
629                                 return $this->showSkipped();
630                         } else {
631                                 $options->setTidy( true );
632                         }
633                 }
635                 if ( isset( $opts['title'] ) ) {
636                         $titleText = $opts['title'];
637                 } else {
638                         $titleText = 'Parser test';
639                 }
641                 ObjectCache::getMainWANInstance()->clearProcessCache();
642                 $local = isset( $opts['local'] );
643                 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
644                 $parser = $this->getParser( $preprocessor );
645                 $title = Title::newFromText( $titleText );
647                 if ( isset( $opts['pst'] ) ) {
648                         $out = $parser->preSaveTransform( $input, $title, $user, $options );
649                 } elseif ( isset( $opts['msg'] ) ) {
650                         $out = $parser->transformMsg( $input, $options, $title );
651                 } elseif ( isset( $opts['section'] ) ) {
652                         $section = $opts['section'];
653                         $out = $parser->getSection( $input, $section );
654                 } elseif ( isset( $opts['replace'] ) ) {
655                         $section = $opts['replace'][0];
656                         $replace = $opts['replace'][1];
657                         $out = $parser->replaceSection( $input, $section, $replace );
658                 } elseif ( isset( $opts['comment'] ) ) {
659                         $out = Linker::formatComment( $input, $title, $local );
660                 } elseif ( isset( $opts['preload'] ) ) {
661                         $out = $parser->getPreloadText( $input, $title, $options );
662                 } else {
663                         $output = $parser->parse( $input, $title, $options, true, true, 1337 );
664                         $output->setTOCEnabled( !isset( $opts['notoc'] ) );
665                         $out = $output->getText();
666                         if ( isset( $opts['tidy'] ) ) {
667                                 $out = preg_replace( '/\s+$/', '', $out );
668                         }
670                         if ( isset( $opts['showtitle'] ) ) {
671                                 if ( $output->getTitleText() ) {
672                                         $title = $output->getTitleText();
673                                 }
675                                 $out = "$title\n$out";
676                         }
678                         if ( isset( $opts['showindicators'] ) ) {
679                                 $indicators = '';
680                                 foreach ( $output->getIndicators() as $id => $content ) {
681                                         $indicators .= "$id=$content\n";
682                                 }
683                                 $out = $indicators . $out;
684                         }
686                         if ( isset( $opts['ill'] ) ) {
687                                 $out = implode( ' ', $output->getLanguageLinks() );
688                         } elseif ( isset( $opts['cat'] ) ) {
689                                 $outputPage = $context->getOutput();
690                                 $outputPage->addCategoryLinks( $output->getCategories() );
691                                 $cats = $outputPage->getCategoryLinks();
693                                 if ( isset( $cats['normal'] ) ) {
694                                         $out = implode( ' ', $cats['normal'] );
695                                 } else {
696                                         $out = '';
697                                 }
698                         }
699                 }
701                 $this->teardownGlobals();
703                 $testResult = new ParserTestResult( $desc );
704                 $testResult->expected = $result;
705                 $testResult->actual = $out;
707                 return $this->showTestResult( $testResult );
708         }
710         /**
711          * Refactored in 1.22 to use ParserTestResult
712          * @param ParserTestResult $testResult
713          * @return bool
714          */
715         function showTestResult( ParserTestResult $testResult ) {
716                 if ( $testResult->isSuccess() ) {
717                         $this->showSuccess( $testResult );
718                         return true;
719                 } else {
720                         $this->showFailure( $testResult );
721                         return false;
722                 }
723         }
725         /**
726          * Use a regex to find out the value of an option
727          * @param string $key Name of option val to retrieve
728          * @param array $opts Options array to look in
729          * @param mixed $default Default value returned if not found
730          * @return mixed
731          */
732         private static function getOptionValue( $key, $opts, $default ) {
733                 $key = strtolower( $key );
735                 if ( isset( $opts[$key] ) ) {
736                         return $opts[$key];
737                 } else {
738                         return $default;
739                 }
740         }
742         private function parseOptions( $instring ) {
743                 $opts = [];
744                 // foo
745                 // foo=bar
746                 // foo="bar baz"
747                 // foo=[[bar baz]]
748                 // foo=bar,"baz quux"
749                 // foo={...json...}
750                 $defs = '(?(DEFINE)
751                         (?<qstr>                                        # Quoted string
752                                 "
753                                 (?:[^\\\\"] | \\\\.)*
754                                 "
755                         )
756                         (?<json>
757                                 \{              # Open bracket
758                                 (?:
759                                         [^"{}] |                                # Not a quoted string or object, or
760                                         (?&qstr) |                              # A quoted string, or
761                                         (?&json)                                # A json object (recursively)
762                                 )*
763                                 \}              # Close bracket
764                         )
765                         (?<value>
766                                 (?:
767                                         (?&qstr)                        # Quoted val
768                                 |
769                                         \[\[
770                                                 [^]]*                   # Link target
771                                         \]\]
772                                 |
773                                         [\w-]+                          # Plain word
774                                 |
775                                         (?&json)                        # JSON object
776                                 )
777                         )
778                 )';
779                 $regex = '/' . $defs . '\b
780                         (?<k>[\w-]+)                            # Key
781                         \b
782                         (?:\s*
783                                 =                                               # First sub-value
784                                 \s*
785                                 (?<v>
786                                         (?&value)
787                                         (?:\s*
788                                                 ,                               # Sub-vals 1..N
789                                                 \s*
790                                                 (?&value)
791                                         )*
792                                 )
793                         )?
794                         /x';
795                 $valueregex = '/' . $defs . '(?&value)/x';
797                 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
798                         foreach ( $matches as $bits ) {
799                                 $key = strtolower( $bits['k'] );
800                                 if ( !isset( $bits['v'] ) ) {
801                                         $opts[$key] = true;
802                                 } else {
803                                         preg_match_all( $valueregex, $bits['v'], $vmatches );
804                                         $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
805                                         if ( count( $opts[$key] ) == 1 ) {
806                                                 $opts[$key] = $opts[$key][0];
807                                         }
808                                 }
809                         }
810                 }
811                 return $opts;
812         }
814         private function cleanupOption( $opt ) {
815                 if ( substr( $opt, 0, 1 ) == '"' ) {
816                         return stripcslashes( substr( $opt, 1, -1 ) );
817                 }
819                 if ( substr( $opt, 0, 2 ) == '[[' ) {
820                         return substr( $opt, 2, -2 );
821                 }
823                 if ( substr( $opt, 0, 1 ) == '{' ) {
824                         return FormatJson::decode( $opt, true );
825                 }
826                 return $opt;
827         }
829         /**
830          * Set up the global variables for a consistent environment for each test.
831          * Ideally this should replace the global configuration entirely.
832          * @param string $opts
833          * @param string $config
834          * @return RequestContext
835          */
836         private function setupGlobals( $opts = '', $config = '' ) {
837                 global $IP;
839                 # Find out values for some special options.
840                 $lang =
841                         self::getOptionValue( 'language', $opts, 'en' );
842                 $variant =
843                         self::getOptionValue( 'variant', $opts, false );
844                 $maxtoclevel =
845                         self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
846                 $linkHolderBatchSize =
847                         self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
849                 $settings = [
850                         'wgServer' => 'http://example.org',
851                         'wgServerName' => 'example.org',
852                         'wgScript' => '/index.php',
853                         'wgScriptPath' => '',
854                         'wgArticlePath' => '/wiki/$1',
855                         'wgActionPaths' => [],
856                         'wgLockManagers' => [ [
857                                 'name' => 'fsLockManager',
858                                 'class' => 'FSLockManager',
859                                 'lockDirectory' => $this->uploadDir . '/lockdir',
860                         ], [
861                                 'name' => 'nullLockManager',
862                                 'class' => 'NullLockManager',
863                         ] ],
864                         'wgLocalFileRepo' => [
865                                 'class' => 'LocalRepo',
866                                 'name' => 'local',
867                                 'url' => 'http://example.com/images',
868                                 'hashLevels' => 2,
869                                 'transformVia404' => false,
870                                 'backend' => new FSFileBackend( [
871                                         'name' => 'local-backend',
872                                         'wikiId' => wfWikiID(),
873                                         'containerPaths' => [
874                                                 'local-public' => $this->uploadDir,
875                                                 'local-thumb' => $this->uploadDir . '/thumb',
876                                                 'local-temp' => $this->uploadDir . '/temp',
877                                                 'local-deleted' => $this->uploadDir . '/delete',
878                                         ]
879                                 ] )
880                         ],
881                         'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
882                         'wgUploadNavigationUrl' => false,
883                         'wgStylePath' => '/skins',
884                         'wgSitename' => 'MediaWiki',
885                         'wgLanguageCode' => $lang,
886                         'wgDBprefix' => $this->db->getType() != 'oracle' ? 'parsertest_' : 'pt_',
887                         'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
888                         'wgLang' => null,
889                         'wgContLang' => null,
890                         'wgNamespacesWithSubpages' => [ 0 => isset( $opts['subpage'] ) ],
891                         'wgMaxTocLevel' => $maxtoclevel,
892                         'wgCapitalLinks' => true,
893                         'wgNoFollowLinks' => true,
894                         'wgNoFollowDomainExceptions' => [ 'no-nofollow.org' ],
895                         'wgThumbnailScriptPath' => false,
896                         'wgUseImageResize' => true,
897                         'wgSVGConverter' => 'null',
898                         'wgSVGConverters' => [ 'null' => 'echo "1">$output' ],
899                         'wgLocaltimezone' => 'UTC',
900                         'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
901                         'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
902                         'wgDefaultLanguageVariant' => $variant,
903                         'wgVariantArticlePath' => false,
904                         'wgGroupPermissions' => [ '*' => [
905                                 'createaccount' => true,
906                                 'read' => true,
907                                 'edit' => true,
908                                 'createpage' => true,
909                                 'createtalk' => true,
910                         ] ],
911                         'wgNamespaceProtection' => [ NS_MEDIAWIKI => 'editinterface' ],
912                         'wgDefaultExternalStore' => [],
913                         'wgForeignFileRepos' => [],
914                         'wgLinkHolderBatchSize' => $linkHolderBatchSize,
915                         'wgExperimentalHtmlIds' => false,
916                         'wgExternalLinkTarget' => false,
917                         'wgHtml5' => true,
918                         'wgAdaptiveMessageCache' => true,
919                         'wgDisableLangConversion' => false,
920                         'wgDisableTitleConversion' => false,
921                         // Tidy options.
922                         'wgUseTidy' => isset( $opts['tidy'] ),
923                         'wgTidyConfig' => null,
924                         'wgDebugTidy' => false,
925                         'wgTidyConf' => $IP . '/includes/tidy/tidy.conf',
926                         'wgTidyOpts' => '',
927                         'wgTidyInternal' => $this->tidySupport->isInternal(),
928                 ];
930                 if ( $config ) {
931                         $configLines = explode( "\n", $config );
933                         foreach ( $configLines as $line ) {
934                                 list( $var, $value ) = explode( '=', $line, 2 );
936                                 $settings[$var] = eval( "return $value;" );
937                         }
938                 }
940                 $this->savedGlobals = [];
942                 /** @since 1.20 */
943                 Hooks::run( 'ParserTestGlobals', [ &$settings ] );
945                 foreach ( $settings as $var => $val ) {
946                         if ( array_key_exists( $var, $GLOBALS ) ) {
947                                 $this->savedGlobals[$var] = $GLOBALS[$var];
948                         }
950                         $GLOBALS[$var] = $val;
951                 }
953                 // Must be set before $context as user language defaults to $wgContLang
954                 $GLOBALS['wgContLang'] = Language::factory( $lang );
955                 $GLOBALS['wgMemc'] = new EmptyBagOStuff;
957                 RequestContext::resetMain();
958                 $context = RequestContext::getMain();
959                 $GLOBALS['wgLang'] = $context->getLanguage();
960                 $GLOBALS['wgOut'] = $context->getOutput();
961                 $GLOBALS['wgUser'] = $context->getUser();
963                 // We (re)set $wgThumbLimits to a single-element array above.
964                 $context->getUser()->setOption( 'thumbsize', 0 );
966                 global $wgHooks;
968                 $wgHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
969                 $wgHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
971                 MagicWord::clearCache();
972                 MWTidy::destroySingleton();
973                 RepoGroup::destroySingleton();
975                 self::resetTitleServices();
977                 return $context;
978         }
980         /**
981          * List of temporary tables to create, without prefix.
982          * Some of these probably aren't necessary.
983          * @return array
984          */
985         private function listTables() {
986                 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
987                         'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
988                         'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
989                         'site_stats', 'ipblocks', 'image', 'oldimage',
990                         'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
991                         'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
992                         'archive', 'user_groups', 'page_props', 'category'
993                 ];
995                 if ( in_array( $this->db->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
996                         array_push( $tables, 'searchindex' );
997                 }
999                 // Allow extensions to add to the list of tables to duplicate;
1000                 // may be necessary if they hook into page save or other code
1001                 // which will require them while running tests.
1002                 Hooks::run( 'ParserTestTables', [ &$tables ] );
1004                 return $tables;
1005         }
1007         /**
1008          * Set up a temporary set of wiki tables to work with for the tests.
1009          * Currently this will only be done once per run, and any changes to
1010          * the db will be visible to later tests in the run.
1011          */
1012         public function setupDatabase() {
1013                 global $wgDBprefix;
1015                 if ( $this->databaseSetupDone ) {
1016                         return;
1017                 }
1019                 $this->db = wfGetDB( DB_MASTER );
1020                 $dbType = $this->db->getType();
1022                 if ( $wgDBprefix === 'parsertest_' || ( $dbType == 'oracle' && $wgDBprefix === 'pt_' ) ) {
1023                         throw new MWException( 'setupDatabase should be called before setupGlobals' );
1024                 }
1026                 $this->databaseSetupDone = true;
1028                 # SqlBagOStuff broke when using temporary tables on r40209 (bug 15892).
1029                 # It seems to have been fixed since (r55079?), but regressed at some point before r85701.
1030                 # This works around it for now...
1031                 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
1033                 # CREATE TEMPORARY TABLE breaks if there is more than one server
1034                 if ( wfGetLB()->getServerCount() != 1 ) {
1035                         $this->useTemporaryTables = false;
1036                 }
1038                 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
1039                 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
1041                 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
1042                 $this->dbClone->useTemporaryTables( $temporary );
1043                 $this->dbClone->cloneTableStructure();
1045                 if ( $dbType == 'oracle' ) {
1046                         $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1047                         # Insert 0 user to prevent FK violations
1049                         # Anonymous user
1050                         $this->db->insert( 'user', [
1051                                 'user_id' => 0,
1052                                 'user_name' => 'Anonymous' ] );
1053                 }
1055                 # Update certain things in site_stats
1056                 $this->db->insert( 'site_stats',
1057                         [ 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ] );
1059                 # Reinitialise the LocalisationCache to match the database state
1060                 Language::getLocalisationCache()->unloadAll();
1062                 # Clear the message cache
1063                 MessageCache::singleton()->clear();
1065                 // Remember to update newParserTests.php after changing the below
1066                 // (and it uses a slightly different syntax just for teh lulz)
1067                 $this->setupUploadDir();
1068                 $user = User::createNew( 'WikiSysop' );
1069                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
1070                 # note that the size/width/height/bits/etc of the file
1071                 # are actually set by inspecting the file itself; the arguments
1072                 # to recordUpload2 have no effect.  That said, we try to make things
1073                 # match up so it is less confusing to readers of the code & tests.
1074                 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1075                         'size' => 7881,
1076                         'width' => 1941,
1077                         'height' => 220,
1078                         'bits' => 8,
1079                         'media_type' => MEDIATYPE_BITMAP,
1080                         'mime' => 'image/jpeg',
1081                         'metadata' => serialize( [] ),
1082                         'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
1083                         'fileExists' => true
1084                 ], $this->db->timestamp( '20010115123500' ), $user );
1086                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
1087                 # again, note that size/width/height below are ignored; see above.
1088                 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1089                         'size' => 22589,
1090                         'width' => 135,
1091                         'height' => 135,
1092                         'bits' => 8,
1093                         'media_type' => MEDIATYPE_BITMAP,
1094                         'mime' => 'image/png',
1095                         'metadata' => serialize( [] ),
1096                         'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
1097                         'fileExists' => true
1098                 ], $this->db->timestamp( '20130225203040' ), $user );
1100                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
1101                 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1102                                 'size'        => 12345,
1103                                 'width'       => 240,
1104                                 'height'      => 180,
1105                                 'bits'        => 0,
1106                                 'media_type'  => MEDIATYPE_DRAWING,
1107                                 'mime'        => 'image/svg+xml',
1108                                 'metadata'    => serialize( [] ),
1109                                 'sha1'        => Wikimedia\base_convert( '', 16, 36, 31 ),
1110                                 'fileExists'  => true
1111                 ], $this->db->timestamp( '20010115123500' ), $user );
1113                 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1114                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
1115                 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1116                         'size' => 12345,
1117                         'width' => 320,
1118                         'height' => 240,
1119                         'bits' => 24,
1120                         'media_type' => MEDIATYPE_BITMAP,
1121                         'mime' => 'image/jpeg',
1122                         'metadata' => serialize( [] ),
1123                         'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
1124                         'fileExists' => true
1125                 ], $this->db->timestamp( '20010115123500' ), $user );
1127                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
1128                 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1129                         'size' => 12345,
1130                         'width' => 320,
1131                         'height' => 240,
1132                         'bits' => 0,
1133                         'media_type' => MEDIATYPE_VIDEO,
1134                         'mime' => 'application/ogg',
1135                         'metadata' => serialize( [] ),
1136                         'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1137                         'fileExists' => true
1138                 ], $this->db->timestamp( '20010115123500' ), $user );
1140                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
1141                 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1142                         'size' => 12345,
1143                         'width' => 0,
1144                         'height' => 0,
1145                         'bits' => 0,
1146                         'media_type' => MEDIATYPE_AUDIO,
1147                         'mime' => 'application/ogg',
1148                         'metadata' => serialize( [] ),
1149                         'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1150                         'fileExists' => true
1151                 ], $this->db->timestamp( '20010115123500' ), $user );
1153                 # A DjVu file
1154                 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
1155                 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1156                         'size' => 3249,
1157                         'width' => 2480,
1158                         'height' => 3508,
1159                         'bits' => 0,
1160                         'media_type' => MEDIATYPE_BITMAP,
1161                         'mime' => 'image/vnd.djvu',
1162                         'metadata' => '<?xml version="1.0" ?>
1163 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1164 <DjVuXML>
1165 <HEAD></HEAD>
1166 <BODY><OBJECT height="3508" width="2480">
1167 <PARAM name="DPI" value="300" />
1168 <PARAM name="GAMMA" value="2.2" />
1169 </OBJECT>
1170 <OBJECT height="3508" width="2480">
1171 <PARAM name="DPI" value="300" />
1172 <PARAM name="GAMMA" value="2.2" />
1173 </OBJECT>
1174 <OBJECT height="3508" width="2480">
1175 <PARAM name="DPI" value="300" />
1176 <PARAM name="GAMMA" value="2.2" />
1177 </OBJECT>
1178 <OBJECT height="3508" width="2480">
1179 <PARAM name="DPI" value="300" />
1180 <PARAM name="GAMMA" value="2.2" />
1181 </OBJECT>
1182 <OBJECT height="3508" width="2480">
1183 <PARAM name="DPI" value="300" />
1184 <PARAM name="GAMMA" value="2.2" />
1185 </OBJECT>
1186 </BODY>
1187 </DjVuXML>',
1188                         'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1189                         'fileExists' => true
1190                 ], $this->db->timestamp( '20010115123600' ), $user );
1191         }
1193         public function teardownDatabase() {
1194                 if ( !$this->databaseSetupDone ) {
1195                         $this->teardownGlobals();
1196                         return;
1197                 }
1198                 $this->teardownUploadDir( $this->uploadDir );
1200                 $this->dbClone->destroy();
1201                 $this->databaseSetupDone = false;
1203                 if ( $this->useTemporaryTables ) {
1204                         if ( $this->db->getType() == 'sqlite' ) {
1205                                 # Under SQLite the searchindex table is virtual and need
1206                                 # to be explicitly destroyed. See bug 29912
1207                                 # See also MediaWikiTestCase::destroyDB()
1208                                 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1209                                 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
1210                         }
1211                         # Don't need to do anything
1212                         $this->teardownGlobals();
1213                         return;
1214                 }
1216                 $tables = $this->listTables();
1218                 foreach ( $tables as $table ) {
1219                         if ( $this->db->getType() == 'oracle' ) {
1220                                 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1221                         } else {
1222                                 $this->db->query( "DROP TABLE `parsertest_$table`" );
1223                         }
1224                 }
1226                 if ( $this->db->getType() == 'oracle' ) {
1227                         $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1228                 }
1230                 $this->teardownGlobals();
1231         }
1233         /**
1234          * Create a dummy uploads directory which will contain a couple
1235          * of files in order to pass existence tests.
1236          *
1237          * @return string The directory
1238          */
1239         private function setupUploadDir() {
1240                 global $IP;
1242                 $dir = $this->uploadDir;
1243                 if ( $this->keepUploads && is_dir( $dir ) ) {
1244                         return;
1245                 }
1247                 // wfDebug( "Creating upload directory $dir\n" );
1248                 if ( file_exists( $dir ) ) {
1249                         wfDebug( "Already exists!\n" );
1250                         return;
1251                 }
1253                 wfMkdirParents( $dir . '/3/3a', null, __METHOD__ );
1254                 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/3/3a/Foobar.jpg" );
1255                 wfMkdirParents( $dir . '/e/ea', null, __METHOD__ );
1256                 copy( "$IP/tests/phpunit/data/parser/wiki.png", "$dir/e/ea/Thumb.png" );
1257                 wfMkdirParents( $dir . '/0/09', null, __METHOD__ );
1258                 copy( "$IP/tests/phpunit/data/parser/headbg.jpg", "$dir/0/09/Bad.jpg" );
1259                 wfMkdirParents( $dir . '/f/ff', null, __METHOD__ );
1260                 file_put_contents( "$dir/f/ff/Foobar.svg",
1261                         '<?xml version="1.0" encoding="utf-8"?>' .
1262                         '<svg xmlns="http://www.w3.org/2000/svg"' .
1263                         ' version="1.1" width="240" height="180"/>' );
1264                 wfMkdirParents( $dir . '/5/5f', null, __METHOD__ );
1265                 copy( "$IP/tests/phpunit/data/parser/LoremIpsum.djvu", "$dir/5/5f/LoremIpsum.djvu" );
1266                 wfMkdirParents( $dir . '/0/00', null, __METHOD__ );
1267                 copy( "$IP/tests/phpunit/data/parser/320x240.ogv", "$dir/0/00/Video.ogv" );
1268                 wfMkdirParents( $dir . '/4/41', null, __METHOD__ );
1269                 copy( "$IP/tests/phpunit/data/media/say-test.ogg", "$dir/4/41/Audio.oga" );
1271                 return;
1272         }
1274         /**
1275          * Restore default values and perform any necessary clean-up
1276          * after each test runs.
1277          */
1278         private function teardownGlobals() {
1279                 RepoGroup::destroySingleton();
1280                 FileBackendGroup::destroySingleton();
1281                 LockManagerGroup::destroySingletons();
1282                 LinkCache::singleton()->clear();
1283                 MWTidy::destroySingleton();
1285                 foreach ( $this->savedGlobals as $var => $val ) {
1286                         $GLOBALS[$var] = $val;
1287                 }
1288         }
1290         /**
1291          * Remove the dummy uploads directory
1292          * @param string $dir
1293          */
1294         private function teardownUploadDir( $dir ) {
1295                 if ( $this->keepUploads ) {
1296                         return;
1297                 }
1299                 // delete the files first, then the dirs.
1300                 self::deleteFiles(
1301                         [
1302                                 "$dir/3/3a/Foobar.jpg",
1303                                 "$dir/thumb/3/3a/Foobar.jpg/*.jpg",
1304                                 "$dir/e/ea/Thumb.png",
1305                                 "$dir/0/09/Bad.jpg",
1306                                 "$dir/5/5f/LoremIpsum.djvu",
1307                                 "$dir/thumb/5/5f/LoremIpsum.djvu/*-LoremIpsum.djvu.jpg",
1308                                 "$dir/f/ff/Foobar.svg",
1309                                 "$dir/thumb/f/ff/Foobar.svg/*-Foobar.svg.png",
1310                                 "$dir/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
1311                                 "$dir/0/00/Video.ogv",
1312                                 "$dir/thumb/0/00/Video.ogv/120px--Video.ogv.jpg",
1313                                 "$dir/thumb/0/00/Video.ogv/180px--Video.ogv.jpg",
1314                                 "$dir/thumb/0/00/Video.ogv/240px--Video.ogv.jpg",
1315                                 "$dir/thumb/0/00/Video.ogv/320px--Video.ogv.jpg",
1316                                 "$dir/thumb/0/00/Video.ogv/270px--Video.ogv.jpg",
1317                                 "$dir/thumb/0/00/Video.ogv/320px-seek=2-Video.ogv.jpg",
1318                                 "$dir/thumb/0/00/Video.ogv/320px-seek=3.3666666666667-Video.ogv.jpg",
1319                                 "$dir/4/41/Audio.oga",
1320                         ]
1321                 );
1323                 self::deleteDirs(
1324                         [
1325                                 "$dir/3/3a",
1326                                 "$dir/3",
1327                                 "$dir/thumb/3/3a/Foobar.jpg",
1328                                 "$dir/thumb/3/3a",
1329                                 "$dir/thumb/3",
1330                                 "$dir/e/ea",
1331                                 "$dir/e",
1332                                 "$dir/f/ff/",
1333                                 "$dir/f/",
1334                                 "$dir/thumb/f/ff/Foobar.svg",
1335                                 "$dir/thumb/f/ff/",
1336                                 "$dir/thumb/f/",
1337                                 "$dir/0/00/",
1338                                 "$dir/0/09/",
1339                                 "$dir/0/",
1340                                 "$dir/5/5f",
1341                                 "$dir/5",
1342                                 "$dir/thumb/0/00/Video.ogv",
1343                                 "$dir/thumb/0/00",
1344                                 "$dir/thumb/0",
1345                                 "$dir/thumb/5/5f/LoremIpsum.djvu",
1346                                 "$dir/thumb/5/5f",
1347                                 "$dir/thumb/5",
1348                                 "$dir/thumb",
1349                                 "$dir/4/41",
1350                                 "$dir/4",
1351                                 "$dir/math/f/a/5",
1352                                 "$dir/math/f/a",
1353                                 "$dir/math/f",
1354                                 "$dir/math",
1355                                 "$dir/lockdir",
1356                                 "$dir",
1357                         ]
1358                 );
1359         }
1361         /**
1362          * Delete the specified files, if they exist.
1363          * @param array $files Full paths to files to delete.
1364          */
1365         private static function deleteFiles( $files ) {
1366                 foreach ( $files as $pattern ) {
1367                         foreach ( glob( $pattern ) as $file ) {
1368                                 if ( file_exists( $file ) ) {
1369                                         unlink( $file );
1370                                 }
1371                         }
1372                 }
1373         }
1375         /**
1376          * Delete the specified directories, if they exist. Must be empty.
1377          * @param array $dirs Full paths to directories to delete.
1378          */
1379         private static function deleteDirs( $dirs ) {
1380                 foreach ( $dirs as $dir ) {
1381                         if ( is_dir( $dir ) ) {
1382                                 rmdir( $dir );
1383                         }
1384                 }
1385         }
1387         /**
1388          * "Running test $desc..."
1389          * @param string $desc
1390          */
1391         protected function showTesting( $desc ) {
1392                 print "Running test $desc... ";
1393         }
1395         /**
1396          * Print a happy success message.
1397          *
1398          * Refactored in 1.22 to use ParserTestResult
1399          *
1400          * @param ParserTestResult $testResult
1401          * @return bool
1402          */
1403         protected function showSuccess( ParserTestResult $testResult ) {
1404                 if ( $this->showProgress ) {
1405                         print $this->term->color( '1;32' ) . 'PASSED' . $this->term->reset() . "\n";
1406                 }
1408                 return true;
1409         }
1411         /**
1412          * Print a failure message and provide some explanatory output
1413          * about what went wrong if so configured.
1414          *
1415          * Refactored in 1.22 to use ParserTestResult
1416          *
1417          * @param ParserTestResult $testResult
1418          * @return bool
1419          */
1420         protected function showFailure( ParserTestResult $testResult ) {
1421                 if ( $this->showFailure ) {
1422                         if ( !$this->showProgress ) {
1423                                 # In quiet mode we didn't show the 'Testing' message before the
1424                                 # test, in case it succeeded. Show it now:
1425                                 $this->showTesting( $testResult->description );
1426                         }
1428                         print $this->term->color( '31' ) . 'FAILED!' . $this->term->reset() . "\n";
1430                         if ( $this->showOutput ) {
1431                                 print "--- Expected ---\n{$testResult->expected}\n";
1432                                 print "--- Actual ---\n{$testResult->actual}\n";
1433                         }
1435                         if ( $this->showDiffs ) {
1436                                 print $this->quickDiff( $testResult->expected, $testResult->actual );
1437                                 if ( !$this->wellFormed( $testResult->actual ) ) {
1438                                         print "XML error: $this->mXmlError\n";
1439                                 }
1440                         }
1441                 }
1443                 return false;
1444         }
1446         /**
1447          * Print a skipped message.
1448          *
1449          * @return bool
1450          */
1451         protected function showSkipped() {
1452                 if ( $this->showProgress ) {
1453                         print $this->term->color( '1;33' ) . 'SKIPPED' . $this->term->reset() . "\n";
1454                 }
1456                 return true;
1457         }
1459         /**
1460          * Run given strings through a diff and return the (colorized) output.
1461          * Requires writable /tmp directory and a 'diff' command in the PATH.
1462          *
1463          * @param string $input
1464          * @param string $output
1465          * @param string $inFileTail Tailing for the input file name
1466          * @param string $outFileTail Tailing for the output file name
1467          * @return string
1468          */
1469         protected function quickDiff( $input, $output,
1470                 $inFileTail = 'expected', $outFileTail = 'actual'
1471         ) {
1472                 # Windows, or at least the fc utility, is retarded
1473                 $slash = wfIsWindows() ? '\\' : '/';
1474                 $prefix = wfTempDir() . "{$slash}mwParser-" . mt_rand();
1476                 $infile = "$prefix-$inFileTail";
1477                 $this->dumpToFile( $input, $infile );
1479                 $outfile = "$prefix-$outFileTail";
1480                 $this->dumpToFile( $output, $outfile );
1482                 $shellInfile = wfEscapeShellArg( $infile );
1483                 $shellOutfile = wfEscapeShellArg( $outfile );
1485                 global $wgDiff3;
1486                 // we assume that people with diff3 also have usual diff
1487                 $shellCommand = ( wfIsWindows() && !$wgDiff3 ) ? 'fc' : 'diff -au';
1489                 $diff = wfShellExec( "$shellCommand $shellInfile $shellOutfile" );
1491                 unlink( $infile );
1492                 unlink( $outfile );
1494                 return $this->colorDiff( $diff );
1495         }
1497         /**
1498          * Write the given string to a file, adding a final newline.
1499          *
1500          * @param string $data
1501          * @param string $filename
1502          */
1503         private function dumpToFile( $data, $filename ) {
1504                 $file = fopen( $filename, "wt" );
1505                 fwrite( $file, $data . "\n" );
1506                 fclose( $file );
1507         }
1509         /**
1510          * Colorize unified diff output if set for ANSI color output.
1511          * Subtractions are colored blue, additions red.
1512          *
1513          * @param string $text
1514          * @return string
1515          */
1516         protected function colorDiff( $text ) {
1517                 return preg_replace(
1518                         [ '/^(-.*)$/m', '/^(\+.*)$/m' ],
1519                         [ $this->term->color( 34 ) . '$1' . $this->term->reset(),
1520                                 $this->term->color( 31 ) . '$1' . $this->term->reset() ],
1521                         $text );
1522         }
1524         /**
1525          * Show "Reading tests from ..."
1526          *
1527          * @param string $path
1528          */
1529         public function showRunFile( $path ) {
1530                 print $this->term->color( 1 ) .
1531                         "Reading tests from \"$path\"..." .
1532                         $this->term->reset() .
1533                         "\n";
1534         }
1536         /**
1537          * Insert a temporary test article
1538          * @param string $name The title, including any prefix
1539          * @param string $text The article text
1540          * @param int|string $line The input line number, for reporting errors
1541          * @param bool|string $ignoreDuplicate Whether to silently ignore duplicate pages
1542          * @throws Exception
1543          * @throws MWException
1544          */
1545         public static function addArticle( $name, $text, $line = 'unknown', $ignoreDuplicate = '' ) {
1546                 global $wgCapitalLinks;
1548                 $oldCapitalLinks = $wgCapitalLinks;
1549                 $wgCapitalLinks = true; // We only need this from SetupGlobals() See r70917#c8637
1551                 $text = self::chomp( $text );
1552                 $name = self::chomp( $name );
1554                 $title = Title::newFromText( $name );
1556                 if ( is_null( $title ) ) {
1557                         throw new MWException( "invalid title '$name' at line $line\n" );
1558                 }
1560                 $page = WikiPage::factory( $title );
1561                 $page->loadPageData( 'fromdbmaster' );
1563                 if ( $page->exists() ) {
1564                         if ( $ignoreDuplicate == 'ignoreduplicate' ) {
1565                                 return;
1566                         } else {
1567                                 throw new MWException( "duplicate article '$name' at line $line\n" );
1568                         }
1569                 }
1571                 $page->doEditContent( ContentHandler::makeContent( $text, $title ), '', EDIT_NEW );
1573                 $wgCapitalLinks = $oldCapitalLinks;
1574         }
1576         /**
1577          * Steal a callback function from the primary parser, save it for
1578          * application to our scary parser. If the hook is not installed,
1579          * abort processing of this file.
1580          *
1581          * @param string $name
1582          * @return bool True if tag hook is present
1583          */
1584         public function requireHook( $name ) {
1585                 global $wgParser;
1587                 $wgParser->firstCallInit(); // make sure hooks are loaded.
1589                 if ( isset( $wgParser->mTagHooks[$name] ) ) {
1590                         $this->hooks[$name] = $wgParser->mTagHooks[$name];
1591                 } else {
1592                         echo "   This test suite requires the '$name' hook extension, skipping.\n";
1593                         return false;
1594                 }
1596                 return true;
1597         }
1599         /**
1600          * Steal a callback function from the primary parser, save it for
1601          * application to our scary parser. If the hook is not installed,
1602          * abort processing of this file.
1603          *
1604          * @param string $name
1605          * @return bool True if function hook is present
1606          */
1607         public function requireFunctionHook( $name ) {
1608                 global $wgParser;
1610                 $wgParser->firstCallInit(); // make sure hooks are loaded.
1612                 if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
1613                         $this->functionHooks[$name] = $wgParser->mFunctionHooks[$name];
1614                 } else {
1615                         echo "   This test suite requires the '$name' function hook extension, skipping.\n";
1616                         return false;
1617                 }
1619                 return true;
1620         }
1622         /**
1623          * Steal a callback function from the primary parser, save it for
1624          * application to our scary parser. If the hook is not installed,
1625          * abort processing of this file.
1626          *
1627          * @param string $name
1628          * @return bool True if function hook is present
1629          */
1630         public function requireTransparentHook( $name ) {
1631                 global $wgParser;
1633                 $wgParser->firstCallInit(); // make sure hooks are loaded.
1635                 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
1636                         $this->transparentHooks[$name] = $wgParser->mTransparentTagHooks[$name];
1637                 } else {
1638                         echo "   This test suite requires the '$name' transparent hook extension, skipping.\n";
1639                         return false;
1640                 }
1642                 return true;
1643         }
1645         private function wellFormed( $text ) {
1646                 $html =
1647                         Sanitizer::hackDocType() .
1648                                 '<html>' .
1649                                 $text .
1650                                 '</html>';
1652                 $parser = xml_parser_create( "UTF-8" );
1654                 # case folding violates XML standard, turn it off
1655                 xml_parser_set_option( $parser, XML_OPTION_CASE_FOLDING, false );
1657                 if ( !xml_parse( $parser, $html, true ) ) {
1658                         $err = xml_error_string( xml_get_error_code( $parser ) );
1659                         $position = xml_get_current_byte_index( $parser );
1660                         $fragment = $this->extractFragment( $html, $position );
1661                         $this->mXmlError = "$err at byte $position:\n$fragment";
1662                         xml_parser_free( $parser );
1664                         return false;
1665                 }
1667                 xml_parser_free( $parser );
1669                 return true;
1670         }
1672         private function extractFragment( $text, $position ) {
1673                 $start = max( 0, $position - 10 );
1674                 $before = $position - $start;
1675                 $fragment = '...' .
1676                         $this->term->color( 34 ) .
1677                         substr( $text, $start, $before ) .
1678                         $this->term->color( 0 ) .
1679                         $this->term->color( 31 ) .
1680                         $this->term->color( 1 ) .
1681                         substr( $text, $position, 1 ) .
1682                         $this->term->color( 0 ) .
1683                         $this->term->color( 34 ) .
1684                         substr( $text, $position + 1, 9 ) .
1685                         $this->term->color( 0 ) .
1686                         '...';
1687                 $display = str_replace( "\n", ' ', $fragment );
1688                 $caret = '   ' .
1689                         str_repeat( ' ', $before ) .
1690                         $this->term->color( 31 ) .
1691                         '^' .
1692                         $this->term->color( 0 );
1694                 return "$display\n$caret";
1695         }
1697         static function getFakeTimestamp( &$parser, &$ts ) {
1698                 $ts = 123; // parsed as '1970-01-01T00:02:03Z'
1699                 return true;
1700         }