Run some parser tests with tidy.
[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 * @todo covers tags
11 */
12 class NewParserTest extends MediaWikiTestCase {
13 static protected $articles = array(); // Array of test articles defined by the tests
14 /* The data provider is run on a different instance than the test, so it must be static
15 * When running tests from several files, all tests will see all articles.
16 */
17 static protected $backendToUse;
18
19 public $keepUploads = false;
20 public $runDisabled = false;
21 public $runParsoid = false;
22 public $regex = '';
23 public $showProgress = true;
24 public $savedWeirdGlobals = array();
25 public $savedGlobals = array();
26 public $hooks = array();
27 public $functionHooks = array();
28 public $transparentHooks = array();
29
30 //Fuzz test
31 public $maxFuzzTestLength = 300;
32 public $fuzzSeed = 0;
33 public $memoryLimit = 50;
34
35 /**
36 * @var DjVuSupport
37 */
38 private $djVuSupport;
39 /**
40 * @var TidySupport
41 */
42 private $tidySupport;
43
44 protected $file = false;
45
46 public static function setUpBeforeClass() {
47 // Inject ParserTest well-known interwikis
48 ParserTest::setupInterwikis();
49 }
50
51 protected function setUp() {
52 global $wgNamespaceAliases, $wgContLang;
53 global $wgHooks, $IP;
54
55 parent::setUp();
56
57 //Setup CLI arguments
58 if ( $this->getCliArg( 'regex' ) ) {
59 $this->regex = $this->getCliArg( 'regex' );
60 } else {
61 # Matches anything
62 $this->regex = '';
63 }
64
65 $this->keepUploads = $this->getCliArg( 'keep-uploads' );
66
67 $tmpGlobals = array();
68
69 $tmpGlobals['wgLanguageCode'] = 'en';
70 $tmpGlobals['wgContLang'] = Language::factory( 'en' );
71 $tmpGlobals['wgSitename'] = 'MediaWiki';
72 $tmpGlobals['wgServer'] = 'http://example.org';
73 $tmpGlobals['wgServerName'] = 'example.org';
74 $tmpGlobals['wgScript'] = '/index.php';
75 $tmpGlobals['wgScriptPath'] = '/';
76 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
77 $tmpGlobals['wgActionPaths'] = array();
78 $tmpGlobals['wgVariantArticlePath'] = false;
79 $tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
80 $tmpGlobals['wgStylePath'] = '/skins';
81 $tmpGlobals['wgEnableUploads'] = true;
82 $tmpGlobals['wgThumbnailScriptPath'] = false;
83 $tmpGlobals['wgLocalFileRepo'] = array(
84 'class' => 'LocalRepo',
85 'name' => 'local',
86 'url' => 'http://example.com/images',
87 'hashLevels' => 2,
88 'transformVia404' => false,
89 'backend' => 'local-backend'
90 );
91 $tmpGlobals['wgForeignFileRepos'] = array();
92 $tmpGlobals['wgDefaultExternalStore'] = array();
93 $tmpGlobals['wgEnableParserCache'] = false;
94 $tmpGlobals['wgCapitalLinks'] = true;
95 $tmpGlobals['wgNoFollowLinks'] = true;
96 $tmpGlobals['wgNoFollowDomainExceptions'] = array();
97 $tmpGlobals['wgExternalLinkTarget'] = false;
98 $tmpGlobals['wgThumbnailScriptPath'] = false;
99 $tmpGlobals['wgUseImageResize'] = true;
100 $tmpGlobals['wgAllowExternalImages'] = true;
101 $tmpGlobals['wgRawHtml'] = false;
102 $tmpGlobals['wgWellFormedXml'] = true;
103 $tmpGlobals['wgAllowMicrodataAttributes'] = true;
104 $tmpGlobals['wgExperimentalHtmlIds'] = false;
105 $tmpGlobals['wgAdaptiveMessageCache'] = true;
106 $tmpGlobals['wgUseDatabaseMessages'] = true;
107 $tmpGlobals['wgLocaltimezone'] = 'UTC';
108 $tmpGlobals['wgDeferredUpdateList'] = array();
109 $tmpGlobals['wgGroupPermissions'] = array(
110 '*' => array(
111 'createaccount' => true,
112 'read' => true,
113 'edit' => true,
114 'createpage' => true,
115 'createtalk' => true,
116 ) );
117 $tmpGlobals['wgNamespaceProtection'] = array( NS_MEDIAWIKI => 'editinterface' );
118
119 $tmpGlobals['wgParser'] = new StubObject(
120 'wgParser', $GLOBALS['wgParserConf']['class'],
121 array( $GLOBALS['wgParserConf'] ) );
122
123 $tmpGlobals['wgFileExtensions'][] = 'svg';
124 $tmpGlobals['wgSVGConverter'] = 'rsvg';
125 $tmpGlobals['wgSVGConverters']['rsvg'] =
126 '$path/rsvg-convert -w $width -h $height $input -o $output';
127
128 if ( $GLOBALS['wgStyleDirectory'] === false ) {
129 $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
130 }
131
132 # Replace all media handlers with a mock. We do not need to generate
133 # actual thumbnails to do parser testing, we only care about receiving
134 # a ThumbnailImage properly initialized.
135 global $wgMediaHandlers;
136 foreach ( $wgMediaHandlers as $type => $handler ) {
137 $tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
138 }
139 // Vector images have to be handled slightly differently
140 $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
141
142 // DjVu images have to be handled slightly differently
143 $tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
144
145 $tmpHooks = $wgHooks;
146 $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
147 $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
148 $tmpGlobals['wgHooks'] = $tmpHooks;
149 # add a namespace shadowing a interwiki link, to test
150 # proper precedence when resolving links. (bug 51680)
151 $tmpGlobals['wgExtraNamespaces'] = array( 100 => 'MemoryAlpha' );
152
153 $tmpGlobals['wgLocalInterwikis'] = array( 'local', 'mi' );
154 # "extra language links"
155 # see https://gerrit.wikimedia.org/r/111390
156 $tmpGlobals['wgExtraInterlanguageLinkPrefixes'] = array( 'mul' );
157
158 // DjVu support
159 $this->djVuSupport = new DjVuSupport();
160 // Tidy support
161 $this->tidySupport = new TidySupport();
162 // We always set 'wgUseTidy' to false when parsing, but certain
163 // test-running modes still use tidy if available, so ensure
164 // that the tidy-related options are all set to their defaults.
165 $tmpGlobals['wgUseTidy'] = false;
166 $tmpGlobals['wgAlwaysUseTidy'] = false;
167 $tmpGlobals['wgDebugTidy'] = false;
168 $tmpGlobals['wgTidyConf'] = $IP . '/includes/tidy.conf';
169 $tmpGlobals['wgTidyOpts'] = '';
170 $tmpGlobals['wgTidyInternal'] = $this->tidySupport->isInternal();
171
172 $this->setMwGlobals( $tmpGlobals );
173
174 $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
175 $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
176
177 $wgNamespaceAliases['Image'] = NS_FILE;
178 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
179
180 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
181 $wgContLang->resetNamespaces(); # reset namespace cache
182 }
183
184 protected function tearDown() {
185 global $wgNamespaceAliases, $wgContLang;
186
187 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
188 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
189
190 // Restore backends
191 RepoGroup::destroySingleton();
192 FileBackendGroup::destroySingleton();
193
194 // Remove temporary pages from the link cache
195 LinkCache::singleton()->clear();
196
197 // Restore message cache (temporary pages and $wgUseDatabaseMessages)
198 MessageCache::destroyInstance();
199
200 parent::tearDown();
201
202 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
203 $wgContLang->resetNamespaces(); # reset namespace cache
204 }
205
206 public static function tearDownAfterClass() {
207 ParserTest::tearDownInterwikis();
208 parent::tearDownAfterClass();
209 }
210
211 function addDBData() {
212 $this->tablesUsed[] = 'site_stats';
213 # disabled for performance
214 #$this->tablesUsed[] = 'image';
215
216 # Update certain things in site_stats
217 $this->db->insert( 'site_stats',
218 array( 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ),
219 __METHOD__
220 );
221
222 $user = User::newFromId( 0 );
223 LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
224
225 # Upload DB table entries for files.
226 # We will upload the actual files later. Note that if anything causes LocalFile::load()
227 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
228 # member to false and storing it in cache.
229 # note that the size/width/height/bits/etc of the file
230 # are actually set by inspecting the file itself; the arguments
231 # to recordUpload2 have no effect. That said, we try to make things
232 # match up so it is less confusing to readers of the code & tests.
233 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
234 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
235 $image->recordUpload2(
236 '', // archive name
237 'Upload of some lame file',
238 'Some lame file',
239 array(
240 'size' => 7881,
241 'width' => 1941,
242 'height' => 220,
243 'bits' => 8,
244 'media_type' => MEDIATYPE_BITMAP,
245 'mime' => 'image/jpeg',
246 'metadata' => serialize( array() ),
247 'sha1' => wfBaseConvert( '1', 16, 36, 31 ),
248 'fileExists' => true ),
249 $this->db->timestamp( '20010115123500' ), $user
250 );
251 }
252
253 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
254 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
255 $image->recordUpload2(
256 '', // archive name
257 'Upload of some lame thumbnail',
258 'Some lame thumbnail',
259 array(
260 'size' => 22589,
261 'width' => 135,
262 'height' => 135,
263 'bits' => 8,
264 'media_type' => MEDIATYPE_BITMAP,
265 'mime' => 'image/png',
266 'metadata' => serialize( array() ),
267 'sha1' => wfBaseConvert( '2', 16, 36, 31 ),
268 'fileExists' => true ),
269 $this->db->timestamp( '20130225203040' ), $user
270 );
271 }
272
273 # This image will be blacklisted in [[MediaWiki:Bad image list]]
274 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
275 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
276 $image->recordUpload2(
277 '', // archive name
278 'zomgnotcensored',
279 'Borderline image',
280 array(
281 'size' => 12345,
282 'width' => 320,
283 'height' => 240,
284 'bits' => 24,
285 'media_type' => MEDIATYPE_BITMAP,
286 'mime' => 'image/jpeg',
287 'metadata' => serialize( array() ),
288 'sha1' => wfBaseConvert( '3', 16, 36, 31 ),
289 'fileExists' => true ),
290 $this->db->timestamp( '20010115123500' ), $user
291 );
292 }
293 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
294 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
295 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', array(
296 'size' => 12345,
297 'width' => 240,
298 'height' => 180,
299 'bits' => 0,
300 'media_type' => MEDIATYPE_DRAWING,
301 'mime' => 'image/svg+xml',
302 'metadata' => serialize( array() ),
303 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
304 'fileExists' => true
305 ), $this->db->timestamp( '20010115123500' ), $user );
306 }
307
308 # A DjVu file
309 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
310 if ( !$this->db->selectField( 'image', '1', array( 'img_name' => $image->getName() ) ) ) {
311 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', array(
312 'size' => 3249,
313 'width' => 2480,
314 'height' => 3508,
315 'bits' => 0,
316 'media_type' => MEDIATYPE_BITMAP,
317 'mime' => 'image/vnd.djvu',
318 'metadata' => '<?xml version="1.0" ?>
319 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
320 <DjVuXML>
321 <HEAD></HEAD>
322 <BODY><OBJECT height="3508" width="2480">
323 <PARAM name="DPI" value="300" />
324 <PARAM name="GAMMA" value="2.2" />
325 </OBJECT>
326 <OBJECT height="3508" width="2480">
327 <PARAM name="DPI" value="300" />
328 <PARAM name="GAMMA" value="2.2" />
329 </OBJECT>
330 <OBJECT height="3508" width="2480">
331 <PARAM name="DPI" value="300" />
332 <PARAM name="GAMMA" value="2.2" />
333 </OBJECT>
334 <OBJECT height="3508" width="2480">
335 <PARAM name="DPI" value="300" />
336 <PARAM name="GAMMA" value="2.2" />
337 </OBJECT>
338 <OBJECT height="3508" width="2480">
339 <PARAM name="DPI" value="300" />
340 <PARAM name="GAMMA" value="2.2" />
341 </OBJECT>
342 </BODY>
343 </DjVuXML>',
344 'sha1' => wfBaseConvert( '', 16, 36, 31 ),
345 'fileExists' => true
346 ), $this->db->timestamp( '20140115123600' ), $user );
347 }
348 }
349
350 //ParserTest setup/teardown functions
351
352 /**
353 * Set up the global variables for a consistent environment for each test.
354 * Ideally this should replace the global configuration entirely.
355 */
356 protected function setupGlobals( $opts = array(), $config = '' ) {
357 global $wgFileBackends;
358 # Find out values for some special options.
359 $lang =
360 self::getOptionValue( 'language', $opts, 'en' );
361 $variant =
362 self::getOptionValue( 'variant', $opts, false );
363 $maxtoclevel =
364 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
365 $linkHolderBatchSize =
366 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
367
368 $uploadDir = $this->getUploadDir();
369 if ( $this->getCliArg( 'use-filebackend' ) ) {
370 if ( self::$backendToUse ) {
371 $backend = self::$backendToUse;
372 } else {
373 $name = $this->getCliArg( 'use-filebackend' );
374 $useConfig = array();
375 foreach ( $wgFileBackends as $conf ) {
376 if ( $conf['name'] == $name ) {
377 $useConfig = $conf;
378 }
379 }
380 $useConfig['name'] = 'local-backend'; // swap name
381 unset( $useConfig['lockManager'] );
382 unset( $useConfig['fileJournal'] );
383 $class = $useConfig['class'];
384 self::$backendToUse = new $class( $useConfig );
385 $backend = self::$backendToUse;
386 }
387 } else {
388 # Replace with a mock. We do not care about generating real
389 # files on the filesystem, just need to expose the file
390 # informations.
391 $backend = new MockFileBackend( array(
392 'name' => 'local-backend',
393 'wikiId' => wfWikiId()
394 ) );
395 }
396
397 $settings = array(
398 'wgLocalFileRepo' => array(
399 'class' => 'LocalRepo',
400 'name' => 'local',
401 'url' => 'http://example.com/images',
402 'hashLevels' => 2,
403 'transformVia404' => false,
404 'backend' => $backend
405 ),
406 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
407 'wgLanguageCode' => $lang,
408 'wgDBprefix' => $this->db->getType() != 'oracle' ? 'unittest_' : 'ut_',
409 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
410 'wgNamespacesWithSubpages' => array( NS_MAIN => isset( $opts['subpage'] ) ),
411 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
412 'wgThumbLimits' => array( self::getOptionValue( 'thumbsize', $opts, 180 ) ),
413 'wgMaxTocLevel' => $maxtoclevel,
414 'wgUseTeX' => isset( $opts['math'] ) || isset( $opts['texvc'] ),
415 'wgMathDirectory' => $uploadDir . '/math',
416 'wgDefaultLanguageVariant' => $variant,
417 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
418 );
419
420 if ( $config ) {
421 $configLines = explode( "\n", $config );
422
423 foreach ( $configLines as $line ) {
424 list( $var, $value ) = explode( '=', $line, 2 );
425
426 $settings[$var] = eval( "return $value;" ); //???
427 }
428 }
429
430 $this->savedGlobals = array();
431
432 /** @since 1.20 */
433 wfRunHooks( 'ParserTestGlobals', array( &$settings ) );
434
435 $langObj = Language::factory( $lang );
436 $settings['wgContLang'] = $langObj;
437 $settings['wgLang'] = $langObj;
438
439 $context = new RequestContext();
440 $settings['wgOut'] = $context->getOutput();
441 $settings['wgUser'] = $context->getUser();
442 $settings['wgRequest'] = $context->getRequest();
443
444 // We (re)set $wgThumbLimits to a single-element array above.
445 $context->getUser()->setOption( 'thumbsize', 0 );
446
447 foreach ( $settings as $var => $val ) {
448 if ( array_key_exists( $var, $GLOBALS ) ) {
449 $this->savedGlobals[$var] = $GLOBALS[$var];
450 }
451
452 $GLOBALS[$var] = $val;
453 }
454
455 MagicWord::clearCache();
456
457 # The entries saved into RepoGroup cache with previous globals will be wrong.
458 RepoGroup::destroySingleton();
459 FileBackendGroup::destroySingleton();
460
461 # Create dummy files in storage
462 $this->setupUploads();
463
464 # Publish the articles after we have the final language set
465 $this->publishTestArticles();
466
467 MessageCache::destroyInstance();
468
469 return $context;
470 }
471
472 /**
473 * Get an FS upload directory (only applies to FSFileBackend)
474 *
475 * @return string The directory
476 */
477 protected function getUploadDir() {
478 if ( $this->keepUploads ) {
479 $dir = wfTempDir() . '/mwParser-images';
480
481 if ( is_dir( $dir ) ) {
482 return $dir;
483 }
484 } else {
485 $dir = wfTempDir() . "/mwParser-" . mt_rand() . "-images";
486 }
487
488 // wfDebug( "Creating upload directory $dir\n" );
489 if ( file_exists( $dir ) ) {
490 wfDebug( "Already exists!\n" );
491
492 return $dir;
493 }
494
495 return $dir;
496 }
497
498 /**
499 * Create a dummy uploads directory which will contain a couple
500 * of files in order to pass existence tests.
501 *
502 * @return string The directory
503 */
504 protected function setupUploads() {
505 global $IP;
506
507 $base = $this->getBaseDir();
508 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
509 $backend->prepare( array( 'dir' => "$base/local-public/3/3a" ) );
510 $backend->store( array(
511 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
512 'dst' => "$base/local-public/3/3a/Foobar.jpg"
513 ) );
514 $backend->prepare( array( 'dir' => "$base/local-public/e/ea" ) );
515 $backend->store( array(
516 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
517 'dst' => "$base/local-public/e/ea/Thumb.png"
518 ) );
519 $backend->prepare( array( 'dir' => "$base/local-public/0/09" ) );
520 $backend->store( array(
521 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
522 'dst' => "$base/local-public/0/09/Bad.jpg"
523 ) );
524 $backend->prepare( array( 'dir' => "$base/local-public/5/5f" ) );
525 $backend->store( array(
526 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
527 'dst' => "$base/local-public/5/5f/LoremIpsum.djvu"
528 ) );
529
530 // No helpful SVG file to copy, so make one ourselves
531 $data = '<?xml version="1.0" encoding="utf-8"?>' .
532 '<svg xmlns="http://www.w3.org/2000/svg"' .
533 ' version="1.1" width="240" height="180"/>';
534
535 $backend->prepare( array( 'dir' => "$base/local-public/f/ff" ) );
536 $backend->quickCreate( array(
537 'content' => $data, 'dst' => "$base/local-public/f/ff/Foobar.svg"
538 ) );
539 }
540
541 /**
542 * Restore default values and perform any necessary clean-up
543 * after each test runs.
544 */
545 protected function teardownGlobals() {
546 $this->teardownUploads();
547
548 foreach ( $this->savedGlobals as $var => $val ) {
549 $GLOBALS[$var] = $val;
550 }
551 }
552
553 /**
554 * Remove the dummy uploads directory
555 */
556 private function teardownUploads() {
557 if ( $this->keepUploads ) {
558 return;
559 }
560
561 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
562 if ( $backend instanceof MockFileBackend ) {
563 # In memory backend, so dont bother cleaning them up.
564 return;
565 }
566
567 $base = $this->getBaseDir();
568 // delete the files first, then the dirs.
569 self::deleteFiles(
570 array(
571 "$base/local-public/3/3a/Foobar.jpg",
572 "$base/local-thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
573 "$base/local-thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
574 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
575 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
576 "$base/local-thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
577 "$base/local-thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
578 "$base/local-thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
579 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
580 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
581 "$base/local-thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
582 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
583 "$base/local-thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
584 "$base/local-thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
585 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
586 "$base/local-thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
587 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
588 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
589 "$base/local-thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
590 "$base/local-thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
591 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
592 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
593 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
594 "$base/local-thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
595 "$base/local-thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
596 "$base/local-thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
597 "$base/local-thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
598 "$base/local-thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
599 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
600 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
601 "$base/local-thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
602 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
603
604 "$base/local-public/e/ea/Thumb.png",
605
606 "$base/local-public/0/09/Bad.jpg",
607
608 "$base/local-public/5/5f/LoremIpsum.djvu",
609 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg",
610 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg",
611 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg",
612
613 "$base/local-public/f/ff/Foobar.svg",
614 "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
615 "$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
616 "$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
617 "$base/local-thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
618 "$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
619 "$base/local-thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
620 "$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
621 "$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
622 "$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
623
624 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
625 )
626 );
627 }
628
629 /**
630 * Delete the specified files, if they exist.
631 * @param array $files Full paths to files to delete.
632 */
633 private static function deleteFiles( $files ) {
634 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
635 foreach ( $files as $file ) {
636 $backend->delete( array( 'src' => $file ), array( 'force' => 1 ) );
637 }
638 foreach ( $files as $file ) {
639 $tmp = $file;
640 while ( $tmp = FileBackend::parentStoragePath( $tmp ) ) {
641 if ( !$backend->clean( array( 'dir' => $tmp ) )->isOK() ) {
642 break;
643 }
644 }
645 }
646 }
647
648 protected function getBaseDir() {
649 return 'mwstore://local-backend';
650 }
651
652 public function parserTestProvider() {
653 if ( $this->file === false ) {
654 global $wgParserTestFiles;
655 $this->file = $wgParserTestFiles[0];
656 }
657
658 return new TestFileIterator( $this->file, $this );
659 }
660
661 /**
662 * Set the file from whose tests will be run by this instance
663 * @param string $filename
664 */
665 public function setParserTestFile( $filename ) {
666 $this->file = $filename;
667 }
668
669 /**
670 * @group medium
671 * @dataProvider parserTestProvider
672 * @param string $desc
673 * @param string $input
674 * @param string $result
675 * @param array $opts
676 * @param array $config
677 */
678 public function testParserTest( $desc, $input, $result, $opts, $config ) {
679 if ( $this->regex != '' && !preg_match( '/' . $this->regex . '/', $desc ) ) {
680 $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
681 //$this->markTestSkipped( 'Filtered out by the user' );
682 return;
683 }
684
685 if ( !$this->isWikitextNS( NS_MAIN ) ) {
686 // parser tests frequently assume that the main namespace contains wikitext.
687 // @todo When setting up pages, force the content model. Only skip if
688 // $wgtContentModelUseDB is false.
689 $this->markTestSkipped( "Main namespace does not support wikitext,"
690 . "skipping parser test: $desc" );
691 }
692
693 wfDebug( "Running parser test: $desc\n" );
694
695 $opts = $this->parseOptions( $opts );
696 $context = $this->setupGlobals( $opts, $config );
697
698 $user = $context->getUser();
699 $options = ParserOptions::newFromContext( $context );
700
701 if ( isset( $opts['title'] ) ) {
702 $titleText = $opts['title'];
703 } else {
704 $titleText = 'Parser test';
705 }
706
707 $local = isset( $opts['local'] );
708 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
709 $parser = $this->getParser( $preprocessor );
710
711 $title = Title::newFromText( $titleText );
712
713 # Parser test requiring math. Make sure texvc is executable
714 # or just skip such tests.
715 if ( isset( $opts['math'] ) || isset( $opts['texvc'] ) ) {
716 global $wgTexvc;
717
718 if ( !isset( $wgTexvc ) ) {
719 $this->markTestSkipped( "SKIPPED: \$wgTexvc is not set" );
720 } elseif ( !is_executable( $wgTexvc ) ) {
721 $this->markTestSkipped( "SKIPPED: texvc binary does not exist"
722 . " or is not executable.\n"
723 . "Current configuration is:\n\$wgTexvc = '$wgTexvc'" );
724 }
725 }
726 if ( isset( $opts['djvu'] ) ) {
727 if ( !$this->djVuSupport->isEnabled() ) {
728 $this->markTestSkipped( "SKIPPED: djvu binaries do not exist or are not executable.\n" );
729 }
730 }
731
732 if ( isset( $opts['pst'] ) ) {
733 $out = $parser->preSaveTransform( $input, $title, $user, $options );
734 } elseif ( isset( $opts['msg'] ) ) {
735 $out = $parser->transformMsg( $input, $options, $title );
736 } elseif ( isset( $opts['section'] ) ) {
737 $section = $opts['section'];
738 $out = $parser->getSection( $input, $section );
739 } elseif ( isset( $opts['replace'] ) ) {
740 $section = $opts['replace'][0];
741 $replace = $opts['replace'][1];
742 $out = $parser->replaceSection( $input, $section, $replace );
743 } elseif ( isset( $opts['comment'] ) ) {
744 $out = Linker::formatComment( $input, $title, $local );
745 } elseif ( isset( $opts['preload'] ) ) {
746 $out = $parser->getPreloadText( $input, $title, $options );
747 } else {
748 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
749 $output->setTOCEnabled( !isset( $opts['notoc'] ) );
750 $out = $output->getText();
751 if ( isset( $opts['tidy'] ) ) {
752 if ( !$this->tidySupport->isEnabled() ) {
753 $this->markTestSkipped( "SKIPPED: tidy extension is not installed.\n" );
754 } else {
755 $out = MWTidy::tidy( $out );
756 $out = preg_replace( '/\s+$/', '', $out);
757 }
758 }
759
760 if ( isset( $opts['showtitle'] ) ) {
761 if ( $output->getTitleText() ) {
762 $title = $output->getTitleText();
763 }
764
765 $out = "$title\n$out";
766 }
767
768 if ( isset( $opts['ill'] ) ) {
769 $out = implode( ' ', $output->getLanguageLinks() );
770 } elseif ( isset( $opts['cat'] ) ) {
771 $outputPage = $context->getOutput();
772 $outputPage->addCategoryLinks( $output->getCategories() );
773 $cats = $outputPage->getCategoryLinks();
774
775 if ( isset( $cats['normal'] ) ) {
776 $out = implode( ' ', $cats['normal'] );
777 } else {
778 $out = '';
779 }
780 }
781 $parser->mPreprocessor = null;
782 }
783
784 $this->teardownGlobals();
785
786 $this->assertEquals( $result, $out, $desc );
787 }
788
789 /**
790 * Run a fuzz test series
791 * Draw input from a set of test files
792 *
793 * @todo fixme Needs some work to not eat memory until the world explodes
794 *
795 * @group ParserFuzz
796 */
797 public function testFuzzTests() {
798 global $wgParserTestFiles;
799
800 $files = $wgParserTestFiles;
801
802 if ( $this->getCliArg( 'file' ) ) {
803 $files = array( $this->getCliArg( 'file' ) );
804 }
805
806 $dict = $this->getFuzzInput( $files );
807 $dictSize = strlen( $dict );
808 $logMaxLength = log( $this->maxFuzzTestLength );
809
810 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
811
812 $user = new User;
813 $opts = ParserOptions::newFromUser( $user );
814 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
815
816 $id = 1;
817
818 while ( true ) {
819
820 // Generate test input
821 mt_srand( ++$this->fuzzSeed );
822 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
823 $input = '';
824
825 while ( strlen( $input ) < $totalLength ) {
826 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
827 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
828 $offset = mt_rand( 0, $dictSize - $hairLength );
829 $input .= substr( $dict, $offset, $hairLength );
830 }
831
832 $this->setupGlobals();
833 $parser = $this->getParser();
834
835 // Run the test
836 try {
837 $parser->parse( $input, $title, $opts );
838 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
839 } catch ( Exception $exception ) {
840 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
841
842 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\n" .
843 "Input: $input_dump\n\nError: {$exception->getMessage()}\n\n" .
844 "Backtrace: {$exception->getTraceAsString()}" );
845 }
846
847 $this->teardownGlobals();
848 $parser->__destruct();
849
850 if ( $id % 100 == 0 ) {
851 $usage = intval( memory_get_usage( true ) / $this->memoryLimit / 1048576 * 100 );
852 //echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
853 if ( $usage > 90 ) {
854 $ret = "Out of memory:\n";
855 $memStats = $this->getMemoryBreakdown();
856
857 foreach ( $memStats as $name => $usage ) {
858 $ret .= "$name: $usage\n";
859 }
860
861 throw new MWException( $ret );
862 }
863 }
864
865 $id++;
866 }
867 }
868
869 //Various getter functions
870
871 /**
872 * Get an input dictionary from a set of parser test files
873 * @param array $filenames
874 */
875 function getFuzzInput( $filenames ) {
876 $dict = '';
877
878 foreach ( $filenames as $filename ) {
879 $contents = file_get_contents( $filename );
880 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
881
882 foreach ( $matches[1] as $match ) {
883 $dict .= $match . "\n";
884 }
885 }
886
887 return $dict;
888 }
889
890 /**
891 * Get a memory usage breakdown
892 */
893 function getMemoryBreakdown() {
894 $memStats = array();
895
896 foreach ( $GLOBALS as $name => $value ) {
897 $memStats['$' . $name] = strlen( serialize( $value ) );
898 }
899
900 $classes = get_declared_classes();
901
902 foreach ( $classes as $class ) {
903 $rc = new ReflectionClass( $class );
904 $props = $rc->getStaticProperties();
905 $memStats[$class] = strlen( serialize( $props ) );
906 $methods = $rc->getMethods();
907
908 foreach ( $methods as $method ) {
909 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
910 }
911 }
912
913 $functions = get_defined_functions();
914
915 foreach ( $functions['user'] as $function ) {
916 $rf = new ReflectionFunction( $function );
917 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
918 }
919
920 asort( $memStats );
921
922 return $memStats;
923 }
924
925 /**
926 * Get a Parser object
927 * @param Preprocessor $preprocessor
928 */
929 function getParser( $preprocessor = null ) {
930 global $wgParserConf;
931
932 $class = $wgParserConf['class'];
933 $parser = new $class( array( 'preprocessorClass' => $preprocessor ) + $wgParserConf );
934
935 wfRunHooks( 'ParserTestParser', array( &$parser ) );
936
937 return $parser;
938 }
939
940 //Various action functions
941
942 public function addArticle( $name, $text, $line ) {
943 self::$articles[$name] = array( $text, $line );
944 }
945
946 public function publishTestArticles() {
947 if ( empty( self::$articles ) ) {
948 return;
949 }
950
951 foreach ( self::$articles as $name => $info ) {
952 list( $text, $line ) = $info;
953 ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
954 }
955 }
956
957 /**
958 * Steal a callback function from the primary parser, save it for
959 * application to our scary parser. If the hook is not installed,
960 * abort processing of this file.
961 *
962 * @param string $name
963 * @return bool True if tag hook is present
964 */
965 public function requireHook( $name ) {
966 global $wgParser;
967 $wgParser->firstCallInit(); // make sure hooks are loaded.
968 return isset( $wgParser->mTagHooks[$name] );
969 }
970
971 public function requireFunctionHook( $name ) {
972 global $wgParser;
973 $wgParser->firstCallInit(); // make sure hooks are loaded.
974 return isset( $wgParser->mFunctionHooks[$name] );
975 }
976
977 public function requireTransparentHook( $name ) {
978 global $wgParser;
979 $wgParser->firstCallInit(); // make sure hooks are loaded.
980 return isset( $wgParser->mTransparentTagHooks[$name] );
981 }
982
983 //Various "cleanup" functions
984
985 /**
986 * Remove last character if it is a newline
987 * @param string $s
988 */
989 public function removeEndingNewline( $s ) {
990 if ( substr( $s, -1 ) === "\n" ) {
991 return substr( $s, 0, -1 );
992 } else {
993 return $s;
994 }
995 }
996
997 //Test options parser functions
998
999 protected function parseOptions( $instring ) {
1000 $opts = array();
1001 // foo
1002 // foo=bar
1003 // foo="bar baz"
1004 // foo=[[bar baz]]
1005 // foo=bar,"baz quux"
1006 $regex = '/\b
1007 ([\w-]+) # Key
1008 \b
1009 (?:\s*
1010 = # First sub-value
1011 \s*
1012 (
1013 "
1014 [^"]* # Quoted val
1015 "
1016 |
1017 \[\[
1018 [^]]* # Link target
1019 \]\]
1020 |
1021 [\w-]+ # Plain word
1022 )
1023 (?:\s*
1024 , # Sub-vals 1..N
1025 \s*
1026 (
1027 "[^"]*" # Quoted val
1028 |
1029 \[\[[^]]*\]\] # Link target
1030 |
1031 [\w-]+ # Plain word
1032 )
1033 )*
1034 )?
1035 /x';
1036
1037 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
1038 foreach ( $matches as $bits ) {
1039 array_shift( $bits );
1040 $key = strtolower( array_shift( $bits ) );
1041 if ( count( $bits ) == 0 ) {
1042 $opts[$key] = true;
1043 } elseif ( count( $bits ) == 1 ) {
1044 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
1045 } else {
1046 // Array!
1047 $opts[$key] = array_map( array( $this, 'cleanupOption' ), $bits );
1048 }
1049 }
1050 }
1051
1052 return $opts;
1053 }
1054
1055 protected function cleanupOption( $opt ) {
1056 if ( substr( $opt, 0, 1 ) == '"' ) {
1057 return substr( $opt, 1, -1 );
1058 }
1059
1060 if ( substr( $opt, 0, 2 ) == '[[' ) {
1061 return substr( $opt, 2, -2 );
1062 }
1063
1064 return $opt;
1065 }
1066
1067 /**
1068 * Use a regex to find out the value of an option
1069 * @param string $key Name of option val to retrieve
1070 * @param array $opts Options array to look in
1071 * @param mixed $default Default value returned if not found
1072 */
1073 protected static function getOptionValue( $key, $opts, $default ) {
1074 $key = strtolower( $key );
1075
1076 if ( isset( $opts[$key] ) ) {
1077 return $opts[$key];
1078 } else {
1079 return $default;
1080 }
1081 }
1082 }