3 * Although marked as a stub, can work independently.
11 class NewParserTest
extends MediaWikiTestCase
{
12 static protected $articles = []; // Array of test articles defined by the tests
13 /* The data provider is run on a different instance than the test, so it must be static
14 * When running tests from several files, all tests will see all articles.
16 static protected $backendToUse;
18 public $keepUploads = false;
19 public $runDisabled = false;
20 public $runParsoid = false;
22 public $showProgress = true;
23 public $savedWeirdGlobals = [];
24 public $savedGlobals = [];
26 public $functionHooks = [];
27 public $transparentHooks = [];
30 public $maxFuzzTestLength = 300;
32 public $memoryLimit = 50;
43 protected $file = false;
45 public static function setUpBeforeClass() {
46 // Inject ParserTest well-known interwikis
47 ParserTest
::setupInterwikis();
50 protected function setUp() {
51 global $wgNamespaceAliases, $wgContLang;
56 // Setup CLI arguments
57 if ( $this->getCliArg( 'regex' ) ) {
58 $this->regex
= $this->getCliArg( 'regex' );
64 $this->keepUploads
= $this->getCliArg( 'keep-uploads' );
68 $tmpGlobals['wgLanguageCode'] = 'en';
69 $tmpGlobals['wgContLang'] = Language
::factory( 'en' );
70 $tmpGlobals['wgSitename'] = 'MediaWiki';
71 $tmpGlobals['wgServer'] = 'http://example.org';
72 $tmpGlobals['wgServerName'] = 'example.org';
73 $tmpGlobals['wgScriptPath'] = '';
74 $tmpGlobals['wgScript'] = '/index.php';
75 $tmpGlobals['wgResourceBasePath'] = '';
76 $tmpGlobals['wgStylePath'] = '/skins';
77 $tmpGlobals['wgExtensionAssetsPath'] = '/extensions';
78 $tmpGlobals['wgArticlePath'] = '/wiki/$1';
79 $tmpGlobals['wgActionPaths'] = [];
80 $tmpGlobals['wgVariantArticlePath'] = false;
81 $tmpGlobals['wgEnableUploads'] = true;
82 $tmpGlobals['wgUploadNavigationUrl'] = false;
83 $tmpGlobals['wgThumbnailScriptPath'] = false;
84 $tmpGlobals['wgLocalFileRepo'] = [
85 'class' => 'LocalRepo',
87 'url' => 'http://example.com/images',
89 'transformVia404' => false,
90 'backend' => 'local-backend'
92 $tmpGlobals['wgForeignFileRepos'] = [];
93 $tmpGlobals['wgDefaultExternalStore'] = [];
94 $tmpGlobals['wgParserCacheType'] = CACHE_NONE
;
95 $tmpGlobals['wgCapitalLinks'] = true;
96 $tmpGlobals['wgNoFollowLinks'] = true;
97 $tmpGlobals['wgNoFollowDomainExceptions'] = [];
98 $tmpGlobals['wgExternalLinkTarget'] = false;
99 $tmpGlobals['wgThumbnailScriptPath'] = false;
100 $tmpGlobals['wgUseImageResize'] = true;
101 $tmpGlobals['wgAllowExternalImages'] = true;
102 $tmpGlobals['wgRawHtml'] = false;
103 $tmpGlobals['wgWellFormedXml'] = true;
104 $tmpGlobals['wgExperimentalHtmlIds'] = false;
105 $tmpGlobals['wgAdaptiveMessageCache'] = true;
106 $tmpGlobals['wgUseDatabaseMessages'] = true;
107 $tmpGlobals['wgLocaltimezone'] = 'UTC';
108 $tmpGlobals['wgGroupPermissions'] = [
110 'createaccount' => true,
113 'createpage' => true,
114 'createtalk' => true,
116 $tmpGlobals['wgNamespaceProtection'] = [ NS_MEDIAWIKI
=> 'editinterface' ];
118 $tmpGlobals['wgParser'] = new StubObject(
119 'wgParser', $GLOBALS['wgParserConf']['class'],
120 [ $GLOBALS['wgParserConf'] ] );
122 $tmpGlobals['wgFileExtensions'][] = 'svg';
123 $tmpGlobals['wgSVGConverter'] = 'rsvg';
124 $tmpGlobals['wgSVGConverters']['rsvg'] =
125 '$path/rsvg-convert -w $width -h $height -o $output $input';
127 if ( $GLOBALS['wgStyleDirectory'] === false ) {
128 $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
131 # Replace all media handlers with a mock. We do not need to generate
132 # actual thumbnails to do parser testing, we only care about receiving
133 # a ThumbnailImage properly initialized.
134 global $wgMediaHandlers;
135 foreach ( $wgMediaHandlers as $type => $handler ) {
136 $tmpGlobals['wgMediaHandlers'][$type] = 'MockBitmapHandler';
138 // Vector images have to be handled slightly differently
139 $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
141 // DjVu images have to be handled slightly differently
142 $tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
144 // Ogg video/audio increasingly more differently
145 $tmpGlobals['wgMediaHandlers']['application/ogg'] = 'MockOggHandler';
147 $tmpHooks = $wgHooks;
148 $tmpHooks['ParserTestParser'][] = 'ParserTestParserHook::setup';
149 $tmpHooks['ParserGetVariableValueTs'][] = 'ParserTest::getFakeTimestamp';
150 $tmpGlobals['wgHooks'] = $tmpHooks;
151 # add a namespace shadowing a interwiki link, to test
152 # proper precedence when resolving links. (bug 51680)
153 $tmpGlobals['wgExtraNamespaces'] = [
154 100 => 'MemoryAlpha',
155 101 => 'MemoryAlpha_talk'
158 $tmpGlobals['wgLocalInterwikis'] = [ 'local', 'mi' ];
159 # "extra language links"
160 # see https://gerrit.wikimedia.org/r/111390
161 $tmpGlobals['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
164 $this->djVuSupport
= new DjVuSupport();
166 $this->tidySupport
= new TidySupport();
167 $tmpGlobals['wgTidyConfig'] = null;
168 $tmpGlobals['wgUseTidy'] = false;
169 $tmpGlobals['wgDebugTidy'] = false;
170 $tmpGlobals['wgTidyConf'] = $IP . '/includes/tidy/tidy.conf';
171 $tmpGlobals['wgTidyOpts'] = '';
172 $tmpGlobals['wgTidyInternal'] = $this->tidySupport
->isInternal();
174 $this->setMwGlobals( $tmpGlobals );
176 $this->savedWeirdGlobals
['image_alias'] = $wgNamespaceAliases['Image'];
177 $this->savedWeirdGlobals
['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
179 $wgNamespaceAliases['Image'] = NS_FILE
;
180 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK
;
182 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
183 $wgContLang->resetNamespaces(); # reset namespace cache
184 ParserTest
::resetTitleServices();
187 protected function tearDown() {
188 global $wgNamespaceAliases, $wgContLang;
190 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals
['image_alias'];
191 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals
['image_talk_alias'];
193 MWTidy
::destroySingleton();
196 RepoGroup
::destroySingleton();
197 FileBackendGroup
::destroySingleton();
199 // Remove temporary pages from the link cache
200 LinkCache
::singleton()->clear();
202 // Restore message cache (temporary pages and $wgUseDatabaseMessages)
203 MessageCache
::destroyInstance();
207 MWNamespace
::getCanonicalNamespaces( true ); # reset namespace cache
208 $wgContLang->resetNamespaces(); # reset namespace cache
211 public static function tearDownAfterClass() {
212 ParserTest
::tearDownInterwikis();
213 parent
::tearDownAfterClass();
216 function addDBDataOnce() {
217 # disabled for performance
218 # $this->tablesUsed[] = 'image';
220 # Update certain things in site_stats
221 $this->db
->insert( 'site_stats',
222 [ 'ss_row_id' => 1, 'ss_images' => 2, 'ss_good_articles' => 1 ],
227 $user = User
::newFromId( 0 );
228 LinkCache
::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
230 # Upload DB table entries for files.
231 # We will upload the actual files later. Note that if anything causes LocalFile::load()
232 # to be triggered before then, it will break via maybeUpgrade() setting the fileExists
233 # member to false and storing it in cache.
234 # note that the size/width/height/bits/etc of the file
235 # are actually set by inspecting the file itself; the arguments
236 # to recordUpload2 have no effect. That said, we try to make things
237 # match up so it is less confusing to readers of the code & tests.
238 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.jpg' ) );
239 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
240 $image->recordUpload2(
242 'Upload of some lame file',
249 'media_type' => MEDIATYPE_BITMAP
,
250 'mime' => 'image/jpeg',
251 'metadata' => serialize( [] ),
252 'sha1' => Wikimedia\base_convert
( '1', 16, 36, 31 ),
253 'fileExists' => true ],
254 $this->db
->timestamp( '20010115123500' ), $user
258 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Thumb.png' ) );
259 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
260 $image->recordUpload2(
262 'Upload of some lame thumbnail',
263 'Some lame thumbnail',
269 'media_type' => MEDIATYPE_BITMAP
,
270 'mime' => 'image/png',
271 'metadata' => serialize( [] ),
272 'sha1' => Wikimedia\base_convert
( '2', 16, 36, 31 ),
273 'fileExists' => true ],
274 $this->db
->timestamp( '20130225203040' ), $user
278 # This image will be blacklisted in [[MediaWiki:Bad image list]]
279 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Bad.jpg' ) );
280 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
281 $image->recordUpload2(
290 'media_type' => MEDIATYPE_BITMAP
,
291 'mime' => 'image/jpeg',
292 'metadata' => serialize( [] ),
293 'sha1' => Wikimedia\base_convert
( '3', 16, 36, 31 ),
294 'fileExists' => true ],
295 $this->db
->timestamp( '20010115123500' ), $user
298 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Foobar.svg' ) );
299 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
300 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
305 'media_type' => MEDIATYPE_DRAWING
,
306 'mime' => 'image/svg+xml',
307 'metadata' => serialize( [] ),
308 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
310 ], $this->db
->timestamp( '20010115123500' ), $user );
313 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'Video.ogv' ) );
314 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
315 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
320 'media_type' => MEDIATYPE_VIDEO
,
321 'mime' => 'application/ogg',
322 'metadata' => serialize( [] ),
323 'sha1' => Wikimedia\base_convert
( '', 16, 36, 32 ),
325 ], $this->db
->timestamp( '20010115123500' ), $user );
330 $image = wfLocalFile( Title
::makeTitle( NS_FILE
, 'LoremIpsum.djvu' ) );
331 if ( !$this->db
->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
332 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
337 'media_type' => MEDIATYPE_BITMAP
,
338 'mime' => 'image/vnd.djvu',
339 'metadata' => '<?xml version="1.0" ?>
340 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
343 <BODY><OBJECT height="3508" width="2480">
344 <PARAM name="DPI" value="300" />
345 <PARAM name="GAMMA" value="2.2" />
347 <OBJECT height="3508" width="2480">
348 <PARAM name="DPI" value="300" />
349 <PARAM name="GAMMA" value="2.2" />
351 <OBJECT height="3508" width="2480">
352 <PARAM name="DPI" value="300" />
353 <PARAM name="GAMMA" value="2.2" />
355 <OBJECT height="3508" width="2480">
356 <PARAM name="DPI" value="300" />
357 <PARAM name="GAMMA" value="2.2" />
359 <OBJECT height="3508" width="2480">
360 <PARAM name="DPI" value="300" />
361 <PARAM name="GAMMA" value="2.2" />
365 'sha1' => Wikimedia\base_convert
( '', 16, 36, 31 ),
367 ], $this->db
->timestamp( '20140115123600' ), $user );
371 // ParserTest setup/teardown functions
374 * Set up the global variables for a consistent environment for each test.
375 * Ideally this should replace the global configuration entirely.
377 * @param string $config
378 * @return RequestContext
380 protected function setupGlobals( $opts = [], $config = '' ) {
381 global $wgFileBackends;
382 # Find out values for some special options.
384 self
::getOptionValue( 'language', $opts, 'en' );
386 self
::getOptionValue( 'variant', $opts, false );
388 self
::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
389 $linkHolderBatchSize =
390 self
::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
392 $uploadDir = $this->getUploadDir();
393 if ( $this->getCliArg( 'use-filebackend' ) ) {
394 if ( self
::$backendToUse ) {
395 $backend = self
::$backendToUse;
397 $name = $this->getCliArg( 'use-filebackend' );
399 foreach ( $wgFileBackends as $conf ) {
400 if ( $conf['name'] == $name ) {
404 $useConfig['name'] = 'local-backend'; // swap name
405 unset( $useConfig['lockManager'] );
406 unset( $useConfig['fileJournal'] );
407 $class = $useConfig['class'];
408 self
::$backendToUse = new $class( $useConfig );
409 $backend = self
::$backendToUse;
412 # Replace with a mock. We do not care about generating real
413 # files on the filesystem, just need to expose the file
415 $backend = new MockFileBackend( [
416 'name' => 'local-backend',
417 'wikiId' => wfWikiID()
422 'wgLocalFileRepo' => [
423 'class' => 'LocalRepo',
425 'url' => 'http://example.com/images',
427 'transformVia404' => false,
428 'backend' => $backend
430 'wgEnableUploads' => self
::getOptionValue( 'wgEnableUploads', $opts, true ),
431 'wgLanguageCode' => $lang,
432 'wgDBprefix' => $this->db
->getType() != 'oracle' ?
'unittest_' : 'ut_',
433 'wgRawHtml' => self
::getOptionValue( 'wgRawHtml', $opts, false ),
434 'wgNamespacesWithSubpages' => [ NS_MAIN
=> isset( $opts['subpage'] ) ],
435 'wgAllowExternalImages' => self
::getOptionValue( 'wgAllowExternalImages', $opts, true ),
436 'wgThumbLimits' => [ self
::getOptionValue( 'thumbsize', $opts, 180 ) ],
437 'wgMaxTocLevel' => $maxtoclevel,
438 'wgUseTeX' => isset( $opts['math'] ) ||
isset( $opts['texvc'] ),
439 'wgWellFormedXml' => true,
440 'wgMathDirectory' => $uploadDir . '/math',
441 'wgDefaultLanguageVariant' => $variant,
442 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
443 'wgUseTidy' => isset( $opts['tidy'] ),
447 $configLines = explode( "\n", $config );
449 foreach ( $configLines as $line ) {
450 list( $var, $value ) = explode( '=', $line, 2 );
452 $settings[$var] = eval( "return $value;" ); // ???
456 $this->savedGlobals
= [];
459 Hooks
::run( 'ParserTestGlobals', [ &$settings ] );
461 $langObj = Language
::factory( $lang );
462 $settings['wgContLang'] = $langObj;
463 $settings['wgLang'] = $langObj;
465 $context = new RequestContext();
466 $settings['wgOut'] = $context->getOutput();
467 $settings['wgUser'] = $context->getUser();
468 $settings['wgRequest'] = $context->getRequest();
470 // We (re)set $wgThumbLimits to a single-element array above.
471 $context->getUser()->setOption( 'thumbsize', 0 );
473 foreach ( $settings as $var => $val ) {
474 if ( array_key_exists( $var, $GLOBALS ) ) {
475 $this->savedGlobals
[$var] = $GLOBALS[$var];
478 $GLOBALS[$var] = $val;
481 MWTidy
::destroySingleton();
482 MagicWord
::clearCache();
484 # The entries saved into RepoGroup cache with previous globals will be wrong.
485 RepoGroup
::destroySingleton();
486 FileBackendGroup
::destroySingleton();
488 # Create dummy files in storage
489 $this->setupUploads();
491 # Publish the articles after we have the final language set
492 $this->publishTestArticles();
494 MessageCache
::destroyInstance();
500 * Get an FS upload directory (only applies to FSFileBackend)
502 * @return string The directory
504 protected function getUploadDir() {
505 if ( $this->keepUploads
) {
506 // Don't use getNewTempDirectory() as this is meant to persist
507 $dir = wfTempDir() . '/mwParser-images';
509 if ( is_dir( $dir ) ) {
513 $dir = $this->getNewTempDirectory();
516 if ( file_exists( $dir ) ) {
517 wfDebug( "Already exists!\n" );
526 * Create a dummy uploads directory which will contain a couple
527 * of files in order to pass existence tests.
529 * @return string The directory
531 protected function setupUploads() {
534 $base = $this->getBaseDir();
535 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
536 $backend->prepare( [ 'dir' => "$base/local-public/3/3a" ] );
538 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
539 'dst' => "$base/local-public/3/3a/Foobar.jpg"
541 $backend->prepare( [ 'dir' => "$base/local-public/e/ea" ] );
543 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
544 'dst' => "$base/local-public/e/ea/Thumb.png"
546 $backend->prepare( [ 'dir' => "$base/local-public/0/09" ] );
548 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
549 'dst' => "$base/local-public/0/09/Bad.jpg"
551 $backend->prepare( [ 'dir' => "$base/local-public/5/5f" ] );
553 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
554 'dst' => "$base/local-public/5/5f/LoremIpsum.djvu"
557 // No helpful SVG file to copy, so make one ourselves
558 $data = '<?xml version="1.0" encoding="utf-8"?>' .
559 '<svg xmlns="http://www.w3.org/2000/svg"' .
560 ' version="1.1" width="240" height="180"/>';
562 $backend->prepare( [ 'dir' => "$base/local-public/f/ff" ] );
563 $backend->quickCreate( [
564 'content' => $data, 'dst' => "$base/local-public/f/ff/Foobar.svg"
569 * Restore default values and perform any necessary clean-up
570 * after each test runs.
572 protected function teardownGlobals() {
573 $this->teardownUploads();
575 foreach ( $this->savedGlobals
as $var => $val ) {
576 $GLOBALS[$var] = $val;
581 * Remove the dummy uploads directory
583 private function teardownUploads() {
584 if ( $this->keepUploads
) {
588 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
589 if ( $backend instanceof MockFileBackend
) {
590 # In memory backend, so dont bother cleaning them up.
594 $base = $this->getBaseDir();
595 // delete the files first, then the dirs.
598 "$base/local-public/3/3a/Foobar.jpg",
599 "$base/local-thumb/3/3a/Foobar.jpg/1000px-Foobar.jpg",
600 "$base/local-thumb/3/3a/Foobar.jpg/100px-Foobar.jpg",
601 "$base/local-thumb/3/3a/Foobar.jpg/120px-Foobar.jpg",
602 "$base/local-thumb/3/3a/Foobar.jpg/1280px-Foobar.jpg",
603 "$base/local-thumb/3/3a/Foobar.jpg/137px-Foobar.jpg",
604 "$base/local-thumb/3/3a/Foobar.jpg/1500px-Foobar.jpg",
605 "$base/local-thumb/3/3a/Foobar.jpg/177px-Foobar.jpg",
606 "$base/local-thumb/3/3a/Foobar.jpg/180px-Foobar.jpg",
607 "$base/local-thumb/3/3a/Foobar.jpg/200px-Foobar.jpg",
608 "$base/local-thumb/3/3a/Foobar.jpg/206px-Foobar.jpg",
609 "$base/local-thumb/3/3a/Foobar.jpg/20px-Foobar.jpg",
610 "$base/local-thumb/3/3a/Foobar.jpg/220px-Foobar.jpg",
611 "$base/local-thumb/3/3a/Foobar.jpg/265px-Foobar.jpg",
612 "$base/local-thumb/3/3a/Foobar.jpg/270px-Foobar.jpg",
613 "$base/local-thumb/3/3a/Foobar.jpg/274px-Foobar.jpg",
614 "$base/local-thumb/3/3a/Foobar.jpg/300px-Foobar.jpg",
615 "$base/local-thumb/3/3a/Foobar.jpg/30px-Foobar.jpg",
616 "$base/local-thumb/3/3a/Foobar.jpg/330px-Foobar.jpg",
617 "$base/local-thumb/3/3a/Foobar.jpg/353px-Foobar.jpg",
618 "$base/local-thumb/3/3a/Foobar.jpg/360px-Foobar.jpg",
619 "$base/local-thumb/3/3a/Foobar.jpg/400px-Foobar.jpg",
620 "$base/local-thumb/3/3a/Foobar.jpg/40px-Foobar.jpg",
621 "$base/local-thumb/3/3a/Foobar.jpg/440px-Foobar.jpg",
622 "$base/local-thumb/3/3a/Foobar.jpg/442px-Foobar.jpg",
623 "$base/local-thumb/3/3a/Foobar.jpg/450px-Foobar.jpg",
624 "$base/local-thumb/3/3a/Foobar.jpg/50px-Foobar.jpg",
625 "$base/local-thumb/3/3a/Foobar.jpg/600px-Foobar.jpg",
626 "$base/local-thumb/3/3a/Foobar.jpg/640px-Foobar.jpg",
627 "$base/local-thumb/3/3a/Foobar.jpg/70px-Foobar.jpg",
628 "$base/local-thumb/3/3a/Foobar.jpg/75px-Foobar.jpg",
629 "$base/local-thumb/3/3a/Foobar.jpg/960px-Foobar.jpg",
631 "$base/local-public/e/ea/Thumb.png",
633 "$base/local-public/0/09/Bad.jpg",
635 "$base/local-public/5/5f/LoremIpsum.djvu",
636 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-2480px-LoremIpsum.djvu.jpg",
637 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-3720px-LoremIpsum.djvu.jpg",
638 "$base/local-thumb/5/5f/LoremIpsum.djvu/page2-4960px-LoremIpsum.djvu.jpg",
640 "$base/local-public/f/ff/Foobar.svg",
641 "$base/local-thumb/f/ff/Foobar.svg/180px-Foobar.svg.png",
642 "$base/local-thumb/f/ff/Foobar.svg/2000px-Foobar.svg.png",
643 "$base/local-thumb/f/ff/Foobar.svg/270px-Foobar.svg.png",
644 "$base/local-thumb/f/ff/Foobar.svg/3000px-Foobar.svg.png",
645 "$base/local-thumb/f/ff/Foobar.svg/360px-Foobar.svg.png",
646 "$base/local-thumb/f/ff/Foobar.svg/4000px-Foobar.svg.png",
647 "$base/local-thumb/f/ff/Foobar.svg/langde-180px-Foobar.svg.png",
648 "$base/local-thumb/f/ff/Foobar.svg/langde-270px-Foobar.svg.png",
649 "$base/local-thumb/f/ff/Foobar.svg/langde-360px-Foobar.svg.png",
651 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
657 * Delete the specified files, if they exist.
658 * @param array $files Full paths to files to delete.
660 private static function deleteFiles( $files ) {
661 $backend = RepoGroup
::singleton()->getLocalRepo()->getBackend();
662 foreach ( $files as $file ) {
663 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
665 foreach ( $files as $file ) {
666 $tmp = FileBackend
::parentStoragePath( $file );
668 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
671 $tmp = FileBackend
::parentStoragePath( $tmp );
676 protected function getBaseDir() {
677 return 'mwstore://local-backend';
680 public function parserTestProvider() {
681 if ( $this->file
=== false ) {
682 global $wgParserTestFiles;
683 $this->file
= $wgParserTestFiles[0];
686 return new TestFileDataProvider( $this->file
, $this );
690 * Set the file from whose tests will be run by this instance
691 * @param string $filename
693 public function setParserTestFile( $filename ) {
694 $this->file
= $filename;
700 * @dataProvider parserTestProvider
701 * @param string $desc
702 * @param string $input
703 * @param string $result
705 * @param array $config
707 public function testParserTest( $desc, $input, $result, $opts, $config ) {
708 if ( $this->regex
!= '' && !preg_match( '/' . $this->regex
. '/', $desc ) ) {
709 $this->assertTrue( true ); // XXX: don't flood output with "test made no assertions"
710 // $this->markTestSkipped( 'Filtered out by the user' );
714 if ( !$this->isWikitextNS( NS_MAIN
) ) {
715 // parser tests frequently assume that the main namespace contains wikitext.
716 // @todo When setting up pages, force the content model. Only skip if
717 // $wgtContentModelUseDB is false.
718 $this->markTestSkipped( "Main namespace does not support wikitext,"
719 . "skipping parser test: $desc" );
722 wfDebug( "Running parser test: $desc\n" );
724 $opts = $this->parseOptions( $opts );
725 $context = $this->setupGlobals( $opts, $config );
727 $user = $context->getUser();
728 $options = ParserOptions
::newFromContext( $context );
730 if ( isset( $opts['title'] ) ) {
731 $titleText = $opts['title'];
733 $titleText = 'Parser test';
736 $local = isset( $opts['local'] );
737 $preprocessor = isset( $opts['preprocessor'] ) ?
$opts['preprocessor'] : null;
738 $parser = $this->getParser( $preprocessor );
740 $title = Title
::newFromText( $titleText );
742 # Parser test requiring math. Make sure texvc is executable
743 # or just skip such tests.
744 if ( isset( $opts['math'] ) ||
isset( $opts['texvc'] ) ) {
747 if ( !isset( $wgTexvc ) ) {
748 $this->markTestSkipped( "SKIPPED: \$wgTexvc is not set" );
749 } elseif ( !is_executable( $wgTexvc ) ) {
750 $this->markTestSkipped( "SKIPPED: texvc binary does not exist"
751 . " or is not executable.\n"
752 . "Current configuration is:\n\$wgTexvc = '$wgTexvc'" );
756 if ( isset( $opts['djvu'] ) ) {
757 if ( !$this->djVuSupport
->isEnabled() ) {
758 $this->markTestSkipped( "SKIPPED: djvu binaries do not exist or are not executable.\n" );
762 if ( isset( $opts['tidy'] ) ) {
763 if ( !$this->tidySupport
->isEnabled() ) {
764 $this->markTestSkipped( "SKIPPED: tidy extension is not installed.\n" );
766 $options->setTidy( true );
770 if ( isset( $opts['pst'] ) ) {
771 $out = $parser->preSaveTransform( $input, $title, $user, $options );
772 } elseif ( isset( $opts['msg'] ) ) {
773 $out = $parser->transformMsg( $input, $options, $title );
774 } elseif ( isset( $opts['section'] ) ) {
775 $section = $opts['section'];
776 $out = $parser->getSection( $input, $section );
777 } elseif ( isset( $opts['replace'] ) ) {
778 $section = $opts['replace'][0];
779 $replace = $opts['replace'][1];
780 $out = $parser->replaceSection( $input, $section, $replace );
781 } elseif ( isset( $opts['comment'] ) ) {
782 $out = Linker
::formatComment( $input, $title, $local );
783 } elseif ( isset( $opts['preload'] ) ) {
784 $out = $parser->getPreloadText( $input, $title, $options );
786 $output = $parser->parse( $input, $title, $options, true, true, 1337 );
787 $output->setTOCEnabled( !isset( $opts['notoc'] ) );
788 $out = $output->getText();
789 if ( isset( $opts['tidy'] ) ) {
790 $out = preg_replace( '/\s+$/', '', $out );
793 if ( isset( $opts['showtitle'] ) ) {
794 if ( $output->getTitleText() ) {
795 $title = $output->getTitleText();
798 $out = "$title\n$out";
801 if ( isset( $opts['showindicators'] ) ) {
803 foreach ( $output->getIndicators() as $id => $content ) {
804 $indicators .= "$id=$content\n";
806 $out = $indicators . $out;
809 if ( isset( $opts['ill'] ) ) {
810 $out = implode( ' ', $output->getLanguageLinks() );
811 } elseif ( isset( $opts['cat'] ) ) {
812 $outputPage = $context->getOutput();
813 $outputPage->addCategoryLinks( $output->getCategories() );
814 $cats = $outputPage->getCategoryLinks();
816 if ( isset( $cats['normal'] ) ) {
817 $out = implode( ' ', $cats['normal'] );
822 $parser->mPreprocessor
= null;
825 $this->teardownGlobals();
827 $this->assertEquals( $result, $out, $desc );
831 * Run a fuzz test series
832 * Draw input from a set of test files
834 * @todo fixme Needs some work to not eat memory until the world explodes
838 public function testFuzzTests() {
839 global $wgParserTestFiles;
841 $files = $wgParserTestFiles;
843 if ( $this->getCliArg( 'file' ) ) {
844 $files = [ $this->getCliArg( 'file' ) ];
847 $dict = $this->getFuzzInput( $files );
848 $dictSize = strlen( $dict );
849 $logMaxLength = log( $this->maxFuzzTestLength
);
851 ini_set( 'memory_limit', $this->memoryLimit
* 1048576 );
854 $opts = ParserOptions
::newFromUser( $user );
855 $title = Title
::makeTitle( NS_MAIN
, 'Parser_test' );
861 // Generate test input
862 mt_srand( ++
$this->fuzzSeed
);
863 $totalLength = mt_rand( 1, $this->maxFuzzTestLength
);
866 while ( strlen( $input ) < $totalLength ) {
867 $logHairLength = mt_rand( 0, 1000000 ) / 1000000 * $logMaxLength;
868 $hairLength = min( intval( exp( $logHairLength ) ), $dictSize );
869 $offset = mt_rand( 0, $dictSize - $hairLength );
870 $input .= substr( $dict, $offset, $hairLength );
873 $this->setupGlobals();
874 $parser = $this->getParser();
878 $parser->parse( $input, $title, $opts );
879 $this->assertTrue( true, "Test $id, fuzz seed {$this->fuzzSeed}" );
880 } catch ( Exception
$exception ) {
881 $input_dump = sprintf( "string(%d) \"%s\"\n", strlen( $input ), $input );
883 $this->assertTrue( false, "Test $id, fuzz seed {$this->fuzzSeed}. \n\n" .
884 "Input: $input_dump\n\nError: {$exception->getMessage()}\n\n" .
885 "Backtrace: {$exception->getTraceAsString()}" );
888 $this->teardownGlobals();
889 $parser->__destruct();
891 if ( $id %
100 == 0 ) {
892 $usage = intval( memory_get_usage( true ) / $this->memoryLimit
/ 1048576 * 100 );
893 // echo "{$this->fuzzSeed}: $numSuccess/$numTotal (mem: $usage%)\n";
895 $ret = "Out of memory:\n";
896 $memStats = $this->getMemoryBreakdown();
898 foreach ( $memStats as $name => $usage ) {
899 $ret .= "$name: $usage\n";
902 throw new MWException( $ret );
910 // Various getter functions
913 * Get an input dictionary from a set of parser test files
914 * @param array $filenames
917 function getFuzzInput( $filenames ) {
920 foreach ( $filenames as $filename ) {
921 $contents = file_get_contents( $filename );
922 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
924 foreach ( $matches[1] as $match ) {
925 $dict .= $match . "\n";
933 * Get a memory usage breakdown
936 function getMemoryBreakdown() {
939 foreach ( $GLOBALS as $name => $value ) {
940 $memStats['$' . $name] = strlen( serialize( $value ) );
943 $classes = get_declared_classes();
945 foreach ( $classes as $class ) {
946 $rc = new ReflectionClass( $class );
947 $props = $rc->getStaticProperties();
948 $memStats[$class] = strlen( serialize( $props ) );
949 $methods = $rc->getMethods();
951 foreach ( $methods as $method ) {
952 $memStats[$class] +
= strlen( serialize( $method->getStaticVariables() ) );
956 $functions = get_defined_functions();
958 foreach ( $functions['user'] as $function ) {
959 $rf = new ReflectionFunction( $function );
960 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
969 * Get a Parser object
970 * @param Preprocessor $preprocessor
973 function getParser( $preprocessor = null ) {
974 global $wgParserConf;
976 $class = $wgParserConf['class'];
977 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] +
$wgParserConf );
979 Hooks
::run( 'ParserTestParser', [ &$parser ] );
984 // Various action functions
986 public function addArticle( $name, $text, $line ) {
987 self
::$articles[$name] = [ $text, $line ];
990 public function publishTestArticles() {
991 if ( empty( self
::$articles ) ) {
995 foreach ( self
::$articles as $name => $info ) {
996 list( $text, $line ) = $info;
997 ParserTest
::addArticle( $name, $text, $line, 'ignoreduplicate' );
1002 * Steal a callback function from the primary parser, save it for
1003 * application to our scary parser. If the hook is not installed,
1004 * abort processing of this file.
1006 * @param string $name
1007 * @return bool True if tag hook is present
1009 public function requireHook( $name ) {
1011 $wgParser->firstCallInit(); // make sure hooks are loaded.
1012 return isset( $wgParser->mTagHooks
[$name] );
1015 public function requireFunctionHook( $name ) {
1017 $wgParser->firstCallInit(); // make sure hooks are loaded.
1018 return isset( $wgParser->mFunctionHooks
[$name] );
1021 public function requireTransparentHook( $name ) {
1023 $wgParser->firstCallInit(); // make sure hooks are loaded.
1024 return isset( $wgParser->mTransparentTagHooks
[$name] );
1027 // Various "cleanup" functions
1030 * Remove last character if it is a newline
1034 public function removeEndingNewline( $s ) {
1035 if ( substr( $s, -1 ) === "\n" ) {
1036 return substr( $s, 0, -1 );
1042 // Test options parser functions
1044 protected function parseOptions( $instring ) {
1050 // foo=bar,"baz quux"
1072 "[^"]*" # Quoted val
1074 \[\[[^]]*\]\] # Link target
1082 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER
) ) {
1083 foreach ( $matches as $bits ) {
1084 array_shift( $bits );
1085 $key = strtolower( array_shift( $bits ) );
1086 if ( count( $bits ) == 0 ) {
1088 } elseif ( count( $bits ) == 1 ) {
1089 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
1092 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $bits );
1100 protected function cleanupOption( $opt ) {
1101 if ( substr( $opt, 0, 1 ) == '"' ) {
1102 return substr( $opt, 1, -1 );
1105 if ( substr( $opt, 0, 2 ) == '[[' ) {
1106 return substr( $opt, 2, -2 );
1113 * Use a regex to find out the value of an option
1114 * @param string $key Name of option val to retrieve
1115 * @param array $opts Options array to look in
1116 * @param mixed $default Default value returned if not found
1119 protected static function getOptionValue( $key, $opts, $default ) {
1120 $key = strtolower( $key );
1122 if ( isset( $opts[$key] ) ) {