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()

4 calls to TestBase::prepareEnvironment()
InstallerTest::setUp in drupal/core/modules/system/lib/Drupal/system/Tests/InstallerTest.php
Sets up a Drupal site for running functional and integration tests.
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 849
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->originalSettings = settings()
    ->getAll();
  $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);

  // Run all tests as a anonymous user by default, web tests will replace that
  // during the test set up.
  $user = drupal_anonymous_user();

  // 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.
  $this
    ->prepareConfigDirectories();

  // Reset statics before the old container is replaced so that objects with a
  // __destruct() method still have access to it.
  // @todo: Remove once they have been converted to services.
  drupal_static_reset();

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

  // @todo Remove this once this class has no calls to t() and format_plural()
  $this->container
    ->register('string_translation', 'Drupal\\Core\\StringTranslation\\TranslationManager');
  \Drupal::setContainer($this->container);

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

  // 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;
}