Merge "Reimplement Special:Filepath as a redirect through Special:Redirect/File."
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / NewParserTest.php
1 <?php
2
3 /**
4 * Although marked as a stub, can work independently.
5 *
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.
14 */
15 static protected $backendToUse;
16
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();
26
27 //Fuzz test
28 public $maxFuzzTestLength = 300;
29 public $fuzzSeed = 0;
30 public $memoryLimit = 50;
31
32 protected $file = false;
33
34 protected function setUp() {
35 global $wgNamespaceAliases;
36 global $wgHooks, $IP;
37
38 parent::setUp();
39
40 //Setup CLI arguments
41 if ( $this->getCliArg( 'regex=' ) ) {
42 $this->regex = $this->getCliArg( 'regex=' );
43 } else {
44 # Matches anything
45 $this->regex = '';
46 }
47
48 $this->keepUploads = $this->getCliArg( 'keep-uploads' );
49
50 $tmpGlobals = array();
51
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['wgStyleSheetPath'] = '/skins';
63 $tmpGlobals['wgStylePath'] = '/skins';
64 $tmpGlobals['wgEnableUploads'] = true;
65 $tmpGlobals['wgThumbnailScriptPath'] = false;
66 $tmpGlobals['wgLocalFileRepo'] = array(
67 'class' => 'LocalRepo',
68 'name' => 'local',
69 'url' => 'http://example.com/images',
70 'hashLevels' => 2,
71 'transformVia404' => false,
72 'backend' => 'local-backend'
73 );
74 $tmpGlobals['wgForeignFileRepos'] = array();
75 $tmpGlobals['wgDefaultExternalStore'] = array();
76 $tmpGlobals['wgEnableParserCache'] = false;
77 $tmpGlobals['wgCapitalLinks'] = true;
78 $tmpGlobals['wgNoFollowLinks'] = true;
79 $tmpGlobals['wgNoFollowDomainExceptions'] = array();
80 $tmpGlobals['wgExternalLinkTarget'] = false;
81 $tmpGlobals['wgThumbnailScriptPath'] = false;
82 $tmpGlobals['wgUseImageResize'] = true;
83 $tmpGlobals['wgAllowExternalImages'] = true;
84 $tmpGlobals['wgRawHtml'] = false;
85 $tmpGlobals['wgUseTidy'] = false;
86 $tmpGlobals['wgAlwaysUseTidy'] = false;
87 $tmpGlobals['wgHtml5'] = true;
88 $tmpGlobals['wgWellFormedXml'] = true;
89 $tmpGlobals['wgAllowMicrodataAttributes'] = true;
90 $tmpGlobals['wgExperimentalHtmlIds'] = false;
91 $tmpGlobals['wgAdaptiveMessageCache'] = true;
92 $tmpGlobals['wgUseDatabaseMessages'] = true;
93 $tmpGlobals['wgLocaltimezone'] = 'UTC';
94 $tmpGlobals['wgDeferredUpdateList'] = array();
95 $tmpGlobals['wgGroupPermissions'] = array(
96 '*' => array(
97 'createaccount' => true,
98 'read' => true,
99 'edit' => true,
100 'createpage' => true,
101 'createtalk' => true,
102 ) );
103 $tmpGlobals['wgNamespaceProtection'] = array( NS_MEDIAWIKI => 'editinterface' );
104 $tmpGlobals['wgMemc'] = new EmptyBagOStuff;
105 $tmpGlobals['messageMemc'] = wfGetMessageCacheStorage();
106 $tmpGlobals['parserMemc'] = wfGetParserCacheStorage();
107
108 $tmpGlobals['wgParser'] = new StubObject( 'wgParser', $GLOBALS['wgParserConf']['class'], array( $GLOBALS['wgParserConf'] ) );
109
110 if ( $GLOBALS['wgStyleDirectory'] === false ) {
111 $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
112 }
113
114 $tmpHooks = $wgHooks;
115 $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
116 $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
117 $tmpGlobals['wgHooks'] = $tmpHooks;
118
119 $this->setMwGlobals( $tmpGlobals );
120
121 $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
122 $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
123
124 $wgNamespaceAliases['Image'] = NS_FILE;
125 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
126 }
127
128 protected function tearDown() {
129 global $wgNamespaceAliases;
130
131 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
132 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
133
134 // Restore backends
135 RepoGroup::destroySingleton();
136 FileBackendGroup::destroySingleton();
137
138 parent::tearDown();
139 }
140
141 function addDBData() {
142 $this->tablesUsed[] = 'site_stats';
143 $this->tablesUsed[] = 'interwiki';
144 # disabled for performance
145 #$this->tablesUsed[] = 'image';
146
147 # Hack: insert a few Wikipedia in-project interwiki prefixes,
148 # for testing inter-language links
149 $this->db->insert( 'interwiki', array(
150 array( 'iw_prefix' => 'wikipedia',
151 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
152 'iw_api' => '',
153 'iw_wikiid' => '',
154 'iw_local' => 0 ),
155 array( 'iw_prefix' => 'meatball',
156 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
157 'iw_api' => '',
158 'iw_wikiid' => '',
159 'iw_local' => 0 ),
160 array( 'iw_prefix' => 'zh',
161 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
162 'iw_api' => '',
163 'iw_wikiid' => '',
164 'iw_local' => 1 ),
165 array( 'iw_prefix' => 'es',
166 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
167 'iw_api' => '',
168 'iw_wikiid' => '',
169 'iw_local' => 1 ),
170 array( 'iw_prefix' => 'fr',
171 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
172 'iw_api' => '',
173 'iw_wikiid' => '',
174 'iw_local' => 1 ),
175 array( 'iw_prefix' => 'ru',
176 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
177 'iw_api' => '',
178 'iw_wikiid' => '',
179 'iw_local' => 1 ),
180 /**
181 * @todo Fixme! Why are we inserting duplicate data here? Shouldn't
182 * need this IGNORE or shouldn't need the insert at all.
183 */
184 ), __METHOD__, array( 'IGNORE' )
185 );
186
187 # Update certain things in site_stats
188 $this->db->insert( 'site_stats',
189 array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
190 __METHOD__
191 );
192
193 # Reinitialise the LocalisationCache to match the database state
194 Language::getLocalisationCache()->unloadAll();
195
196 # Clear the message cache
197 MessageCache::singleton()->clear();
198
199 $user = User::newFromId( 0 );
200 LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
201
202 # Upload DB table entries for files.
203 # We will upload the actual files later. Note that if anything causes LocalFile::load()
204 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
205 # member to false and storing it in cache.
206 # note that the size/width/height/bits/etc of the file
207 # are actually set by inspecting the file itself; the arguments
208 # to recordUpload2 have no effect. That said, we try to make things
209 # match up so it is less confusing to readers of the code & tests.
210 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
211 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
212 $image->recordUpload2(
213 '', // archive name
214 'Upload of some lame file',
215 'Some lame file',
216 array(
217 'size' => 7881,
218 'width' => 1941,
219 'height' => 220,
220 'bits' => 8,
221 'media_type' => MEDIATYPE_BITMAP,
222 'mime' => 'image/jpeg',
223 'metadata' => serialize( array() ),
224 'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
225 'fileExists' => true ),
226 $this->db->timestamp( '20010115123500' ), $user
227 );
228 }
229
230 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
231 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
232 $image->recordUpload2(
233 '', // archive name
234 'Upload of some lame thumbnail',
235 'Some lame thumbnail',
236 array(
237 'size' => 22589,
238 'width' => 135,
239 'height' => 135,
240 'bits' => 8,
241 'media_type' => MEDIATYPE_BITMAP,
242 'mime' => 'image/png',
243 'metadata' => serialize( array() ),
244 'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
245 'fileExists' => true ),
246 $this->db->timestamp( '20130225203040' ), $user
247 );
248 }
249
250 # This image will be blacklisted in [[MediaWiki:Bad image list]]
251 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
252 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
253 $image->recordUpload2(
254 '', // archive name
255 'zomgnotcensored',
256 'Borderline image',
257 array(
258 'size' => 12345,
259 'width' => 320,
260 'height' => 240,
261 'bits' => 24,
262 'media_type' => MEDIATYPE_BITMAP,
263 'mime' => 'image/jpeg',
264 'metadata' => serialize( array() ),
265 'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
266 'fileExists' => true ),
267 $this->db->timestamp( '20010115123500' ), $user
268 );
269 }
270 }
271
272 //ParserTest setup/teardown functions
273
274 /**
275 * Set up the global variables for a consistent environment for each test.
276 * Ideally this should replace the global configuration entirely.
277 */
278 protected function setupGlobals( $opts = array(), $config = '' ) {
279 global $wgFileBackends;
280 # Find out values for some special options.
281 $lang =
282 self::getOptionValue( 'language', $opts, 'en' );
283 $variant =
284 self::getOptionValue( 'variant', $opts, false );
285 $maxtoclevel =
286 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
287 $linkHolderBatchSize =
288 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
289
290 $uploadDir = $this->getUploadDir();
291 if ( $this->getCliArg( 'use-filebackend=' ) ) {
292 if ( self::$backendToUse ) {
293 $backend = self::$backendToUse;
294 } else {
295 $name = $this->getCliArg( 'use-filebackend=' );
296 $useConfig = array();
297 foreach ( $wgFileBackends as $conf ) {
298 if ( $conf['name'] == $name ) {
299 $useConfig = $conf;
300 }
301 }
302 $useConfig['name'] = 'local-backend'; // swap name
303 $class = $conf['class'];
304 self::$backendToUse = new $class( $useConfig );
305 $backend = self::$backendToUse;
306 }
307 } else {
308 $backend = new FSFileBackend( array(
309 'name' => 'local-backend',
310 'lockManager' => 'nullLockManager',
311 'containerPaths' => array(
312 'local-public' => "$uploadDir",
313 'local-thumb' => "$uploadDir/thumb",
314 )
315 ) );
316 }
317
318 $settings = array(
319 'wgLocalFileRepo' => array(
320 'class' => 'LocalRepo',
321 'name' => 'local',
322 'url' => 'http://example.com/images',
323 'hashLevels' => 2,
324 'transformVia404' => false,
325 'backend' => $backend
326 ),
327 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
328 'wgLanguageCode' => $lang,
329 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
330 'wgRawHtml' => isset( $opts['rawhtml'] ),
331 'wgNamespacesWithSubpages' => array( NS_MAIN => isset( $opts['subpage'] ) ),
332 'wgMaxTocLevel' => $maxtoclevel,
333 'wgUseTeX' => isset( $opts['math'] ),
334 'wgMathDirectory' => $uploadDir . '/math',
335 'wgDefaultLanguageVariant' => $variant,
336 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
337 );
338
339 if ( $config ) {
340 $configLines = explode( "\n", $config );
341
342 foreach ( $configLines as $line ) {
343 list( $var, $value ) = explode( '=', $line, 2 );
344
345 $settings[$var] = eval( "return $value;" ); //???
346 }
347 }
348
349 $this->savedGlobals = array();
350
351 /** @since 1.20 */
352 wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
353
354 $langObj = Language::factory( $lang );
355 $settings['wgContLang'] = $langObj;
356 $settings['wgLang'] = $langObj;
357
358 $context = new RequestContext();
359 $settings['wgOut'] = $context->getOutput();
360 $settings['wgUser'] = $context->getUser();
361 $settings['wgRequest'] = $context->getRequest();
362
363 foreach ( $settings as $var => $val ) {
364 if ( array_key_exists( $var, $GLOBALS ) ) {
365 $this->savedGlobals[$var] = $GLOBALS[$var];
366 }
367
368 $GLOBALS[$var] = $val;
369 }
370
371 MagicWord::clearCache();
372 RepoGroup::destroySingleton();
373 FileBackendGroup::destroySingleton();
374
375 # Create dummy files in storage
376 $this->setupUploads();
377
378 # Publish the articles after we have the final language set
379 $this->publishTestArticles();
380
381 # The entries saved into RepoGroup cache with previous globals will be wrong.
382 RepoGroup::destroySingleton();
383 FileBackendGroup::destroySingleton();
384 MessageCache::destroyInstance();
385
386 return $context;
387 }
388
389 /**
390 * Get an FS upload directory (only applies to FSFileBackend)
391 *
392 * @return String: the directory
393 */
394 protected function getUploadDir() {
395 if ( $this->keepUploads ) {
396 $dir = wfTempDir() . '/mwParser-images';
397
398 if ( is_dir( $dir ) ) {
399 return $dir;
400 }
401 } else {
402 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
403 }
404
405 // wfDebug( "Creating upload directory $dir\n" );
406 if ( file_exists( $dir ) ) {
407 wfDebug( "Already exists!\n" );
408
409 return $dir;
410 }
411
412 return $dir;
413 }
414
415 /**
416 * Create a dummy uploads directory which will contain a couple
417 * of files in order to pass existence tests.
418 *
419 * @return String: the directory
420 */
421 protected function setupUploads() {
422 global $IP;
423
424 $base = $this->getBaseDir();
425 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
426 $backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
427 $backend->store( array(
428 'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/3/3a/Foobar.jpg"
429 ) );
430 $backend->prepare( array( 'dir' => "$base/local-public/e/ea" ) );
431 $backend->store( array(
432 'src' => "$IP/skins/monobook/wiki.png", 'dst' => "$base/local-public/e/ea/Thumb.png"
433 ) );
434 $backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
435 $backend->store( array(
436 'src' => "$IP/skins/monobook/headbg.jpg", 'dst' => "$base/local-public/0/09/Bad.jpg"
437 ) );
438 }
439
440 /**
441 * Restore default values and perform any necessary clean-up
442 * after each test runs.
443 */
444 protected function teardownGlobals() {
445 $this->teardownUploads();
446
447 foreach ( $this->savedGlobals as $var => $val ) {
448 $GLOBALS[$var] = $val;
449 }
450
451 RepoGroup::destroySingleton();
452 LinkCache::singleton()->clear();
453 }
454
455 /**
456 * Remove the dummy uploads directory
457 */
458 private function teardownUploads() {
459 if ( $this->keepUploads ) {
460 return;
461 }
462
463 $base = $this->getBaseDir();
464 // delete the files first, then the dirs.
465 self::deleteFiles(
466 array(
467 "$base/local-public/3/3a/Foobar.jpg",
468 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
469 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
470 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
471 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
472 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
473 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
474 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
475 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
476 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
477 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
478 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
479 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
480 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
481 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
482
483 "$base/local-public/e/ea/Thumb.png",
484
485 "$base/local-public/0/09/Bad.jpg",
486
487 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
488 )
489 );
490 }
491
492 /**
493 * Delete the specified files, if they exist.
494 * @param $files Array: full paths to files to delete.
495 */
496 private static function deleteFiles( $files ) {
497 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
498 foreach ( $files as $file ) {
499 $backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
500 }
501 foreach ( $files as $file ) {
502 $tmp = $file;
503 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
504 if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
505 break;
506 }
507 }
508 }
509 }
510
511 protected function getBaseDir() {
512 return 'mwstore://local-backend';
513 }
514
515 public function parserTestProvider() {
516 if ( $this->file === false ) {
517 global $wgParserTestFiles;
518 $this->file = $wgParserTestFiles[0];
519 }
520
521 return new TestFileIterator( $this->file, $this );
522 }
523
524 /**
525 * Set the file from whose tests will be run by this instance
526 */
527 public function setParserTestFile( $filename ) {
528 $this->file = $filename;
529 }
530
531 /**
532 * @group medium
533 * @dataProvider parserTestProvider
534 */
535 public function testParserTest( $desc, $input, $result, $opts, $config ) {
536 if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
537 $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
538 //$this->markTestSkipped( 'Filtered out by the user' );
539 return;
540 }
541
542 if ( !$this->isWikitextNS( NS_MAIN ) ) {
543 // parser tests frequently assume that the main namespace contains wikitext.
544 // @todo: When setting up pages, force the content model. Only skip if
545 // $wgtContentModelUseDB is false.
546 $this->markTestSkipped( "Main namespace does not support wikitext,"
547 . "skipping parser test: $desc" );
548 }
549
550 wfDebug( "Running parser test: $desc\n" );
551
552 $opts = $this->parseOptions( $opts );
553 $context = $this->setupGlobals( $opts, $config );
554
555 $user = $context->getUser();
556 $options = ParserOptions::newFromContext( $context );
557
558 if ( isset( $opts['title'] ) ) {
559 $titleText = $opts['title'];
560 } else {
561 $titleText = 'Parser test';
562 }
563
564 $local = isset( $opts['local'] );
565 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
566 $parser = $this->getParser( $preprocessor );
567
568 $title = Title::newFromText( $titleText );
569
570 if ( isset( $opts['pst'] ) ) {
571 $out = $parser->preSaveTransform( $input, $title, $user, $options );
572 } elseif ( isset( $opts['msg'] ) ) {
573 $out = $parser->transformMsg( $input, $options, $title );
574 } elseif ( isset( $opts['section'] ) ) {
575 $section = $opts['section'];
576 $out = $parser->getSection( $input, $section );
577 } elseif ( isset( $opts['replace'] ) ) {
578 $section = $opts['replace'][0];
579 $replace = $opts['replace'][1];
580 $out = $parser->replaceSection( $input, $section, $replace );
581 } elseif ( isset( $opts['comment'] ) ) {
582 $out = Linker::formatComment( $input, $title, $local );
583 } elseif ( isset( $opts['preload'] ) ) {
584 $out = $parser->getPreloadText( $input, $title, $options );
585 } else {
586 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
587 $out = $output->getText();
588
589 if ( isset( $opts['showtitle'] ) ) {
590 if ( $output->getTitleText() ) {
591 $title = $output->getTitleText();
592 }
593
594 $out = "$title\n$out";
595 }
596
597 if ( isset( $opts['ill'] ) ) {
598 $out = $this->tidy( implode( ' ', $output->getLanguageLinks() ) );
599 } elseif ( isset( $opts['cat'] ) ) {
600 $outputPage = $context->getOutput();
601 $outputPage->addCategoryLinks( $output->getCategories() );
602 $cats = $outputPage->getCategoryLinks();
603
604 if ( isset( $cats['normal'] ) ) {
605 $out = $this->tidy( implode( ' ', $cats['normal'] ) );
606 } else {
607 $out = '';
608 }
609 }
610 $parser->mPreprocessor = null;
611
612 $result = $this->tidy( $result );
613 }
614
615 $this->teardownGlobals();
616
617 $this->assertEquals( $result, $out, $desc );
618 }
619
620 /**
621 * Run a fuzz test series
622 * Draw input from a set of test files
623 *
624 * @todo fixme Needs some work to not eat memory until the world explodes
625 *
626 * @group ParserFuzz
627 */
628 function testFuzzTests() {
629 global $wgParserTestFiles;
630
631 $files = $wgParserTestFiles;
632
633 if ( $this->getCliArg( 'file=' ) ) {
634 $files = array( $this->getCliArg( 'file=' ) );
635 }
636
637 $dict = $this->getFuzzInput( $files );
638 $dictSize = strlen( $dict );
639 $logMaxLength = log( $this->maxFuzzTestLength );
640
641 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
642
643 $user = new User;
644 $opts = ParserOptions::newFromUser( $user );
645 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
646
647 $id = 1;
648
649 while ( true ) {
650
651 // Generate test input
652 mt_srand( ++$this->fuzzSeed );
653 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
654 $input = '';
655
656 while ( strlen( $input ) < $totalLength ) {
657 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
658 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
659 $offset = mt_rand( 0, $dictSize - $hairLength );
660 $input .= substr( $dict, $offset, $hairLength );
661 }
662
663 $this->setupGlobals();
664 $parser = $this->getParser();
665
666 // Run the test
667 try {
668 $parser->parse( $input, $title, $opts );
669 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
670 } catch ( Exception $exception ) {
671 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
672
673 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\nInput: $input_dump\n\nError: {$exception->getMessage()}\n\nBacktrace: {$exception->getTraceAsString()}" );
674 }
675
676 $this->teardownGlobals();
677 $parser->__destruct();
678
679 if ( $id % 100 == 0 ) {
680 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
681 //echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
682 if ( $usage > 90 ) {
683 $ret = "Out of memory:\n";
684 $memStats = $this->getMemoryBreakdown();
685
686 foreach ( $memStats as $name => $usage ) {
687 $ret .= "$name: $usage\n";
688 }
689
690 throw new MWException( $ret );
691 }
692 }
693
694 $id++;
695 }
696 }
697
698 //Various getter functions
699
700 /**
701 * Get an input dictionary from a set of parser test files
702 */
703 function getFuzzInput( $filenames ) {
704 $dict = '';
705
706 foreach ( $filenames as $filename ) {
707 $contents = file_get_contents( $filename );
708 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
709
710 foreach ( $matches[1] as $match ) {
711 $dict .= $match . "\n";
712 }
713 }
714
715 return $dict;
716 }
717
718 /**
719 * Get a memory usage breakdown
720 */
721 function getMemoryBreakdown() {
722 $memStats = array();
723
724 foreach ( $GLOBALS as $name => $value ) {
725 $memStats['$' . $name] = strlen( serialize( $value ) );
726 }
727
728 $classes = get_declared_classes();
729
730 foreach ( $classes as $class ) {
731 $rc = new ReflectionClass( $class );
732 $props = $rc->getStaticProperties();
733 $memStats[$class] = strlen( serialize( $props ) );
734 $methods = $rc->getMethods();
735
736 foreach ( $methods as $method ) {
737 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
738 }
739 }
740
741 $functions = get_defined_functions();
742
743 foreach ( $functions['user'] as $function ) {
744 $rf = new ReflectionFunction( $function );
745 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
746 }
747
748 asort( $memStats );
749
750 return $memStats;
751 }
752
753 /**
754 * Get a Parser object
755 */
756 function getParser( $preprocessor = null ) {
757 global $wgParserConf;
758
759 $class = $wgParserConf['class'];
760 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
761
762 wfRunHooks( 'ParserTestParser', array( &$parser ) );
763
764 return $parser;
765 }
766
767 //Various action functions
768
769 public function addArticle( $name, $text, $line ) {
770 self::$articles[$name] = array( $text, $line );
771 }
772
773 public function publishTestArticles() {
774 if ( empty( self::$articles ) ) {
775 return;
776 }
777
778 foreach ( self::$articles as $name => $info ) {
779 list( $text, $line ) = $info;
780 ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
781 }
782 }
783
784 /**
785 * Steal a callback function from the primary parser, save it for
786 * application to our scary parser. If the hook is not installed,
787 * abort processing of this file.
788 *
789 * @param $name String
790 * @return Bool true if tag hook is present
791 */
792 public function requireHook( $name ) {
793 global $wgParser;
794 $wgParser->firstCallInit(); // make sure hooks are loaded.
795 return isset( $wgParser->mTagHooks[$name] );
796 }
797
798 public function requireFunctionHook( $name ) {
799 global $wgParser;
800 $wgParser->firstCallInit(); // make sure hooks are loaded.
801 return isset( $wgParser->mFunctionHooks[$name] );
802 }
803
804 //Various "cleanup" functions
805
806 /**
807 * Run the "tidy" command on text if the $wgUseTidy
808 * global is true
809 *
810 * @param $text String: the text to tidy
811 * @return String
812 */
813 protected function tidy( $text ) {
814 global $wgUseTidy;
815
816 if ( $wgUseTidy ) {
817 $text = MWTidy::tidy( $text );
818 }
819
820 return $text;
821 }
822
823 /**
824 * Remove last character if it is a newline
825 */
826 public function removeEndingNewline( $s ) {
827 if ( substr( $s, -1 ) === "\n" ) {
828 return substr( $s, 0, -1 );
829 } else {
830 return $s;
831 }
832 }
833
834 //Test options parser functions
835
836 protected function parseOptions( $instring ) {
837 $opts = array();
838 // foo
839 // foo=bar
840 // foo="bar baz"
841 // foo=[[bar baz]]
842 // foo=bar,"baz quux"
843 $regex = '/\b
844 ([\w-]+) # Key
845 \b
846 (?:\s*
847 = # First sub-value
848 \s*
849 (
850 "
851 [^"]* # Quoted val
852 "
853 |
854 \[\[
855 [^]]* # Link target
856 \]\]
857 |
858 [\w-]+ # Plain word
859 )
860 (?:\s*
861 , # Sub-vals 1..N
862 \s*
863 (
864 "[^"]*" # Quoted val
865 |
866 \[\[[^]]*\]\] # Link target
867 |
868 [\w-]+ # Plain word
869 )
870 )*
871 )?
872 /x';
873
874 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
875 foreach ( $matches as $bits ) {
876 array_shift( $bits );
877 $key = strtolower( array_shift( $bits ) );
878 if ( count( $bits ) == 0 ) {
879 $opts[$key] = true;
880 } elseif ( count( $bits ) == 1 ) {
881 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
882 } else {
883 // Array!
884 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
885 }
886 }
887 }
888
889 return $opts;
890 }
891
892 protected function cleanupOption( $opt ) {
893 if ( substr( $opt, 0, 1 ) == '"' ) {
894 return substr( $opt, 1, -1 );
895 }
896
897 if ( substr( $opt, 0, 2 ) == '[[' ) {
898 return substr( $opt, 2, -2 );
899 }
900
901 return $opt;
902 }
903
904 /**
905 * Use a regex to find out the value of an option
906 * @param $key String: name of option val to retrieve
907 * @param $opts Options array to look in
908 * @param $default Mixed: default value returned if not found
909 */
910 protected static function getOptionValue( $key, $opts, $default ) {
911 $key = strtolower( $key );
912
913 if ( isset( $opts[$key] ) ) {
914 return $opts[$key];
915 } else {
916 return $default;
917 }
918 }
919 }