1.20.x backdrop_web_test_case.php protected BackdropWebTestCase::prepareEnvironment()

Prepares the current environment for running the test.

Backups various current environment variables and resets them, so they do not interfere with the Backdrop site installation in which tests are executed and can be restored in tearDown().

Also sets up new resources for the testing environment, such as the public filesystem and configuration directories.

See also

BackdropWebTestCase::setUp()

BackdropWebTestCase::tearDown()

File

modules/simpletest/backdrop_web_test_case.php, line 1541

Class

BackdropWebTestCase
Test case for typical Backdrop tests.

Code

protected function prepareEnvironment() {
  global $user, $language, $language_url, $settings, $config_directories;
  $site_config = config('system.core');

  // Store necessary current values before switching to prefixed database.
  $this->originalLanguage = $language;
  $this->originalLanguageUrl = $language_url;
  $this->originalConfigDirectories = $config_directories;
  $this->originalFileDirectory = $site_config->get('file_public_path');
  $this->verboseDirectoryUrl = file_create_url($this->originalFileDirectory . '/simpletest/verbose');
  $this->originalProfile = backdrop_get_profile();
  $this->originalCleanUrl = $site_config->get('clean_url');
  $this->originalUser = $user;
  $this->originalSettings = $settings;

  // 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_url = $language = (object) array(
    'langcode' => 'en',
    'name' => 'English',
    'enabled' => 1,
    'weight' => 0,
  );

  // Save and clean the shutdown callbacks array because it is static cached
  // and will be changed by the test run. Otherwise it will contain callbacks
  // from both environments and the testing environment will try to call the
  // handlers defined by the original one.
  $callbacks = &backdrop_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.
  $this->public_files_directory = $this->originalFileDirectory . '/simpletest/' . $this->fileDirectoryName;
  $this->private_files_directory = $this->public_files_directory . '/private';
  $this->temp_files_directory = $this->private_files_directory . '/temp';

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

  // Set the new config directories. During test execution, these values are
  // manually set directly in config_get_config_directory().
  $config_base_path = 'files/simpletest/' . $this->fileDirectoryName . '/config_';
  $config_directories['active'] = $config_base_path . 'active';
  $config_directories['staging'] = $config_base_path . 'staging';
  config_get_config_storage('active')->initializeStorage();
  config_get_config_storage('staging')->initializeStorage();

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

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

  // Disable Drupal compatibility for test runs.
  $settings['backdrop_drupal_compatibility'] = FALSE;

  // Indicate the environment was set up correctly.
  $this->setupEnvironment = TRUE;
}