protected function TestBase::prepareEnvironment

Prepares the current environment for running the test.

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

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

See also

TestBase::tearDown()

3 calls to TestBase::prepareEnvironment()
UnitTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/UnitTestBase.php
Sets up unit test environment.
UpgradePathTestBase::setUp in drupal/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php
Overrides Drupal\simpletest\WebTestBase::setUp() for upgrade testing.
WebTestBase::setUp in drupal/core/modules/simpletest/lib/Drupal/simpletest/WebTestBase.php
Sets up a Drupal site for running functional and integration tests.

File

drupal/core/modules/simpletest/lib/Drupal/simpletest/TestBase.php, line 805
Definition of Drupal\simpletest\TestBase.

Class

TestBase
Base class for Drupal tests.

Namespace

Drupal\simpletest

Code

protected function prepareEnvironment() {
  global $user, $conf;
  $language_interface = language(LANGUAGE_TYPE_INTERFACE);

  // When running the test runner within a test, back up the original database
  // prefix and re-set the new/nested prefix in drupal_valid_test_ua().
  if (drupal_valid_test_ua()) {
    $this->originalPrefix = drupal_valid_test_ua();
    drupal_valid_test_ua($this->databasePrefix);
  }

  // Backup current in-memory configuration.
  $this->originalConf = $conf;

  // Backup statics and globals.
  $this->originalContainer = clone drupal_container();
  $this->originalLanguage = $language_interface;
  $this->originalConfigDirectories = $GLOBALS['config_directories'];
  if (isset($GLOBALS['theme_key'])) {
    $this->originalThemeKey = $GLOBALS['theme_key'];
  }
  $this->originalTheme = isset($GLOBALS['theme']) ? $GLOBALS['theme'] : NULL;

  // Save further contextual information.
  $this->originalFileDirectory = variable_get('file_public_path', conf_path() . '/files');
  $this->originalProfile = drupal_get_profile();
  $this->originalUser = isset($user) ? clone $user : NULL;

  // Ensure that the current session is not changed by the new environment.
  drupal_save_session(FALSE);

  // 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 =& 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.
  $this->public_files_directory = $this->originalFileDirectory . '/simpletest/' . substr($this->databasePrefix, 10);
  $this->private_files_directory = $this->public_files_directory . '/private';
  $this->temp_files_directory = $this->private_files_directory . '/temp';
  $this->translation_files_directory = $this->public_files_directory . '/translations';

  // 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);
  file_prepare_directory($this->translation_files_directory, FILE_CREATE_DIRECTORY);
  $this->generatedTestFiles = FALSE;

  // Create and set new configuration directories. The child site
  // uses drupal_valid_test_ua() to adjust the config directory paths to
  // a test-prefix-specific directory within the public files directory.
  // @see config_get_config_directory()
  $GLOBALS['config_directories'] = array();
  $this->configDirectories = array();
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
  foreach (array(
    CONFIG_ACTIVE_DIRECTORY,
    CONFIG_STAGING_DIRECTORY,
  ) as $type) {

    // Assign the relative path to the global variable.
    $path = 'simpletest/' . substr($this->databasePrefix, 10) . '/config_' . $type;
    $GLOBALS['config_directories'][$type]['path'] = $path;

    // Ensure the directory can be created and is writeable.
    if (!install_ensure_config_directory($type)) {
      return FALSE;
    }

    // Provide the already resolved path for tests.
    $this->configDirectories[$type] = $this->originalFileDirectory . '/' . $path;
  }

  // Reset and create a new service container.
  $this->container = new ContainerBuilder();
  drupal_container($this->container);

  // Unset globals.
  unset($GLOBALS['theme_key']);
  unset($GLOBALS['theme']);

  // Re-initialize the theme to ensure that tests do not see an inconsistent
  // behavior when calling functions that would initialize the theme if it has
  // not been initialized yet.
  drupal_theme_initialize();

  // 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 Drupal.
  $test_info =& $GLOBALS['drupal_test_info'];
  $test_info['test_run_id'] = $this->databasePrefix;
  $test_info['in_child_site'] = FALSE;

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