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