1. drupal
    1. 8
    2. 7
7 protected DrupalWebTestCase::setUp()

Generates a random database prefix, runs the install scripts on the prefixed database and enable the specified modules. After installation many caches are flushed and the internal browser is setup so that the page requests will run on the new prefix. A temporary files directory is created with the same name as the database prefix.


... 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.


modules/simpletest/drupal_web_test_case.php, line 1231

protected function setUp() {
  global $user, $language, $conf;

  // Generate a temporary prefixed database to ensure that tests have a clean starting point.
  $this->databasePrefix = 'simpletest' . mt_rand(1000, 1000000);
      ->fields(array('last_prefix' => $this->databasePrefix))
      ->condition('test_id', $this->testId)

  // Clone the current connection and replace the current prefix.
  $connection_info = Database::getConnectionInfo('default');
  Database::renameConnection('default', 'simpletest_original_default');
  foreach ($connection_info as $target => $value) {
    $connection_info[$target]['prefix'] = array(
      'default' => $value['prefix']['default'] . $this->databasePrefix,
  Database::addConnectionInfo('default', 'default', $connection_info['default']);

  // Store necessary current values before switching to prefixed database.
  $this->originalLanguage = $language;
  $this->originalLanguageDefault = variable_get('language_default');
  $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
  $this->originalProfile = drupal_get_profile();
  $clean_url_original = variable_get('clean_url', 0);

  // Set to English to prevent exceptions from utf8_truncate() from t()
  // during install if the current language is not 'en'.
  // The following array/object conversion is copied from language_default().
  $language = (object) array(
    'language' => 'en',
    'name' => 'English',
    'native' => 'English',
    'direction' => 0,
    'enabled' => 1,
    'plurals' => 0,
    'formula' => '',
    'domain' => '',
    'prefix' => '',
    'weight' => 0,
    'javascript' => '',

  // Save and clean shutdown callbacks array because it static cached and
  // will be changed by the test run. If we don't, then it will contain
  // callbacks from both environments. So testing environment will try
  // to call handlers from original environment.
  $callbacks = &drupal_register_shutdown_function();
  $this->originalShutdownCallbacks = $callbacks;
  $callbacks = array();

  // Create test directory ahead of installation so fatal errors and debug
  // information can be logged during installation process.
  // Use temporary files directory with the same prefix as the database.
  $public_files_directory  = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
  $private_files_directory = $public_files_directory . '/private';
  $temp_files_directory    = $private_files_directory . '/temp';

  // Create the directories
  file_prepare_directory($public_files_directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  file_prepare_directory($private_files_directory, FILE_CREATE_DIRECTORY);
  file_prepare_directory($temp_files_directory, FILE_CREATE_DIRECTORY);
  $this->generatedTestFiles = FALSE;

  // Log fatal errors.
  ini_set('log_errors', 1);
  ini_set('error_log', $public_files_directory . '/error.log');

  // Reset all statics and variables to perform tests in a clean environment.
  $conf = array();

  // Set the test information for use in other parts of Drupal.
  $test_info = &$GLOBALS['drupal_test_info'];
  $test_info['test_run_id'] = $this->databasePrefix;
  $test_info['in_child_site'] = FALSE;

  include_once DRUPAL_ROOT . '/includes/install.inc';


  // Set path variables.
  variable_set('file_public_path', $public_files_directory);
  variable_set('file_private_path', $private_files_directory);
  variable_set('file_temporary_path', $temp_files_directory);

  // Include the testing profile.
  variable_set('install_profile', $this->profile);
  $profile_details = install_profile_info($this->profile, 'en');

  // Install the modules specified by the testing profile.
  module_enable($profile_details['dependencies'], FALSE);

  // Install modules needed for this test. This could have been passed in as
  // either a single array argument or a variable number of string arguments.
  // @todo Remove this compatibility layer in Drupal 8, and only accept
  // $modules as a single array argument.
  $modules = func_get_args();
  if (isset($modules[0]) && is_array($modules[0])) {
    $modules = $modules[0];
  if ($modules) {
    $success = module_enable($modules, TRUE);
    $this->assertTrue($success, t('Enabled modules: %modules', array('%modules' => implode(', ', $modules))));

  // Run the profile tasks.
  $install_profile_module_exists = db_query("SELECT 1 FROM {system} WHERE type = 'module' AND name = :name", array(
    ':name' => $this->profile,
  if ($install_profile_module_exists) {
    module_enable(array($this->profile), FALSE);

  // Reset/rebuild all data structures after enabling the modules.

  // Run cron once in that environment, as install.php does at the end of
  // the installation process.

  // Log in with a clean $user.
  $this->originalUser = $user;
  $user = user_load(1);

  // Restore necessary variables.
  variable_set('install_task', 'done');
  variable_set('clean_url', $clean_url_original);
  variable_set('site_mail', 'simpletest@example.com');
  variable_set('date_default_timezone', date_default_timezone_get());
  // Set up English language.
  $language = language_default();

  // Use the test mail class instead of the default mail handler class.
  variable_set('mail_system', array('default-system' => 'TestingMailSystem'));