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[
'wgWellFormedXml'] =
true;
94 $tmpGlobals[
'wgAllowMicrodataAttributes'] =
true;
95 $tmpGlobals[
'wgExperimentalHtmlIds'] =
false;
96 $tmpGlobals[
'wgAdaptiveMessageCache'] =
true;
97 $tmpGlobals[
'wgUseDatabaseMessages'] =
true;
98 $tmpGlobals[
'wgLocaltimezone'] =
'UTC';
99 $tmpGlobals[
'wgDeferredUpdateList'] =
array();
100 $tmpGlobals[
'wgGroupPermissions'] =
array(
102 'createaccount' =>
true,
105 'createpage' =>
true,
106 'createtalk' =>
true,
111 'wgParser',
$GLOBALS[
'wgParserConf'][
'class'],
114 $tmpGlobals[
'wgFileExtensions'][] =
'svg';
115 $tmpGlobals[
'wgSVGConverter'] =
'rsvg';
116 $tmpGlobals[
'wgSVGConverters'][
'rsvg'] =
117 '$path/rsvg-convert -w $width -h $height $input -o $output';
119 if (
$GLOBALS[
'wgStyleDirectory'] ===
false ) {
120 $tmpGlobals[
'wgStyleDirectory'] =
"$IP/skins";
123 # Replace all media handlers with a mock. We do not need to generate
124 # actual thumbnails to do parser testing, we only care about receiving
125 # a ThumbnailImage properly initialized.
127 foreach ( $wgMediaHandlers
as $type => $handler ) {
128 $tmpGlobals[
'wgMediaHandlers'][
$type] =
'MockBitmapHandler';
131 $tmpGlobals[
'wgMediaHandlers'][
'image/svg+xml'] =
'MockSvgHandler';
134 $tmpHooks[
'ParserTestParser'][] =
'ParserTestParserHook::setup';
135 $tmpHooks[
'ParserGetVariableValueTs'][] =
'ParserTest::getFakeTimestamp';
136 $tmpGlobals[
'wgHooks'] = $tmpHooks;
137 # add a namespace shadowing a interwiki link, to test
138 # proper precedence when resolving links. (bug 51680)
139 $tmpGlobals[
'wgExtraNamespaces'] =
array( 100 =>
'MemoryAlpha' );
175 public static
function tearDownAfterClass() {
176 ParserTest::tearDownInterwikis();
177 parent::tearDownAfterClass();
180 function addDBData() {
181 $this->tablesUsed[] =
'site_stats';
182 # disabled for performance
183 #$this->tablesUsed[] = 'image';
185 # Update certain things in site_stats
186 $this->db->insert(
'site_stats',
187 array(
'ss_row_id' => 1,
'ss_images' => 2,
'ss_good_articles' => 1 ),
194 # Upload DB table entries for files.
195 # We will upload the actual files later. Note that if anything causes LocalFile::load()
196 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
197 # member to false and storing it in cache.
198 # note that the size/width/height/bits/etc of the file
199 # are actually set by inspecting the file itself; the arguments
200 # to recordUpload2 have no effect. That said, we try to make things
201 # match up so it is less confusing to readers of the code & tests.
203 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
204 $image->recordUpload2(
206 'Upload of some lame file',
214 'mime' =>
'image/jpeg',
215 'metadata' => serialize(
array() ),
217 'fileExists' =>
true ),
218 $this->db->timestamp(
'20010115123500' ),
$user
223 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
224 $image->recordUpload2(
226 'Upload of some lame thumbnail',
227 'Some lame thumbnail',
234 'mime' =>
'image/png',
235 'metadata' => serialize(
array() ),
237 'fileExists' =>
true ),
238 $this->db->timestamp(
'20130225203040' ),
$user
242 # This image will be blacklisted in [[MediaWiki:Bad image list]]
244 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
245 $image->recordUpload2(
255 'mime' =>
'image/jpeg',
256 'metadata' => serialize(
array() ),
258 'fileExists' =>
true ),
259 $this->db->timestamp(
'20010115123500' ),
$user
263 if ( !$this->db->selectField(
'image',
'1',
array(
'img_name' => $image->getName() ) ) ) {
264 $image->recordUpload2(
'',
'Upload of some lame SVG',
'Some lame SVG',
array(
270 'mime' =>
'image/svg+xml',
271 'metadata' => serialize(
array() ),
274 ), $this->db->timestamp(
'20010115123500' ),
$user );
284 protected function setupGlobals( $opts =
array(), $config =
'' ) {
286 # Find out values for some special options.
288 self::getOptionValue(
'language', $opts,
'en' );
290 self::getOptionValue(
'variant', $opts,
false );
292 self::getOptionValue(
'wgMaxTocLevel', $opts, 999 );
293 $linkHolderBatchSize =
294 self::getOptionValue(
'wgLinkHolderBatchSize', $opts, 1000 );
296 $uploadDir = $this->getUploadDir();
297 if ( $this->getCliArg(
'use-filebackend=' ) ) {
298 if ( self::$backendToUse ) {
299 $backend = self::$backendToUse;
301 $name = $this->getCliArg(
'use-filebackend=' );
302 $useConfig =
array();
303 foreach ( $wgFileBackends
as $conf ) {
304 if ( $conf[
'name'] ==
$name ) {
308 $useConfig[
'name'] =
'local-backend';
309 unset( $useConfig[
'lockManager'] );
310 unset( $useConfig[
'fileJournal'] );
311 $class = $useConfig[
'class'];
312 self::$backendToUse =
new $class( $useConfig );
313 $backend = self::$backendToUse;
316 # Replace with a mock. We do not care about generating real
317 # files on the filesystem, just need to expose the file
320 'name' =>
'local-backend',
321 'wikiId' => wfWikiId()
326 'wgLocalFileRepo' =>
array(
327 'class' =>
'LocalRepo',
329 'url' =>
'http://example.com/images',
331 'transformVia404' =>
false,
332 'backend' => $backend
334 'wgEnableUploads' => self::getOptionValue(
'wgEnableUploads', $opts,
true ),
335 'wgLanguageCode' => $lang,
336 'wgDBprefix' => $this->db->getType() !=
'oracle' ?
'unittest_' :
'ut_',
337 'wgRawHtml' => self::getOptionValue(
'wgRawHtml', $opts,
false ),
338 'wgNamespacesWithSubpages' =>
array(
NS_MAIN => isset( $opts[
'subpage'] ) ),
339 'wgAllowExternalImages' => self::getOptionValue(
'wgAllowExternalImages', $opts,
true ),
340 'wgThumbLimits' =>
array( self::getOptionValue(
'thumbsize', $opts, 180 ) ),
341 'wgMaxTocLevel' => $maxtoclevel,
342 'wgUseTeX' => isset( $opts[
'math'] ) || isset( $opts[
'texvc'] ),
343 'wgMathDirectory' => $uploadDir .
'/math',
344 'wgDefaultLanguageVariant' => $variant,
345 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
349 $configLines = explode(
"\n", $config );
351 foreach ( $configLines
as $line ) {
354 $settings[$var] = eval(
"return $value;" );
358 $this->savedGlobals =
array();
364 $settings[
'wgContLang'] = $langObj;
365 $settings[
'wgLang'] = $langObj;
368 $settings[
'wgOut'] = $context->getOutput();
369 $settings[
'wgUser'] = $context->getUser();
370 $settings[
'wgRequest'] = $context->getRequest();
373 $context->getUser()->setOption(
'thumbsize', 0 );
375 foreach ( $settings
as $var => $val ) {
376 if ( array_key_exists( $var,
$GLOBALS ) ) {
377 $this->savedGlobals[$var] =
$GLOBALS[$var];
385 # The entries saved into RepoGroup cache with previous globals will be wrong.
389 # Create dummy files in storage
390 $this->setupUploads();
392 # Publish the articles after we have the final language set
393 $this->publishTestArticles();
405 protected function getUploadDir() {
406 if ( $this->keepUploads ) {
409 if ( is_dir(
$dir ) ) {
417 if ( file_exists(
$dir ) ) {
418 wfDebug(
"Already exists!\n" );
432 protected function setupUploads() {
435 $base = $this->getBaseDir();
437 $backend->prepare(
array(
'dir' =>
"$base/local-public/3/3a" ) );
438 $backend->store(
array(
439 'src' =>
"$IP/skins/monobook/headbg.jpg",
'dst' =>
"$base/local-public/3/3a/Foobar.jpg"
441 $backend->prepare(
array(
'dir' =>
"$base/local-public/e/ea" ) );
442 $backend->store(
array(
443 'src' =>
"$IP/skins/monobook/wiki.png",
'dst' =>
"$base/local-public/e/ea/Thumb.png"
445 $backend->prepare(
array(
'dir' =>
"$base/local-public/0/09" ) );
446 $backend->store(
array(
447 'src' =>
"$IP/skins/monobook/headbg.jpg",
'dst' =>
"$base/local-public/0/09/Bad.jpg"
451 $data =
'<?xml version="1.0" encoding="utf-8"?>' .
452 '<svg xmlns="http://www.w3.org/2000/svg"' .
453 ' version="1.1" width="240" height="180"/>';
455 $backend->prepare(
array(
'dir' =>
"$base/local-public/f/ff" ) );
456 $backend->quickCreate(
array(
457 'content' => $data,
'dst' =>
"$base/local-public/f/ff/Foobar.svg"
465 protected function teardownGlobals() {
466 $this->teardownUploads();
468 foreach ( $this->savedGlobals
as $var => $val ) {
476 private function teardownUploads() {
477 if ( $this->keepUploads ) {
483 # In memory backend, so dont bother cleaning them up.
487 $base = $this->getBaseDir();
491 "$base/local-public/3/3a/Foobar.jpg",
492 "$base/local-thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
493 "$base/local-thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
494 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
495 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
496 "$base/local-thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
497 "$base/local-thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
498 "$base/local-thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
499 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
500 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
501 "$base/local-thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
502 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
503 "$base/local-thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
504 "$base/local-thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
505 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
506 "$base/local-thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
507 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
508 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
509 "$base/local-thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
510 "$base/local-thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
511 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
512 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
513 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
514 "$base/local-thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
515 "$base/local-thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
516 "$base/local-thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
517 "$base/local-thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
518 "$base/local-thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
519 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
520 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
521 "$base/local-thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
522 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
524 "$base/local-public/e/ea/Thumb.png",
526 "$base/local-public/0/09/Bad.jpg",
528 "$base/local-public/f/ff/Foobar.svg",
529 "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
530 "$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
531 "$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
532 "$base/local-thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
533 "$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
534 "$base/local-thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
535 "$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
536 "$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
537 "$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
539 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
548 private static function deleteFiles(
$files ) {
556 if ( !$backend->clean(
array(
'dir' => $tmp ) )->isOK() ) {
563 protected function getBaseDir() {
564 return 'mwstore://local-backend';
567 public function parserTestProvider() {
568 if ( $this->
file ===
false ) {
569 global $wgParserTestFiles;
570 $this->
file = $wgParserTestFiles[0];
579 public function setParserTestFile( $filename ) {
580 $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] ) ) {