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