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