Merge "Make User::isBot() also check the "bot" right for sanity"
[lhc/web/wiklou.git] / tests / phpunit / includes / parser / NewParserTest.php
1 <?php
2 /**
3 * Although marked as a stub, can work independently.
4 *
5 * @group Database
6 * @group Parser
7 * @group Stub
8 *
9 * @todo covers tags
10 */
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.
15 */
16 static protected $backendToUse;
17
18 public $keepUploads = false;
19 public $runDisabled = false;
20 public $runParsoid = false;
21 public $regex = '';
22 public $showProgress = true;
23 public $savedWeirdGlobals = [];
24 public $savedGlobals = [];
25 public $hooks = [];
26 public $functionHooks = [];
27 public $transparentHooks = [];
28
29 // Fuzz test
30 public $maxFuzzTestLength = 300;
31 public $fuzzSeed = 0;
32 public $memoryLimit = 50;
33
34 /**
35 * @var DjVuSupport
36 */
37 private $djVuSupport;
38 /**
39 * @var TidySupport
40 */
41 private $tidySupport;
42
43 protected $file = false;
44
45 public static function setUpBeforeClass() {
46 // Inject ParserTest well-known interwikis
47 ParserTest::setupInterwikis();
48 }
49
50 protected function setUp() {
51 global $wgNamespaceAliases, $wgContLang;
52 global $wgHooks, $IP;
53
54 parent::setUp();
55
56 // Setup CLI arguments
57 if ( $this->getCliArg( 'regex' ) ) {
58 $this->regex = $this->getCliArg( 'regex' );
59 } else {
60 # Matches anything
61 $this->regex = '';
62 }
63
64 $this->keepUploads = $this->getCliArg( 'keep-uploads' );
65
66 $tmpGlobals = [];
67
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',
86 'name' => 'local',
87 'url' => 'http://example.com/images',
88 'hashLevels' => 2,
89 'transformVia404' => false,
90 'backend' => 'local-backend'
91 ];
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'] = [
109 '*' => [
110 'createaccount' => true,
111 'read' => true,
112 'edit' => true,
113 'createpage' => true,
114 'createtalk' => true,
115 ] ];
116 $tmpGlobals['wgNamespaceProtection'] = [ NS_MEDIAWIKI => 'editinterface' ];
117
118 $tmpGlobals['wgParser'] = new StubObject(
119 'wgParser', $GLOBALS['wgParserConf']['class'],
120 [ $GLOBALS['wgParserConf'] ] );
121
122 $tmpGlobals['wgFileExtensions'][] = 'svg';
123 $tmpGlobals['wgSVGConverter'] = 'rsvg';
124 $tmpGlobals['wgSVGConverters']['rsvg'] =
125 '$path/rsvg-convert -w $width -h $height -o $output $input';
126
127 if ( $GLOBALS['wgStyleDirectory'] === false ) {
128 $tmpGlobals['wgStyleDirectory'] = "$IP/skins";
129 }
130
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';
137 }
138 // Vector images have to be handled slightly differently
139 $tmpGlobals['wgMediaHandlers']['image/svg+xml'] = 'MockSvgHandler';
140
141 // DjVu images have to be handled slightly differently
142 $tmpGlobals['wgMediaHandlers']['image/vnd.djvu'] = 'MockDjVuHandler';
143
144 // Ogg video/audio increasingly more differently
145 $tmpGlobals['wgMediaHandlers']['application/ogg'] = 'MockOggHandler';
146
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'
156 ];
157
158 $tmpGlobals['wgLocalInterwikis'] = [ 'local', 'mi' ];
159 # "extra language links"
160 # see https://gerrit.wikimedia.org/r/111390
161 $tmpGlobals['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
162
163 // DjVu support
164 $this->djVuSupport = new DjVuSupport();
165 // Tidy support
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();
173
174 $this->setMwGlobals( $tmpGlobals );
175
176 $this->savedWeirdGlobals['image_alias'] = $wgNamespaceAliases['Image'];
177 $this->savedWeirdGlobals['image_talk_alias'] = $wgNamespaceAliases['Image_talk'];
178
179 $wgNamespaceAliases['Image'] = NS_FILE;
180 $wgNamespaceAliases['Image_talk'] = NS_FILE_TALK;
181
182 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
183 $wgContLang->resetNamespaces(); # reset namespace cache
184 ParserTest::resetTitleServices();
185 }
186
187 protected function tearDown() {
188 global $wgNamespaceAliases, $wgContLang;
189
190 $wgNamespaceAliases['Image'] = $this->savedWeirdGlobals['image_alias'];
191 $wgNamespaceAliases['Image_talk'] = $this->savedWeirdGlobals['image_talk_alias'];
192
193 MWTidy::destroySingleton();
194
195 // Restore backends
196 RepoGroup::destroySingleton();
197 FileBackendGroup::destroySingleton();
198
199 // Remove temporary pages from the link cache
200 LinkCache::singleton()->clear();
201
202 // Restore message cache (temporary pages and $wgUseDatabaseMessages)
203 MessageCache::destroyInstance();
204
205 parent::tearDown();
206
207 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
208 $wgContLang->resetNamespaces(); # reset namespace cache
209 }
210
211 public static function tearDownAfterClass() {
212 ParserTest::tearDownInterwikis();
213 parent::tearDownAfterClass();
214 }
215
216 function addDBDataOnce() {
217 # disabled for performance
218 # $this->tablesUsed[] = 'image';
219
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 ],
223 __METHOD__,
224 [ 'IGNORE' ]
225 );
226
227 $user = User::newFromId( 0 );
228 LinkCache::singleton()->clear(); # Avoids the odd failure at creating the nullRevision
229
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(
241 '', // archive name
242 'Upload of some lame file',
243 'Some lame file',
244 [
245 'size' => 7881,
246 'width' => 1941,
247 'height' => 220,
248 'bits' => 8,
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
255 );
256 }
257
258 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
259 if ( !$this->db->selectField( 'image', '1', [ 'img_name' => $image->getName() ] ) ) {
260 $image->recordUpload2(
261 '', // archive name
262 'Upload of some lame thumbnail',
263 'Some lame thumbnail',
264 [
265 'size' => 22589,
266 'width' => 135,
267 'height' => 135,
268 'bits' => 8,
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
275 );
276 }
277
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(
282 '', // archive name
283 'zomgnotcensored',
284 'Borderline image',
285 [
286 'size' => 12345,
287 'width' => 320,
288 'height' => 240,
289 'bits' => 24,
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
296 );
297 }
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', [
301 'size' => 12345,
302 'width' => 240,
303 'height' => 180,
304 'bits' => 0,
305 'media_type' => MEDIATYPE_DRAWING,
306 'mime' => 'image/svg+xml',
307 'metadata' => serialize( [] ),
308 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
309 'fileExists' => true
310 ], $this->db->timestamp( '20010115123500' ), $user );
311 }
312
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', [
316 'size' => 12345,
317 'width' => 320,
318 'height' => 240,
319 'bits' => 0,
320 'media_type' => MEDIATYPE_VIDEO,
321 'mime' => 'application/ogg',
322 'metadata' => serialize( [] ),
323 'sha1' => Wikimedia\base_convert( '', 16, 36, 32 ),
324 'fileExists' => true
325 ], $this->db->timestamp( '20010115123500' ), $user );
326 }
327
328 # A DjVu file
329 # A DjVu file
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', [
333 'size' => 3249,
334 'width' => 2480,
335 'height' => 3508,
336 'bits' => 0,
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">
341 <DjVuXML>
342 <HEAD></HEAD>
343 <BODY><OBJECT height="3508" width="2480">
344 <PARAM name="DPI" value="300" />
345 <PARAM name="GAMMA" value="2.2" />
346 </OBJECT>
347 <OBJECT height="3508" width="2480">
348 <PARAM name="DPI" value="300" />
349 <PARAM name="GAMMA" value="2.2" />
350 </OBJECT>
351 <OBJECT height="3508" width="2480">
352 <PARAM name="DPI" value="300" />
353 <PARAM name="GAMMA" value="2.2" />
354 </OBJECT>
355 <OBJECT height="3508" width="2480">
356 <PARAM name="DPI" value="300" />
357 <PARAM name="GAMMA" value="2.2" />
358 </OBJECT>
359 <OBJECT height="3508" width="2480">
360 <PARAM name="DPI" value="300" />
361 <PARAM name="GAMMA" value="2.2" />
362 </OBJECT>
363 </BODY>
364 </DjVuXML>',
365 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
366 'fileExists' => true
367 ], $this->db->timestamp( '20140115123600' ), $user );
368 }
369 }
370
371 // ParserTest setup/teardown functions
372
373 /**
374 * Set up the global variables for a consistent environment for each test.
375 * Ideally this should replace the global configuration entirely.
376 * @param array $opts
377 * @param string $config
378 * @return RequestContext
379 */
380 protected function setupGlobals( $opts = [], $config = '' ) {
381 global $wgFileBackends;
382 # Find out values for some special options.
383 $lang =
384 self::getOptionValue( 'language', $opts, 'en' );
385 $variant =
386 self::getOptionValue( 'variant', $opts, false );
387 $maxtoclevel =
388 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
389 $linkHolderBatchSize =
390 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
391
392 $uploadDir = $this->getUploadDir();
393 if ( $this->getCliArg( 'use-filebackend' ) ) {
394 if ( self::$backendToUse ) {
395 $backend = self::$backendToUse;
396 } else {
397 $name = $this->getCliArg( 'use-filebackend' );
398 $useConfig = [];
399 foreach ( $wgFileBackends as $conf ) {
400 if ( $conf['name'] == $name ) {
401 $useConfig = $conf;
402 }
403 }
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;
410 }
411 } else {
412 # Replace with a mock. We do not care about generating real
413 # files on the filesystem, just need to expose the file
414 # informations.
415 $backend = new MockFileBackend( [
416 'name' => 'local-backend',
417 'wikiId' => wfWikiID()
418 ] );
419 }
420
421 $settings = [
422 'wgLocalFileRepo' => [
423 'class' => 'LocalRepo',
424 'name' => 'local',
425 'url' => 'http://example.com/images',
426 'hashLevels' => 2,
427 'transformVia404' => false,
428 'backend' => $backend
429 ],
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'] ),
444 ];
445
446 if ( $config ) {
447 $configLines = explode( "\n", $config );
448
449 foreach ( $configLines as $line ) {
450 list( $var, $value ) = explode( '=', $line, 2 );
451
452 $settings[$var] = eval( "return $value;" ); // ???
453 }
454 }
455
456 $this->savedGlobals = [];
457
458 /** @since 1.20 */
459 Hooks::run( 'ParserTestGlobals', [ &$settings ] );
460
461 $langObj = Language::factory( $lang );
462 $settings['wgContLang'] = $langObj;
463 $settings['wgLang'] = $langObj;
464
465 $context = new RequestContext();
466 $settings['wgOut'] = $context->getOutput();
467 $settings['wgUser'] = $context->getUser();
468 $settings['wgRequest'] = $context->getRequest();
469
470 // We (re)set $wgThumbLimits to a single-element array above.
471 $context->getUser()->setOption( 'thumbsize', 0 );
472
473 foreach ( $settings as $var => $val ) {
474 if ( array_key_exists( $var, $GLOBALS ) ) {
475 $this->savedGlobals[$var] = $GLOBALS[$var];
476 }
477
478 $GLOBALS[$var] = $val;
479 }
480
481 MWTidy::destroySingleton();
482 MagicWord::clearCache();
483
484 # The entries saved into RepoGroup cache with previous globals will be wrong.
485 RepoGroup::destroySingleton();
486 FileBackendGroup::destroySingleton();
487
488 # Create dummy files in storage
489 $this->setupUploads();
490
491 # Publish the articles after we have the final language set
492 $this->publishTestArticles();
493
494 MessageCache::destroyInstance();
495
496 return $context;
497 }
498
499 /**
500 * Get an FS upload directory (only applies to FSFileBackend)
501 *
502 * @return string The directory
503 */
504 protected function getUploadDir() {
505 if ( $this->keepUploads ) {
506 // Don't use getNewTempDirectory() as this is meant to persist
507 $dir = wfTempDir() . '/mwParser-images';
508
509 if ( is_dir( $dir ) ) {
510 return $dir;
511 }
512 } else {
513 $dir = $this->getNewTempDirectory();
514 }
515
516 if ( file_exists( $dir ) ) {
517 wfDebug( "Already exists!\n" );
518
519 return $dir;
520 }
521
522 return $dir;
523 }
524
525 /**
526 * Create a dummy uploads directory which will contain a couple
527 * of files in order to pass existence tests.
528 *
529 * @return string The directory
530 */
531 protected function setupUploads() {
532 global $IP;
533
534 $base = $this->getBaseDir();
535 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
536 $backend->prepare( [ 'dir' => "$base/local-public/3/3a" ] );
537 $backend->store( [
538 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
539 'dst' => "$base/local-public/3/3a/Foobar.jpg"
540 ] );
541 $backend->prepare( [ 'dir' => "$base/local-public/e/ea" ] );
542 $backend->store( [
543 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
544 'dst' => "$base/local-public/e/ea/Thumb.png"
545 ] );
546 $backend->prepare( [ 'dir' => "$base/local-public/0/09" ] );
547 $backend->store( [
548 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
549 'dst' => "$base/local-public/0/09/Bad.jpg"
550 ] );
551 $backend->prepare( [ 'dir' => "$base/local-public/5/5f" ] );
552 $backend->store( [
553 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
554 'dst' => "$base/local-public/5/5f/LoremIpsum.djvu"
555 ] );
556
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"/>';
561
562 $backend->prepare( [ 'dir' => "$base/local-public/f/ff" ] );
563 $backend->quickCreate( [
564 'content' => $data, 'dst' => "$base/local-public/f/ff/Foobar.svg"
565 ] );
566 }
567
568 /**
569 * Restore default values and perform any necessary clean-up
570 * after each test runs.
571 */
572 protected function teardownGlobals() {
573 $this->teardownUploads();
574
575 foreach ( $this->savedGlobals as $var => $val ) {
576 $GLOBALS[$var] = $val;
577 }
578 }
579
580 /**
581 * Remove the dummy uploads directory
582 */
583 private function teardownUploads() {
584 if ( $this->keepUploads ) {
585 return;
586 }
587
588 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
589 if ( $backend instanceof MockFileBackend ) {
590 # In memory backend, so dont bother cleaning them up.
591 return;
592 }
593
594 $base = $this->getBaseDir();
595 // delete the files first, then the dirs.
596 self::deleteFiles(
597 [
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",
630
631 "$base/local-public/e/ea/Thumb.png",
632
633 "$base/local-public/0/09/Bad.jpg",
634
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",
639
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",
650
651 "$base/local-public/math/f/a/5/fa50b8b616463173474302ca3e63586b.png",
652 ]
653 );
654 }
655
656 /**
657 * Delete the specified files, if they exist.
658 * @param array $files Full paths to files to delete.
659 */
660 private static function deleteFiles( $files ) {
661 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
662 foreach ( $files as $file ) {
663 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
664 }
665 foreach ( $files as $file ) {
666 $tmp = FileBackend::parentStoragePath( $file );
667 while ( $tmp ) {
668 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
669 break;
670 }
671 $tmp = FileBackend::parentStoragePath( $tmp );
672 }
673 }
674 }
675
676 protected function getBaseDir() {
677 return 'mwstore://local-backend';
678 }
679
680 public function parserTestProvider() {
681 if ( $this->file === false ) {
682 global $wgParserTestFiles;
683 $this->file = $wgParserTestFiles[0];
684 }
685
686 return new TestFileDataProvider( $this->file, $this );
687 }
688
689 /**
690 * Set the file from whose tests will be run by this instance
691 * @param string $filename
692 */
693 public function setParserTestFile( $filename ) {
694 $this->file = $filename;
695 }
696
697 /**
698 * @group medium
699 * @group ParserTests
700 * @dataProvider parserTestProvider
701 * @param string $desc
702 * @param string $input
703 * @param string $result
704 * @param array $opts
705 * @param array $config
706 */
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' );
711 return;
712 }
713
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" );
720 }
721
722 wfDebug( "Running parser test: $desc\n" );
723
724 $opts = $this->parseOptions( $opts );
725 $context = $this->setupGlobals( $opts, $config );
726
727 $user = $context->getUser();
728 $options = ParserOptions::newFromContext( $context );
729
730 if ( isset( $opts['title'] ) ) {
731 $titleText = $opts['title'];
732 } else {
733 $titleText = 'Parser test';
734 }
735
736 $local = isset( $opts['local'] );
737 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
738 $parser = $this->getParser( $preprocessor );
739
740 $title = Title::newFromText( $titleText );
741
742 # Parser test requiring math. Make sure texvc is executable
743 # or just skip such tests.
744 if ( isset( $opts['math'] ) || isset( $opts['texvc'] ) ) {
745 global $wgTexvc;
746
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'" );
753 }
754 }
755
756 if ( isset( $opts['djvu'] ) ) {
757 if ( !$this->djVuSupport->isEnabled() ) {
758 $this->markTestSkipped( "SKIPPED: djvu binaries do not exist or are not executable.\n" );
759 }
760 }
761
762 if ( isset( $opts['tidy'] ) ) {
763 if ( !$this->tidySupport->isEnabled() ) {
764 $this->markTestSkipped( "SKIPPED: tidy extension is not installed.\n" );
765 } else {
766 $options->setTidy( true );
767 }
768 }
769
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 );
785 } else {
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 );
791 }
792
793 if ( isset( $opts['showtitle'] ) ) {
794 if ( $output->getTitleText() ) {
795 $title = $output->getTitleText();
796 }
797
798 $out = "$title\n$out";
799 }
800
801 if ( isset( $opts['showindicators'] ) ) {
802 $indicators = '';
803 foreach ( $output->getIndicators() as $id => $content ) {
804 $indicators .= "$id=$content\n";
805 }
806 $out = $indicators . $out;
807 }
808
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();
815
816 if ( isset( $cats['normal'] ) ) {
817 $out = implode( ' ', $cats['normal'] );
818 } else {
819 $out = '';
820 }
821 }
822 $parser->mPreprocessor = null;
823 }
824
825 $this->teardownGlobals();
826
827 $this->assertEquals( $result, $out, $desc );
828 }
829
830 /**
831 * Run a fuzz test series
832 * Draw input from a set of test files
833 *
834 * @todo fixme Needs some work to not eat memory until the world explodes
835 *
836 * @group ParserFuzz
837 */
838 public function testFuzzTests() {
839 global $wgParserTestFiles;
840
841 $files = $wgParserTestFiles;
842
843 if ( $this->getCliArg( 'file' ) ) {
844 $files = [ $this->getCliArg( 'file' ) ];
845 }
846
847 $dict = $this->getFuzzInput( $files );
848 $dictSize = strlen( $dict );
849 $logMaxLength = log( $this->maxFuzzTestLength );
850
851 ini_set( 'memory_limit', $this->memoryLimit * 1048576 );
852
853 $user = new User;
854 $opts = ParserOptions::newFromUser( $user );
855 $title = Title::makeTitle( NS_MAIN, 'Parser_test' );
856
857 $id = 1;
858
859 while ( true ) {
860
861 // Generate test input
862 mt_srand( ++$this->fuzzSeed );
863 $totalLength = mt_rand( 1, $this->maxFuzzTestLength );
864 $input = '';
865
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 );
871 }
872
873 $this->setupGlobals();
874 $parser = $this->getParser();
875
876 // Run the test
877 try {
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 );
882
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()}" );
886 }
887
888 $this->teardownGlobals();
889 $parser->__destruct();
890
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";
894 if ( $usage > 90 ) {
895 $ret = "Out of memory:\n";
896 $memStats = $this->getMemoryBreakdown();
897
898 foreach ( $memStats as $name => $usage ) {
899 $ret .= "$name: $usage\n";
900 }
901
902 throw new MWException( $ret );
903 }
904 }
905
906 $id++;
907 }
908 }
909
910 // Various getter functions
911
912 /**
913 * Get an input dictionary from a set of parser test files
914 * @param array $filenames
915 * @return string
916 */
917 function getFuzzInput( $filenames ) {
918 $dict = '';
919
920 foreach ( $filenames as $filename ) {
921 $contents = file_get_contents( $filename );
922 preg_match_all( '/!!\s*input\n(.*?)\n!!\s*result/s', $contents, $matches );
923
924 foreach ( $matches[1] as $match ) {
925 $dict .= $match . "\n";
926 }
927 }
928
929 return $dict;
930 }
931
932 /**
933 * Get a memory usage breakdown
934 * @return array
935 */
936 function getMemoryBreakdown() {
937 $memStats = [];
938
939 foreach ( $GLOBALS as $name => $value ) {
940 $memStats['$' . $name] = strlen( serialize( $value ) );
941 }
942
943 $classes = get_declared_classes();
944
945 foreach ( $classes as $class ) {
946 $rc = new ReflectionClass( $class );
947 $props = $rc->getStaticProperties();
948 $memStats[$class] = strlen( serialize( $props ) );
949 $methods = $rc->getMethods();
950
951 foreach ( $methods as $method ) {
952 $memStats[$class] += strlen( serialize( $method->getStaticVariables() ) );
953 }
954 }
955
956 $functions = get_defined_functions();
957
958 foreach ( $functions['user'] as $function ) {
959 $rf = new ReflectionFunction( $function );
960 $memStats["$function()"] = strlen( serialize( $rf->getStaticVariables() ) );
961 }
962
963 asort( $memStats );
964
965 return $memStats;
966 }
967
968 /**
969 * Get a Parser object
970 * @param Preprocessor $preprocessor
971 * @return Parser
972 */
973 function getParser( $preprocessor = null ) {
974 global $wgParserConf;
975
976 $class = $wgParserConf['class'];
977 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] + $wgParserConf );
978
979 Hooks::run( 'ParserTestParser', [ &$parser ] );
980
981 return $parser;
982 }
983
984 // Various action functions
985
986 public function addArticle( $name, $text, $line ) {
987 self::$articles[$name] = [ $text, $line ];
988 }
989
990 public function publishTestArticles() {
991 if ( empty( self::$articles ) ) {
992 return;
993 }
994
995 foreach ( self::$articles as $name => $info ) {
996 list( $text, $line ) = $info;
997 ParserTest::addArticle( $name, $text, $line, 'ignoreduplicate' );
998 }
999 }
1000
1001 /**
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.
1005 *
1006 * @param string $name
1007 * @return bool True if tag hook is present
1008 */
1009 public function requireHook( $name ) {
1010 global $wgParser;
1011 $wgParser->firstCallInit(); // make sure hooks are loaded.
1012 return isset( $wgParser->mTagHooks[$name] );
1013 }
1014
1015 public function requireFunctionHook( $name ) {
1016 global $wgParser;
1017 $wgParser->firstCallInit(); // make sure hooks are loaded.
1018 return isset( $wgParser->mFunctionHooks[$name] );
1019 }
1020
1021 public function requireTransparentHook( $name ) {
1022 global $wgParser;
1023 $wgParser->firstCallInit(); // make sure hooks are loaded.
1024 return isset( $wgParser->mTransparentTagHooks[$name] );
1025 }
1026
1027 // Various "cleanup" functions
1028
1029 /**
1030 * Remove last character if it is a newline
1031 * @param string $s
1032 * @return string
1033 */
1034 public function removeEndingNewline( $s ) {
1035 if ( substr( $s, -1 ) === "\n" ) {
1036 return substr( $s, 0, -1 );
1037 } else {
1038 return $s;
1039 }
1040 }
1041
1042 // Test options parser functions
1043
1044 protected function parseOptions( $instring ) {
1045 $opts = [];
1046 // foo
1047 // foo=bar
1048 // foo="bar baz"
1049 // foo=[[bar baz]]
1050 // foo=bar,"baz quux"
1051 $regex = '/\b
1052 ([\w-]+) # Key
1053 \b
1054 (?:\s*
1055 = # First sub-value
1056 \s*
1057 (
1058 "
1059 [^"]* # Quoted val
1060 "
1061 |
1062 \[\[
1063 [^]]* # Link target
1064 \]\]
1065 |
1066 [\w-]+ # Plain word
1067 )
1068 (?:\s*
1069 , # Sub-vals 1..N
1070 \s*
1071 (
1072 "[^"]*" # Quoted val
1073 |
1074 \[\[[^]]*\]\] # Link target
1075 |
1076 [\w-]+ # Plain word
1077 )
1078 )*
1079 )?
1080 /x';
1081
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 ) {
1087 $opts[$key] = true;
1088 } elseif ( count( $bits ) == 1 ) {
1089 $opts[$key] = $this->cleanupOption( array_shift( $bits ) );
1090 } else {
1091 // Array!
1092 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $bits );
1093 }
1094 }
1095 }
1096
1097 return $opts;
1098 }
1099
1100 protected function cleanupOption( $opt ) {
1101 if ( substr( $opt, 0, 1 ) == '"' ) {
1102 return substr( $opt, 1, -1 );
1103 }
1104
1105 if ( substr( $opt, 0, 2 ) == '[[' ) {
1106 return substr( $opt, 2, -2 );
1107 }
1108
1109 return $opt;
1110 }
1111
1112 /**
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
1117 * @return mixed
1118 */
1119 protected static function getOptionValue( $key, $opts, $default ) {
1120 $key = strtolower( $key );
1121
1122 if ( isset( $opts[$key] ) ) {
1123 return $opts[$key];
1124 } else {
1125 return $default;
1126 }
1127 }
1128 }