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