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