You are here

public function BrowserTestBase::installDrupal in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/simpletest/src/BrowserTestBase.php \Drupal\simpletest\BrowserTestBase::installDrupal()

Installs Drupal into the Simpletest site.

1 call to BrowserTestBase::installDrupal()
BrowserTestBase::setUp in core/modules/simpletest/src/BrowserTestBase.php

File

core/modules/simpletest/src/BrowserTestBase.php, line 824
Contains \Drupal\simpletest\BrowserTestBase.

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\simpletest

Code

public function installDrupal() {

  // Define information about the user 1 account.
  $this->rootUser = new UserSession(array(
    'uid' => 1,
    'name' => 'admin',
    'mail' => 'admin@example.com',
    'passRaw' => $this
      ->randomMachineName(),
  ));

  // The child site derives its session name from the database prefix when
  // running web tests.
  $this
    ->generateSessionName($this->databasePrefix);

  // Get parameters for install_drupal() before removing global variables.
  $parameters = $this
    ->installParameters();

  // Prepare installer settings that are not install_drupal() parameters.
  // Copy and prepare an actual settings.php, so as to resemble a regular
  // installation.
  // Not using File API; a potential error must trigger a PHP warning.
  $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
  copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');

  // All file system paths are created by System module during installation.
  // @see system_requirements()
  // @see TestBase::prepareEnvironment()
  $settings['settings']['file_public_path'] = (object) array(
    'value' => $this->publicFilesDirectory,
    'required' => TRUE,
  );
  $this
    ->writeSettings($settings);

  // Allow for test-specific overrides.
  $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSiteDirectory . '/settings.testing.php';
  if (file_exists($settings_testing_file)) {

    // Copy the testing-specific settings.php overrides in place.
    copy($settings_testing_file, $directory . '/settings.testing.php');

    // Add the name of the testing class to settings.php and include the
    // testing specific overrides.
    file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
  }
  $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSiteDirectory . '/testing.services.yml';
  if (file_exists($settings_services_file)) {

    // Copy the testing-specific service overrides in place.
    copy($settings_services_file, $directory . '/services.yml');
  }

  // Since Drupal is bootstrapped already, install_begin_request() will not
  // bootstrap into DRUPAL_BOOTSTRAP_CONFIGURATION (again). Hence, we have to
  // reload the newly written custom settings.php manually.
  Settings::initialize(DRUPAL_ROOT, $directory, $this->classLoader);

  // Execute the non-interactive installer.
  require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
  install_drupal($parameters);

  // Import new settings.php written by the installer.
  Settings::initialize(DRUPAL_ROOT, $directory, $this->classLoader);
  foreach ($GLOBALS['config_directories'] as $type => $path) {
    $this->configDirectories[$type] = $path;
  }

  // After writing settings.php, the installer removes write permissions from
  // the site directory. To allow drupal_generate_test_ua() to write a file
  // containing the private key for drupal_valid_test_ua(), the site directory
  // has to be writable.
  // TestBase::restoreEnvironment() will delete the entire site directory. Not
  // using File API; a potential error must trigger a PHP warning.
  chmod($directory, 0777);
  $request = \Drupal::request();
  $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
  $this->kernel
    ->prepareLegacyRequest($request);

  // Force the container to be built from scratch instead of loaded from the
  // disk. This forces us to not accidentally load the parent site.
  $container = $this->kernel
    ->rebuildContainer();
  $config = $container
    ->get('config.factory');

  // Manually create and configure private and temporary files directories.
  // While these could be preset/enforced in settings.php like the public
  // files directory above, some tests expect them to be configurable in the
  // UI. If declared in settings.php, they would no longer be configurable.
  file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
  file_prepare_directory($this->tempFilesDirectory, FILE_CREATE_DIRECTORY);
  $config
    ->getEditable('system.file')
    ->set('path.private', $this->privateFilesDirectory)
    ->set('path.temporary', $this->tempFilesDirectory)
    ->save();

  // Manually configure the test mail collector implementation to prevent
  // tests from sending out emails and collect them in state instead.
  // While this should be enforced via settings.php prior to installation,
  // some tests expect to be able to test mail system implementations.
  $config
    ->getEditable('system.mail')
    ->set('interface.default', 'test_mail_collector')
    ->save();

  // By default, verbosely display all errors and disable all production
  // environment optimizations for all tests to avoid needless overhead and
  // ensure a sane default experience for test authors.
  // @see https://www.drupal.org/node/2259167
  $config
    ->getEditable('system.logging')
    ->set('error_level', 'verbose')
    ->save();
  $config
    ->getEditable('system.performance')
    ->set('css.preprocess', FALSE)
    ->set('js.preprocess', FALSE)
    ->save();

  // Collect modules to install.
  $class = get_class($this);
  $modules = array();
  while ($class) {
    if (property_exists($class, 'modules')) {
      $modules = array_merge($modules, $class::$modules);
    }
    $class = get_parent_class($class);
  }
  if ($modules) {
    $modules = array_unique($modules);
    $success = $container
      ->get('module_installer')
      ->install($modules, TRUE);
    $this
      ->assertTrue($success, SafeMarkup::format('Enabled modules: %modules', array(
      '%modules' => implode(', ', $modules),
    )));
    $this
      ->rebuildContainer();
  }

  // Reset/rebuild all data structures after enabling the modules, primarily
  // to synchronize all data structures and caches between the test runner and
  // the child site.
  // Affects e.g. StreamWrapperManagerInterface::getWrappers().
  // @see \Drupal\Core\DrupalKernel::bootCode()
  // @todo Test-specific setUp() methods may set up further fixtures; find a
  //   way to execute this after setUp() is done, or to eliminate it entirely.
  $this
    ->resetAll();
  $this->kernel
    ->prepareLegacyRequest($request);
}