Remove unused messages
[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 (bug 15892).
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. (bug 51680)
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' => [ 0 => isset( $opts['subpage'] ) ],
978 'wgMaxTocLevel' => $maxtoclevel,
979 'wgAllowExternalImages' => self::getOptionValue( 'wgAllowExternalImages', $opts, true ),
980 'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
981 'wgDefaultLanguageVariant' => $variant,
982 'wgLinkHolderBatchSize' => $linkHolderBatchSize,
983 // Set as a JSON object like:
984 // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
985 'wgEnableMagicLinks' => self::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
986 + [ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
987 ];
988
989 if ( $config ) {
990 $configLines = explode( "\n", $config );
991
992 foreach ( $configLines as $line ) {
993 list( $var, $value ) = explode( '=', $line, 2 );
994 $setup[$var] = eval( "return $value;" );
995 }
996 }
997
998 /** @since 1.20 */
999 Hooks::run( 'ParserTestGlobals', [ &$setup ] );
1000
1001 // Create tidy driver
1002 if ( isset( $opts['tidy'] ) ) {
1003 // Cache a driver instance
1004 if ( $this->tidyDriver === null ) {
1005 $this->tidyDriver = MWTidy::factory( $this->tidySupport->getConfig() );
1006 }
1007 $tidy = $this->tidyDriver;
1008 } else {
1009 $tidy = false;
1010 }
1011 MWTidy::setInstance( $tidy );
1012 $teardown[] = function () {
1013 MWTidy::destroySingleton();
1014 };
1015
1016 // Set content language. This invalidates the magic word cache and title services
1017 $lang = Language::factory( $langCode );
1018 $setup['wgContLang'] = $lang;
1019 $reset = function () {
1020 MagicWord::clearCache();
1021 $this->resetTitleServices();
1022 };
1023 $setup[] = $reset;
1024 $teardown[] = $reset;
1025
1026 // Make a user object with the same language
1027 $user = new User;
1028 $user->setOption( 'language', $langCode );
1029 $setup['wgLang'] = $lang;
1030
1031 // We (re)set $wgThumbLimits to a single-element array above.
1032 $user->setOption( 'thumbsize', 0 );
1033
1034 $setup['wgUser'] = $user;
1035
1036 // And put both user and language into the context
1037 $context = RequestContext::getMain();
1038 $context->setUser( $user );
1039 $context->setLanguage( $lang );
1040 $teardown[] = function () use ( $context ) {
1041 // Reset context to the restored globals
1042 $context->setUser( $GLOBALS['wgUser'] );
1043 $context->setLanguage( $GLOBALS['wgContLang'] );
1044 };
1045
1046 $teardown[] = $this->executeSetupSnippets( $setup );
1047
1048 return $this->createTeardownObject( $teardown, $nextTeardown );
1049 }
1050
1051 /**
1052 * List of temporary tables to create, without prefix.
1053 * Some of these probably aren't necessary.
1054 * @return array
1055 */
1056 private function listTables() {
1057 $tables = [ 'user', 'user_properties', 'user_former_groups', 'page', 'page_restrictions',
1058 'protected_titles', 'revision', 'text', 'pagelinks', 'imagelinks',
1059 'categorylinks', 'templatelinks', 'externallinks', 'langlinks', 'iwlinks',
1060 'site_stats', 'ipblocks', 'image', 'oldimage',
1061 'recentchanges', 'watchlist', 'interwiki', 'logging', 'log_search',
1062 'querycache', 'objectcache', 'job', 'l10n_cache', 'redirect', 'querycachetwo',
1063 'archive', 'user_groups', 'page_props', 'category'
1064 ];
1065
1066 if ( in_array( $this->db->getType(), [ 'mysql', 'sqlite', 'oracle' ] ) ) {
1067 array_push( $tables, 'searchindex' );
1068 }
1069
1070 // Allow extensions to add to the list of tables to duplicate;
1071 // may be necessary if they hook into page save or other code
1072 // which will require them while running tests.
1073 Hooks::run( 'ParserTestTables', [ &$tables ] );
1074
1075 return $tables;
1076 }
1077
1078 public function setDatabase( IDatabase $db ) {
1079 $this->db = $db;
1080 $this->setupDone['setDatabase'] = true;
1081 }
1082
1083 /**
1084 * Set up temporary DB tables.
1085 *
1086 * For best performance, call this once only for all tests. However, it can
1087 * be called at the start of each test if more isolation is desired.
1088 *
1089 * @todo: This is basically an unrefactored copy of
1090 * MediaWikiTestCase::setupAllTestDBs. They should be factored out somehow.
1091 *
1092 * Do not call this function from a MediaWikiTestCase subclass, since
1093 * MediaWikiTestCase does its own DB setup. Instead use setDatabase().
1094 *
1095 * @see staticSetup() for more information about setup/teardown
1096 *
1097 * @param ScopedCallback|null $nextTeardown The next teardown object
1098 * @return ScopedCallback The teardown object
1099 */
1100 public function setupDatabase( $nextTeardown = null ) {
1101 global $wgDBprefix;
1102
1103 $this->db = wfGetDB( DB_MASTER );
1104 $dbType = $this->db->getType();
1105
1106 if ( $dbType == 'oracle' ) {
1107 $suspiciousPrefixes = [ 'pt_', MediaWikiTestCase::ORA_DB_PREFIX ];
1108 } else {
1109 $suspiciousPrefixes = [ 'parsertest_', MediaWikiTestCase::DB_PREFIX ];
1110 }
1111 if ( in_array( $wgDBprefix, $suspiciousPrefixes ) ) {
1112 throw new MWException( "\$wgDBprefix=$wgDBprefix suggests DB setup is already done" );
1113 }
1114
1115 $teardown = [];
1116
1117 $teardown[] = $this->markSetupDone( 'setupDatabase' );
1118
1119 # CREATE TEMPORARY TABLE breaks if there is more than one server
1120 if ( wfGetLB()->getServerCount() != 1 ) {
1121 $this->useTemporaryTables = false;
1122 }
1123
1124 $temporary = $this->useTemporaryTables || $dbType == 'postgres';
1125 $prefix = $dbType != 'oracle' ? 'parsertest_' : 'pt_';
1126
1127 $this->dbClone = new CloneDatabase( $this->db, $this->listTables(), $prefix );
1128 $this->dbClone->useTemporaryTables( $temporary );
1129 $this->dbClone->cloneTableStructure();
1130
1131 if ( $dbType == 'oracle' ) {
1132 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1133 # Insert 0 user to prevent FK violations
1134
1135 # Anonymous user
1136 $this->db->insert( 'user', [
1137 'user_id' => 0,
1138 'user_name' => 'Anonymous' ] );
1139 }
1140
1141 $teardown[] = function () {
1142 $this->teardownDatabase();
1143 };
1144
1145 // Wipe some DB query result caches on setup and teardown
1146 $reset = function () {
1147 LinkCache::singleton()->clear();
1148
1149 // Clear the message cache
1150 MessageCache::singleton()->clear();
1151 };
1152 $reset();
1153 $teardown[] = $reset;
1154 return $this->createTeardownObject( $teardown, $nextTeardown );
1155 }
1156
1157 /**
1158 * Add data about uploads to the new test DB, and set up the upload
1159 * directory. This should be called after either setDatabase() or
1160 * setupDatabase().
1161 *
1162 * @param ScopedCallback|null $nextTeardown The next teardown object
1163 * @return ScopedCallback The teardown object
1164 */
1165 public function setupUploads( $nextTeardown = null ) {
1166 $teardown = [];
1167
1168 $this->checkSetupDone( 'setupDatabase', 'setDatabase' );
1169 $teardown[] = $this->markSetupDone( 'setupUploads' );
1170
1171 // Create the files in the upload directory (or pretend to create them
1172 // in a MockFileBackend). Append teardown callback.
1173 $teardown[] = $this->setupUploadBackend();
1174
1175 // Create a user
1176 $user = User::createNew( 'WikiSysop' );
1177
1178 // Register the uploads in the database
1179
1180 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.jpg' ) );
1181 # note that the size/width/height/bits/etc of the file
1182 # are actually set by inspecting the file itself; the arguments
1183 # to recordUpload2 have no effect. That said, we try to make things
1184 # match up so it is less confusing to readers of the code & tests.
1185 $image->recordUpload2( '', 'Upload of some lame file', 'Some lame file', [
1186 'size' => 7881,
1187 'width' => 1941,
1188 'height' => 220,
1189 'bits' => 8,
1190 'media_type' => MEDIATYPE_BITMAP,
1191 'mime' => 'image/jpeg',
1192 'metadata' => serialize( [] ),
1193 'sha1' => Wikimedia\base_convert( '1', 16, 36, 31 ),
1194 'fileExists' => true
1195 ], $this->db->timestamp( '20010115123500' ), $user );
1196
1197 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Thumb.png' ) );
1198 # again, note that size/width/height below are ignored; see above.
1199 $image->recordUpload2( '', 'Upload of some lame thumbnail', 'Some lame thumbnail', [
1200 'size' => 22589,
1201 'width' => 135,
1202 'height' => 135,
1203 'bits' => 8,
1204 'media_type' => MEDIATYPE_BITMAP,
1205 'mime' => 'image/png',
1206 'metadata' => serialize( [] ),
1207 'sha1' => Wikimedia\base_convert( '2', 16, 36, 31 ),
1208 'fileExists' => true
1209 ], $this->db->timestamp( '20130225203040' ), $user );
1210
1211 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Foobar.svg' ) );
1212 $image->recordUpload2( '', 'Upload of some lame SVG', 'Some lame SVG', [
1213 'size' => 12345,
1214 'width' => 240,
1215 'height' => 180,
1216 'bits' => 0,
1217 'media_type' => MEDIATYPE_DRAWING,
1218 'mime' => 'image/svg+xml',
1219 'metadata' => serialize( [] ),
1220 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1221 'fileExists' => true
1222 ], $this->db->timestamp( '20010115123500' ), $user );
1223
1224 # This image will be blacklisted in [[MediaWiki:Bad image list]]
1225 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Bad.jpg' ) );
1226 $image->recordUpload2( '', 'zomgnotcensored', 'Borderline image', [
1227 'size' => 12345,
1228 'width' => 320,
1229 'height' => 240,
1230 'bits' => 24,
1231 'media_type' => MEDIATYPE_BITMAP,
1232 'mime' => 'image/jpeg',
1233 'metadata' => serialize( [] ),
1234 'sha1' => Wikimedia\base_convert( '3', 16, 36, 31 ),
1235 'fileExists' => true
1236 ], $this->db->timestamp( '20010115123500' ), $user );
1237
1238 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Video.ogv' ) );
1239 $image->recordUpload2( '', 'A pretty movie', 'Will it play', [
1240 'size' => 12345,
1241 'width' => 320,
1242 'height' => 240,
1243 'bits' => 0,
1244 'media_type' => MEDIATYPE_VIDEO,
1245 'mime' => 'application/ogg',
1246 'metadata' => serialize( [] ),
1247 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1248 'fileExists' => true
1249 ], $this->db->timestamp( '20010115123500' ), $user );
1250
1251 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'Audio.oga' ) );
1252 $image->recordUpload2( '', 'An awesome hitsong', 'Will it play', [
1253 'size' => 12345,
1254 'width' => 0,
1255 'height' => 0,
1256 'bits' => 0,
1257 'media_type' => MEDIATYPE_AUDIO,
1258 'mime' => 'application/ogg',
1259 'metadata' => serialize( [] ),
1260 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1261 'fileExists' => true
1262 ], $this->db->timestamp( '20010115123500' ), $user );
1263
1264 # A DjVu file
1265 $image = wfLocalFile( Title::makeTitle( NS_FILE, 'LoremIpsum.djvu' ) );
1266 $image->recordUpload2( '', 'Upload a DjVu', 'A DjVu', [
1267 'size' => 3249,
1268 'width' => 2480,
1269 'height' => 3508,
1270 'bits' => 0,
1271 'media_type' => MEDIATYPE_BITMAP,
1272 'mime' => 'image/vnd.djvu',
1273 'metadata' => '<?xml version="1.0" ?>
1274 <!DOCTYPE DjVuXML PUBLIC "-//W3C//DTD DjVuXML 1.1//EN" "pubtext/DjVuXML-s.dtd">
1275 <DjVuXML>
1276 <HEAD></HEAD>
1277 <BODY><OBJECT height="3508" width="2480">
1278 <PARAM name="DPI" value="300" />
1279 <PARAM name="GAMMA" value="2.2" />
1280 </OBJECT>
1281 <OBJECT height="3508" width="2480">
1282 <PARAM name="DPI" value="300" />
1283 <PARAM name="GAMMA" value="2.2" />
1284 </OBJECT>
1285 <OBJECT height="3508" width="2480">
1286 <PARAM name="DPI" value="300" />
1287 <PARAM name="GAMMA" value="2.2" />
1288 </OBJECT>
1289 <OBJECT height="3508" width="2480">
1290 <PARAM name="DPI" value="300" />
1291 <PARAM name="GAMMA" value="2.2" />
1292 </OBJECT>
1293 <OBJECT height="3508" width="2480">
1294 <PARAM name="DPI" value="300" />
1295 <PARAM name="GAMMA" value="2.2" />
1296 </OBJECT>
1297 </BODY>
1298 </DjVuXML>',
1299 'sha1' => Wikimedia\base_convert( '', 16, 36, 31 ),
1300 'fileExists' => true
1301 ], $this->db->timestamp( '20010115123600' ), $user );
1302
1303 return $this->createTeardownObject( $teardown, $nextTeardown );
1304 }
1305
1306 /**
1307 * Helper for database teardown, called from the teardown closure. Destroy
1308 * the database clone and fix up some things that CloneDatabase doesn't fix.
1309 *
1310 * @todo Move most things here to CloneDatabase
1311 */
1312 private function teardownDatabase() {
1313 $this->checkSetupDone( 'setupDatabase' );
1314
1315 $this->dbClone->destroy();
1316 $this->databaseSetupDone = false;
1317
1318 if ( $this->useTemporaryTables ) {
1319 if ( $this->db->getType() == 'sqlite' ) {
1320 # Under SQLite the searchindex table is virtual and need
1321 # to be explicitly destroyed. See bug 29912
1322 # See also MediaWikiTestCase::destroyDB()
1323 wfDebug( __METHOD__ . " explicitly destroying sqlite virtual table parsertest_searchindex\n" );
1324 $this->db->query( "DROP TABLE `parsertest_searchindex`" );
1325 }
1326 # Don't need to do anything
1327 return;
1328 }
1329
1330 $tables = $this->listTables();
1331
1332 foreach ( $tables as $table ) {
1333 if ( $this->db->getType() == 'oracle' ) {
1334 $this->db->query( "DROP TABLE pt_$table DROP CONSTRAINTS" );
1335 } else {
1336 $this->db->query( "DROP TABLE `parsertest_$table`" );
1337 }
1338 }
1339
1340 if ( $this->db->getType() == 'oracle' ) {
1341 $this->db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1342 }
1343 }
1344
1345 /**
1346 * Upload test files to the backend created by createRepoGroup().
1347 *
1348 * @return callable The teardown callback
1349 */
1350 private function setupUploadBackend() {
1351 global $IP;
1352
1353 $repo = RepoGroup::singleton()->getLocalRepo();
1354 $base = $repo->getZonePath( 'public' );
1355 $backend = $repo->getBackend();
1356 $backend->prepare( [ 'dir' => "$base/3/3a" ] );
1357 $backend->store( [
1358 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1359 'dst' => "$base/3/3a/Foobar.jpg"
1360 ] );
1361 $backend->prepare( [ 'dir' => "$base/e/ea" ] );
1362 $backend->store( [
1363 'src' => "$IP/tests/phpunit/data/parser/wiki.png",
1364 'dst' => "$base/e/ea/Thumb.png"
1365 ] );
1366 $backend->prepare( [ 'dir' => "$base/0/09" ] );
1367 $backend->store( [
1368 'src' => "$IP/tests/phpunit/data/parser/headbg.jpg",
1369 'dst' => "$base/0/09/Bad.jpg"
1370 ] );
1371 $backend->prepare( [ 'dir' => "$base/5/5f" ] );
1372 $backend->store( [
1373 'src' => "$IP/tests/phpunit/data/parser/LoremIpsum.djvu",
1374 'dst' => "$base/5/5f/LoremIpsum.djvu"
1375 ] );
1376
1377 // No helpful SVG file to copy, so make one ourselves
1378 $data = '<?xml version="1.0" encoding="utf-8"?>' .
1379 '<svg xmlns="http://www.w3.org/2000/svg"' .
1380 ' version="1.1" width="240" height="180"/>';
1381
1382 $backend->prepare( [ 'dir' => "$base/f/ff" ] );
1383 $backend->quickCreate( [
1384 'content' => $data, 'dst' => "$base/f/ff/Foobar.svg"
1385 ] );
1386
1387 return function () use ( $backend ) {
1388 if ( $backend instanceof MockFileBackend ) {
1389 // In memory backend, so dont bother cleaning them up.
1390 return;
1391 }
1392 $this->teardownUploadBackend();
1393 };
1394 }
1395
1396 /**
1397 * Remove the dummy uploads directory
1398 */
1399 private function teardownUploadBackend() {
1400 if ( $this->keepUploads ) {
1401 return;
1402 }
1403
1404 $repo = RepoGroup::singleton()->getLocalRepo();
1405 $public = $repo->getZonePath( 'public' );
1406
1407 $this->deleteFiles(
1408 [
1409 "$public/3/3a/Foobar.jpg",
1410 "$public/e/ea/Thumb.png",
1411 "$public/0/09/Bad.jpg",
1412 "$public/5/5f/LoremIpsum.djvu",
1413 "$public/f/ff/Foobar.svg",
1414 "$public/0/00/Video.ogv",
1415 "$public/4/41/Audio.oga",
1416 ]
1417 );
1418 }
1419
1420 /**
1421 * Delete the specified files and their parent directories
1422 * @param array $files File backend URIs mwstore://...
1423 */
1424 private function deleteFiles( $files ) {
1425 // Delete the files
1426 $backend = RepoGroup::singleton()->getLocalRepo()->getBackend();
1427 foreach ( $files as $file ) {
1428 $backend->delete( [ 'src' => $file ], [ 'force' => 1 ] );
1429 }
1430
1431 // Delete the parent directories
1432 foreach ( $files as $file ) {
1433 $tmp = FileBackend::parentStoragePath( $file );
1434 while ( $tmp ) {
1435 if ( !$backend->clean( [ 'dir' => $tmp ] )->isOK() ) {
1436 break;
1437 }
1438 $tmp = FileBackend::parentStoragePath( $tmp );
1439 }
1440 }
1441 }
1442
1443 /**
1444 * Add articles to the test DB.
1445 *
1446 * @param $articles Article info array from TestFileReader
1447 */
1448 public function addArticles( $articles ) {
1449 global $wgContLang;
1450 $setup = [];
1451 $teardown = [];
1452
1453 // Be sure ParserTestRunner::addArticle has correct language set,
1454 // so that system messages get into the right language cache
1455 if ( $wgContLang->getCode() !== 'en' ) {
1456 $setup['wgLanguageCode'] = 'en';
1457 $setup['wgContLang'] = Language::factory( 'en' );
1458 }
1459
1460 // Add special namespaces, in case that hasn't been done by staticSetup() yet
1461 $this->appendNamespaceSetup( $setup, $teardown );
1462
1463 // wgCapitalLinks obviously needs initialisation
1464 $setup['wgCapitalLinks'] = true;
1465
1466 $teardown[] = $this->executeSetupSnippets( $setup );
1467
1468 foreach ( $articles as $info ) {
1469 $this->addArticle( $info['name'], $info['text'], $info['file'], $info['line'] );
1470 }
1471
1472 // Wipe WANObjectCache process cache, which is invalidated by article insertion
1473 // due to T144706
1474 ObjectCache::getMainWANInstance()->clearProcessCache();
1475
1476 $this->executeSetupSnippets( $teardown );
1477 }
1478
1479 /**
1480 * Insert a temporary test article
1481 * @param string $name The title, including any prefix
1482 * @param string $text The article text
1483 * @param string $file The input file name
1484 * @param int|string $line The input line number, for reporting errors
1485 * @throws Exception
1486 * @throws MWException
1487 */
1488 private function addArticle( $name, $text, $file, $line ) {
1489 $text = self::chomp( $text );
1490 $name = self::chomp( $name );
1491
1492 $title = Title::newFromText( $name );
1493 wfDebug( __METHOD__ . ": adding $name" );
1494
1495 if ( is_null( $title ) ) {
1496 throw new MWException( "invalid title '$name' at $file:$line\n" );
1497 }
1498
1499 $page = WikiPage::factory( $title );
1500 $page->loadPageData( 'fromdbmaster' );
1501
1502 if ( $page->exists() ) {
1503 throw new MWException( "duplicate article '$name' at $file:$line\n" );
1504 }
1505
1506 // Use mock parser, to make debugging of actual parser tests simpler.
1507 // But initialise the MessageCache clone first, don't let MessageCache
1508 // get a reference to the mock object.
1509 MessageCache::singleton()->getParser();
1510 $restore = $this->executeSetupSnippets( [ 'wgParser' => new ParserTestMockParser ] );
1511 $status = $page->doEditContent( ContentHandler::makeContent( $text, $title ), '', EDIT_NEW );
1512 $restore();
1513
1514 if ( !$status->isOK() ) {
1515 throw new MWException( $status->getWikiText( false, false, 'en' ) );
1516 }
1517
1518 // The RepoGroup cache is invalidated by the creation of file redirects
1519 if ( $title->inNamespace( NS_FILE ) ) {
1520 RepoGroup::singleton()->clearCache( $title );
1521 }
1522 }
1523
1524 /**
1525 * Check if a hook is installed
1526 *
1527 * @param string $name
1528 * @return bool True if tag hook is present
1529 */
1530 public function requireHook( $name ) {
1531 global $wgParser;
1532
1533 $wgParser->firstCallInit(); // make sure hooks are loaded.
1534 if ( isset( $wgParser->mTagHooks[$name] ) ) {
1535 return true;
1536 } else {
1537 $this->recorder->warning( " This test suite requires the '$name' hook " .
1538 "extension, skipping." );
1539 return false;
1540 }
1541 }
1542
1543 /**
1544 * Check if a function hook is installed
1545 *
1546 * @param string $name
1547 * @return bool True if function hook is present
1548 */
1549 public function requireFunctionHook( $name ) {
1550 global $wgParser;
1551
1552 $wgParser->firstCallInit(); // make sure hooks are loaded.
1553
1554 if ( isset( $wgParser->mFunctionHooks[$name] ) ) {
1555 return true;
1556 } else {
1557 $this->recorder->warning( " This test suite requires the '$name' function " .
1558 "hook extension, skipping." );
1559 return false;
1560 }
1561 }
1562
1563 /**
1564 * Check if a transparent tag hook is installed
1565 *
1566 * @param string $name
1567 * @return bool True if function hook is present
1568 */
1569 public function requireTransparentHook( $name ) {
1570 global $wgParser;
1571
1572 $wgParser->firstCallInit(); // make sure hooks are loaded.
1573
1574 if ( isset( $wgParser->mTransparentTagHooks[$name] ) ) {
1575 return true;
1576 } else {
1577 $this->recorder->warning( " This test suite requires the '$name' transparent " .
1578 "hook extension, skipping.\n" );
1579 return false;
1580 }
1581 }
1582
1583 /**
1584 * The ParserGetVariableValueTs hook, used to make sure time-related parser
1585 * functions give a persistent value.
1586 */
1587 static function getFakeTimestamp( &$parser, &$ts ) {
1588 $ts = 123; // parsed as '1970-01-01T00:02:03Z'
1589 return true;
1590 }
1591 }