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