tests: mock parser tests file access
[mediawiki.git] / tests / phpunit / includes / parser / NewParserTest.php
blobf41c71cc7654eb2c859656fddb622918c9a788b8
1 <?php
3 /**
4 * Although marked as a stub, can work independently.
6 * @group Database
7 * @group Parser
8 * @group Stub
9 */
10 class NewParserTest extends MediaWikiTestCase {
11 static protected $articles = array(); // Array of test articles defined by the tests
12 /* The data provider is run on a different instance than the test, so it must be static
13 * When running tests from several files, all tests will see all articles.
15 static protected $backendToUse;
17 public $keepUploads = false;
18 public $runDisabled = false;
19 public $runParsoid = false;
20 public $regex = '';
21 public $showProgress = true;
22 public $savedWeirdGlobals = array();
23 public $savedGlobals = array();
24 public $hooks = array();
25 public $functionHooks = array();
27 //Fuzz test
28 public $maxFuzzTestLength = 300;
29 public $fuzzSeed = 0;
30 public $memoryLimit = 50;
32 protected $file = false;
34 protected function setUp() {
35 global $wgNamespaceAliases;
36 global $wgHooks, $IP;
38 parent::setUp();
40 //Setup CLI arguments
41 if ( $this->getCliArg( 'regex=' ) ) {
42 $this->regex = $this->getCliArg( 'regex=' );
43 } else {
44 # Matches anything
45 $this->regex = '';
48 $this->keepUploads = $this->getCliArg( 'keep-uploads' );
50 $tmpGlobals = array();
52 $tmpGlobals['wgLanguageCode'] = 'en';
53 $tmpGlobals['wgContLang'] = Language::factory( 'en' );
54 $tmpGlobals['wgSitename'] = 'MediaWiki';
55 $tmpGlobals['wgServer'] = 'http://example.org';
56 $tmpGlobals['wgScript'] = '/index.php';
57 $tmpGlobals['wgScriptPath'] = '/';
58 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
59 $tmpGlobals['wgActionPaths'] = array();
60 $tmpGlobals['wgVariantArticlePath'] = false;
61 $tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
62 $tmpGlobals['wgStylePath'] = '/skins';
63 $tmpGlobals['wgEnableUploads'] = true;
64 $tmpGlobals['wgThumbnailScriptPath'] = false;
65 $tmpGlobals['wgLocalFileRepo'] = array(
66 'class' => 'LocalRepo',
67 'name' => 'local',
68 'url' => 'http://example.com/images',
69 'hashLevels' => 2,
70 'transformVia404' => false,
71 'backend' => 'local-backend'
73 $tmpGlobals['wgForeignFileRepos'] = array();
74 $tmpGlobals['wgDefaultExternalStore'] = array();
75 $tmpGlobals['wgEnableParserCache'] = false;
76 $tmpGlobals['wgCapitalLinks'] = true;
77 $tmpGlobals['wgNoFollowLinks'] = true;
78 $tmpGlobals['wgNoFollowDomainExceptions'] = array();
79 $tmpGlobals['wgExternalLinkTarget'] = false;
80 $tmpGlobals['wgThumbnailScriptPath'] = false;
81 $tmpGlobals['wgUseImageResize'] = true;
82 $tmpGlobals['wgAllowExternalImages'] = true;
83 $tmpGlobals['wgRawHtml'] = false;
84 $tmpGlobals['wgUseTidy'] = false;
85 $tmpGlobals['wgAlwaysUseTidy'] = false;
86 $tmpGlobals['wgHtml5'] = true;
87 $tmpGlobals['wgWellFormedXml'] = true;
88 $tmpGlobals['wgAllowMicrodataAttributes'] = true;
89 $tmpGlobals['wgExperimentalHtmlIds'] = false;
90 $tmpGlobals['wgAdaptiveMessageCache'] = true;
91 $tmpGlobals['wgUseDatabaseMessages'] = true;
92 $tmpGlobals['wgLocaltimezone'] = 'UTC';
93 $tmpGlobals['wgDeferredUpdateList'] = array();
94 $tmpGlobals['wgGroupPermissions'] = array(
95 '*' => array(
96 'createaccount' => true,
97 'read' => true,
98 'edit' => true,
99 'createpage' => true,
100 'createtalk' => true,
101 ) );
102 $tmpGlobals['wgNamespaceProtection'] = array( NS_MEDIAWIKI => 'editinterface' );
103 $tmpGlobals['wgMemc'] = new EmptyBagOStuff;
104 $tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
105 $tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
107 $tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
109 if ( $GLOBALS['wgStyleDirectory'] === false ) {
110 $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
113 # Replace all media handlers with a mock. We do not need to generate
114 # actual thumbnails to do parser testing, we only care about receiving
115 # a ThumbnailImage properly initialized.
116 global $wgMediaHandlers;
117 foreach( $wgMediaHandlers as $type => $handler ) {
118 $tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
121 $tmpHooks = $wgHooks;
122 $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
123 $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
124 $tmpGlobals['wgHooks'] = $tmpHooks;
126 $this->setMwGlobals( $tmpGlobals );
128 $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
129 $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
131 $wgNamespaceAliases['Image'] = NS_FILE;
132 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
135 protected function tearDown() {
136 global $wgNamespaceAliases;
138 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
139 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
141 // Restore backends
142 RepoGroup::destroySingleton();
143 FileBackendGroup::destroySingleton();
145 parent::tearDown();
148 function addDBData() {
149 $this->tablesUsed[] = 'site_stats';
150 $this->tablesUsed[] = 'interwiki';
151 # disabled for performance
152 #$this->tablesUsed[] = 'image';
154 # Hack: insert a few Wikipedia in-project interwiki prefixes,
155 # for testing inter-language links
156 $this->db->insert( 'interwiki', array(
157 array( 'iw_prefix' => 'wikipedia',
158 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
159 'iw_api' => '',
160 'iw_wikiid' => '',
161 'iw_local' => 0 ),
162 array( 'iw_prefix' => 'meatball',
163 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
164 'iw_api' => '',
165 'iw_wikiid' => '',
166 'iw_local' => 0 ),
167 array( 'iw_prefix' => 'zh',
168 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
169 'iw_api' => '',
170 'iw_wikiid' => '',
171 'iw_local' => 1 ),
172 array( 'iw_prefix' => 'es',
173 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
174 'iw_api' => '',
175 'iw_wikiid' => '',
176 'iw_local' => 1 ),
177 array( 'iw_prefix' => 'fr',
178 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
179 'iw_api' => '',
180 'iw_wikiid' => '',
181 'iw_local' => 1 ),
182 array( 'iw_prefix' => 'ru',
183 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
184 'iw_api' => '',
185 'iw_wikiid' => '',
186 'iw_local' => 1 ),
188 * @todo Fixme! Why are we inserting duplicate data here? Shouldn't
189 * need this IGNORE or shouldn't need the insert at all.
191 ), __METHOD__, array( 'IGNORE' )
194 # Update certain things in site_stats
195 $this->db->insert( 'site_stats',
196 array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
197 __METHOD__
200 # Clear the message cache
201 MessageCache::singleton()->clear();
203 $user = User::newFromId( 0 );
204 LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
206 # Upload DB table entries for files.
207 # We will upload the actual files later. Note that if anything causes LocalFile::load()
208 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
209 # member to false and storing it in cache.
210 # note that the size/width/height/bits/etc of the file
211 # are actually set by inspecting the file itself; the arguments
212 # to recordUpload2 have no effect. That said, we try to make things
213 # match up so it is less confusing to readers of the code & tests.
214 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
215 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
216 $image->recordUpload2(
217 '', // archive name
218 'Upload of some lame file',
219 'Some lame file',
220 array(
221 'size' => 7881,
222 'width' => 1941,
223 'height' => 220,
224 'bits' => 8,
225 'media_type' => MEDIATYPE_BITMAP,
226 'mime' => 'image/jpeg',
227 'metadata' => serialize( array() ),
228 'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
229 'fileExists' => true ),
230 $this->db->timestamp( '20010115123500' ), $user
234 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
235 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
236 $image->recordUpload2(
237 '', // archive name
238 'Upload of some lame thumbnail',
239 'Some lame thumbnail',
240 array(
241 'size' => 22589,
242 'width' => 135,
243 'height' => 135,
244 'bits' => 8,
245 'media_type' => MEDIATYPE_BITMAP,
246 'mime' => 'image/png',
247 'metadata' => serialize( array() ),
248 'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
249 'fileExists' => true ),
250 $this->db->timestamp( '20130225203040' ), $user
254 # This image will be blacklisted in [[MediaWiki:Bad image list]]
255 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
256 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
257 $image->recordUpload2(
258 '', // archive name
259 'zomgnotcensored',
260 'Borderline image',
261 array(
262 'size' => 12345,
263 'width' => 320,
264 'height' => 240,
265 'bits' => 24,
266 'media_type' => MEDIATYPE_BITMAP,
267 'mime' => 'image/jpeg',
268 'metadata' => serialize( array() ),
269 'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
270 'fileExists' => true ),
271 $this->db->timestamp( '20010115123500' ), $user
276 //ParserTest setup/teardown functions
279 * Set up the global variables for a consistent environment for each test.
280 * Ideally this should replace the global configuration entirely.
282 protected function setupGlobals( $opts = array(), $config = '' ) {
283 global $wgFileBackends;
284 # Find out values for some special options.
285 $lang =
286 self::getOptionValue( 'language', $opts, 'en' );
287 $variant =
288 self::getOptionValue( 'variant', $opts, false );
289 $maxtoclevel =
290 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
291 $linkHolderBatchSize =
292 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
294 $uploadDir = $this->getUploadDir();
295 if ( $this->getCliArg( 'use-filebackend=' ) ) {
296 if ( self::$backendToUse ) {
297 $backend = self::$backendToUse;
298 } else {
299 $name = $this->getCliArg( 'use-filebackend=' );
300 $useConfig = array();
301 foreach ( $wgFileBackends as $conf ) {
302 if ( $conf['name'] == $name ) {
303 $useConfig = $conf;
306 $useConfig['name'] = 'local-backend'; // swap name
307 $class = $conf['class'];
308 self::$backendToUse = new $class( $useConfig );
309 $backend = self::$backendToUse;
311 } else {
312 # Replace with a mock. We do not care about generating real
313 # files on the filesystem, just need to expose the file
314 # informations.
315 $backend = new MockFileBackend( array(
316 'name' => 'local-backend',
317 'lockManager' => 'nullLockManager',
318 'containerPaths' => array(
319 'local-public' => "$uploadDir",
320 'local-thumb' => "$uploadDir/thumb",
322 ) );
325 $settings = array(
326 'wgLocalFileRepo' => array(
327 'class' => 'LocalRepo',
328 'name' => 'local',
329 'url' => 'http://example.com/images',
330 'hashLevels' => 2,
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' => isset( $opts['rawhtml'] ),
338 'wgNamespacesWithSubpages' => array( NS_MAIN => isset( $opts['subpage'] ) ),
339 'wgMaxTocLevel' => $maxtoclevel,
340 'wgUseTeX' => isset( $opts['math'] ),
341 'wgMathDirectory' => $uploadDir . '/math',
342 'wgDefaultLanguageVariant' => $variant,
343 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
346 if ( $config ) {
347 $configLines = explode( "\n", $config );
349 foreach ( $configLines as $line ) {
350 list( $var, $value ) = explode( '=', $line, 2 );
352 $settings[$var] = eval( "return $value;" ); //???
356 $this->savedGlobals = array();
358 /** @since 1.20 */
359 wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
361 $langObj = Language::factory( $lang );
362 $settings['wgContLang'] = $langObj;
363 $settings['wgLang'] = $langObj;
365 $context = new RequestContext();
366 $settings['wgOut'] = $context->getOutput();
367 $settings['wgUser'] = $context->getUser();
368 $settings['wgRequest'] = $context->getRequest();
370 foreach ( $settings as $var => $val ) {
371 if ( array_key_exists( $var, $GLOBALS ) ) {
372 $this->savedGlobals[$var] = $GLOBALS[$var];
375 $GLOBALS[$var] = $val;
378 MagicWord::clearCache();
379 RepoGroup::destroySingleton();
380 FileBackendGroup::destroySingleton();
382 # Create dummy files in storage
383 $this->setupUploads();
385 # Publish the articles after we have the final language set
386 $this->publishTestArticles();
388 # The entries saved into RepoGroup cache with previous globals will be wrong.
389 RepoGroup::destroySingleton();
390 FileBackendGroup::destroySingleton();
391 MessageCache::destroyInstance();
393 return $context;
397 * Get an FS upload directory (only applies to FSFileBackend)
399 * @return String: the directory
401 protected function getUploadDir() {
402 if ( $this->keepUploads ) {
403 $dir = wfTempDir() . '/mwParser-images';
405 if ( is_dir( $dir ) ) {
406 return $dir;
408 } else {
409 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
412 // wfDebug( "Creating upload directory $dir\n" );
413 if ( file_exists( $dir ) ) {
414 wfDebug( "Already exists!\n" );
416 return $dir;
419 return $dir;
423 * Create a dummy uploads directory which will contain a couple
424 * of files in order to pass existence tests.
426 * @return String: the directory
428 protected function setupUploads() {
429 global $IP;
431 $base = $this->getBaseDir();
432 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
433 $backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
434 $backend->store( array(
435 'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/3/3a/Foobar.jpg"
436 ) );
437 $backend->prepare( array( 'dir' => "$base/local-public/e/ea" ) );
438 $backend->store( array(
439 'src' => "$IP/skins/monobook/wiki.png", 'dst' => "$base/local-public/e/ea/Thumb.png"
440 ) );
441 $backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
442 $backend->store( array(
443 'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
444 ) );
448 * Restore default values and perform any necessary clean-up
449 * after each test runs.
451 protected function teardownGlobals() {
452 $this->teardownUploads();
454 foreach ( $this->savedGlobals as $var => $val ) {
455 $GLOBALS[$var] = $val;
458 RepoGroup::destroySingleton();
459 LinkCache::singleton()->clear();
463 * Remove the dummy uploads directory
465 private function teardownUploads() {
466 if ( $this->keepUploads ) {
467 return;
470 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
471 if( $backend instanceof MockFileBackend ) {
472 # In memory backend, so dont bother cleaning them up.
473 return;
476 $base = $this->getBaseDir();
477 // delete the files first, then the dirs.
478 self::deleteFiles(
479 array(
480 "$base/local-public/3/3a/Foobar.jpg",
481 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
482 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
483 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
484 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
485 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
486 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
487 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
488 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
489 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
490 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
491 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
492 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
493 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
494 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
496 "$base/local-public/e/ea/Thumb.png",
498 "$base/local-public/0/09/Bad.jpg",
500 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
506 * Delete the specified files, if they exist.
507 * @param $files Array: full paths to files to delete.
509 private static function deleteFiles( $files ) {
510 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
511 foreach ( $files as $file ) {
512 $backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
514 foreach ( $files as $file ) {
515 $tmp = $file;
516 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
517 if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
518 break;
524 protected function getBaseDir() {
525 return 'mwstore://local-backend';
528 public function parserTestProvider() {
529 if ( $this->file === false ) {
530 global $wgParserTestFiles;
531 $this->file = $wgParserTestFiles[0];
534 return new TestFileIterator( $this->file, $this );
538 * Set the file from whose tests will be run by this instance
540 public function setParserTestFile( $filename ) {
541 $this->file = $filename;
545 * @group medium
546 * @dataProvider parserTestProvider
548 public function testParserTest( $desc, $input, $result, $opts, $config ) {
549 if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
550 $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
551 //$this->markTestSkipped( 'Filtered out by the user' );
552 return;
555 if ( !$this->isWikitextNS( NS_MAIN ) ) {
556 // parser tests frequently assume that the main namespace contains wikitext.
557 // @todo: When setting up pages, force the content model. Only skip if
558 // $wgtContentModelUseDB is false.
559 $this->markTestSkipped( "Main namespace does not support wikitext,"
560 . "skipping parser test: $desc" );
563 wfDebug( "Running parser test: $desc\n" );
565 $opts = $this->parseOptions( $opts );
566 $context = $this->setupGlobals( $opts, $config );
568 $user = $context->getUser();
569 $options = ParserOptions::newFromContext( $context );
571 if ( isset( $opts['title'] ) ) {
572 $titleText = $opts['title'];
573 } else {
574 $titleText = 'Parser test';
577 $local = isset( $opts['local'] );
578 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
579 $parser = $this->getParser( $preprocessor );
581 $title = Title::newFromText( $titleText );
583 if ( isset( $opts['pst'] ) ) {
584 $out = $parser->preSaveTransform( $input, $title, $user, $options );
585 } elseif ( isset( $opts['msg'] ) ) {
586 $out = $parser->transformMsg( $input, $options, $title );
587 } elseif ( isset( $opts['section'] ) ) {
588 $section = $opts['section'];
589 $out = $parser->getSection( $input, $section );
590 } elseif ( isset( $opts['replace'] ) ) {
591 $section = $opts['replace'][0];
592 $replace = $opts['replace'][1];
593 $out = $parser->replaceSection( $input, $section, $replace );
594 } elseif ( isset( $opts['comment'] ) ) {
595 $out = Linker::formatComment( $input, $title, $local );
596 } elseif ( isset( $opts['preload'] ) ) {
597 $out = $parser->getPreloadText( $input, $title, $options );
598 } else {
599 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
600 $out = $output->getText();
602 if ( isset( $opts['showtitle'] ) ) {
603 if ( $output->getTitleText() ) {
604 $title = $output->getTitleText();
607 $out = "$title\n$out";
610 if ( isset( $opts['ill'] ) ) {
611 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
612 } elseif ( isset( $opts['cat'] ) ) {
613 $outputPage = $context->getOutput();
614 $outputPage->addCategoryLinks( $output->getCategories() );
615 $cats = $outputPage->getCategoryLinks();
617 if ( isset( $cats['normal'] ) ) {
618 $out = $this->tidy( implode( ' ', $cats['normal'] ) );
619 } else {
620 $out = '';
623 $parser->mPreprocessor = null;
625 $result = $this->tidy( $result );
628 $this->teardownGlobals();
630 $this->assertEquals( $result, $out, $desc );
634 * Run a fuzz test series
635 * Draw input from a set of test files
637 * @todo fixme Needs some work to not eat memory until the world explodes
639 * @group ParserFuzz
641 function testFuzzTests() {
642 global $wgParserTestFiles;
644 $files = $wgParserTestFiles;
646 if ( $this->getCliArg( 'file=' ) ) {
647 $files = array( $this->getCliArg( 'file=' ) );
650 $dict = $this->getFuzzInput( $files );
651 $dictSize = strlen( $dict );
652 $logMaxLength = log( $this->maxFuzzTestLength );
654 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
656 $user = new User;
657 $opts = ParserOptions::newFromUser( $user );
658 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
660 $id = 1;
662 while ( true ) {
664 // Generate test input
665 mt_srand( ++$this->fuzzSeed );
666 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
667 $input = '';
669 while ( strlen( $input ) < $totalLength ) {
670 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
671 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
672 $offset = mt_rand( 0, $dictSize - $hairLength );
673 $input .= substr( $dict, $offset, $hairLength );
676 $this->setupGlobals();
677 $parser = $this->getParser();
679 // Run the test
680 try {
681 $parser->parse( $input, $title, $opts );
682 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
683 } catch ( Exception $exception ) {
684 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
686 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
689 $this->teardownGlobals();
690 $parser->__destruct();
692 if ( $id % 100 == 0 ) {
693 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
694 //echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
695 if ( $usage > 90 ) {
696 $ret = "Out of memory:\n";
697 $memStats = $this->getMemoryBreakdown();
699 foreach ( $memStats as $name => $usage ) {
700 $ret .= "$name: $usage\n";
703 throw new MWException( $ret );
707 $id++;
711 //Various getter functions
714 * Get an input dictionary from a set of parser test files
716 function getFuzzInput( $filenames ) {
717 $dict = '';
719 foreach ( $filenames as $filename ) {
720 $contents = file_get_contents( $filename );
721 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
723 foreach ( $matches[1] as $match ) {
724 $dict .= $match . "\n";
728 return $dict;
732 * Get a memory usage breakdown
734 function getMemoryBreakdown() {
735 $memStats = array();
737 foreach ( $GLOBALS as $name => $value ) {
738 $memStats['$' . $name] = strlen( serialize( $value ) );
741 $classes = get_declared_classes();
743 foreach ( $classes as $class ) {
744 $rc = new ReflectionClass( $class );
745 $props = $rc->getStaticProperties();
746 $memStats[$class] = strlen( serialize( $props ) );
747 $methods = $rc->getMethods();
749 foreach ( $methods as $method ) {
750 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
754 $functions = get_defined_functions();
756 foreach ( $functions['user'] as $function ) {
757 $rf = new ReflectionFunction( $function );
758 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
761 asort( $memStats );
763 return $memStats;
767 * Get a Parser object
769 function getParser( $preprocessor = null ) {
770 global $wgParserConf;
772 $class = $wgParserConf['class'];
773 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
775 wfRunHooks( 'ParserTestParser', array( &$parser ) );
777 return $parser;
780 //Various action functions
782 public function addArticle( $name, $text, $line ) {
783 self::$articles[$name] = array( $text, $line );
786 public function publishTestArticles() {
787 if ( empty( self::$articles ) ) {
788 return;
791 foreach ( self::$articles as $name => $info ) {
792 list( $text, $line ) = $info;
793 ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
798 * Steal a callback function from the primary parser, save it for
799 * application to our scary parser. If the hook is not installed,
800 * abort processing of this file.
802 * @param $name String
803 * @return Bool true if tag hook is present
805 public function requireHook( $name ) {
806 global $wgParser;
807 $wgParser->firstCallInit(); // make sure hooks are loaded.
808 return isset( $wgParser->mTagHooks[$name] );
811 public function requireFunctionHook( $name ) {
812 global $wgParser;
813 $wgParser->firstCallInit(); // make sure hooks are loaded.
814 return isset( $wgParser->mFunctionHooks[$name] );
817 //Various "cleanup" functions
820 * Run the "tidy" command on text if the $wgUseTidy
821 * global is true
823 * @param $text String: the text to tidy
824 * @return String
826 protected function tidy( $text ) {
827 global $wgUseTidy;
829 if ( $wgUseTidy ) {
830 $text = MWTidy::tidy( $text );
833 return $text;
837 * Remove last character if it is a newline
839 public function removeEndingNewline( $s ) {
840 if ( substr( $s, -1 ) === "\n" ) {
841 return substr( $s, 0, -1 );
842 } else {
843 return $s;
847 //Test options parser functions
849 protected function parseOptions( $instring ) {
850 $opts = array();
851 // foo
852 // foo=bar
853 // foo="bar baz"
854 // foo=[[bar baz]]
855 // foo=bar,"baz quux"
856 $regex = '/\b
857 ([\w-]+) # Key
859 (?:\s*
860 = # First sub-value
864 [^"]* # Quoted val
867 \[\[
868 [^]]* # Link target
869 \]\]
871 [\w-]+ # Plain word
873 (?:\s*
874 , # Sub-vals 1..N
877 "[^"]*" # Quoted val
879 \[\[[^]]*\]\] # Link target
881 [\w-]+ # Plain word
885 /x';
887 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
888 foreach ( $matches as $bits ) {
889 array_shift( $bits );
890 $key = strtolower( array_shift( $bits ) );
891 if ( count( $bits ) == 0 ) {
892 $opts[$key] = true;
893 } elseif ( count( $bits ) == 1 ) {
894 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
895 } else {
896 // Array!
897 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
902 return $opts;
905 protected function cleanupOption( $opt ) {
906 if ( substr( $opt, 0, 1 ) == '"' ) {
907 return substr( $opt, 1, -1 );
910 if ( substr( $opt, 0, 2 ) == '[[' ) {
911 return substr( $opt, 2, -2 );
914 return $opt;
918 * Use a regex to find out the value of an option
919 * @param $key String: name of option val to retrieve
920 * @param $opts Options array to look in
921 * @param $default Mixed: default value returned if not found
923 protected static function getOptionValue( $key, $opts, $default ) {
924 $key = strtolower( $key );
926 if ( isset( $opts[$key] ) ) {
927 return $opts[$key];
928 } else {
929 return $default;