private static $useTemporaryTables = true;
private static $reuseDB = false;
private static $dbSetup = false;
- private static $oldTablePrefix = false;
+ private static $oldTablePrefix = '';
/**
* Original value of PHP's error_reporting setting.
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
- // NOTE: Usually, PHPUnitMaintClass::finalSetup already called this,
- // but let's make doubly sure.
- self::prepareServices( new GlobalVarConfig() );
+ // Get the service locator, and reset services if it's not done already
+ self::$serviceLocator = self::prepareServices( new GlobalVarConfig() );
}
/**
*
* @param Config $bootstrapConfig The bootstrap config to use with the new
* MediaWikiServices. Only used for the first call to this method.
+ * @return MediaWikiServices
*/
public static function prepareServices( Config $bootstrapConfig ) {
- static $servicesPrepared = false;
+ static $services = null;
- if ( $servicesPrepared ) {
- return;
- } else {
- $servicesPrepared = true;
+ if ( !$services ) {
+ $services = self::resetGlobalServices( $bootstrapConfig );
}
-
- self::resetGlobalServices( $bootstrapConfig );
+ return $services;
}
/**
* Reset global services, and install testing environment.
* This is the testing equivalent of MediaWikiServices::resetGlobalInstance().
* This should only be used to set up the testing environment, not when
- * running unit tests. Use overrideMwServices() for that.
+ * running unit tests. Use MediaWikiTestCase::overrideMwServices() for that.
*
* @see MediaWikiServices::resetGlobalInstance()
* @see prepareServices()
- * @see overrideMwServices()
+ * @see MediaWikiTestCase::overrideMwServices()
*
* @param Config|null $bootstrapConfig The bootstrap config to use with the new
* MediaWikiServices.
MediaWikiServices::resetGlobalInstance( $testConfig );
- self::$serviceLocator = MediaWikiServices::getInstance();
+ $serviceLocator = MediaWikiServices::getInstance();
self::installTestServices(
$oldConfigFactory,
- self::$serviceLocator
+ $serviceLocator
);
+ return $serviceLocator;
}
/**
$defaultOverrides->set( 'ObjectCaches', $objectCaches );
$defaultOverrides->set( 'MainCacheType', CACHE_NONE );
+ $defaultOverrides->set( 'JobTypeConf', [ 'default' => [ 'class' => 'JobQueueMemory' ] ] );
// Use a fast hash algorithm to hash passwords.
$defaultOverrides->set( 'PasswordDefault', 'A' );
while ( $this->db->trxLevel() > 0 ) {
$this->db->rollback( __METHOD__, 'flush' );
}
+ // Check for unsafe queries
+ if ( $this->db->getType() === 'mysql' ) {
+ $this->db->query( "SET sql_mode = 'STRICT_ALL_TABLES'" );
+ }
}
DeferredUpdates::clearPendingUpdates();
}
protected function tearDown() {
- global $wgRequest;
+ global $wgRequest, $wgSQLMode;
$status = ob_get_status();
if ( isset( $status['name'] ) &&
while ( $this->db->trxLevel() > 0 ) {
$this->db->rollback( __METHOD__, 'flush' );
}
+ if ( $this->db->getType() === 'mysql' ) {
+ $this->db->query( "SET sql_mode = " . $this->db->addQuotes( $wgSQLMode ) );
+ }
}
// Restore mw globals
*
* Should be called from addDBData().
*
- * @since 1.25
- * @param string $pageName Page name
+ * @since 1.25 ($namespace in 1.28)
+ * @param string|title $pageName Page name or title
* @param string $text Page's content
+ * @param int $namespace Namespace id (name cannot already contain namespace)
* @return array Title object and page id
*/
- protected function insertPage( $pageName, $text = 'Sample page for unit test.' ) {
- $title = Title::newFromText( $pageName, 0 );
+ protected function insertPage(
+ $pageName,
+ $text = 'Sample page for unit test.',
+ $namespace = null
+ ) {
+ if ( is_string( $pageName ) ) {
+ $title = Title::newFromText( $pageName, $namespace );
+ } else {
+ $title = $pageName;
+ }
$user = static::getTestSysop()->getUser();
$comment = __METHOD__ . ': Sample page for unit test.';
* @throws MWException If the database table prefix is already $prefix
*/
public static function setupTestDB( DatabaseBase $db, $prefix ) {
+ if ( self::$dbSetup ) {
+ return;
+ }
+
if ( $db->tablePrefix() === $prefix ) {
throw new MWException(
'Cannot run unit tests, the database prefix is already "' . $prefix . '"' );
}
- if ( self::$dbSetup ) {
- return;
- }
-
// TODO: the below should be re-written as soon as LBFactory, LoadBalancer,
// and DatabaseBase no longer use global state.