Merge "Make infused and non-infused DateInputWidget consistent"
[lhc/web/wiklou.git] / tests / parser / ParserTestRunner.php
1 <?php
2 /**
3 * Generic backend for the MediaWiki parser test suite, used by both the
4 * standalone parserTests.php and the PHPUnit "parsertests" suite.
5 *
6 * Copyright © 2004, 2010 Brion Vibber <brion@pobox.com>
7 * https://www.mediawiki.org/
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @todo Make this more independent of the configuration (and if possible the database)
25 * @file
26 * @ingroup Testing
27 */
28 use MediaWiki\MediaWikiServices;
29 use Wikimedia\ScopedCallback;
30
31 /**
32 * @ingroup Testing
33 */
34 class ParserTestRunner {
35 /**
36 * @var bool $useTemporaryTables Use temporary tables for the temporary database
37 */
38 private $useTemporaryTables = true;
39
40 /**
41 * @var array $setupDone The status of each setup function
42 */
43 private $setupDone = [
44 'staticSetup' => false,
45 'perTestSetup' => false,
46 'setupDatabase' => false,
47 'setDatabase' => false,
48 'setupUploads' => false,
49 ];
50
51 /**
52 * Our connection to the database
53 * @var Database
54 */
55 private $db;
56
57 /**
58 * Database clone helper
59 * @var CloneDatabase
60 */
61 private $dbClone;
62
63 /**
64 * @var TidySupport
65 */
66 private $tidySupport;
67
68 /**
69 * @var TidyDriverBase
70 */
71 private $tidyDriver = null;
72
73 /**
74 * @var TestRecorder
75 */
76 private $recorder;
77
78 /**
79 * The upload directory, or null to not set up an upload directory
80 *
81 * @var string|null
82 */
83 private $uploadDir = null;
84
85 /**
86 * The name of the file backend to use, or null to use MockFileBackend.
87 * @var string|null
88 */
89 private $fileBackendName;
90
91 /**
92 * A complete regex for filtering tests.
93 * @var string
94 */
95 private $regex;
96
97 /**
98 * A list of normalization functions to apply to the expected and actual
99 * output.
100 * @var array
101 */
102 private $normalizationFunctions = [];
103
104 /**
105 * @param TestRecorder $recorder
106 * @param array $options
107 */
108 public function __construct( TestRecorder $recorder, $options = [] ) {
109 $this->recorder = $recorder;
110
111 if ( isset( $options['norm'] ) ) {
112 foreach ( $options['norm'] as $func ) {
113 if ( in_array( $func, [ 'removeTbody', 'trimWhitespace' ] ) ) {
114 $this->normalizationFunctions[] = $func;
115 } else {
116 $this->recorder->warning(
117 "Warning: unknown normalization option \"$func\"\n" );
118 }
119 }
120 }
121
122 if ( isset( $options['regex'] ) && $options['regex'] !== false ) {
123 $this->regex = $options['regex'];
124 } else {
125 # Matches anything
126 $this->regex = '//';
127 }
128
129 $this->keepUploads = !empty( $options['keep-uploads'] );
130
131 $this->fileBackendName = isset( $options['file-backend'] ) ?
132 $options['file-backend'] : false;
133
134 $this->runDisabled = !empty( $options['run-disabled'] );
135 $this->runParsoid = !empty( $options['run-parsoid'] );
136
137 $this->tidySupport = new TidySupport( !empty( $options['use-tidy-config'] ) );
138 if ( !$this->tidySupport->isEnabled() ) {
139 $this->recorder->warning(
140 "Warning: tidy is not installed, skipping some tests\n" );
141 }
142
143 if ( isset( $options['upload-dir'] ) ) {
144 $this->uploadDir = $options['upload-dir'];
145 }
146 }
147
148 public function getRecorder() {
149 return $this->recorder;
150 }
151
152 /**
153 * Do any setup which can be done once for all tests, independent of test
154 * options, except for database setup.
155 *
156 * Public setup functions in this class return a ScopedCallback object. When
157 * this object is destroyed by going out of scope, teardown of the
158 * corresponding test setup is performed.
159 *
160 * Teardown objects may be chained by passing a ScopedCallback from a
161 * previous setup stage as the $nextTeardown parameter. This enforces the
162 * convention that teardown actions are taken in reverse order to the
163 * corresponding setup actions. When $nextTeardown is specified, a
164 * ScopedCallback will be returned which first tears down the current
165 * setup stage, and then tears down the previous setup stage which was
166 * specified by $nextTeardown.
167 *
168 * @param ScopedCallback|null $nextTeardown
169 * @return ScopedCallback
170 */
171 public function staticSetup( $nextTeardown = null ) {
172 // A note on coding style:
173
174 // The general idea here is to keep setup code together with
175 // corresponding teardown code, in a fine-grained manner. We have two
176 // arrays: $setup and $teardown. The code snippets in the $setup array
177 // are executed at the end of the method, before it returns, and the
178 // code snippets in the $teardown array are executed in reverse order
179 // when the Wikimedia\ScopedCallback object is consumed.
180
181 // Because it is a common operation to save, set and restore global
182 // variables, we have an additional convention: when the array key of
183 // $setup is a string, the string is taken to be the name of the global
184 // variable, and the element value is taken to be the desired new value.
185
186 // It's acceptable to just do the setup immediately, instead of adding
187 // a closure to $setup, except when the setup action depends on global
188 // variable initialisation being done first. In this case, you have to
189 // append a closure to $setup after the global variable is appended.
190
191 // When you add to setup functions in this class, please keep associated
192 // setup and teardown actions together in the source code, and please
193 // add comments explaining why the setup action is necessary.
194
195 $setup = [];
196 $teardown = [];
197
198 $teardown[] = $this->markSetupDone( 'staticSetup' );
199
200 // Some settings which influence HTML output
201 $setup['wgSitename'] = 'MediaWiki';
202 $setup['wgServer'] = 'http://example.org';
203 $setup['wgServerName'] = 'example.org';
204 $setup['wgScriptPath'] = '';
205 $setup['wgScript'] = '/index.php';
206 $setup['wgResourceBasePath'] = '';
207 $setup['wgStylePath'] = '/skins';
208 $setup['wgExtensionAssetsPath'] = '/extensions';
209 $setup['wgArticlePath'] = '/wiki/$1';
210 $setup['wgActionPaths'] = [];
211 $setup['wgVariantArticlePath'] = false;
212 $setup['wgUploadNavigationUrl'] = false;
213 $setup['wgCapitalLinks'] = true;
214 $setup['wgNoFollowLinks'] = true;
215 $setup['wgNoFollowDomainExceptions'] = [ 'no-nofollow.org' ];
216 $setup['wgExternalLinkTarget'] = false;
217 $setup['wgExperimentalHtmlIds'] = false;
218 $setup['wgLocaltimezone'] = 'UTC';
219 $setup['wgHtml5'] = true;
220 $setup['wgDisableLangConversion'] = false;
221 $setup['wgDisableTitleConversion'] = false;
222
223 // "extra language links"
224 // see https://gerrit.wikimedia.org/r/111390
225 $setup['wgExtraInterlanguageLinkPrefixes'] = [ 'mul' ];
226
227 // All FileRepo changes should be done here by injecting services,
228 // there should be no need to change global variables.
229 RepoGroup::setSingleton( $this->createRepoGroup() );
230 $teardown[] = function () {
231 RepoGroup::destroySingleton();
232 };
233
234 // Set up null lock managers
235 $setup['wgLockManagers'] = [ [
236 'name' => 'fsLockManager',
237 'class' => 'NullLockManager',
238 ], [
239 'name' => 'nullLockManager',
240 'class' => 'NullLockManager',
241 ] ];
242 $reset = function() {
243 LockManagerGroup::destroySingletons();
244 };
245 $setup[] = $reset;
246 $teardown[] = $reset;
247
248 // This allows article insertion into the prefixed DB
249 $setup['wgDefaultExternalStore'] = false;
250
251 // This might slightly reduce memory usage
252 $setup['wgAdaptiveMessageCache'] = true;
253
254 // This is essential and overrides disabling of database messages in TestSetup
255 $setup['wgUseDatabaseMessages'] = true;
256 $reset = function () {
257 MessageCache::destroyInstance();
258 };
259 $setup[] = $reset;
260 $teardown[] = $reset;
261
262 // It's not necessary to actually convert any files
263 $setup['wgSVGConverter'] = 'null';
264 $setup['wgSVGConverters'] = [ 'null' => 'echo "1">$output' ];
265
266 // Fake constant timestamp
267 Hooks::register( 'ParserGetVariableValueTs', 'ParserTestRunner::getFakeTimestamp' );
268 $teardown[] = function () {
269 Hooks::clear( 'ParserGetVariableValueTs' );
270 };
271
272 $this->appendNamespaceSetup( $setup, $teardown );
273
274 // Set up interwikis and append teardown function
275 $teardown[] = $this->setupInterwikis();
276
277 // This affects title normalization in links. It invalidates
278 // MediaWikiTitleCodec objects.
279 $setup['wgLocalInterwikis'] = [ 'local', 'mi' ];
280 $reset = function () {
281 $this->resetTitleServices();
282 };
283 $setup[] = $reset;
284 $teardown[] = $reset;
285
286 // Set up a mock MediaHandlerFactory
287 MediaWikiServices::getInstance()->disableService( 'MediaHandlerFactory' );
288 MediaWikiServices::getInstance()->redefineService(
289 'MediaHandlerFactory',
290 function() {
291 return new MockMediaHandlerFactory();
292 }
293 );
294 $teardown[] = function () {
295 MediaWikiServices::getInstance()->resetServiceForTesting( 'MediaHandlerFactory' );
296 };
297
298 // SqlBagOStuff broke when using temporary tables on r40209 (T17892).
299 // It seems to have been fixed since (r55079?), but regressed at some point before r85701.
300 // This works around it for now...
301 global $wgObjectCaches;
302 $setup['wgObjectCaches'] = [ CACHE_DB => $wgObjectCaches['hash'] ] + $wgObjectCaches;
303 if ( isset( ObjectCache::$instances[CACHE_DB] ) ) {
304 $savedCache = ObjectCache::$instances[CACHE_DB];
305 ObjectCache::$instances[CACHE_DB] = new HashBagOStuff;
306 $teardown[] = function () use ( $savedCache ) {
307 ObjectCache::$instances[CACHE_DB] = $savedCache;
308 };
309 }
310
311 $teardown[] = $this->executeSetupSnippets( $setup );
312
313 // Schedule teardown snippets in reverse order
314 return $this->createTeardownObject( $teardown, $nextTeardown );
315 }
316
317 private function appendNamespaceSetup( &$setup, &$teardown ) {
318 // Add a namespace shadowing a interwiki link, to test
319 // proper precedence when resolving links. (T53680)
320 $setup['wgExtraNamespaces'] = [
321 100 => 'MemoryAlpha',
322 101 => 'MemoryAlpha_talk'
323 ];
324 // Changing wgExtraNamespaces invalidates caches in MWNamespace and
325 // any live Language object, both on setup and teardown
326 $reset = function () {
327 MWNamespace::getCanonicalNamespaces( true );
328 $GLOBALS['wgContLang']->resetNamespaces();
329 };
330 $setup[] = $reset;
331 $teardown[] = $reset;
332 }
333
334 /**
335 * Create a RepoGroup object appropriate for the current configuration
336 * @return RepoGroup
337 */
338 protected function createRepoGroup() {
339 if ( $this->uploadDir ) {
340 if ( $this->fileBackendName ) {
341 throw new MWException( 'You cannot specify both use-filebackend and upload-dir' );
342 }
343 $backend = new FSFileBackend( [
344 'name' => 'local-backend',
345 'wikiId' => wfWikiID(),
346 'basePath' => $this->uploadDir,
347 'tmpDirectory' => wfTempDir()
348 ] );
349 } elseif ( $this->fileBackendName ) {
350 global $wgFileBackends;
351 $name = $this->fileBackendName;
352 $useConfig = false;
353 foreach ( $wgFileBackends as $conf ) {
354 if ( $conf['name'] === $name ) {
355 $useConfig = $conf;
356 }
357 }
358 if ( $useConfig === false ) {
359 throw new MWException( "Unable to find file backend \"$name\"" );
360 }
361 $useConfig['name'] = 'local-backend'; // swap name
362 unset( $useConfig['lockManager'] );
363 unset( $useConfig['fileJournal'] );
364 $class = $useConfig['class'];
365 $backend = new $class( $useConfig );
366 } else {
367 # Replace with a mock. We do not care about generating real
368 # files on the filesystem, just need to expose the file
369 # informations.
370 $backend = new MockFileBackend( [
371 'name' => 'local-backend',
372 'wikiId' => wfWikiID()
373 ] );
374 }
375
376 return new RepoGroup(
377 [
378 'class' => 'MockLocalRepo',
379 'name' => 'local',
380 'url' => 'http://example.com/images',
381 'hashLevels' => 2,
382 'transformVia404' => false,
383 'backend' => $backend
384 ],
385 []
386 );
387 }
388
389 /**
390 * Execute an array in which elements with integer keys are taken to be
391 * callable objects, and other elements are taken to be global variable
392 * set operations, with the key giving the variable name and the value
393 * giving the new global variable value. A closure is returned which, when
394 * executed, sets the global variables back to the values they had before
395 * this function was called.
396 *
397 * @see staticSetup
398 *
399 * @param array $setup
400 * @return closure
401 */
402 protected function executeSetupSnippets( $setup ) {
403 $saved = [];
404 foreach ( $setup as $name => $value ) {
405 if ( is_int( $name ) ) {
406 $value();
407 } else {
408 $saved[$name] = isset( $GLOBALS[$name] ) ? $GLOBALS[$name] : null;
409 $GLOBALS[$name] = $value;
410 }
411 }
412 return function () use ( $saved ) {
413 $this->executeSetupSnippets( $saved );
414 };
415 }
416
417 /**
418 * Take a setup array in the same format as the one given to
419 * executeSetupSnippets(), and return a ScopedCallback which, when consumed,
420 * executes the snippets in the setup array in reverse order. This is used
421 * to create "teardown objects" for the public API.
422 *
423 * @see staticSetup
424 *
425 * @param array $teardown The snippet array
426 * @param ScopedCallback|null A ScopedCallback to consume
427 * @return ScopedCallback
428 */
429 protected function createTeardownObject( $teardown, $nextTeardown = null ) {
430 return new ScopedCallback( function() use ( $teardown, $nextTeardown ) {
431 // Schedule teardown snippets in reverse order
432 $teardown = array_reverse( $teardown );
433
434 $this->executeSetupSnippets( $teardown );
435 if ( $nextTeardown ) {
436 ScopedCallback::consume( $nextTeardown );
437 }
438 } );
439 }
440
441 /**
442 * Set a setupDone flag to indicate that setup has been done, and return
443 * the teardown closure. If the flag was already set, throw an exception.
444 *
445 * @param string $funcName The setup function name
446 * @return closure
447 */
448 protected function markSetupDone( $funcName ) {
449 if ( $this->setupDone[$funcName] ) {
450 throw new MWException( "$funcName is already done" );
451 }
452 $this->setupDone[$funcName] = true;
453 return function () use ( $funcName ) {
454 $this->setupDone[$funcName] = false;
455 };
456 }
457
458 /**
459 * Ensure a given setup stage has been done, throw an exception if it has
460 * not.
461 */
462 protected function checkSetupDone( $funcName, $funcName2 = null ) {
463 if ( !$this->setupDone[$funcName]
464 && ( $funcName === null || !$this->setupDone[$funcName2] )
465 ) {
466 throw new MWException( "$funcName must be called before calling " .
467 wfGetCaller() );
468 }
469 }
470
471 /**
472 * Determine whether a particular setup function has been run
473 *
474 * @param string $funcName
475 * @return boolean
476 */
477 public function isSetupDone( $funcName ) {
478 return isset( $this->setupDone[$funcName] ) ? $this->setupDone[$funcName] : false;
479 }
480
481 /**
482 * Insert hardcoded interwiki in the lookup table.
483 *
484 * This function insert a set of well known interwikis that are used in
485 * the parser tests. They can be considered has fixtures are injected in
486 * the interwiki cache by using the 'InterwikiLoadPrefix' hook.
487 * Since we are not interested in looking up interwikis in the database,
488 * the hook completely replace the existing mechanism (hook returns false).
489 *
490 * @return closure for teardown
491 */
492 private function setupInterwikis() {
493 # Hack: insert a few Wikipedia in-project interwiki prefixes,
494 # for testing inter-language links
495 Hooks::register( 'InterwikiLoadPrefix', function ( $prefix, &$iwData ) {
496 static $testInterwikis = [
497 'local' => [
498 'iw_url' => 'http://doesnt.matter.org/$1',
499 'iw_api' => '',
500 'iw_wikiid' => '',
501 'iw_local' => 0 ],
502 'wikipedia' => [
503 'iw_url' => 'http://en.wikipedia.org/wiki/$1',
504 'iw_api' => '',
505 'iw_wikiid' => '',
506 'iw_local' => 0 ],
507 'meatball' => [
508 'iw_url' => 'http://www.usemod.com/cgi-bin/mb.pl?$1',
509 'iw_api' => '',
510 'iw_wikiid' => '',
511 'iw_local' => 0 ],
512 'memoryalpha' => [
513 'iw_url' => 'http://www.memory-alpha.org/en/index.php/$1',
514 'iw_api' => '',
515 'iw_wikiid' => '',
516 'iw_local' => 0 ],
517 'zh' => [
518 'iw_url' => 'http://zh.wikipedia.org/wiki/$1',
519 'iw_api' => '',
520 'iw_wikiid' => '',
521 'iw_local' => 1 ],
522 'es' => [
523 'iw_url' => 'http://es.wikipedia.org/wiki/$1',
524 'iw_api' => '',
525 'iw_wikiid' => '',
526 'iw_local' => 1 ],
527 'fr' => [
528 'iw_url' => 'http://fr.wikipedia.org/wiki/$1',
529 'iw_api' => '',
530 'iw_wikiid' => '',
531 'iw_local' => 1 ],
532 'ru' => [
533 'iw_url' => 'http://ru.wikipedia.org/wiki/$1',
534 'iw_api' => '',
535 'iw_wikiid' => '',
536 'iw_local' => 1 ],
537 'mi' => [
538 'iw_url' => 'http://mi.wikipedia.org/wiki/$1',
539 'iw_api' => '',
540 'iw_wikiid' => '',
541 'iw_local' => 1 ],
542 'mul' => [
543 'iw_url' => 'http://wikisource.org/wiki/$1',
544 'iw_api' => '',
545 'iw_wikiid' => '',
546 'iw_local' => 1 ],
547 ];
548 if ( array_key_exists( $prefix, $testInterwikis ) ) {
549 $iwData = $testInterwikis[$prefix];
550 }
551
552 // We only want to rely on the above fixtures
553 return false;
554 } );// hooks::register
555
556 return function () {
557 // Tear down
558 Hooks::clear( 'InterwikiLoadPrefix' );
559 };
560 }
561
562 /**
563 * Reset the Title-related services that need resetting
564 * for each test
565 */
566 private function resetTitleServices() {
567 $services = MediaWikiServices::getInstance();
568 $services->resetServiceForTesting( 'TitleFormatter' );
569 $services->resetServiceForTesting( 'TitleParser' );
570 $services->resetServiceForTesting( '_MediaWikiTitleCodec' );
571 $services->resetServiceForTesting( 'LinkRenderer' );
572 $services->resetServiceForTesting( 'LinkRendererFactory' );
573 }
574
575 /**
576 * Remove last character if it is a newline
577 * @group utility
578 * @param string $s
579 * @return string
580 */
581 public static function chomp( $s ) {
582 if ( substr( $s, -1 ) === "\n" ) {
583 return substr( $s, 0, -1 );
584 } else {
585 return $s;
586 }
587 }
588
589 /**
590 * Run a series of tests listed in the given text files.
591 * Each test consists of a brief description, wikitext input,
592 * and the expected HTML output.
593 *
594 * Prints status updates on stdout and counts up the total
595 * number and percentage of passed tests.
596 *
597 * Handles all setup and teardown.
598 *
599 * @param array $filenames Array of strings
600 * @return bool True if passed all tests, false if any tests failed.
601 */
602 public function runTestsFromFiles( $filenames ) {
603 $ok = false;
604
605 $teardownGuard = $this->staticSetup();
606 $teardownGuard = $this->setupDatabase( $teardownGuard );
607 $teardownGuard = $this->setupUploads( $teardownGuard );
608
609 $this->recorder->start();
610 try {
611 $ok = true;
612
613 foreach ( $filenames as $filename ) {
614 $testFileInfo = TestFileReader::read( $filename, [
615 'runDisabled' => $this->runDisabled,
616 'runParsoid' => $this->runParsoid,
617 'regex' => $this->regex ] );
618
619 // Don't start the suite if there are no enabled tests in the file
620 if ( !$testFileInfo['tests'] ) {
621 continue;
622 }
623
624 $this->recorder->startSuite( $filename );
625 $ok = $this->runTests( $testFileInfo ) && $ok;
626 $this->recorder->endSuite( $filename );
627 }
628
629 $this->recorder->report();
630 } catch ( DBError $e ) {
631 $this->recorder->warning( $e->getMessage() );
632 }
633 $this->recorder->end();
634
635 ScopedCallback::consume( $teardownGuard );
636
637 return $ok;
638 }
639
640 /**
641 * Determine whether the current parser has the hooks registered in it
642 * that are required by a file read by TestFileReader.
643 */
644 public function meetsRequirements( $requirements ) {
645 foreach ( $requirements as $requirement ) {
646 switch ( $requirement['type'] ) {
647 case 'hook':
648 $ok = $this->requireHook( $requirement['name'] );
649 break;
650 case 'functionHook':
651 $ok = $this->requireFunctionHook( $requirement['name'] );
652 break;
653 case 'transparentHook':
654 $ok = $this->requireTransparentHook( $requirement['name'] );
655 break;
656 }
657 if ( !$ok ) {
658 return false;
659 }
660 }
661 return true;
662 }
663
664 /**
665 * Run the tests from a single file. staticSetup() and setupDatabase()
666 * must have been called already.
667 *
668 * @param array $testFileInfo Parsed file info returned by TestFileReader
669 * @return bool True if passed all tests, false if any tests failed.
670 */
671 public function runTests( $testFileInfo ) {
672 $ok = true;
673
674 $this->checkSetupDone( 'staticSetup' );
675
676 // Don't add articles from the file if there are no enabled tests from the file
677 if ( !$testFileInfo['tests'] ) {
678 return true;
679 }
680
681 // If any requirements are not met, mark all tests from the file as skipped
682 if ( !$this->meetsRequirements( $testFileInfo['requirements'] ) ) {
683 foreach ( $testFileInfo['tests'] as $test ) {
684 $this->recorder->startTest( $test );
685 $this->recorder->skipped( $test, 'required extension not enabled' );
686 }
687 return true;
688 }
689
690 // Add articles
691 $this->addArticles( $testFileInfo['articles'] );
692
693 // Run tests
694 foreach ( $testFileInfo['tests'] as $test ) {
695 $this->recorder->startTest( $test );
696 $result =
697 $this->runTest( $test );
698 if ( $result !== false ) {
699 $ok = $ok && $result->isSuccess();
700 $this->recorder->record( $test, $result );
701 }
702 }
703
704 return $ok;
705 }
706
707 /**
708 * Get a Parser object
709 *
710 * @param string $preprocessor
711 * @return Parser
712 */
713 function getParser( $preprocessor = null ) {
714 global $wgParserConf;
715
716 $class = $wgParserConf['class'];
717 $parser = new $class( [ 'preprocessorClass' => $preprocessor ] + $wgParserConf );
718 ParserTestParserHook::setup( $parser );
719
720 return $parser;
721 }
722
723 /**
724 * Run a given wikitext input through a freshly-constructed wiki parser,
725 * and compare the output against the expected results.
726 * Prints status and explanatory messages to stdout.
727 *
728 * staticSetup() and setupWikiData() must be called before this function
729 * is entered.
730 *
731 * @param array $test The test parameters:
732 * - test: The test name
733 * - desc: The subtest description
734 * - input: Wikitext to try rendering
735 * - options: Array of test options
736 * - config: Overrides for global variables, one per line
737 *
738 * @return ParserTestResult or false if skipped
739 */
740 public function runTest( $test ) {
741 wfDebug( __METHOD__.": running {$test['desc']}" );
742 $opts = $this->parseOptions( $test['options'] );
743 $teardownGuard = $this->perTestSetup( $test );
744
745 $context = RequestContext::getMain();
746 $user = $context->getUser();
747 $options = ParserOptions::newFromContext( $context );
748
749 if ( isset( $opts['tidy'] ) ) {
750 if ( !$this->tidySupport->isEnabled() ) {
751 $this->recorder->skipped( $test, 'tidy extension is not installed' );
752 return false;
753 } else {
754 $options->setTidy( true );
755 }
756 }
757
758 if ( isset( $opts['title'] ) ) {
759 $titleText = $opts['title'];
760 } else {
761 $titleText = 'Parser test';
762 }
763
764 $local = isset( $opts['local'] );
765 $preprocessor = isset( $opts['preprocessor'] ) ? $opts['preprocessor'] : null;
766 $parser = $this->getParser( $preprocessor );
767 $title = Title::newFromText( $titleText );
768
769 if ( isset( $opts['pst'] ) ) {
770 $out = $parser->preSaveTransform( $test['input'], $title, $user, $options );
771 } elseif ( isset( $opts['msg'] ) ) {
772 $out = $parser->transformMsg( $test['input'], $options, $title );
773 } elseif ( isset( $opts['section'] ) ) {
774 $section = $opts['section'];
775 $out = $parser->getSection( $test['input'], $section );
776 } elseif ( isset( $opts['replace'] ) ) {
777 $section = $opts['replace'][0];
778 $replace = $opts['replace'][1];
779 $out = $parser->replaceSection( $test['input'], $section, $replace );
780 } elseif ( isset( $opts['comment'] ) ) {
781 $out = Linker::formatComment( $test['input'], $title, $local );
782 } elseif ( isset( $opts['preload'] ) ) {
783 $out = $parser->getPreloadText( $test['input'], $title, $options );
784 } else {
785 $output = $parser->parse( $test['input'], $title, $options, true, true, 1337 );
786 $output->setTOCEnabled( !isset( $opts['notoc'] ) );
787 $out = $output->getText();
788 if ( isset( $opts['tidy'] ) ) {
789 $out = preg_replace( '/\s+$/', '', $out );
790 }
791
792 if ( isset( $opts['showtitle'] ) ) {
793 if ( $output->getTitleText() ) {
794 $title = $output->getTitleText();
795 }
796
797 $out = "$title\n$out";
798 }
799
800 if ( isset( $opts['showindicators'] ) ) {
801 $indicators = '';
802 foreach ( $output->getIndicators() as $id => $content ) {
803 $indicators .= "$id=$content\n";
804 }
805 $out = $indicators . $out;
806 }
807
808 if ( isset( $opts['ill'] ) ) {
809 $out = implode( ' ', $output->getLanguageLinks() );
810 } elseif ( isset( $opts['cat'] ) ) {
811 $out = '';
812 foreach ( $output->getCategories() as $name => $sortkey ) {
813 if ( $out !== '' ) {
814 $out .= "\n";
815 }
816 $out .= "cat=$name sort=$sortkey";
817 }
818 }
819 }
820
821 ScopedCallback::consume( $teardownGuard );
822
823 $expected = $test['result'];
824 if ( count( $this->normalizationFunctions ) ) {
825 $expected = ParserTestResultNormalizer::normalize(
826 $test['expected'], $this->normalizationFunctions );
827 $out = ParserTestResultNormalizer::normalize( $out, $this->normalizationFunctions );
828 }
829
830 $testResult = new ParserTestResult( $test, $expected, $out );
831 return $testResult;
832 }
833
834 /**
835 * Use a regex to find out the value of an option
836 * @param string $key Name of option val to retrieve
837 * @param array $opts Options array to look in
838 * @param mixed $default Default value returned if not found
839 * @return mixed
840 */
841 private static function getOptionValue( $key, $opts, $default ) {
842 $key = strtolower( $key );
843
844 if ( isset( $opts[$key] ) ) {
845 return $opts[$key];
846 } else {
847 return $default;
848 }
849 }
850
851 /**
852 * Given the options string, return an associative array of options.
853 * @todo Move this to TestFileReader
854 *
855 * @param string $instring
856 * @return array
857 */
858 private function parseOptions( $instring ) {
859 $opts = [];
860 // foo
861 // foo=bar
862 // foo="bar baz"
863 // foo=[[bar baz]]
864 // foo=bar,"baz quux"
865 // foo={...json...}
866 $defs = '(?(DEFINE)
867 (?<qstr> # Quoted string
868 "
869 (?:[^\\\\"] | \\\\.)*
870 "
871 )
872 (?<json>
873 \{ # Open bracket
874 (?:
875 [^"{}] | # Not a quoted string or object, or
876 (?&qstr) | # A quoted string, or
877 (?&json) # A json object (recursively)
878 )*
879 \} # Close bracket
880 )
881 (?<value>
882 (?:
883 (?&qstr) # Quoted val
884 |
885 \[\[
886 [^]]* # Link target
887 \]\]
888 |
889 [\w-]+ # Plain word
890 |
891 (?&json) # JSON object
892 )
893 )
894 )';
895 $regex = '/' . $defs . '\b
896 (?<k>[\w-]+) # Key
897 \b
898 (?:\s*
899 = # First sub-value
900 \s*
901 (?<v>
902 (?&value)
903 (?:\s*
904 , # Sub-vals 1..N
905 \s*
906 (?&value)
907 )*
908 )
909 )?
910 /x';
911 $valueregex = '/' . $defs . '(?&value)/x';
912
913 if ( preg_match_all( $regex, $instring, $matches, PREG_SET_ORDER ) ) {
914 foreach ( $matches as $bits ) {
915 $key = strtolower( $bits['k'] );
916 if ( !isset( $bits['v'] ) ) {
917 $opts[$key] = true;
918 } else {
919 preg_match_all( $valueregex, $bits['v'], $vmatches );
920 $opts[$key] = array_map( [ $this, 'cleanupOption' ], $vmatches[0] );
921 if ( count( $opts[$key] ) == 1 ) {
922 $opts[$key] = $opts[$key][0];
923 }
924 }
925 }
926 }
927 return $opts;
928 }
929
930 private function cleanupOption( $opt ) {
931 if ( substr( $opt, 0, 1 ) == '"' ) {
932 return stripcslashes( substr( $opt, 1, -1 ) );
933 }
934
935 if ( substr( $opt, 0, 2 ) == '[[' ) {
936 return substr( $opt, 2, -2 );
937 }
938
939 if ( substr( $opt, 0, 1 ) == '{' ) {
940 return FormatJson::decode( $opt, true );
941 }
942 return $opt;
943 }
944
945 /**
946 * Do any required setup which is dependent on test options.
947 *
948 * @see staticSetup() for more information about setup/teardown
949 *
950 * @param array $test Test info supplied by TestFileReader
951 * @param callable|null $nextTeardown
952 * @return ScopedCallback
953 */
954 public function perTestSetup( $test, $nextTeardown = null ) {
955 $teardown = [];
956
957 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
958 $teardown[] = $this->markSetupDone( 'perTestSetup' );
959
960 $opts = $this->parseOptions( $test['options'] );
961 $config = $test['config'];
962
963 // Find out values for some special options.
964 $langCode =
965 self::getOptionValue( 'language', $opts, 'en' );
966 $variant =
967 self::getOptionValue( 'variant', $opts, false );
968 $maxtoclevel =
969 self::getOptionValue( 'wgMaxTocLevel', $opts, 999 );
970 $linkHolderBatchSize =
971 self::getOptionValue( 'wgLinkHolderBatchSize', $opts, 1000 );
972
973 $setup = [
974 'wgEnableUploads' => self::getOptionValue( 'wgEnableUploads', $opts, true ),
975 'wgLanguageCode' => $langCode,
976 'wgRawHtml' => self::getOptionValue( 'wgRawHtml', $opts, false ),
977 'wgNamespacesWithSubpages' => array_fill_keys(
978 MWNamespace::getValidNamespaces(), isset( $opts['subpage'] )
979 ),
980 'wgMaxTocLevel' => $maxtoclevel,
981 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
982 'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
983 'wgDefaultLanguageVariant' => $variant,
984 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
985 // Set as a JSON object like:
986 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
987 'wgEnableMagicLinks' => self::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
988 + [ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
989 ];
990
991 if ( $config ) {
992 $configLines = explode( "\n", $config );
993
994 foreach ( $configLines as $line ) {
995 list( $var, $value ) = explode( '=', $line, 2 );
996 $setup[$var] = eval( "return $value;" );
997 }
998 }
999
1000 /** @since 1.20 */
1001 Hooks::run( 'ParserTestGlobals', [ &$setup ] );
1002
1003 // Create tidy driver
1004 if ( isset( $opts['tidy'] ) ) {
1005 // Cache a driver instance
1006 if ( $this->tidyDriver === null ) {
1007 $this->tidyDriver = MWTidy::factory( $this->tidySupport->getConfig() );
1008 }
1009 $tidy = $this->tidyDriver;
1010 } else {
1011 $tidy = false;
1012 }
1013 MWTidy::setInstance( $tidy );
1014 $teardown[] = function () {
1015 MWTidy::destroySingleton();
1016 };
1017
1018 // Set content language. This invalidates the magic word cache and title services
1019 $lang = Language::factory( $langCode );
1020 $setup['wgContLang'] = $lang;
1021 $reset = function () {
1022 MagicWord::clearCache();
1023 $this->resetTitleServices();
1024 };
1025 $setup[] = $reset;
1026 $teardown[] = $reset;
1027
1028 // Make a user object with the same language
1029 $user = new User;
1030 $user->setOption( 'language', $langCode );
1031 $setup['wgLang'] = $lang;
1032
1033 // We (re)set $wgThumbLimits to a single-element array above.
1034 $user->setOption( 'thumbsize', 0 );
1035
1036 $setup['wgUser'] = $user;
1037
1038 // And put both user and language into the context
1039 $context = RequestContext::getMain();
1040 $context->setUser( $user );
1041 $context->setLanguage( $lang );
1042 $teardown[] = function () use ( $context ) {
1043 // Reset context to the restored globals
1044 $context->setUser( $GLOBALS['wgUser'] );
1045 $context->setLanguage( $GLOBALS['wgContLang'] );
1046 };
1047
1048 $teardown[] = $this->executeSetupSnippets( $setup );
1049
1050 return $this->createTeardownObject( $teardown, $nextTeardown );
1051 }
1052
1053 /**
1054 * List of temporary tables to create, without prefix.
1055 * Some of these probably aren't necessary.
1056 * @return array
1057 */
1058 private function listTables() {
1059 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1060 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
1061 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1062 'site_stats', 'ipblocks', 'image', 'oldimage',
1063 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1064 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1065 'archive', 'user_groups', 'page_props', 'category'
1066 ];
1067
1068 if ( in_array( $this->db->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1069 array_push( $tables, 'searchindex' );
1070 }
1071
1072 // Allow extensions to add to the list of tables to duplicate;
1073 // may be necessary if they hook into page save or other code
1074 // which will require them while running tests.
1075 Hooks::run( 'ParserTestTables', [ &$tables ] );
1076
1077 return $tables;
1078 }
1079
1080 public function setDatabase( IDatabase $db ) {
1081 $this->db = $db;
1082 $this->setupDone['setDatabase'] = true;
1083 }
1084
1085 /**
1086 * Set up temporary DB tables.
1087 *
1088 * For best performance, call this once only for all tests. However, it can
1089 * be called at the start of each test if more isolation is desired.
1090 *
1091 * @todo: This is basically an unrefactored copy of
1092 * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.
1093 *
1094 * Do not call this function from a MediaWikiTestCase subclass, since
1095 * MediaWikiTestCase does its own DB setup. Instead use setDatabase().
1096 *
1097 * @see staticSetup() for more information about setup/teardown
1098 *
1099 * @param ScopedCallback|null $nextTeardown The next teardown object
1100 * @return ScopedCallback The teardown object
1101 */
1102 public function setupDatabase( $nextTeardown = null ) {
1103 global $wgDBprefix;
1104
1105 $this->db = wfGetDB( DB_MASTER );
1106 $dbType = $this->db->getType();
1107
1108 if ( $dbType == 'oracle' ) {
1109 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase::ORA_DB_PREFIX ];
1110 } else {
1111 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase::DB_PREFIX ];
1112 }
1113 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1114 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1115 }
1116
1117 $teardown = [];
1118
1119 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1120
1121 # CREATE TEMPORARY TABLE breaks if there is more than one server
1122 if ( wfGetLB()->getServerCount() != 1 ) {
1123 $this->useTemporaryTables = false;
1124 }
1125
1126 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
1127 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
1128
1129 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
1130 $this->dbClone->useTemporaryTables( $temporary );
1131 $this->dbClone->cloneTableStructure();
1132
1133 if ( $dbType == 'oracle' ) {
1134 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1135 # Insert 0 user to prevent FK violations
1136
1137 # Anonymous user
1138 $this->db->insert( 'user', [
1139 'user_id' => 0,
1140 'user_name' => 'Anonymous' ] );
1141 }
1142
1143 $teardown[] = function () {
1144 $this->teardownDatabase();
1145 };
1146
1147 // Wipe some DB query result caches on setup and teardown
1148 $reset = function () {
1149 LinkCache::singleton()->clear();
1150
1151 // Clear the message cache
1152 MessageCache::singleton()->clear();
1153 };
1154 $reset();
1155 $teardown[] = $reset;
1156 return $this->createTeardownObject( $teardown, $nextTeardown );
1157 }
1158
1159 /**
1160 * Add data about uploads to the new test DB, and set up the upload
1161 * directory. This should be called after either setDatabase() or
1162 * setupDatabase().
1163 *
1164 * @param ScopedCallback|null $nextTeardown The next teardown object
1165 * @return ScopedCallback The teardown object
1166 */
1167 public function setupUploads( $nextTeardown = null ) {
1168 $teardown = [];
1169
1170 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1171 $teardown[] = $this->markSetupDone( 'setupUploads' );
1172
1173 // Create the files in the upload directory (or pretend to create them
1174 // in a MockFileBackend). Append teardown callback.
1175 $teardown[] = $this->setupUploadBackend();
1176
1177 // Create a user
1178 $user = User::createNew( 'WikiSysop' );
1179
1180 // Register the uploads in the database
1181
1182 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
1183 # note that the size/width/height/bits/etc of the file
1184 # are actually set by inspecting the file itself; the arguments
1185 # to recordUpload2 have no effect. That said, we try to make things
1186 # match up so it is less confusing to readers of the code & tests.
1187 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1188 'size' => 7881,
1189 'width' => 1941,
1190 'height' => 220,
1191 'bits' => 8,
1192 'media_type' => MEDIATYPE_BITMAP,
1193 'mime' => 'image/jpeg',
1194 'metadata' => serialize( [] ),
1195 'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
1196 'fileExists' => true
1197 ], $this->db->timestamp( '20010115123500' ), $user );
1198
1199 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
1200 # again, note that size/width/height below are ignored; see above.
1201 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1202 'size' => 22589,
1203 'width' => 135,
1204 'height' => 135,
1205 'bits' => 8,
1206 'media_type' => MEDIATYPE_BITMAP,
1207 'mime' => 'image/png',
1208 'metadata' => serialize( [] ),
1209 'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
1210 'fileExists' => true
1211 ], $this->db->timestamp( '20130225203040' ), $user );
1212
1213 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
1214 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1215 'size' => 12345,
1216 'width' => 240,
1217 'height' => 180,
1218 'bits' => 0,
1219 'media_type' => MEDIATYPE_DRAWING,
1220 'mime' => 'image/svg+xml',
1221 'metadata' => serialize( [] ),
1222 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1223 'fileExists' => true
1224 ], $this->db->timestamp( '20010115123500' ), $user );
1225
1226 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1227 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
1228 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1229 'size' => 12345,
1230 'width' => 320,
1231 'height' => 240,
1232 'bits' => 24,
1233 'media_type' => MEDIATYPE_BITMAP,
1234 'mime' => 'image/jpeg',
1235 'metadata' => serialize( [] ),
1236 'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
1237 'fileExists' => true
1238 ], $this->db->timestamp( '20010115123500' ), $user );
1239
1240 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
1241 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1242 'size' => 12345,
1243 'width' => 320,
1244 'height' => 240,
1245 'bits' => 0,
1246 'media_type' => MEDIATYPE_VIDEO,
1247 'mime' => 'application/ogg',
1248 'metadata' => serialize( [] ),
1249 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1250 'fileExists' => true
1251 ], $this->db->timestamp( '20010115123500' ), $user );
1252
1253 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
1254 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1255 'size' => 12345,
1256 'width' => 0,
1257 'height' => 0,
1258 'bits' => 0,
1259 'media_type' => MEDIATYPE_AUDIO,
1260 'mime' => 'application/ogg',
1261 'metadata' => serialize( [] ),
1262 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1263 'fileExists' => true
1264 ], $this->db->timestamp( '20010115123500' ), $user );
1265
1266 # A DjVu file
1267 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
1268 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1269 'size' => 3249,
1270 'width' => 2480,
1271 'height' => 3508,
1272 'bits' => 0,
1273 'media_type' => MEDIATYPE_BITMAP,
1274 'mime' => 'image/vnd.djvu',
1275 'metadata' => '<?xml version="1.0" ?>
1276 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1277 <DjVuXML>
1278 <HEAD></HEAD>
1279 <BODY><OBJECT height="3508" width="2480">
1280 <PARAM name="DPI" value="300" />
1281 <PARAM name="GAMMA" value="2.2" />
1282 </OBJECT>
1283 <OBJECT height="3508" width="2480">
1284 <PARAM name="DPI" value="300" />
1285 <PARAM name="GAMMA" value="2.2" />
1286 </OBJECT>
1287 <OBJECT height="3508" width="2480">
1288 <PARAM name="DPI" value="300" />
1289 <PARAM name="GAMMA" value="2.2" />
1290 </OBJECT>
1291 <OBJECT height="3508" width="2480">
1292 <PARAM name="DPI" value="300" />
1293 <PARAM name="GAMMA" value="2.2" />
1294 </OBJECT>
1295 <OBJECT height="3508" width="2480">
1296 <PARAM name="DPI" value="300" />
1297 <PARAM name="GAMMA" value="2.2" />
1298 </OBJECT>
1299 </BODY>
1300 </DjVuXML>',
1301 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1302 'fileExists' => true
1303 ], $this->db->timestamp( '20010115123600' ), $user );
1304
1305 return $this->createTeardownObject( $teardown, $nextTeardown );
1306 }
1307
1308 /**
1309 * Helper for database teardown, called from the teardown closure. Destroy
1310 * the database clone and fix up some things that CloneDatabase doesn't fix.
1311 *
1312 * @todo Move most things here to CloneDatabase
1313 */
1314 private function teardownDatabase() {
1315 $this->checkSetupDone( 'setupDatabase' );
1316
1317 $this->dbClone->destroy();
1318 $this->databaseSetupDone = false;
1319
1320 if ( $this->useTemporaryTables ) {
1321 if ( $this->db->getType() == 'sqlite' ) {
1322 # Under SQLite the searchindex table is virtual and need
1323 # to be explicitly destroyed. See T31912
1324 # See also MediaWikiTestCase::destroyDB()
1325 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1326 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
1327 }
1328 # Don't need to do anything
1329 return;
1330 }
1331
1332 $tables = $this->listTables();
1333
1334 foreach ( $tables as $table ) {
1335 if ( $this->db->getType() == 'oracle' ) {
1336 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1337 } else {
1338 $this->db->query( "DROP TABLE `parsertest_$table`" );
1339 }
1340 }
1341
1342 if ( $this->db->getType() == 'oracle' ) {
1343 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1344 }
1345 }
1346
1347 /**
1348 * Upload test files to the backend created by createRepoGroup().
1349 *
1350 * @return callable The teardown callback
1351 */
1352 private function setupUploadBackend() {
1353 global $IP;
1354
1355 $repo = RepoGroup::singleton()->getLocalRepo();
1356 $base = $repo->getZonePath( 'public' );
1357 $backend = $repo->getBackend();
1358 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1359 $backend->store( [
1360 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1361 'dst' => "$base/3/3a/Foobar.jpg"
1362 ] );
1363 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1364 $backend->store( [
1365 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1366 'dst' => "$base/e/ea/Thumb.png"
1367 ] );
1368 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1369 $backend->store( [
1370 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1371 'dst' => "$base/0/09/Bad.jpg"
1372 ] );
1373 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1374 $backend->store( [
1375 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1376 'dst' => "$base/5/5f/LoremIpsum.djvu"
1377 ] );
1378
1379 // No helpful SVG file to copy, so make one ourselves
1380 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1381 '<svg xmlns="http://www.w3.org/2000/svg"' .
1382 ' version="1.1" width="240" height="180"/>';
1383
1384 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1385 $backend->quickCreate( [
1386 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1387 ] );
1388
1389 return function () use ( $backend ) {
1390 if ( $backend instanceof MockFileBackend ) {
1391 // In memory backend, so dont bother cleaning them up.
1392 return;
1393 }
1394 $this->teardownUploadBackend();
1395 };
1396 }
1397
1398 /**
1399 * Remove the dummy uploads directory
1400 */
1401 private function teardownUploadBackend() {
1402 if ( $this->keepUploads ) {
1403 return;
1404 }
1405
1406 $repo = RepoGroup::singleton()->getLocalRepo();
1407 $public = $repo->getZonePath( 'public' );
1408
1409 $this->deleteFiles(
1410 [
1411 "$public/3/3a/Foobar.jpg",
1412 "$public/e/ea/Thumb.png",
1413 "$public/0/09/Bad.jpg",
1414 "$public/5/5f/LoremIpsum.djvu",
1415 "$public/f/ff/Foobar.svg",
1416 "$public/0/00/Video.ogv",
1417 "$public/4/41/Audio.oga",
1418 ]
1419 );
1420 }
1421
1422 /**
1423 * Delete the specified files and their parent directories
1424 * @param array $files File backend URIs mwstore://...
1425 */
1426 private function deleteFiles( $files ) {
1427 // Delete the files
1428 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
1429 foreach ( $files as $file ) {
1430 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1431 }
1432
1433 // Delete the parent directories
1434 foreach ( $files as $file ) {
1435 $tmp = FileBackend::parentStoragePath( $file );
1436 while ( $tmp ) {
1437 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1438 break;
1439 }
1440 $tmp = FileBackend::parentStoragePath( $tmp );
1441 }
1442 }
1443 }
1444
1445 /**
1446 * Add articles to the test DB.
1447 *
1448 * @param $articles Article info array from TestFileReader
1449 */
1450 public function addArticles( $articles ) {
1451 global $wgContLang;
1452 $setup = [];
1453 $teardown = [];
1454
1455 // Be sure ParserTestRunner::addArticle has correct language set,
1456 // so that system messages get into the right language cache
1457 if ( $wgContLang->getCode() !== 'en' ) {
1458 $setup['wgLanguageCode'] = 'en';
1459 $setup['wgContLang'] = Language::factory( 'en' );
1460 }
1461
1462 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1463 $this->appendNamespaceSetup( $setup, $teardown );
1464
1465 // wgCapitalLinks obviously needs initialisation
1466 $setup['wgCapitalLinks'] = true;
1467
1468 $teardown[] = $this->executeSetupSnippets( $setup );
1469
1470 foreach ( $articles as $info ) {
1471 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1472 }
1473
1474 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1475 // due to T144706
1476 ObjectCache::getMainWANInstance()->clearProcessCache();
1477
1478 $this->executeSetupSnippets( $teardown );
1479 }
1480
1481 /**
1482 * Insert a temporary test article
1483 * @param string $name The title, including any prefix
1484 * @param string $text The article text
1485 * @param string $file The input file name
1486 * @param int|string $line The input line number, for reporting errors
1487 * @throws Exception
1488 * @throws MWException
1489 */
1490 private function addArticle( $name, $text, $file, $line ) {
1491 $text = self::chomp( $text );
1492 $name = self::chomp( $name );
1493
1494 $title = Title::newFromText( $name );
1495 wfDebug( __METHOD__ . ": adding $name" );
1496
1497 if ( is_null( $title ) ) {
1498 throw new MWException( "invalid title '$name' at $file:$line\n" );
1499 }
1500
1501 $page = WikiPage::factory( $title );
1502 $page->loadPageData( 'fromdbmaster' );
1503
1504 if ( $page->exists() ) {
1505 throw new MWException( "duplicate article '$name' at $file:$line\n" );
1506 }
1507
1508 // Use mock parser, to make debugging of actual parser tests simpler.
1509 // But initialise the MessageCache clone first, don't let MessageCache
1510 // get a reference to the mock object.
1511 MessageCache::singleton()->getParser();
1512 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
1513 $status = $page->doEditContent(
1514 ContentHandler::makeContent( $text, $title ),
1515 '',
1516 EDIT_NEW | EDIT_INTERNAL
1517 );
1518 $restore();
1519
1520 if ( !$status->isOK() ) {
1521 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1522 }
1523
1524 // The RepoGroup cache is invalidated by the creation of file redirects
1525 if ( $title->inNamespace( NS_FILE ) ) {
1526 RepoGroup::singleton()->clearCache( $title );
1527 }
1528 }
1529
1530 /**
1531 * Check if a hook is installed
1532 *
1533 * @param string $name
1534 * @return bool True if tag hook is present
1535 */
1536 public function requireHook( $name ) {
1537 global $wgParser;
1538
1539 $wgParser->firstCallInit(); // make sure hooks are loaded.
1540 if ( isset( $wgParser->mTagHooks[$name] ) ) {
1541 return true;
1542 } else {
1543 $this->recorder->warning( " This test suite requires the '$name' hook " .
1544 "extension, skipping." );
1545 return false;
1546 }
1547 }
1548
1549 /**
1550 * Check if a function hook is installed
1551 *
1552 * @param string $name
1553 * @return bool True if function hook is present
1554 */
1555 public function requireFunctionHook( $name ) {
1556 global $wgParser;
1557
1558 $wgParser->firstCallInit(); // make sure hooks are loaded.
1559
1560 if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
1561 return true;
1562 } else {
1563 $this->recorder->warning( " This test suite requires the '$name' function " .
1564 "hook extension, skipping." );
1565 return false;
1566 }
1567 }
1568
1569 /**
1570 * Check if a transparent tag hook is installed
1571 *
1572 * @param string $name
1573 * @return bool True if function hook is present
1574 */
1575 public function requireTransparentHook( $name ) {
1576 global $wgParser;
1577
1578 $wgParser->firstCallInit(); // make sure hooks are loaded.
1579
1580 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
1581 return true;
1582 } else {
1583 $this->recorder->warning( " This test suite requires the '$name' transparent " .
1584 "hook extension, skipping.\n" );
1585 return false;
1586 }
1587 }
1588
1589 /**
1590 * The ParserGetVariableValueTs hook, used to make sure time-related parser
1591 * functions give a persistent value.
1592 */
1593 static function getFakeTimestamp( &$parser, &$ts ) {
1594 $ts = 123; // parsed as '1970-01-01T00:02:03Z'
1595 return true;
1596 }
1597 }