Merge "Provide PHPUnit 4 and 6 compatibility layer"
[lhc/web/wiklou.git] / tests / phpunit / MediaWikiTestCase.php
1 <?php
2
3 use MediaWiki\Logger\LegacySpi;
4 use MediaWiki\Logger\LoggerFactory;
5 use MediaWiki\Logger\MonologSpi;
6 use MediaWiki\MediaWikiServices;
7 use Psr\Log\LoggerInterface;
8 use Wikimedia\Rdbms\IDatabase;
9 use Wikimedia\Rdbms\IMaintainableDatabase;
10 use Wikimedia\Rdbms\Database;
11 use Wikimedia\Rdbms\LBFactory;
12 use Wikimedia\TestingAccessWrapper;
13
14 /**
15 * @since 1.18
16 */
17 abstract class MediaWikiTestCase extends PHPUnit\Framework\TestCase {
18
19 use MediaWikiCoversValidator;
20 use PHPUnit4And6Compat;
21
22 /**
23 * The service locator created by prepareServices(). This service locator will
24 * be restored after each test. Tests that pollute the global service locator
25 * instance should use overrideMwServices() to isolate the test.
26 *
27 * @var MediaWikiServices|null
28 */
29 private static $serviceLocator = null;
30
31 /**
32 * $called tracks whether the setUp and tearDown method has been called.
33 * class extending MediaWikiTestCase usually override setUp and tearDown
34 * but forget to call the parent.
35 *
36 * The array format takes a method name as key and anything as a value.
37 * By asserting the key exist, we know the child class has called the
38 * parent.
39 *
40 * This property must be private, we do not want child to override it,
41 * they should call the appropriate parent method instead.
42 */
43 private $called = [];
44
45 /**
46 * @var TestUser[]
47 * @since 1.20
48 */
49 public static $users;
50
51 /**
52 * Primary database
53 *
54 * @var Database
55 * @since 1.18
56 */
57 protected $db;
58
59 /**
60 * @var array
61 * @since 1.19
62 */
63 protected $tablesUsed = []; // tables with data
64
65 private static $useTemporaryTables = true;
66 private static $reuseDB = false;
67 private static $dbSetup = false;
68 private static $oldTablePrefix = '';
69
70 /**
71 * Original value of PHP's error_reporting setting.
72 *
73 * @var int
74 */
75 private $phpErrorLevel;
76
77 /**
78 * Holds the paths of temporary files/directories created through getNewTempFile,
79 * and getNewTempDirectory
80 *
81 * @var array
82 */
83 private $tmpFiles = [];
84
85 /**
86 * Holds original values of MediaWiki configuration settings
87 * to be restored in tearDown().
88 * See also setMwGlobals().
89 * @var array
90 */
91 private $mwGlobals = [];
92
93 /**
94 * Holds list of MediaWiki configuration settings to be unset in tearDown().
95 * See also setMwGlobals().
96 * @var array
97 */
98 private $mwGlobalsToUnset = [];
99
100 /**
101 * Holds original loggers which have been replaced by setLogger()
102 * @var LoggerInterface[]
103 */
104 private $loggers = [];
105
106 /**
107 * Table name prefixes. Oracle likes it shorter.
108 */
109 const DB_PREFIX = 'unittest_';
110 const ORA_DB_PREFIX = 'ut_';
111
112 /**
113 * @var array
114 * @since 1.18
115 */
116 protected $supportedDBs = [
117 'mysql',
118 'sqlite',
119 'postgres',
120 'oracle'
121 ];
122
123 public function __construct( $name = null, array $data = [], $dataName = '' ) {
124 parent::__construct( $name, $data, $dataName );
125
126 $this->backupGlobals = false;
127 $this->backupStaticAttributes = false;
128 }
129
130 public function __destruct() {
131 // Complain if self::setUp() was called, but not self::tearDown()
132 // $this->called['setUp'] will be checked by self::testMediaWikiTestCaseParentSetupCalled()
133 if ( isset( $this->called['setUp'] ) && !isset( $this->called['tearDown'] ) ) {
134 throw new MWException( static::class . "::tearDown() must call parent::tearDown()" );
135 }
136 }
137
138 public static function setUpBeforeClass() {
139 parent::setUpBeforeClass();
140
141 // Get the service locator, and reset services if it's not done already
142 self::$serviceLocator = self::prepareServices( new GlobalVarConfig() );
143 }
144
145 /**
146 * Convenience method for getting an immutable test user
147 *
148 * @since 1.28
149 *
150 * @param string[] $groups Groups the test user should be in.
151 * @return TestUser
152 */
153 public static function getTestUser( $groups = [] ) {
154 return TestUserRegistry::getImmutableTestUser( $groups );
155 }
156
157 /**
158 * Convenience method for getting a mutable test user
159 *
160 * @since 1.28
161 *
162 * @param string[] $groups Groups the test user should be added in.
163 * @return TestUser
164 */
165 public static function getMutableTestUser( $groups = [] ) {
166 return TestUserRegistry::getMutableTestUser( __CLASS__, $groups );
167 }
168
169 /**
170 * Convenience method for getting an immutable admin test user
171 *
172 * @since 1.28
173 *
174 * @param string[] $groups Groups the test user should be added to.
175 * @return TestUser
176 */
177 public static function getTestSysop() {
178 return self::getTestUser( [ 'sysop', 'bureaucrat' ] );
179 }
180
181 /**
182 * Prepare service configuration for unit testing.
183 *
184 * This calls MediaWikiServices::resetGlobalInstance() to allow some critical services
185 * to be overridden for testing.
186 *
187 * prepareServices() only needs to be called once, but should be called as early as possible,
188 * before any class has a chance to grab a reference to any of the global services
189 * instances that get discarded by prepareServices(). Only the first call has any effect,
190 * later calls are ignored.
191 *
192 * @note This is called by PHPUnitMaintClass::finalSetup.
193 *
194 * @see MediaWikiServices::resetGlobalInstance()
195 *
196 * @param Config $bootstrapConfig The bootstrap config to use with the new
197 * MediaWikiServices. Only used for the first call to this method.
198 * @return MediaWikiServices
199 */
200 public static function prepareServices( Config $bootstrapConfig ) {
201 static $services = null;
202
203 if ( !$services ) {
204 $services = self::resetGlobalServices( $bootstrapConfig );
205 }
206 return $services;
207 }
208
209 /**
210 * Reset global services, and install testing environment.
211 * This is the testing equivalent of MediaWikiServices::resetGlobalInstance().
212 * This should only be used to set up the testing environment, not when
213 * running unit tests. Use MediaWikiTestCase::overrideMwServices() for that.
214 *
215 * @see MediaWikiServices::resetGlobalInstance()
216 * @see prepareServices()
217 * @see MediaWikiTestCase::overrideMwServices()
218 *
219 * @param Config|null $bootstrapConfig The bootstrap config to use with the new
220 * MediaWikiServices.
221 * @return MediaWikiServices
222 */
223 protected static function resetGlobalServices( Config $bootstrapConfig = null ) {
224 $oldServices = MediaWikiServices::getInstance();
225 $oldConfigFactory = $oldServices->getConfigFactory();
226 $oldLoadBalancerFactory = $oldServices->getDBLoadBalancerFactory();
227
228 $testConfig = self::makeTestConfig( $bootstrapConfig );
229
230 MediaWikiServices::resetGlobalInstance( $testConfig );
231
232 $serviceLocator = MediaWikiServices::getInstance();
233 self::installTestServices(
234 $oldConfigFactory,
235 $oldLoadBalancerFactory,
236 $serviceLocator
237 );
238 return $serviceLocator;
239 }
240
241 /**
242 * Create a config suitable for testing, based on a base config, default overrides,
243 * and custom overrides.
244 *
245 * @param Config|null $baseConfig
246 * @param Config|null $customOverrides
247 *
248 * @return Config
249 */
250 private static function makeTestConfig(
251 Config $baseConfig = null,
252 Config $customOverrides = null
253 ) {
254 $defaultOverrides = new HashConfig();
255
256 if ( !$baseConfig ) {
257 $baseConfig = MediaWikiServices::getInstance()->getBootstrapConfig();
258 }
259
260 /* Some functions require some kind of caching, and will end up using the db,
261 * which we can't allow, as that would open a new connection for mysql.
262 * Replace with a HashBag. They would not be going to persist anyway.
263 */
264 $hashCache = [ 'class' => HashBagOStuff::class, 'reportDupes' => false ];
265 $objectCaches = [
266 CACHE_DB => $hashCache,
267 CACHE_ACCEL => $hashCache,
268 CACHE_MEMCACHED => $hashCache,
269 'apc' => $hashCache,
270 'apcu' => $hashCache,
271 'wincache' => $hashCache,
272 ] + $baseConfig->get( 'ObjectCaches' );
273
274 $defaultOverrides->set( 'ObjectCaches', $objectCaches );
275 $defaultOverrides->set( 'MainCacheType', CACHE_NONE );
276 $defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => JobQueueMemory::class ] ] );
277
278 // Use a fast hash algorithm to hash passwords.
279 $defaultOverrides->set( 'PasswordDefault', 'A' );
280
281 $testConfig = $customOverrides
282 ? new MultiConfig( [ $customOverrides, $defaultOverrides, $baseConfig ] )
283 : new MultiConfig( [ $defaultOverrides, $baseConfig ] );
284
285 return $testConfig;
286 }
287
288 /**
289 * @param ConfigFactory $oldConfigFactory
290 * @param LBFactory $oldLoadBalancerFactory
291 * @param MediaWikiServices $newServices
292 *
293 * @throws MWException
294 */
295 private static function installTestServices(
296 ConfigFactory $oldConfigFactory,
297 LBFactory $oldLoadBalancerFactory,
298 MediaWikiServices $newServices
299 ) {
300 // Use bootstrap config for all configuration.
301 // This allows config overrides via global variables to take effect.
302 $bootstrapConfig = $newServices->getBootstrapConfig();
303 $newServices->resetServiceForTesting( 'ConfigFactory' );
304 $newServices->redefineService(
305 'ConfigFactory',
306 self::makeTestConfigFactoryInstantiator(
307 $oldConfigFactory,
308 [ 'main' => $bootstrapConfig ]
309 )
310 );
311 $newServices->resetServiceForTesting( 'DBLoadBalancerFactory' );
312 $newServices->redefineService(
313 'DBLoadBalancerFactory',
314 function ( MediaWikiServices $services ) use ( $oldLoadBalancerFactory ) {
315 return $oldLoadBalancerFactory;
316 }
317 );
318 }
319
320 /**
321 * @param ConfigFactory $oldFactory
322 * @param Config[] $configurations
323 *
324 * @return Closure
325 */
326 private static function makeTestConfigFactoryInstantiator(
327 ConfigFactory $oldFactory,
328 array $configurations
329 ) {
330 return function ( MediaWikiServices $services ) use ( $oldFactory, $configurations ) {
331 $factory = new ConfigFactory();
332
333 // clone configurations from $oldFactory that are not overwritten by $configurations
334 $namesToClone = array_diff(
335 $oldFactory->getConfigNames(),
336 array_keys( $configurations )
337 );
338
339 foreach ( $namesToClone as $name ) {
340 $factory->register( $name, $oldFactory->makeConfig( $name ) );
341 }
342
343 foreach ( $configurations as $name => $config ) {
344 $factory->register( $name, $config );
345 }
346
347 return $factory;
348 };
349 }
350
351 /**
352 * Resets some well known services that typically have state that may interfere with unit tests.
353 * This is a lightweight alternative to resetGlobalServices().
354 *
355 * @note There is no guarantee that no references remain to stale service instances destroyed
356 * by a call to doLightweightServiceReset().
357 *
358 * @throws MWException if called outside of PHPUnit tests.
359 *
360 * @see resetGlobalServices()
361 */
362 private function doLightweightServiceReset() {
363 global $wgRequest;
364
365 JobQueueGroup::destroySingletons();
366 ObjectCache::clear();
367 $services = MediaWikiServices::getInstance();
368 $services->resetServiceForTesting( 'MainObjectStash' );
369 $services->resetServiceForTesting( 'LocalServerObjectCache' );
370 $services->getMainWANObjectCache()->clearProcessCache();
371 FileBackendGroup::destroySingleton();
372
373 // TODO: move global state into MediaWikiServices
374 RequestContext::resetMain();
375 if ( session_id() !== '' ) {
376 session_write_close();
377 session_id( '' );
378 }
379
380 $wgRequest = new FauxRequest();
381 MediaWiki\Session\SessionManager::resetCache();
382 }
383
384 public function run( PHPUnit_Framework_TestResult $result = null ) {
385 // Reset all caches between tests.
386 $this->doLightweightServiceReset();
387
388 $needsResetDB = false;
389
390 if ( !self::$dbSetup || $this->needsDB() ) {
391 // set up a DB connection for this test to use
392
393 self::$useTemporaryTables = !$this->getCliArg( 'use-normal-tables' );
394 self::$reuseDB = $this->getCliArg( 'reuse-db' );
395
396 $this->db = wfGetDB( DB_MASTER );
397
398 $this->checkDbIsSupported();
399
400 if ( !self::$dbSetup ) {
401 $this->setupAllTestDBs();
402 $this->addCoreDBData();
403
404 if ( ( $this->db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
405 $this->resetDB( $this->db, $this->tablesUsed );
406 }
407 }
408
409 // TODO: the DB setup should be done in setUpBeforeClass(), so the test DB
410 // is available in subclass's setUpBeforeClass() and setUp() methods.
411 // This would also remove the need for the HACK that is oncePerClass().
412 if ( $this->oncePerClass() ) {
413 $this->setUpSchema( $this->db );
414 $this->addDBDataOnce();
415 }
416
417 $this->addDBData();
418 $needsResetDB = true;
419 }
420
421 parent::run( $result );
422
423 if ( $needsResetDB ) {
424 $this->resetDB( $this->db, $this->tablesUsed );
425 }
426 }
427
428 /**
429 * @return bool
430 */
431 private function oncePerClass() {
432 // Remember current test class in the database connection,
433 // so we know when we need to run addData.
434
435 $class = static::class;
436
437 $first = !isset( $this->db->_hasDataForTestClass )
438 || $this->db->_hasDataForTestClass !== $class;
439
440 $this->db->_hasDataForTestClass = $class;
441 return $first;
442 }
443
444 /**
445 * @since 1.21
446 *
447 * @return bool
448 */
449 public function usesTemporaryTables() {
450 return self::$useTemporaryTables;
451 }
452
453 /**
454 * Obtains a new temporary file name
455 *
456 * The obtained filename is enlisted to be removed upon tearDown
457 *
458 * @since 1.20
459 *
460 * @return string Absolute name of the temporary file
461 */
462 protected function getNewTempFile() {
463 $fileName = tempnam( wfTempDir(), 'MW_PHPUnit_' . static::class . '_' );
464 $this->tmpFiles[] = $fileName;
465
466 return $fileName;
467 }
468
469 /**
470 * obtains a new temporary directory
471 *
472 * The obtained directory is enlisted to be removed (recursively with all its contained
473 * files) upon tearDown.
474 *
475 * @since 1.20
476 *
477 * @return string Absolute name of the temporary directory
478 */
479 protected function getNewTempDirectory() {
480 // Starting of with a temporary /file/.
481 $fileName = $this->getNewTempFile();
482
483 // Converting the temporary /file/ to a /directory/
484 // The following is not atomic, but at least we now have a single place,
485 // where temporary directory creation is bundled and can be improved
486 unlink( $fileName );
487 $this->assertTrue( wfMkdirParents( $fileName ) );
488
489 return $fileName;
490 }
491
492 protected function setUp() {
493 parent::setUp();
494 $this->called['setUp'] = true;
495
496 $this->phpErrorLevel = intval( ini_get( 'error_reporting' ) );
497
498 // Cleaning up temporary files
499 foreach ( $this->tmpFiles as $fileName ) {
500 if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
501 unlink( $fileName );
502 } elseif ( is_dir( $fileName ) ) {
503 wfRecursiveRemoveDir( $fileName );
504 }
505 }
506
507 if ( $this->needsDB() && $this->db ) {
508 // Clean up open transactions
509 while ( $this->db->trxLevel() > 0 ) {
510 $this->db->rollback( __METHOD__, 'flush' );
511 }
512 // Check for unsafe queries
513 if ( $this->db->getType() === 'mysql' ) {
514 $this->db->query( "SET sql_mode = 'STRICT_ALL_TABLES'" );
515 }
516 }
517
518 DeferredUpdates::clearPendingUpdates();
519 ObjectCache::getMainWANInstance()->clearProcessCache();
520
521 // XXX: reset maintenance triggers
522 // Hook into period lag checks which often happen in long-running scripts
523 $services = MediaWikiServices::getInstance();
524 $lbFactory = $services->getDBLoadBalancerFactory();
525 Maintenance::setLBFactoryTriggers( $lbFactory, $services->getMainConfig() );
526
527 ob_start( 'MediaWikiTestCase::wfResetOutputBuffersBarrier' );
528 }
529
530 protected function addTmpFiles( $files ) {
531 $this->tmpFiles = array_merge( $this->tmpFiles, (array)$files );
532 }
533
534 protected function tearDown() {
535 global $wgRequest, $wgSQLMode;
536
537 $status = ob_get_status();
538 if ( isset( $status['name'] ) &&
539 $status['name'] === 'MediaWikiTestCase::wfResetOutputBuffersBarrier'
540 ) {
541 ob_end_flush();
542 }
543
544 $this->called['tearDown'] = true;
545 // Cleaning up temporary files
546 foreach ( $this->tmpFiles as $fileName ) {
547 if ( is_file( $fileName ) || ( is_link( $fileName ) ) ) {
548 unlink( $fileName );
549 } elseif ( is_dir( $fileName ) ) {
550 wfRecursiveRemoveDir( $fileName );
551 }
552 }
553
554 if ( $this->needsDB() && $this->db ) {
555 // Clean up open transactions
556 while ( $this->db->trxLevel() > 0 ) {
557 $this->db->rollback( __METHOD__, 'flush' );
558 }
559 if ( $this->db->getType() === 'mysql' ) {
560 $this->db->query( "SET sql_mode = " . $this->db->addQuotes( $wgSQLMode ) );
561 }
562 }
563
564 // Restore mw globals
565 foreach ( $this->mwGlobals as $key => $value ) {
566 $GLOBALS[$key] = $value;
567 }
568 foreach ( $this->mwGlobalsToUnset as $value ) {
569 unset( $GLOBALS[$value] );
570 }
571 $this->mwGlobals = [];
572 $this->mwGlobalsToUnset = [];
573 $this->restoreLoggers();
574
575 if ( self::$serviceLocator && MediaWikiServices::getInstance() !== self::$serviceLocator ) {
576 MediaWikiServices::forceGlobalInstance( self::$serviceLocator );
577 }
578
579 // TODO: move global state into MediaWikiServices
580 RequestContext::resetMain();
581 if ( session_id() !== '' ) {
582 session_write_close();
583 session_id( '' );
584 }
585 $wgRequest = new FauxRequest();
586 MediaWiki\Session\SessionManager::resetCache();
587 MediaWiki\Auth\AuthManager::resetCache();
588
589 $phpErrorLevel = intval( ini_get( 'error_reporting' ) );
590
591 if ( $phpErrorLevel !== $this->phpErrorLevel ) {
592 ini_set( 'error_reporting', $this->phpErrorLevel );
593
594 $oldHex = strtoupper( dechex( $this->phpErrorLevel ) );
595 $newHex = strtoupper( dechex( $phpErrorLevel ) );
596 $message = "PHP error_reporting setting was left dirty: "
597 . "was 0x$oldHex before test, 0x$newHex after test!";
598
599 $this->fail( $message );
600 }
601
602 parent::tearDown();
603 }
604
605 /**
606 * Make sure MediaWikiTestCase extending classes have called their
607 * parent setUp method
608 *
609 * With strict coverage activated in PHP_CodeCoverage, this test would be
610 * marked as risky without the following annotation (T152923).
611 * @coversNothing
612 */
613 final public function testMediaWikiTestCaseParentSetupCalled() {
614 $this->assertArrayHasKey( 'setUp', $this->called,
615 static::class . '::setUp() must call parent::setUp()'
616 );
617 }
618
619 /**
620 * Sets a service, maintaining a stashed version of the previous service to be
621 * restored in tearDown
622 *
623 * @since 1.27
624 *
625 * @param string $name
626 * @param object $object
627 */
628 protected function setService( $name, $object ) {
629 // If we did not yet override the service locator, so so now.
630 if ( MediaWikiServices::getInstance() === self::$serviceLocator ) {
631 $this->overrideMwServices();
632 }
633
634 MediaWikiServices::getInstance()->disableService( $name );
635 MediaWikiServices::getInstance()->redefineService(
636 $name,
637 function () use ( $object ) {
638 return $object;
639 }
640 );
641 }
642
643 /**
644 * Sets a global, maintaining a stashed version of the previous global to be
645 * restored in tearDown
646 *
647 * The key is added to the array of globals that will be reset afterwards
648 * in the tearDown().
649 *
650 * @par Example
651 * @code
652 * protected function setUp() {
653 * $this->setMwGlobals( 'wgRestrictStuff', true );
654 * }
655 *
656 * function testFoo() {}
657 *
658 * function testBar() {}
659 * $this->assertTrue( self::getX()->doStuff() );
660 *
661 * $this->setMwGlobals( 'wgRestrictStuff', false );
662 * $this->assertTrue( self::getX()->doStuff() );
663 * }
664 *
665 * function testQuux() {}
666 * @endcode
667 *
668 * @param array|string $pairs Key to the global variable, or an array
669 * of key/value pairs.
670 * @param mixed $value Value to set the global to (ignored
671 * if an array is given as first argument).
672 *
673 * @note To allow changes to global variables to take effect on global service instances,
674 * call overrideMwServices().
675 *
676 * @since 1.21
677 */
678 protected function setMwGlobals( $pairs, $value = null ) {
679 if ( is_string( $pairs ) ) {
680 $pairs = [ $pairs => $value ];
681 }
682
683 $this->stashMwGlobals( array_keys( $pairs ) );
684
685 foreach ( $pairs as $key => $value ) {
686 $GLOBALS[$key] = $value;
687 }
688 }
689
690 /**
691 * Check if we can back up a value by performing a shallow copy.
692 * Values which fail this test are copied recursively.
693 *
694 * @param mixed $value
695 * @return bool True if a shallow copy will do; false if a deep copy
696 * is required.
697 */
698 private static function canShallowCopy( $value ) {
699 if ( is_scalar( $value ) || $value === null ) {
700 return true;
701 }
702 if ( is_array( $value ) ) {
703 foreach ( $value as $subValue ) {
704 if ( !is_scalar( $subValue ) && $subValue !== null ) {
705 return false;
706 }
707 }
708 return true;
709 }
710 return false;
711 }
712
713 /**
714 * Stashes the global, will be restored in tearDown()
715 *
716 * Individual test functions may override globals through the setMwGlobals() function
717 * or directly. When directly overriding globals their keys should first be passed to this
718 * method in setUp to avoid breaking global state for other tests
719 *
720 * That way all other tests are executed with the same settings (instead of using the
721 * unreliable local settings for most tests and fix it only for some tests).
722 *
723 * @param array|string $globalKeys Key to the global variable, or an array of keys.
724 *
725 * @note To allow changes to global variables to take effect on global service instances,
726 * call overrideMwServices().
727 *
728 * @since 1.23
729 */
730 protected function stashMwGlobals( $globalKeys ) {
731 if ( is_string( $globalKeys ) ) {
732 $globalKeys = [ $globalKeys ];
733 }
734
735 foreach ( $globalKeys as $globalKey ) {
736 // NOTE: make sure we only save the global once or a second call to
737 // setMwGlobals() on the same global would override the original
738 // value.
739 if (
740 !array_key_exists( $globalKey, $this->mwGlobals ) &&
741 !array_key_exists( $globalKey, $this->mwGlobalsToUnset )
742 ) {
743 if ( !array_key_exists( $globalKey, $GLOBALS ) ) {
744 $this->mwGlobalsToUnset[$globalKey] = $globalKey;
745 continue;
746 }
747 // NOTE: we serialize then unserialize the value in case it is an object
748 // this stops any objects being passed by reference. We could use clone
749 // and if is_object but this does account for objects within objects!
750 if ( self::canShallowCopy( $GLOBALS[$globalKey] ) ) {
751 $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
752 } elseif (
753 // Many MediaWiki types are safe to clone. These are the
754 // ones that are most commonly stashed.
755 $GLOBALS[$globalKey] instanceof Language ||
756 $GLOBALS[$globalKey] instanceof User ||
757 $GLOBALS[$globalKey] instanceof FauxRequest
758 ) {
759 $this->mwGlobals[$globalKey] = clone $GLOBALS[$globalKey];
760 } elseif ( $this->containsClosure( $GLOBALS[$globalKey] ) ) {
761 // Serializing Closure only gives a warning on HHVM while
762 // it throws an Exception on Zend.
763 // Workaround for https://github.com/facebook/hhvm/issues/6206
764 $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
765 } else {
766 try {
767 $this->mwGlobals[$globalKey] = unserialize( serialize( $GLOBALS[$globalKey] ) );
768 } catch ( Exception $e ) {
769 $this->mwGlobals[$globalKey] = $GLOBALS[$globalKey];
770 }
771 }
772 }
773 }
774 }
775
776 /**
777 * @param mixed $var
778 * @param int $maxDepth
779 *
780 * @return bool
781 */
782 private function containsClosure( $var, $maxDepth = 15 ) {
783 if ( $var instanceof Closure ) {
784 return true;
785 }
786 if ( !is_array( $var ) || $maxDepth === 0 ) {
787 return false;
788 }
789
790 foreach ( $var as $value ) {
791 if ( $this->containsClosure( $value, $maxDepth - 1 ) ) {
792 return true;
793 }
794 }
795 return false;
796 }
797
798 /**
799 * Merges the given values into a MW global array variable.
800 * Useful for setting some entries in a configuration array, instead of
801 * setting the entire array.
802 *
803 * @param string $name The name of the global, as in wgFooBar
804 * @param array $values The array containing the entries to set in that global
805 *
806 * @throws MWException If the designated global is not an array.
807 *
808 * @note To allow changes to global variables to take effect on global service instances,
809 * call overrideMwServices().
810 *
811 * @since 1.21
812 */
813 protected function mergeMwGlobalArrayValue( $name, $values ) {
814 if ( !isset( $GLOBALS[$name] ) ) {
815 $merged = $values;
816 } else {
817 if ( !is_array( $GLOBALS[$name] ) ) {
818 throw new MWException( "MW global $name is not an array." );
819 }
820
821 // NOTE: do not use array_merge, it screws up for numeric keys.
822 $merged = $GLOBALS[$name];
823 foreach ( $values as $k => $v ) {
824 $merged[$k] = $v;
825 }
826 }
827
828 $this->setMwGlobals( $name, $merged );
829 }
830
831 /**
832 * Stashes the global instance of MediaWikiServices, and installs a new one,
833 * allowing test cases to override settings and services.
834 * The previous instance of MediaWikiServices will be restored on tearDown.
835 *
836 * @since 1.27
837 *
838 * @param Config $configOverrides Configuration overrides for the new MediaWikiServices instance.
839 * @param callable[] $services An associative array of services to re-define. Keys are service
840 * names, values are callables.
841 *
842 * @return MediaWikiServices
843 * @throws MWException
844 */
845 protected function overrideMwServices( Config $configOverrides = null, array $services = [] ) {
846 if ( !$configOverrides ) {
847 $configOverrides = new HashConfig();
848 }
849
850 $oldInstance = MediaWikiServices::getInstance();
851 $oldConfigFactory = $oldInstance->getConfigFactory();
852 $oldLoadBalancerFactory = $oldInstance->getDBLoadBalancerFactory();
853
854 $testConfig = self::makeTestConfig( null, $configOverrides );
855 $newInstance = new MediaWikiServices( $testConfig );
856
857 // Load the default wiring from the specified files.
858 // NOTE: this logic mirrors the logic in MediaWikiServices::newInstance.
859 $wiringFiles = $testConfig->get( 'ServiceWiringFiles' );
860 $newInstance->loadWiringFiles( $wiringFiles );
861
862 // Provide a traditional hook point to allow extensions to configure services.
863 Hooks::run( 'MediaWikiServices', [ $newInstance ] );
864
865 foreach ( $services as $name => $callback ) {
866 $newInstance->redefineService( $name, $callback );
867 }
868
869 self::installTestServices(
870 $oldConfigFactory,
871 $oldLoadBalancerFactory,
872 $newInstance
873 );
874 MediaWikiServices::forceGlobalInstance( $newInstance );
875
876 return $newInstance;
877 }
878
879 /**
880 * @since 1.27
881 * @param string|Language $lang
882 */
883 public function setUserLang( $lang ) {
884 RequestContext::getMain()->setLanguage( $lang );
885 $this->setMwGlobals( 'wgLang', RequestContext::getMain()->getLanguage() );
886 }
887
888 /**
889 * @since 1.27
890 * @param string|Language $lang
891 */
892 public function setContentLang( $lang ) {
893 if ( $lang instanceof Language ) {
894 $langCode = $lang->getCode();
895 $langObj = $lang;
896 } else {
897 $langCode = $lang;
898 $langObj = Language::factory( $langCode );
899 }
900 $this->setMwGlobals( [
901 'wgLanguageCode' => $langCode,
902 'wgContLang' => $langObj,
903 ] );
904 }
905
906 /**
907 * Sets the logger for a specified channel, for the duration of the test.
908 * @since 1.27
909 * @param string $channel
910 * @param LoggerInterface $logger
911 */
912 protected function setLogger( $channel, LoggerInterface $logger ) {
913 // TODO: Once loggers are managed by MediaWikiServices, use
914 // overrideMwServices() to set loggers.
915
916 $provider = LoggerFactory::getProvider();
917 $wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
918 $singletons = $wrappedProvider->singletons;
919 if ( $provider instanceof MonologSpi ) {
920 if ( !isset( $this->loggers[$channel] ) ) {
921 $this->loggers[$channel] = isset( $singletons['loggers'][$channel] )
922 ? $singletons['loggers'][$channel] : null;
923 }
924 $singletons['loggers'][$channel] = $logger;
925 } elseif ( $provider instanceof LegacySpi ) {
926 if ( !isset( $this->loggers[$channel] ) ) {
927 $this->loggers[$channel] = isset( $singletons[$channel] ) ? $singletons[$channel] : null;
928 }
929 $singletons[$channel] = $logger;
930 } else {
931 throw new LogicException( __METHOD__ . ': setting a logger for ' . get_class( $provider )
932 . ' is not implemented' );
933 }
934 $wrappedProvider->singletons = $singletons;
935 }
936
937 /**
938 * Restores loggers replaced by setLogger().
939 * @since 1.27
940 */
941 private function restoreLoggers() {
942 $provider = LoggerFactory::getProvider();
943 $wrappedProvider = TestingAccessWrapper::newFromObject( $provider );
944 $singletons = $wrappedProvider->singletons;
945 foreach ( $this->loggers as $channel => $logger ) {
946 if ( $provider instanceof MonologSpi ) {
947 if ( $logger === null ) {
948 unset( $singletons['loggers'][$channel] );
949 } else {
950 $singletons['loggers'][$channel] = $logger;
951 }
952 } elseif ( $provider instanceof LegacySpi ) {
953 if ( $logger === null ) {
954 unset( $singletons[$channel] );
955 } else {
956 $singletons[$channel] = $logger;
957 }
958 }
959 }
960 $wrappedProvider->singletons = $singletons;
961 $this->loggers = [];
962 }
963
964 /**
965 * @return string
966 * @since 1.18
967 */
968 public function dbPrefix() {
969 return $this->db->getType() == 'oracle' ? self::ORA_DB_PREFIX : self::DB_PREFIX;
970 }
971
972 /**
973 * @return bool
974 * @since 1.18
975 */
976 public function needsDB() {
977 // If the test says it uses database tables, it needs the database
978 if ( $this->tablesUsed ) {
979 return true;
980 }
981
982 // If the test class says it belongs to the Database group, it needs the database.
983 // NOTE: This ONLY checks for the group in the class level doc comment.
984 $rc = new ReflectionClass( $this );
985 if ( preg_match( '/@group +Database/im', $rc->getDocComment() ) ) {
986 return true;
987 }
988
989 return false;
990 }
991
992 /**
993 * Insert a new page.
994 *
995 * Should be called from addDBData().
996 *
997 * @since 1.25 ($namespace in 1.28)
998 * @param string|title $pageName Page name or title
999 * @param string $text Page's content
1000 * @param int $namespace Namespace id (name cannot already contain namespace)
1001 * @return array Title object and page id
1002 */
1003 protected function insertPage(
1004 $pageName,
1005 $text = 'Sample page for unit test.',
1006 $namespace = null
1007 ) {
1008 if ( is_string( $pageName ) ) {
1009 $title = Title::newFromText( $pageName, $namespace );
1010 } else {
1011 $title = $pageName;
1012 }
1013
1014 $user = static::getTestSysop()->getUser();
1015 $comment = __METHOD__ . ': Sample page for unit test.';
1016
1017 $page = WikiPage::factory( $title );
1018 $page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
1019
1020 return [
1021 'title' => $title,
1022 'id' => $page->getId(),
1023 ];
1024 }
1025
1026 /**
1027 * Stub. If a test suite needs to add additional data to the database, it should
1028 * implement this method and do so. This method is called once per test suite
1029 * (i.e. once per class).
1030 *
1031 * Note data added by this method may be removed by resetDB() depending on
1032 * the contents of $tablesUsed.
1033 *
1034 * To add additional data between test function runs, override prepareDB().
1035 *
1036 * @see addDBData()
1037 * @see resetDB()
1038 *
1039 * @since 1.27
1040 */
1041 public function addDBDataOnce() {
1042 }
1043
1044 /**
1045 * Stub. Subclasses may override this to prepare the database.
1046 * Called before every test run (test function or data set).
1047 *
1048 * @see addDBDataOnce()
1049 * @see resetDB()
1050 *
1051 * @since 1.18
1052 */
1053 public function addDBData() {
1054 }
1055
1056 private function addCoreDBData() {
1057 if ( $this->db->getType() == 'oracle' ) {
1058 # Insert 0 user to prevent FK violations
1059 # Anonymous user
1060 if ( !$this->db->selectField( 'user', '1', [ 'user_id' => 0 ] ) ) {
1061 $this->db->insert( 'user', [
1062 'user_id' => 0,
1063 'user_name' => 'Anonymous' ], __METHOD__, [ 'IGNORE' ] );
1064 }
1065
1066 # Insert 0 page to prevent FK violations
1067 # Blank page
1068 if ( !$this->db->selectField( 'page', '1', [ 'page_id' => 0 ] ) ) {
1069 $this->db->insert( 'page', [
1070 'page_id' => 0,
1071 'page_namespace' => 0,
1072 'page_title' => ' ',
1073 'page_restrictions' => null,
1074 'page_is_redirect' => 0,
1075 'page_is_new' => 0,
1076 'page_random' => 0,
1077 'page_touched' => $this->db->timestamp(),
1078 'page_latest' => 0,
1079 'page_len' => 0 ], __METHOD__, [ 'IGNORE' ] );
1080 }
1081 }
1082
1083 SiteStatsInit::doPlaceholderInit();
1084
1085 User::resetIdByNameCache();
1086
1087 // Make sysop user
1088 $user = static::getTestSysop()->getUser();
1089
1090 // Make 1 page with 1 revision
1091 $page = WikiPage::factory( Title::newFromText( 'UTPage' ) );
1092 if ( $page->getId() == 0 ) {
1093 $page->doEditContent(
1094 new WikitextContent( 'UTContent' ),
1095 'UTPageSummary',
1096 EDIT_NEW | EDIT_SUPPRESS_RC,
1097 false,
1098 $user
1099 );
1100 // an edit always attempt to purge backlink links such as history
1101 // pages. That is unneccessary.
1102 JobQueueGroup::singleton()->get( 'htmlCacheUpdate' )->delete();
1103 // WikiPages::doEditUpdates randomly adds RC purges
1104 JobQueueGroup::singleton()->get( 'recentChangesUpdate' )->delete();
1105
1106 // doEditContent() probably started the session via
1107 // User::loadFromSession(). Close it now.
1108 if ( session_id() !== '' ) {
1109 session_write_close();
1110 session_id( '' );
1111 }
1112 }
1113 }
1114
1115 /**
1116 * Restores MediaWiki to using the table set (table prefix) it was using before
1117 * setupTestDB() was called. Useful if we need to perform database operations
1118 * after the test run has finished (such as saving logs or profiling info).
1119 *
1120 * @since 1.21
1121 */
1122 public static function teardownTestDB() {
1123 global $wgJobClasses;
1124
1125 if ( !self::$dbSetup ) {
1126 return;
1127 }
1128
1129 Hooks::run( 'UnitTestsBeforeDatabaseTeardown' );
1130
1131 foreach ( $wgJobClasses as $type => $class ) {
1132 // Delete any jobs under the clone DB (or old prefix in other stores)
1133 JobQueueGroup::singleton()->get( $type )->delete();
1134 }
1135
1136 CloneDatabase::changePrefix( self::$oldTablePrefix );
1137
1138 self::$oldTablePrefix = false;
1139 self::$dbSetup = false;
1140 }
1141
1142 /**
1143 * Setups a database with the given prefix.
1144 *
1145 * If reuseDB is true and certain conditions apply, it will just change the prefix.
1146 * Otherwise, it will clone the tables and change the prefix.
1147 *
1148 * Clones all tables in the given database (whatever database that connection has
1149 * open), to versions with the test prefix.
1150 *
1151 * @param IMaintainableDatabase $db Database to use
1152 * @param string $prefix Prefix to use for test tables
1153 * @return bool True if tables were cloned, false if only the prefix was changed
1154 */
1155 protected static function setupDatabaseWithTestPrefix( IMaintainableDatabase $db, $prefix ) {
1156 $tablesCloned = self::listTables( $db );
1157 $dbClone = new CloneDatabase( $db, $tablesCloned, $prefix );
1158 $dbClone->useTemporaryTables( self::$useTemporaryTables );
1159
1160 $db->_originalTablePrefix = $db->tablePrefix();
1161
1162 if ( ( $db->getType() == 'oracle' || !self::$useTemporaryTables ) && self::$reuseDB ) {
1163 CloneDatabase::changePrefix( $prefix );
1164
1165 return false;
1166 } else {
1167 $dbClone->cloneTableStructure();
1168 return true;
1169 }
1170 }
1171
1172 /**
1173 * Set up all test DBs
1174 */
1175 public function setupAllTestDBs() {
1176 global $wgDBprefix;
1177
1178 self::$oldTablePrefix = $wgDBprefix;
1179
1180 $testPrefix = $this->dbPrefix();
1181
1182 // switch to a temporary clone of the database
1183 self::setupTestDB( $this->db, $testPrefix );
1184
1185 if ( self::isUsingExternalStoreDB() ) {
1186 self::setupExternalStoreTestDBs( $testPrefix );
1187 }
1188 }
1189
1190 /**
1191 * Creates an empty skeleton of the wiki database by cloning its structure
1192 * to equivalent tables using the given $prefix. Then sets MediaWiki to
1193 * use the new set of tables (aka schema) instead of the original set.
1194 *
1195 * This is used to generate a dummy table set, typically consisting of temporary
1196 * tables, that will be used by tests instead of the original wiki database tables.
1197 *
1198 * @since 1.21
1199 *
1200 * @note the original table prefix is stored in self::$oldTablePrefix. This is used
1201 * by teardownTestDB() to return the wiki to using the original table set.
1202 *
1203 * @note this method only works when first called. Subsequent calls have no effect,
1204 * even if using different parameters.
1205 *
1206 * @param Database $db The database connection
1207 * @param string $prefix The prefix to use for the new table set (aka schema).
1208 *
1209 * @throws MWException If the database table prefix is already $prefix
1210 */
1211 public static function setupTestDB( Database $db, $prefix ) {
1212 if ( self::$dbSetup ) {
1213 return;
1214 }
1215
1216 if ( $db->tablePrefix() === $prefix ) {
1217 throw new MWException(
1218 'Cannot run unit tests, the database prefix is already "' . $prefix . '"' );
1219 }
1220
1221 // TODO: the below should be re-written as soon as LBFactory, LoadBalancer,
1222 // and Database no longer use global state.
1223
1224 self::$dbSetup = true;
1225
1226 if ( !self::setupDatabaseWithTestPrefix( $db, $prefix ) ) {
1227 return;
1228 }
1229
1230 // Assuming this isn't needed for External Store database, and not sure if the procedure
1231 // would be available there.
1232 if ( $db->getType() == 'oracle' ) {
1233 $db->query( 'BEGIN FILL_WIKI_INFO; END;' );
1234 }
1235
1236 Hooks::run( 'UnitTestsAfterDatabaseSetup', [ $db, $prefix ] );
1237 }
1238
1239 /**
1240 * Clones the External Store database(s) for testing
1241 *
1242 * @param string $testPrefix Prefix for test tables
1243 */
1244 protected static function setupExternalStoreTestDBs( $testPrefix ) {
1245 $connections = self::getExternalStoreDatabaseConnections();
1246 foreach ( $connections as $dbw ) {
1247 // Hack: cloneTableStructure sets $wgDBprefix to the unit test
1248 // prefix,. Even though listTables now uses tablePrefix, that
1249 // itself is populated from $wgDBprefix by default.
1250
1251 // We have to set it back, or we won't find the original 'blobs'
1252 // table to copy.
1253
1254 $dbw->tablePrefix( self::$oldTablePrefix );
1255 self::setupDatabaseWithTestPrefix( $dbw, $testPrefix );
1256 }
1257 }
1258
1259 /**
1260 * Gets master database connections for all of the ExternalStoreDB
1261 * stores configured in $wgDefaultExternalStore.
1262 *
1263 * @return Database[] Array of Database master connections
1264 */
1265 protected static function getExternalStoreDatabaseConnections() {
1266 global $wgDefaultExternalStore;
1267
1268 /** @var ExternalStoreDB $externalStoreDB */
1269 $externalStoreDB = ExternalStore::getStoreObject( 'DB' );
1270 $defaultArray = (array)$wgDefaultExternalStore;
1271 $dbws = [];
1272 foreach ( $defaultArray as $url ) {
1273 if ( strpos( $url, 'DB://' ) === 0 ) {
1274 list( $proto, $cluster ) = explode( '://', $url, 2 );
1275 // Avoid getMaster() because setupDatabaseWithTestPrefix()
1276 // requires Database instead of plain DBConnRef/IDatabase
1277 $dbws[] = $externalStoreDB->getMaster( $cluster );
1278 }
1279 }
1280
1281 return $dbws;
1282 }
1283
1284 /**
1285 * Check whether ExternalStoreDB is being used
1286 *
1287 * @return bool True if it's being used
1288 */
1289 protected static function isUsingExternalStoreDB() {
1290 global $wgDefaultExternalStore;
1291 if ( !$wgDefaultExternalStore ) {
1292 return false;
1293 }
1294
1295 $defaultArray = (array)$wgDefaultExternalStore;
1296 foreach ( $defaultArray as $url ) {
1297 if ( strpos( $url, 'DB://' ) === 0 ) {
1298 return true;
1299 }
1300 }
1301
1302 return false;
1303 }
1304
1305 /**
1306 * @throws LogicException if the given database connection is not a set up to use
1307 * mock tables.
1308 */
1309 private function ensureMockDatabaseConnection( IDatabase $db ) {
1310 if ( $db->tablePrefix() !== $this->dbPrefix() ) {
1311 throw new LogicException(
1312 'Trying to delete mock tables, but table prefix does not indicate a mock database.'
1313 );
1314 }
1315 }
1316
1317 private static $schemaOverrideDefaults = [
1318 'scripts' => [],
1319 'create' => [],
1320 'drop' => [],
1321 'alter' => [],
1322 ];
1323
1324 /**
1325 * Stub. If a test suite needs to test against a specific database schema, it should
1326 * override this method and return the appropriate information from it.
1327 *
1328 * @param IMaintainableDatabase $db The DB connection to use for the mock schema.
1329 * May be used to check the current state of the schema, to determine what
1330 * overrides are needed.
1331 *
1332 * @return array An associative array with the following fields:
1333 * - 'scripts': any SQL scripts to run. If empty or not present, schema overrides are skipped.
1334 * - 'create': A list of tables created (may or may not exist in the original schema).
1335 * - 'drop': A list of tables dropped (expected to be present in the original schema).
1336 * - 'alter': A list of tables altered (expected to be present in the original schema).
1337 */
1338 protected function getSchemaOverrides( IMaintainableDatabase $db ) {
1339 return [];
1340 }
1341
1342 /**
1343 * Undoes the dpecified schema overrides..
1344 * Called once per test class, just before addDataOnce().
1345 *
1346 * @param IMaintainableDatabase $db
1347 * @param array $oldOverrides
1348 */
1349 private function undoSchemaOverrides( IMaintainableDatabase $db, $oldOverrides ) {
1350 $this->ensureMockDatabaseConnection( $db );
1351
1352 $oldOverrides = $oldOverrides + self::$schemaOverrideDefaults;
1353 $originalTables = $this->listOriginalTables( $db );
1354
1355 // Drop tables that need to be restored or removed.
1356 $tablesToDrop = array_merge( $oldOverrides['create'], $oldOverrides['alter'] );
1357
1358 // Restore tables that have been dropped or created or altered,
1359 // if they exist in the original schema.
1360 $tablesToRestore = array_merge( $tablesToDrop, $oldOverrides['drop'] );
1361 $tablesToRestore = array_intersect( $originalTables, $tablesToRestore );
1362
1363 if ( $tablesToDrop ) {
1364 $this->dropMockTables( $db, $tablesToDrop );
1365 }
1366
1367 if ( $tablesToRestore ) {
1368 $this->recloneMockTables( $db, $tablesToRestore );
1369 }
1370 }
1371
1372 /**
1373 * Applies the schema overrides returned by getSchemaOverrides(),
1374 * after undoing any previously applied schema overrides.
1375 * Called once per test class, just before addDataOnce().
1376 */
1377 private function setUpSchema( IMaintainableDatabase $db ) {
1378 // Undo any active overrides.
1379 $oldOverrides = isset( $db->_schemaOverrides ) ? $db->_schemaOverrides
1380 : self::$schemaOverrideDefaults;
1381
1382 if ( $oldOverrides['alter'] || $oldOverrides['create'] || $oldOverrides['drop'] ) {
1383 $this->undoSchemaOverrides( $db, $oldOverrides );
1384 }
1385
1386 // Determine new overrides.
1387 $overrides = $this->getSchemaOverrides( $db ) + self::$schemaOverrideDefaults;
1388
1389 $extraKeys = array_diff(
1390 array_keys( $overrides ),
1391 array_keys( self::$schemaOverrideDefaults )
1392 );
1393
1394 if ( $extraKeys ) {
1395 throw new InvalidArgumentException(
1396 'Schema override contains extra keys: ' . var_export( $extraKeys, true )
1397 );
1398 }
1399
1400 if ( !$overrides['scripts'] ) {
1401 // no scripts to run
1402 return;
1403 }
1404
1405 if ( !$overrides['create'] && !$overrides['drop'] && !$overrides['alter'] ) {
1406 throw new InvalidArgumentException(
1407 'Schema override scripts given, but no tables are declared to be '
1408 . 'created, dropped or altered.'
1409 );
1410 }
1411
1412 $this->ensureMockDatabaseConnection( $db );
1413
1414 // Drop the tables that will be created by the schema scripts.
1415 $originalTables = $this->listOriginalTables( $db );
1416 $tablesToDrop = array_intersect( $originalTables, $overrides['create'] );
1417
1418 if ( $tablesToDrop ) {
1419 $this->dropMockTables( $db, $tablesToDrop );
1420 }
1421
1422 // Run schema override scripts.
1423 foreach ( $overrides['scripts'] as $script ) {
1424 $db->sourceFile(
1425 $script,
1426 null,
1427 null,
1428 __METHOD__,
1429 function ( $cmd ) {
1430 return $this->mungeSchemaUpdateQuery( $cmd );
1431 }
1432 );
1433 }
1434
1435 $db->_schemaOverrides = $overrides;
1436 }
1437
1438 private function mungeSchemaUpdateQuery( $cmd ) {
1439 return self::$useTemporaryTables
1440 ? preg_replace( '/\bCREATE\s+TABLE\b/i', 'CREATE TEMPORARY TABLE', $cmd )
1441 : $cmd;
1442 }
1443
1444 /**
1445 * Drops the given mock tables.
1446 *
1447 * @param IMaintainableDatabase $db
1448 * @param array $tables
1449 */
1450 private function dropMockTables( IMaintainableDatabase $db, array $tables ) {
1451 $this->ensureMockDatabaseConnection( $db );
1452
1453 foreach ( $tables as $tbl ) {
1454 $tbl = $db->tableName( $tbl );
1455 $db->query( "DROP TABLE IF EXISTS $tbl", __METHOD__ );
1456
1457 if ( $tbl === 'page' ) {
1458 // Forget about the pages since they don't
1459 // exist in the DB.
1460 LinkCache::singleton()->clear();
1461 }
1462 }
1463 }
1464
1465 /**
1466 * Lists all tables in the live database schema.
1467 *
1468 * @param IMaintainableDatabase $db
1469 * @return array
1470 */
1471 private function listOriginalTables( IMaintainableDatabase $db ) {
1472 if ( !isset( $db->_originalTablePrefix ) ) {
1473 throw new LogicException( 'No original table prefix know, cannot list tables!' );
1474 }
1475
1476 $originalTables = $db->listTables( $db->_originalTablePrefix, __METHOD__ );
1477 return $originalTables;
1478 }
1479
1480 /**
1481 * Re-clones the given mock tables to restore them based on the live database schema.
1482 * The tables listed in $tables are expected to currently not exist, so dropMockTables()
1483 * should be called first.
1484 *
1485 * @param IMaintainableDatabase $db
1486 * @param array $tables
1487 */
1488 private function recloneMockTables( IMaintainableDatabase $db, array $tables ) {
1489 $this->ensureMockDatabaseConnection( $db );
1490
1491 if ( !isset( $db->_originalTablePrefix ) ) {
1492 throw new LogicException( 'No original table prefix know, cannot restore tables!' );
1493 }
1494
1495 $originalTables = $this->listOriginalTables( $db );
1496 $tables = array_intersect( $tables, $originalTables );
1497
1498 $dbClone = new CloneDatabase( $db, $tables, $db->tablePrefix(), $db->_originalTablePrefix );
1499 $dbClone->useTemporaryTables( self::$useTemporaryTables );
1500
1501 $dbClone->cloneTableStructure();
1502 }
1503
1504 /**
1505 * Empty all tables so they can be repopulated for tests
1506 *
1507 * @param Database $db|null Database to reset
1508 * @param array $tablesUsed Tables to reset
1509 */
1510 private function resetDB( $db, $tablesUsed ) {
1511 if ( $db ) {
1512 $userTables = [ 'user', 'user_groups', 'user_properties', 'actor' ];
1513 $pageTables = [ 'page', 'revision', 'ip_changes', 'revision_comment_temp',
1514 'revision_actor_temp', 'comment' ];
1515 $coreDBDataTables = array_merge( $userTables, $pageTables );
1516
1517 // If any of the user or page tables were marked as used, we should clear all of them.
1518 if ( array_intersect( $tablesUsed, $userTables ) ) {
1519 $tablesUsed = array_unique( array_merge( $tablesUsed, $userTables ) );
1520 TestUserRegistry::clear();
1521 }
1522 if ( array_intersect( $tablesUsed, $pageTables ) ) {
1523 $tablesUsed = array_unique( array_merge( $tablesUsed, $pageTables ) );
1524 }
1525
1526 $truncate = in_array( $db->getType(), [ 'oracle', 'mysql' ] );
1527 foreach ( $tablesUsed as $tbl ) {
1528 // TODO: reset interwiki table to its original content.
1529 if ( $tbl == 'interwiki' ) {
1530 continue;
1531 }
1532
1533 if ( !$db->tableExists( $tbl ) ) {
1534 continue;
1535 }
1536
1537 if ( $truncate ) {
1538 $db->query( 'TRUNCATE TABLE ' . $db->tableName( $tbl ), __METHOD__ );
1539 } else {
1540 $db->delete( $tbl, '*', __METHOD__ );
1541 }
1542
1543 if ( $db->getType() === 'postgres' ) {
1544 // Reset the table's sequence too.
1545 $db->resetSequenceForTable( $tbl, __METHOD__ );
1546 }
1547
1548 if ( $tbl === 'page' ) {
1549 // Forget about the pages since they don't
1550 // exist in the DB.
1551 LinkCache::singleton()->clear();
1552 }
1553 }
1554
1555 if ( array_intersect( $tablesUsed, $coreDBDataTables ) ) {
1556 // Re-add core DB data that was deleted
1557 $this->addCoreDBData();
1558 }
1559 }
1560 }
1561
1562 private static function unprefixTable( &$tableName, $ind, $prefix ) {
1563 $tableName = substr( $tableName, strlen( $prefix ) );
1564 }
1565
1566 private static function isNotUnittest( $table ) {
1567 return strpos( $table, self::DB_PREFIX ) !== 0;
1568 }
1569
1570 /**
1571 * @since 1.18
1572 *
1573 * @param IMaintainableDatabase $db
1574 *
1575 * @return array
1576 */
1577 public static function listTables( IMaintainableDatabase $db ) {
1578 $prefix = $db->tablePrefix();
1579 $tables = $db->listTables( $prefix, __METHOD__ );
1580
1581 if ( $db->getType() === 'mysql' ) {
1582 static $viewListCache = null;
1583 if ( $viewListCache === null ) {
1584 $viewListCache = $db->listViews( null, __METHOD__ );
1585 }
1586 // T45571: cannot clone VIEWs under MySQL
1587 $tables = array_diff( $tables, $viewListCache );
1588 }
1589 array_walk( $tables, [ __CLASS__, 'unprefixTable' ], $prefix );
1590
1591 // Don't duplicate test tables from the previous fataled run
1592 $tables = array_filter( $tables, [ __CLASS__, 'isNotUnittest' ] );
1593
1594 if ( $db->getType() == 'sqlite' ) {
1595 $tables = array_flip( $tables );
1596 // these are subtables of searchindex and don't need to be duped/dropped separately
1597 unset( $tables['searchindex_content'] );
1598 unset( $tables['searchindex_segdir'] );
1599 unset( $tables['searchindex_segments'] );
1600 $tables = array_flip( $tables );
1601 }
1602
1603 return $tables;
1604 }
1605
1606 /**
1607 * @throws MWException
1608 * @since 1.18
1609 */
1610 protected function checkDbIsSupported() {
1611 if ( !in_array( $this->db->getType(), $this->supportedDBs ) ) {
1612 throw new MWException( $this->db->getType() . " is not currently supported for unit testing." );
1613 }
1614 }
1615
1616 /**
1617 * @since 1.18
1618 * @param string $offset
1619 * @return mixed
1620 */
1621 public function getCliArg( $offset ) {
1622 if ( isset( PHPUnitMaintClass::$additionalOptions[$offset] ) ) {
1623 return PHPUnitMaintClass::$additionalOptions[$offset];
1624 }
1625
1626 return null;
1627 }
1628
1629 /**
1630 * @since 1.18
1631 * @param string $offset
1632 * @param mixed $value
1633 */
1634 public function setCliArg( $offset, $value ) {
1635 PHPUnitMaintClass::$additionalOptions[$offset] = $value;
1636 }
1637
1638 /**
1639 * Don't throw a warning if $function is deprecated and called later
1640 *
1641 * @since 1.19
1642 *
1643 * @param string $function
1644 */
1645 public function hideDeprecated( $function ) {
1646 Wikimedia\suppressWarnings();
1647 wfDeprecated( $function );
1648 Wikimedia\restoreWarnings();
1649 }
1650
1651 /**
1652 * Asserts that the given database query yields the rows given by $expectedRows.
1653 * The expected rows should be given as indexed (not associative) arrays, with
1654 * the values given in the order of the columns in the $fields parameter.
1655 * Note that the rows are sorted by the columns given in $fields.
1656 *
1657 * @since 1.20
1658 *
1659 * @param string|array $table The table(s) to query
1660 * @param string|array $fields The columns to include in the result (and to sort by)
1661 * @param string|array $condition "where" condition(s)
1662 * @param array $expectedRows An array of arrays giving the expected rows.
1663 * @param array $options Options for the query
1664 * @param array $join_conds Join conditions for the query
1665 *
1666 * @throws MWException If this test cases's needsDB() method doesn't return true.
1667 * Test cases can use "@group Database" to enable database test support,
1668 * or list the tables under testing in $this->tablesUsed, or override the
1669 * needsDB() method.
1670 */
1671 protected function assertSelect(
1672 $table, $fields, $condition, array $expectedRows, array $options = [], array $join_conds = []
1673 ) {
1674 if ( !$this->needsDB() ) {
1675 throw new MWException( 'When testing database state, the test cases\'s needDB()' .
1676 ' method should return true. Use @group Database or $this->tablesUsed.' );
1677 }
1678
1679 $db = wfGetDB( DB_REPLICA );
1680
1681 $res = $db->select(
1682 $table,
1683 $fields,
1684 $condition,
1685 wfGetCaller(),
1686 $options + [ 'ORDER BY' => $fields ],
1687 $join_conds
1688 );
1689 $this->assertNotEmpty( $res, "query failed: " . $db->lastError() );
1690
1691 $i = 0;
1692
1693 foreach ( $expectedRows as $expected ) {
1694 $r = $res->fetchRow();
1695 self::stripStringKeys( $r );
1696
1697 $i += 1;
1698 $this->assertNotEmpty( $r, "row #$i missing" );
1699
1700 $this->assertEquals( $expected, $r, "row #$i mismatches" );
1701 }
1702
1703 $r = $res->fetchRow();
1704 self::stripStringKeys( $r );
1705
1706 $this->assertFalse( $r, "found extra row (after #$i)" );
1707 }
1708
1709 /**
1710 * Utility method taking an array of elements and wrapping
1711 * each element in its own array. Useful for data providers
1712 * that only return a single argument.
1713 *
1714 * @since 1.20
1715 *
1716 * @param array $elements
1717 *
1718 * @return array
1719 */
1720 protected function arrayWrap( array $elements ) {
1721 return array_map(
1722 function ( $element ) {
1723 return [ $element ];
1724 },
1725 $elements
1726 );
1727 }
1728
1729 /**
1730 * Assert that two arrays are equal. By default this means that both arrays need to hold
1731 * the same set of values. Using additional arguments, order and associated key can also
1732 * be set as relevant.
1733 *
1734 * @since 1.20
1735 *
1736 * @param array $expected
1737 * @param array $actual
1738 * @param bool $ordered If the order of the values should match
1739 * @param bool $named If the keys should match
1740 */
1741 protected function assertArrayEquals( array $expected, array $actual,
1742 $ordered = false, $named = false
1743 ) {
1744 if ( !$ordered ) {
1745 $this->objectAssociativeSort( $expected );
1746 $this->objectAssociativeSort( $actual );
1747 }
1748
1749 if ( !$named ) {
1750 $expected = array_values( $expected );
1751 $actual = array_values( $actual );
1752 }
1753
1754 call_user_func_array(
1755 [ $this, 'assertEquals' ],
1756 array_merge( [ $expected, $actual ], array_slice( func_get_args(), 4 ) )
1757 );
1758 }
1759
1760 /**
1761 * Put each HTML element on its own line and then equals() the results
1762 *
1763 * Use for nicely formatting of PHPUnit diff output when comparing very
1764 * simple HTML
1765 *
1766 * @since 1.20
1767 *
1768 * @param string $expected HTML on oneline
1769 * @param string $actual HTML on oneline
1770 * @param string $msg Optional message
1771 */
1772 protected function assertHTMLEquals( $expected, $actual, $msg = '' ) {
1773 $expected = str_replace( '>', ">\n", $expected );
1774 $actual = str_replace( '>', ">\n", $actual );
1775
1776 $this->assertEquals( $expected, $actual, $msg );
1777 }
1778
1779 /**
1780 * Does an associative sort that works for objects.
1781 *
1782 * @since 1.20
1783 *
1784 * @param array &$array
1785 */
1786 protected function objectAssociativeSort( array &$array ) {
1787 uasort(
1788 $array,
1789 function ( $a, $b ) {
1790 return serialize( $a ) > serialize( $b ) ? 1 : -1;
1791 }
1792 );
1793 }
1794
1795 /**
1796 * Utility function for eliminating all string keys from an array.
1797 * Useful to turn a database result row as returned by fetchRow() into
1798 * a pure indexed array.
1799 *
1800 * @since 1.20
1801 *
1802 * @param mixed &$r The array to remove string keys from.
1803 */
1804 protected static function stripStringKeys( &$r ) {
1805 if ( !is_array( $r ) ) {
1806 return;
1807 }
1808
1809 foreach ( $r as $k => $v ) {
1810 if ( is_string( $k ) ) {
1811 unset( $r[$k] );
1812 }
1813 }
1814 }
1815
1816 /**
1817 * Asserts that the provided variable is of the specified
1818 * internal type or equals the $value argument. This is useful
1819 * for testing return types of functions that return a certain
1820 * type or *value* when not set or on error.
1821 *
1822 * @since 1.20
1823 *
1824 * @param string $type
1825 * @param mixed $actual
1826 * @param mixed $value
1827 * @param string $message
1828 */
1829 protected function assertTypeOrValue( $type, $actual, $value = false, $message = '' ) {
1830 if ( $actual === $value ) {
1831 $this->assertTrue( true, $message );
1832 } else {
1833 $this->assertType( $type, $actual, $message );
1834 }
1835 }
1836
1837 /**
1838 * Asserts the type of the provided value. This can be either
1839 * in internal type such as boolean or integer, or a class or
1840 * interface the value extends or implements.
1841 *
1842 * @since 1.20
1843 *
1844 * @param string $type
1845 * @param mixed $actual
1846 * @param string $message
1847 */
1848 protected function assertType( $type, $actual, $message = '' ) {
1849 if ( class_exists( $type ) || interface_exists( $type ) ) {
1850 $this->assertInstanceOf( $type, $actual, $message );
1851 } else {
1852 $this->assertInternalType( $type, $actual, $message );
1853 }
1854 }
1855
1856 /**
1857 * Returns true if the given namespace defaults to Wikitext
1858 * according to $wgNamespaceContentModels
1859 *
1860 * @param int $ns The namespace ID to check
1861 *
1862 * @return bool
1863 * @since 1.21
1864 */
1865 protected function isWikitextNS( $ns ) {
1866 global $wgNamespaceContentModels;
1867
1868 if ( isset( $wgNamespaceContentModels[$ns] ) ) {
1869 return $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT;
1870 }
1871
1872 return true;
1873 }
1874
1875 /**
1876 * Returns the ID of a namespace that defaults to Wikitext.
1877 *
1878 * @throws MWException If there is none.
1879 * @return int The ID of the wikitext Namespace
1880 * @since 1.21
1881 */
1882 protected function getDefaultWikitextNS() {
1883 global $wgNamespaceContentModels;
1884
1885 static $wikitextNS = null; // this is not going to change
1886 if ( $wikitextNS !== null ) {
1887 return $wikitextNS;
1888 }
1889
1890 // quickly short out on most common case:
1891 if ( !isset( $wgNamespaceContentModels[NS_MAIN] ) ) {
1892 return NS_MAIN;
1893 }
1894
1895 // NOTE: prefer content namespaces
1896 $namespaces = array_unique( array_merge(
1897 MWNamespace::getContentNamespaces(),
1898 [ NS_MAIN, NS_HELP, NS_PROJECT ], // prefer these
1899 MWNamespace::getValidNamespaces()
1900 ) );
1901
1902 $namespaces = array_diff( $namespaces, [
1903 NS_FILE, NS_CATEGORY, NS_MEDIAWIKI, NS_USER // don't mess with magic namespaces
1904 ] );
1905
1906 $talk = array_filter( $namespaces, function ( $ns ) {
1907 return MWNamespace::isTalk( $ns );
1908 } );
1909
1910 // prefer non-talk pages
1911 $namespaces = array_diff( $namespaces, $talk );
1912 $namespaces = array_merge( $namespaces, $talk );
1913
1914 // check default content model of each namespace
1915 foreach ( $namespaces as $ns ) {
1916 if ( !isset( $wgNamespaceContentModels[$ns] ) ||
1917 $wgNamespaceContentModels[$ns] === CONTENT_MODEL_WIKITEXT
1918 ) {
1919 $wikitextNS = $ns;
1920
1921 return $wikitextNS;
1922 }
1923 }
1924
1925 // give up
1926 // @todo Inside a test, we could skip the test as incomplete.
1927 // But frequently, this is used in fixture setup.
1928 throw new MWException( "No namespace defaults to wikitext!" );
1929 }
1930
1931 /**
1932 * Check, if $wgDiff3 is set and ready to merge
1933 * Will mark the calling test as skipped, if not ready
1934 *
1935 * @since 1.21
1936 */
1937 protected function markTestSkippedIfNoDiff3() {
1938 global $wgDiff3;
1939
1940 # This check may also protect against code injection in
1941 # case of broken installations.
1942 Wikimedia\suppressWarnings();
1943 $haveDiff3 = $wgDiff3 && file_exists( $wgDiff3 );
1944 Wikimedia\restoreWarnings();
1945
1946 if ( !$haveDiff3 ) {
1947 $this->markTestSkipped( "Skip test, since diff3 is not configured" );
1948 }
1949 }
1950
1951 /**
1952 * Check if $extName is a loaded PHP extension, will skip the
1953 * test whenever it is not loaded.
1954 *
1955 * @since 1.21
1956 * @param string $extName
1957 * @return bool
1958 */
1959 protected function checkPHPExtension( $extName ) {
1960 $loaded = extension_loaded( $extName );
1961 if ( !$loaded ) {
1962 $this->markTestSkipped( "PHP extension '$extName' is not loaded, skipping." );
1963 }
1964
1965 return $loaded;
1966 }
1967
1968 /**
1969 * Asserts that the given string is a valid HTML snippet.
1970 * Wraps the given string in the required top level tags and
1971 * then calls assertValidHtmlDocument().
1972 * The snippet is expected to be HTML 5.
1973 *
1974 * @since 1.23
1975 *
1976 * @note Will mark the test as skipped if the "tidy" module is not installed.
1977 * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
1978 * when automatic tidying is disabled.
1979 *
1980 * @param string $html An HTML snippet (treated as the contents of the body tag).
1981 */
1982 protected function assertValidHtmlSnippet( $html ) {
1983 $html = '<!DOCTYPE html><html><head><title>test</title></head><body>' . $html . '</body></html>';
1984 $this->assertValidHtmlDocument( $html );
1985 }
1986
1987 /**
1988 * Asserts that the given string is valid HTML document.
1989 *
1990 * @since 1.23
1991 *
1992 * @note Will mark the test as skipped if the "tidy" module is not installed.
1993 * @note This ignores $wgUseTidy, so we can check for valid HTML even (and especially)
1994 * when automatic tidying is disabled.
1995 *
1996 * @param string $html A complete HTML document
1997 */
1998 protected function assertValidHtmlDocument( $html ) {
1999 // Note: we only validate if the tidy PHP extension is available.
2000 // In case wgTidyInternal is false, MWTidy would fall back to the command line version
2001 // of tidy. In that case however, we can not reliably detect whether a failing validation
2002 // is due to malformed HTML, or caused by tidy not being installed as a command line tool.
2003 // That would cause all HTML assertions to fail on a system that has no tidy installed.
2004 if ( !$GLOBALS['wgTidyInternal'] || !MWTidy::isEnabled() ) {
2005 $this->markTestSkipped( 'Tidy extension not installed' );
2006 }
2007
2008 $errorBuffer = '';
2009 MWTidy::checkErrors( $html, $errorBuffer );
2010 $allErrors = preg_split( '/[\r\n]+/', $errorBuffer );
2011
2012 // Filter Tidy warnings which aren't useful for us.
2013 // Tidy eg. often cries about parameters missing which have actually
2014 // been deprecated since HTML4, thus we should not care about them.
2015 $errors = preg_grep(
2016 '/^(.*Warning: (trimming empty|.* lacks ".*?" attribute).*|\s*)$/m',
2017 $allErrors, PREG_GREP_INVERT
2018 );
2019
2020 $this->assertEmpty( $errors, implode( "\n", $errors ) );
2021 }
2022
2023 /**
2024 * Used as a marker to prevent wfResetOutputBuffers from breaking PHPUnit.
2025 * @param string $buffer
2026 * @return string
2027 */
2028 public static function wfResetOutputBuffersBarrier( $buffer ) {
2029 return $buffer;
2030 }
2031
2032 /**
2033 * Create a temporary hook handler which will be reset by tearDown.
2034 * This replaces other handlers for the same hook.
2035 * @param string $hookName Hook name
2036 * @param mixed $handler Value suitable for a hook handler
2037 * @since 1.28
2038 */
2039 protected function setTemporaryHook( $hookName, $handler ) {
2040 $this->mergeMwGlobalArrayValue( 'wgHooks', [ $hookName => [ $handler ] ] );
2041 }
2042
2043 /**
2044 * Check whether file contains given data.
2045 * @param string $fileName
2046 * @param string $actualData
2047 * @param bool $createIfMissing If true, and file does not exist, create it with given data
2048 * and skip the test.
2049 * @param string $msg
2050 * @since 1.30
2051 */
2052 protected function assertFileContains(
2053 $fileName,
2054 $actualData,
2055 $createIfMissing = true,
2056 $msg = ''
2057 ) {
2058 if ( $createIfMissing ) {
2059 if ( !file_exists( $fileName ) ) {
2060 file_put_contents( $fileName, $actualData );
2061 $this->markTestSkipped( 'Data file $fileName does not exist' );
2062 }
2063 } else {
2064 self::assertFileExists( $fileName );
2065 }
2066 self::assertEquals( file_get_contents( $fileName ), $actualData, $msg );
2067 }
2068 }