1 backdrop_web_test_case.php protected BackdropWebTestCase::setUp()

Sets up a Backdrop site for running functional and integration tests.

Generates a random database prefix and installs Backdrop with the specified installation profile in BackdropWebTestCase::$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.

Return value

bool: TRUE if set up completes, FALSE if an error occurred.

Overrides BackdropTestCase::setUp

See also





core/modules/simpletest/backdrop_web_test_case.php, line 1686


Test case for typical Backdrop tests.


protected function setUp() {
  global $user, $language, $language_url, $conf;
  // Create the database prefix for this test.

  // Prepare the environment for running tests.
  if (!$this->setupEnvironment) {
    return FALSE;

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

  // Change the database prefix.
  // All static variables need to be reset before the database prefix is
  // changed, since BackdropCacheArray implementations attempt to
  // write back to persistent caches when they are destructed.
  if (!$this->setupDatabasePrefix) {
    return FALSE;

  // Preset the 'install_profile' system variable, so the first call into
  // system_rebuild_module_data() (in backdrop_install_system()) will register
  // the test's profile as a module. Without this, the installation profile of
  // the parent site (executing the test) is registered, and the test
  // profile's hook_install() and other hook implementations are never invoked.
  config_set('system.core', 'install_profile', $this->profile);

  $use_cache = $this->useCache();
  if (!$use_cache) {
    // Perform the actual Backdrop installation.
    include_once BACKDROP_ROOT . '/core/includes/install.inc';

    // Set path variables.
    $core_config = config('system.core');
    $core_config->set('file_default_scheme', 'public');
    $core_config->set('file_public_path', $this->public_files_directory);
    $core_config->set('file_private_path', $this->private_files_directory);
    $core_config->set('file_temporary_path', $this->temp_files_directory);

    // Ensure schema versions are recalculated.

    // Include the testing profile.
    config_set('system.core', '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);
    // Run the profile tasks.
    $install_profile_module_exists = db_query("SELECT 1 FROM {system} WHERE type = 'module' AND name = :name", array(':name' => $this->profile))->fetchField();
    if ($install_profile_module_exists) {
      module_enable(array($this->profile), FALSE);
  else {
    // Set path variables. This must be done even when using a profile cache.
    $core_config = config('system.core');
    $core_config->set('file_default_scheme', 'public');
    $core_config->set('file_public_path', $this->public_files_directory);
    $core_config->set('file_private_path', $this->private_files_directory);
    $core_config->set('file_temporary_path', $this->temp_files_directory);

  // Set 'parent_profile' of simpletest to add the parent profile's
  // search path to the child site's search paths.
  // @see backdrop_system_listing()
  // @todo This may need to be primed like 'install_profile' above.
  config_set('simpletest.settings', 'parent_profile', $this->originalProfile);

  // 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 and only accept a single array.
  $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))));

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

  // Ensure that the session is not written to the new environment and replace
  // the global $user session with uid 1 from the new test site.
  // Login as uid 1.
  $user = user_load(1);

  // Restore necessary variables.
  state_set('install_task', 'done');
  config_set('system.core', 'clean_url', $this->originalCleanUrl);
  config_set('system.core', 'site_mail', 'simpletest@example.com');
  config_set('system.date', 'date_default_timezone', date_default_timezone_get());

  // Set up English language.
  $language_url = $language = language_default();

  // Use the test mail class instead of the default mail handler class.
  config_set('system.mail', 'default-system', 'TestingMailSystem');

  // Disable news checking against BackdropCMS.org.
  $dashboard_config = config('dashboard.settings');
  if (!$dashboard_config->isNew()) {
    $dashboard_config->set('news_feed_url', FALSE);

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

  $this->setup = TRUE;
  return TRUE;