Sets up a Drupal site for running functional and integration tests.
Generates a random database prefix and installs Drupal with the specified installation profile in Drupal\simpletest\WebTestBase::$profile into the prefixed database. Afterwards, installs any additional modules specified by the test.
After installation all caches are flushed and several configuration values are reset to the values of the parent site executing the test, since the default values may be incompatible with the environment in which tests are being executed.
...: List of modules to enable for the duration of the test. This can be either a single array or a variable number of string arguments.
Drupal\simpletest\WebTestBase::prepareDatabasePrefix()
Drupal\simpletest\WebTestBase::changeDatabasePrefix()
Drupal\simpletest\WebTestBase::prepareEnvironment()
protected function setUp() {
global $user, $conf;
// When running tests through the Simpletest UI (vs. on the command line),
// Simpletest's batch conflicts with the installer's batch. Batch API does
// not support the concept of nested batches (in which the nested is not
// progressive), so we need to temporarily pretend there was no batch.
// Backup the currently running Simpletest batch.
$this->originalBatch = batch_get();
// Create the database prefix for this test.
$this
->prepareDatabasePrefix();
// Prepare the environment for running tests.
$this
->prepareEnvironment();
if (!$this->setupEnvironment) {
return FALSE;
}
// Reset all statics and variables to perform tests in a clean environment.
$conf = array();
drupal_static_reset();
// Change the database prefix.
// All static variables need to be reset before the database prefix is
// changed, since Drupal\Core\Utility\CacheArray implementations attempt to
// write back to persistent caches when they are destructed.
$this
->changeDatabasePrefix();
if (!$this->setupDatabasePrefix) {
return FALSE;
}
// Set the 'simpletest_parent_profile' variable to add the parent profile's
// search path to the child site's search paths.
// @see drupal_system_listing()
$conf['simpletest_parent_profile'] = $this->originalProfile;
// Set installer parameters.
// @see install.php, install.core.inc
$connection_info = Database::getConnectionInfo();
$this->root_user = (object) array(
'name' => 'admin',
'mail' => 'admin@example.com',
'pass_raw' => $this
->randomName(),
);
$settings = array(
'interactive' => FALSE,
'parameters' => array(
'profile' => $this->profile,
'langcode' => 'en',
),
'forms' => array(
'install_settings_form' => $connection_info['default'],
'install_configure_form' => array(
'site_name' => 'Drupal',
'site_mail' => 'simpletest@example.com',
'account' => array(
'name' => $this->root_user->name,
'mail' => $this->root_user->mail,
'pass' => array(
'pass1' => $this->root_user->pass_raw,
'pass2' => $this->root_user->pass_raw,
),
),
// form_type_checkboxes_value() requires NULL instead of FALSE values
// for programmatic form submissions to disable a checkbox.
'update_status_module' => array(
1 => NULL,
2 => NULL,
),
),
),
);
// Replace the global $user session with an anonymous user to resemble a
// regular installation.
$user = drupal_anonymous_user();
// Reset the static batch to remove Simpletest's batch operations.
$batch =& batch_get();
$batch = array();
$variables = array(
'file_public_path' => $this->public_files_directory,
'file_private_path' => $this->private_files_directory,
'file_temporary_path' => $this->temp_files_directory,
'locale_translate_file_directory' => $this->translation_files_directory,
);
foreach ($variables as $name => $value) {
$GLOBALS['conf'][$name] = $value;
}
// Execute the non-interactive installer.
require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
install_drupal($settings);
$this
->rebuildContainer();
foreach ($variables as $name => $value) {
variable_set($name, $value);
}
// Restore the original Simpletest batch.
$batch =& batch_get();
$batch = $this->originalBatch;
// Revert install_begin_request() cache and lock service overrides.
unset($conf['cache_classes']);
unset($conf['lock_backend']);
// Set path variables.
// Set 'parent_profile' of simpletest to add the parent profile's
// search path to the child site's search paths.
// @see drupal_system_listing()
config('simpletest.settings')
->set('parent_profile', $this->originalProfile)
->save();
// Collect modules to install.
$class = get_class($this);
$modules = array();
while ($class) {
if (property_exists($class, 'modules')) {
$modules = array_merge($modules, $class::$modules);
}
$class = get_parent_class($class);
}
if ($modules) {
$success = module_enable($modules, TRUE);
$this
->assertTrue($success, t('Enabled modules: %modules', array(
'%modules' => implode(', ', $modules),
)));
$this
->rebuildContainer();
}
// Reset/rebuild all data structures after enabling the modules.
$this
->resetAll();
// Use the test mail class instead of the default mail handler class.
variable_set('mail_system', array(
'default-system' => 'Drupal\\Core\\Mail\\VariableLog',
));
drupal_set_time_limit($this->timeLimit);
// Temporary fix so that when running from run-tests.sh we don't get an
// empty current path which would indicate we're on the home page.
$path = current_path();
if (empty($path)) {
_current_path('run-tests');
}
$this->setup = TRUE;
}