13 static protected $articles =
array();
17 static protected $backendToUse;
19 public $keepUploads =
false;
20 public $runDisabled =
false;
21 public $runParsoid =
false;
23 public $showProgress =
true;
24 public $savedWeirdGlobals =
array();
25 public $savedGlobals =
array();
26 public $hooks =
array();
27 public $functionHooks =
array();
30 public $maxFuzzTestLength = 300;
32 public $memoryLimit = 50;
34 protected $file =
false;
36 public static function setUpBeforeClass() {
38 ParserTest::setupInterwikis();
41 protected function setUp() {
49 $this->regex = $this->
getCliArg(
'regex=' );
55 $this->keepUploads = $this->
getCliArg(
'keep-uploads' );
57 $tmpGlobals =
array();
59 $tmpGlobals[
'wgLanguageCode'] =
'en';
61 $tmpGlobals[
'wgSitename'] =
'MediaWiki';
62 $tmpGlobals[
'wgServer'] =
'http://example.org';
63 $tmpGlobals[
'wgScript'] =
'/index.php';
64 $tmpGlobals[
'wgScriptPath'] =
'/';
65 $tmpGlobals[
'wgArticlePath'] =
'/wiki/$1';
66 $tmpGlobals[
'wgActionPaths'] =
array();
67 $tmpGlobals[
'wgVariantArticlePath'] =
false;
68 $tmpGlobals[
'wgExtensionAssetsPath'] =
'/extensions';
69 $tmpGlobals[
'wgStylePath'] =
'/skins';
70 $tmpGlobals[
'wgEnableUploads'] =
true;
71 $tmpGlobals[
'wgThumbnailScriptPath'] =
false;
72 $tmpGlobals[
'wgLocalFileRepo'] =
array(
73 'class' =>
'LocalRepo',
75 'url' =>
'http://example.com/images',
77 'transformVia404' =>
false,
78 'backend' =>
'local-backend'
80 $tmpGlobals[
'wgForeignFileRepos'] =
array();
81 $tmpGlobals[
'wgDefaultExternalStore'] =
array();
82 $tmpGlobals[
'wgEnableParserCache'] =
false;
83 $tmpGlobals[
'wgCapitalLinks'] =
true;
84 $tmpGlobals[
'wgNoFollowLinks'] =
true;
85 $tmpGlobals[
'wgNoFollowDomainExceptions'] =
array();
86 $tmpGlobals[
'wgExternalLinkTarget'] =
false;
87 $tmpGlobals[
'wgThumbnailScriptPath'] =
false;
88 $tmpGlobals[
'wgUseImageResize'] =
true;
89 $tmpGlobals[
'wgAllowExternalImages'] =
true;
90 $tmpGlobals[
'wgRawHtml'] =
false;
91 $tmpGlobals[
'wgUseTidy'] =
false;
92 $tmpGlobals[
'wgAlwaysUseTidy'] =
false;
93 $tmpGlobals[
'wgAllowMicrodataAttributes'] =
true;
94 $tmpGlobals[
'wgExperimentalHtmlIds'] =
false;
95 $tmpGlobals[
'wgAdaptiveMessageCache'] =
true;
96 $tmpGlobals[
'wgUseDatabaseMessages'] =
true;
97 $tmpGlobals[
'wgLocaltimezone'] =
'UTC';
98 $tmpGlobals[
'wgDeferredUpdateList'] =
array();
99 $tmpGlobals[
'wgGroupPermissions'] =
array(
101 'createaccount' =>
true,
104 'createpage' =>
true,
105 'createtalk' =>
true,
110 'wgParser',
$GLOBALS[
'wgParserConf'][
'class'],
113 $tmpGlobals[
'wgFileExtensions'][] =
'svg';
114 $tmpGlobals[
'wgSVGConverter'] =
'rsvg';
115 $tmpGlobals[
'wgSVGConverters'][
'rsvg'] =
116 '$path/rsvg-convert -w $width -h $height $input -o $output';
118 if (
$GLOBALS[
'wgStyleDirectory'] ===
false ) {
119 $tmpGlobals[
'wgStyleDirectory'] =
"$IP/skins";
122 # Replace all media handlers with a mock. We do not need to generate
123 # actual thumbnails to do parser testing, we only care about receiving
124 # a ThumbnailImage properly initialized.
126 foreach ( $wgMediaHandlers
as $type => $handler ) {
127 $tmpGlobals[
'wgMediaHandlers'][
$type] =
'MockBitmapHandler';
130 $tmpGlobals[
'wgMediaHandlers'][
'image/svg+xml'] =
'MockSvgHandler';
133 $tmpHooks[
'ParserTestParser'][] =
'ParserTestParserHook::setup';
134 $tmpHooks[
'ParserGetVariableValueTs'][] =
'ParserTest::getFakeTimestamp';
135 $tmpGlobals[
'wgHooks'] = $tmpHooks;
136 # add a namespace shadowing a interwiki link, to test
137 # proper precedence when resolving links. (bug 51680)
138 $tmpGlobals[
'wgExtraNamespaces'] =
array( 100 =>
'MemoryAlpha' );
174 public static
function tearDownAfterClass() {
175 ParserTest::tearDownInterwikis();
176 parent::tearDownAfterClass();
179 function addDBData() {
180 $this->tablesUsed[] =
'site_stats';
181 # disabled for performance
182 #$this->tablesUsed[] = 'image';
184 # Update certain things in site_stats
185 $this->db->insert(
'site_stats',
186 array(
'ss_row_id' => 1,
'ss_images' => 2,
'ss_good_articles' => 1 ),
193 # Upload DB table entries for files.
194 # We will upload the actual files later. Note that if anything causes LocalFile::load()
195 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
196 # member to false and storing it in cache.
197 # note that the size/width/height/bits/etc of the file
198 # are actually set by inspecting the file itself; the arguments
199 # to recordUpload2 have no effect. That said, we try to make things
200 # match up so it is less confusing to readers of the code & tests.
202 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
203 $image->recordUpload2(
205 'Upload of some lame file',
213 'mime' =>
'image/jpeg',
214 'metadata' => serialize(
array() ),
216 'fileExists' =>
true ),
217 $this->db->timestamp(
'20010115123500' ),
$user
222 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
223 $image->recordUpload2(
225 'Upload of some lame thumbnail',
226 'Some lame thumbnail',
233 'mime' =>
'image/png',
234 'metadata' => serialize(
array() ),
236 'fileExists' =>
true ),
237 $this->db->timestamp(
'20130225203040' ),
$user
241 # This image will be blacklisted in [[MediaWiki:Bad image list]]
243 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
244 $image->recordUpload2(
254 'mime' =>
'image/jpeg',
255 'metadata' => serialize(
array() ),
257 'fileExists' =>
true ),
258 $this->db->timestamp(
'20010115123500' ),
$user
262 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
263 $image->recordUpload2(
'',
'Upload of some lame SVG',
'Some lame SVG',
array(
269 'mime' =>
'image/svg+xml',
270 'metadata' => serialize(
array() ),
273 ), $this->db->timestamp(
'20010115123500' ),
$user );
283 protected function setupGlobals( $opts =
array(), $config =
'' ) {
285 # Find out values for some special options.
287 self::getOptionValue(
'language', $opts,
'en' );
289 self::getOptionValue(
'variant', $opts,
false );
291 self::getOptionValue(
'wgMaxTocLevel', $opts, 999 );
292 $linkHolderBatchSize =
293 self::getOptionValue(
'wgLinkHolderBatchSize', $opts, 1000 );
295 $uploadDir = $this->getUploadDir();
296 if ( $this->getCliArg(
'use-filebackend=' ) ) {
297 if ( self::$backendToUse ) {
298 $backend = self::$backendToUse;
300 $name = $this->getCliArg(
'use-filebackend=' );
301 $useConfig =
array();
302 foreach ( $wgFileBackends
as $conf ) {
303 if ( $conf[
'name'] ==
$name ) {
307 $useConfig[
'name'] =
'local-backend';
308 unset( $useConfig[
'lockManager'] );
309 unset( $useConfig[
'fileJournal'] );
310 $class = $useConfig[
'class'];
311 self::$backendToUse =
new $class( $useConfig );
312 $backend = self::$backendToUse;
315 # Replace with a mock. We do not care about generating real
316 # files on the filesystem, just need to expose the file
319 'name' =>
'local-backend',
320 'wikiId' => wfWikiId()
325 'wgLocalFileRepo' =>
array(
326 'class' =>
'LocalRepo',
328 'url' =>
'http://example.com/images',
330 'transformVia404' =>
false,
331 'backend' => $backend
333 'wgEnableUploads' => self::getOptionValue(
'wgEnableUploads', $opts,
true ),
334 'wgLanguageCode' => $lang,
335 'wgDBprefix' => $this->db->getType() !=
'oracle' ?
'unittest_' :
'ut_',
336 'wgRawHtml' => self::getOptionValue(
'wgRawHtml', $opts,
false ),
337 'wgNamespacesWithSubpages' =>
array(
NS_MAIN => isset( $opts[
'subpage'] ) ),
338 'wgAllowExternalImages' => self::getOptionValue(
'wgAllowExternalImages', $opts,
true ),
339 'wgThumbLimits' =>
array( self::getOptionValue(
'thumbsize', $opts, 180 ) ),
340 'wgMaxTocLevel' => $maxtoclevel,
341 'wgUseTeX' => isset( $opts[
'math'] ) || isset( $opts[
'texvc'] ),
342 'wgMathDirectory' => $uploadDir .
'/math',
343 'wgDefaultLanguageVariant' => $variant,
344 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
348 $configLines = explode(
"\n", $config );
350 foreach ( $configLines
as $line ) {
353 $settings[$var] = eval(
"return $value;" );
357 $this->savedGlobals =
array();
363 $settings[
'wgContLang'] = $langObj;
364 $settings[
'wgLang'] = $langObj;
367 $settings[
'wgOut'] = $context->getOutput();
368 $settings[
'wgUser'] = $context->getUser();
369 $settings[
'wgRequest'] = $context->getRequest();
372 $context->getUser()->setOption(
'thumbsize', 0 );
374 foreach ( $settings
as $var => $val ) {
375 if ( array_key_exists( $var,
$GLOBALS ) ) {
376 $this->savedGlobals[$var] =
$GLOBALS[$var];
384 # The entries saved into RepoGroup cache with previous globals will be wrong.
388 # Create dummy files in storage
389 $this->setupUploads();
391 # Publish the articles after we have the final language set
392 $this->publishTestArticles();
404 protected function getUploadDir() {
405 if ( $this->keepUploads ) {
408 if ( is_dir(
$dir ) ) {
416 if ( file_exists(
$dir ) ) {
417 wfDebug(
"Already exists!\n" );
431 protected function setupUploads() {
434 $base = $this->getBaseDir();
436 $backend->prepare(
array(
'dir' =>
"$base/local-public/3/3a" ) );
437 $backend->store(
array(
438 'src' =>
"$IP/skins/monobook/headbg.jpg",
'dst' =>
"$base/local-public/3/3a/Foobar.jpg"
440 $backend->prepare(
array(
'dir' =>
"$base/local-public/e/ea" ) );
441 $backend->store(
array(
442 'src' =>
"$IP/skins/monobook/wiki.png",
'dst' =>
"$base/local-public/e/ea/Thumb.png"
444 $backend->prepare(
array(
'dir' =>
"$base/local-public/0/09" ) );
445 $backend->store(
array(
446 'src' =>
"$IP/skins/monobook/headbg.jpg",
'dst' =>
"$base/local-public/0/09/Bad.jpg"
450 $data =
'<?xml version="1.0" encoding="utf-8"?>' .
451 '<svg xmlns="http://www.w3.org/2000/svg"' .
452 ' version="1.1" width="240" height="180"/>';
454 $backend->prepare(
array(
'dir' =>
"$base/local-public/f/ff" ) );
455 $backend->quickCreate(
array(
456 'content' => $data,
'dst' =>
"$base/local-public/f/ff/Foobar.svg"
464 protected function teardownGlobals() {
465 $this->teardownUploads();
467 foreach ( $this->savedGlobals
as $var => $val ) {
475 private function teardownUploads() {
476 if ( $this->keepUploads ) {
482 # In memory backend, so dont bother cleaning them up.
486 $base = $this->getBaseDir();
490 "$base/local-public/3/3a/Foobar.jpg",
491 "$base/local-thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
492 "$base/local-thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
493 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
494 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
495 "$base/local-thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
496 "$base/local-thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
497 "$base/local-thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
498 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
499 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
500 "$base/local-thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
501 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
502 "$base/local-thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
503 "$base/local-thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
504 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
505 "$base/local-thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
506 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
507 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
508 "$base/local-thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
509 "$base/local-thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
510 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
511 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
512 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
513 "$base/local-thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
514 "$base/local-thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
515 "$base/local-thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
516 "$base/local-thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
517 "$base/local-thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
518 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
519 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
520 "$base/local-thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
521 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
523 "$base/local-public/e/ea/Thumb.png",
525 "$base/local-public/0/09/Bad.jpg",
527 "$base/local-public/f/ff/Foobar.svg",
528 "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
529 "$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
530 "$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
531 "$base/local-thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
532 "$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
533 "$base/local-thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
534 "$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
535 "$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
536 "$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
538 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
547 private static function deleteFiles(
$files ) {
555 if ( !$backend->clean(
array(
'dir' => $tmp ) )->isOK() ) {
562 protected function getBaseDir() {
563 return 'mwstore://local-backend';
566 public function parserTestProvider() {
567 if ( $this->
file ===
false ) {
568 global $wgParserTestFiles;
569 $this->
file = $wgParserTestFiles[0];
578 public function setParserTestFile( $filename ) {
579 $this->
file = $filename;
587 public function testParserTest( $desc, $input,
$result, $opts, $config ) {
588 if ( $this->regex !=
'' && !preg_match(
'/' . $this->regex .
'/', $desc ) ) {
589 $this->assertTrue(
true );
594 if ( !$this->isWikitextNS(
NS_MAIN ) ) {
598 $this->markTestSkipped(
"Main namespace does not support wikitext,"
599 .
"skipping parser test: $desc" );
602 wfDebug(
"Running parser test: $desc\n" );
604 $opts = $this->parseOptions( $opts );
605 $context = $this->setupGlobals( $opts, $config );
607 $user = $context->getUser();
610 if ( isset( $opts[
'title'] ) ) {
611 $titleText = $opts[
'title'];
613 $titleText =
'Parser test';
616 $local = isset( $opts[
'local'] );
617 $preprocessor = isset( $opts[
'preprocessor'] ) ? $opts[
'preprocessor'] :
null;
618 $parser = $this->getParser( $preprocessor );
622 # Parser test requiring math. Make sure texvc is executable
623 # or just skip such tests.
624 if ( isset( $opts[
'math'] ) || isset( $opts[
'texvc'] ) ) {
627 if ( !isset( $wgTexvc ) ) {
628 $this->markTestSkipped(
"SKIPPED: \$wgTexvc is not set" );
629 } elseif ( !is_executable( $wgTexvc ) ) {
630 $this->markTestSkipped(
"SKIPPED: texvc binary does not exist"
631 .
" or is not executable.\n"
632 .
"Current configuration is:\n\$wgTexvc = '$wgTexvc'" );
636 if ( isset( $opts[
'pst'] ) ) {
638 } elseif ( isset( $opts[
'msg'] ) ) {
640 } elseif ( isset( $opts[
'section'] ) ) {
643 } elseif ( isset( $opts[
'replace'] ) ) {
645 $replace = $opts[
'replace'][1];
647 } elseif ( isset( $opts[
'comment'] ) ) {
649 } elseif ( isset( $opts[
'preload'] ) ) {
653 $output->setTOCEnabled( !isset( $opts[
'notoc'] ) );
656 if ( isset( $opts[
'showtitle'] ) ) {
657 if (
$output->getTitleText() ) {
661 $out =
"$title\n$out";
664 if ( isset( $opts[
'ill'] ) ) {
665 $out = $this->tidy( implode(
' ',
$output->getLanguageLinks() ) );
666 } elseif ( isset( $opts[
'cat'] ) ) {
667 $outputPage = $context->getOutput();
668 $outputPage->addCategoryLinks(
$output->getCategories() );
669 $cats = $outputPage->getCategoryLinks();
671 if ( isset( $cats[
'normal'] ) ) {
672 $out = $this->tidy( implode(
' ', $cats[
'normal'] ) );
682 $this->teardownGlobals();
695 public function testFuzzTests() {
696 global $wgParserTestFiles;
698 $files = $wgParserTestFiles;
700 if ( $this->getCliArg(
'file=' ) ) {
704 $dict = $this->getFuzzInput(
$files );
705 $dictSize = strlen( $dict );
706 $logMaxLength = log( $this->maxFuzzTestLength );
708 ini_set(
'memory_limit', $this->memoryLimit * 1048576 );
719 mt_srand( ++$this->fuzzSeed );
720 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
723 while ( strlen( $input ) < $totalLength ) {
724 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
725 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
726 $offset = mt_rand( 0, $dictSize - $hairLength );
727 $input .= substr( $dict, $offset, $hairLength );
730 $this->setupGlobals();
736 $this->assertTrue(
true,
"Test $id, fuzz seed {$this->fuzzSeed}" );
737 }
catch ( Exception $exception ) {
738 $input_dump = sprintf(
"string(%d) \"%s\"\n", strlen( $input ), $input );
740 $this->assertTrue(
false,
"Test $id, fuzz seed {$this->fuzzSeed}. \n\n" .
741 "Input: $input_dump\n\nError: {$exception->getMessage()}\n\n" .
742 "Backtrace: {$exception->getTraceAsString()}" );
745 $this->teardownGlobals();
748 if ( $id % 100 == 0 ) {
749 $usage = intval( memory_get_usage(
true ) / $this->memoryLimit / 1048576 * 100 );
752 $ret =
"Out of memory:\n";
753 $memStats = $this->getMemoryBreakdown();
755 foreach ( $memStats
as $name => $usage ) {
756 $ret .=
"$name: $usage\n";
772 function getFuzzInput( $filenames ) {
775 foreach ( $filenames
as $filename ) {
776 $contents = file_get_contents( $filename );
777 preg_match_all(
'/!!\s*input\n(.*?)\n!!\s*result/s', $contents,
$matches );
780 $dict .= $match .
"\n";
790 function getMemoryBreakdown() {
794 $memStats[
'$' .
$name] = strlen( serialize(
$value ) );
797 $classes = get_declared_classes();
799 foreach ( $classes
as $class ) {
800 $rc =
new ReflectionClass( $class );
801 $props = $rc->getStaticProperties();
802 $memStats[$class] = strlen( serialize( $props ) );
803 $methods = $rc->getMethods();
805 foreach ( $methods
as $method ) {
806 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
810 $functions = get_defined_functions();
812 foreach ( $functions[
'user']
as $function ) {
813 $rf =
new ReflectionFunction( $function );
814 $memStats[
"$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
825 function getParser( $preprocessor =
null ) {
828 $class = $wgParserConf[
'class'];
829 $parser =
new $class(
array(
'preprocessorClass' => $preprocessor ) + $wgParserConf );
838 public function addArticle(
$name, $text,
$line ) {
842 public function publishTestArticles() {
843 if ( empty( self::$articles ) ) {
847 foreach ( self::$articles
as $name => $info ) {
849 ParserTest::addArticle(
$name, $text,
$line,
'ignoreduplicate' );
861 public function requireHook(
$name ) {
867 public function requireFunctionHook(
$name ) {
882 protected function tidy( $text ) {
895 public function removeEndingNewline(
$s ) {
896 if ( substr(
$s, -1 ) ===
"\n" ) {
897 return substr(
$s, 0, -1 );
905 protected function parseOptions( $instring ) {
935 \[\[[^]]*\]\] # Link target
943 if ( preg_match_all( $regex, $instring,
$matches, PREG_SET_ORDER ) ) {
945 array_shift( $bits );
946 $key = strtolower( array_shift( $bits ) );
947 if ( count( $bits ) == 0 ) {
949 } elseif ( count( $bits ) == 1 ) {
950 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
953 $opts[$key] = array_map(
array( $this,
'cleanupOption' ), $bits );
961 protected function cleanupOption( $opt ) {
962 if ( substr( $opt, 0, 1 ) ==
'"' ) {
963 return substr( $opt, 1, -1 );
966 if ( substr( $opt, 0, 2 ) ==
'[[' ) {
967 return substr( $opt, 2, -2 );
979 protected static function getOptionValue( $key, $opts, $default ) {
980 $key = strtolower( $key );
982 if ( isset( $opts[$key] ) ) {