You are here

protected function KernelTestBase::setUp in Zircon Profile 8

Same name in this branch
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::setUp()
  2. 8 core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::setUp()
Same name and namespace in other branches
  1. 8.0 core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::setUp()

Performs setup tasks before each individual test method is run.

Overrides TestBase::setUp

149 calls to KernelTestBase::setUp()
ActionUnitTest::setUp in core/modules/system/src/Tests/Action/ActionUnitTest.php
Performs setup tasks before each individual test method is run.
AggregatorTitleTest::setUp in core/modules/aggregator/src/Tests/AggregatorTitleTest.php
Performs setup tasks before each individual test method is run.
AllowedValuesConstraintValidatorTest::setUp in core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php
Performs setup tasks before each individual test method is run.
AnnotatedClassDiscoveryTest::setUp in core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
Performs setup tasks before each individual test method is run.
AttachedAssetsTest::setUp in core/modules/system/src/Tests/Common/AttachedAssetsTest.php
Performs setup tasks before each individual test method is run.

... See full list

154 methods override KernelTestBase::setUp()
ActionUnitTest::setUp in core/modules/system/src/Tests/Action/ActionUnitTest.php
Performs setup tasks before each individual test method is run.
AggregatorTitleTest::setUp in core/modules/aggregator/src/Tests/AggregatorTitleTest.php
Performs setup tasks before each individual test method is run.
AllowedValuesConstraintValidatorTest::setUp in core/modules/system/src/Tests/Validation/AllowedValuesConstraintValidatorTest.php
Performs setup tasks before each individual test method is run.
AnnotatedClassDiscoveryTest::setUp in core/modules/system/src/Tests/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
Performs setup tasks before each individual test method is run.
AttachedAssetsTest::setUp in core/modules/system/src/Tests/Common/AttachedAssetsTest.php
Performs setup tasks before each individual test method is run.

... See full list

File

core/modules/simpletest/src/KernelTestBase.php, line 138
Contains \Drupal\simpletest\KernelTestBase.

Class

KernelTestBase
Base class for integration tests.

Namespace

Drupal\simpletest

Code

protected function setUp() {
  $this->keyValueFactory = new KeyValueMemoryFactory();

  // Back up settings from TestBase::prepareEnvironment().
  $settings = Settings::getAll();

  // Allow for test-specific overrides.
  $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
  $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
  $container_yamls = [];
  if (file_exists($settings_services_file)) {

    // Copy the testing-specific service overrides in place.
    $testing_services_file = $directory . '/services.yml';
    copy($settings_services_file, $testing_services_file);
    $container_yamls[] = $testing_services_file;
  }
  $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/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');
  }
  if (file_exists($directory . '/settings.testing.php')) {

    // Add the name of the testing class to settings.php and include the
    // testing specific overrides
    $hash_salt = Settings::getHashSalt();
    $test_class = get_class($this);
    $container_yamls_export = Variable::export($container_yamls);
    $php = <<<EOD
<?php

\$settings['hash_salt'] = '{<span class="php-variable">$hash_salt</span>}';
\$settings['container_yamls'] = {<span class="php-variable">$container_yamls_export</span>};

\$test_class = '{<span class="php-variable">$test_class</span>}';
include DRUPAL_ROOT . '/' . \$site_path . '/settings.testing.php';
EOD;
    file_put_contents($directory . '/settings.php', $php);
  }

  // Add this test class as a service provider.
  // @todo Remove the indirection; implement ServiceProviderInterface instead.
  $GLOBALS['conf']['container_service_providers']['TestServiceProvider'] = 'Drupal\\simpletest\\TestServiceProvider';

  // Bootstrap a new kernel.
  $class_loader = (require DRUPAL_ROOT . '/autoload.php');
  $this->kernel = new DrupalKernel('testing', $class_loader, FALSE);
  $request = Request::create('/');
  $site_path = DrupalKernel::findSitePath($request);
  $this->kernel
    ->setSitePath($site_path);
  if (file_exists($directory . '/settings.testing.php')) {
    Settings::initialize(DRUPAL_ROOT, $site_path, $class_loader);
  }
  $this->kernel
    ->boot();

  // Ensure database install tasks have been run.
  require_once __DIR__ . '/../../../includes/install.inc';
  $connection = Database::getConnection();
  $errors = db_installer_object($connection
    ->driver())
    ->runTasks();
  if (!empty($errors)) {
    $this
      ->fail('Failed to run installer database tasks: ' . implode(', ', $errors));
  }

  // Reboot the kernel because the container might contain a connection to the
  // database that has been closed during the database install tasks. This
  // prevents any services created during the first boot from having stale
  // database connections, for example, \Drupal\Core\Config\DatabaseStorage.
  $this->kernel
    ->shutdown();
  $this->kernel
    ->boot();

  // Save the original site directory path, so that extensions in the
  // site-specific directory can still be discovered in the test site
  // environment.
  // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
  $settings['test_parent_site'] = $this->originalSite;

  // Restore and merge settings.
  // DrupalKernel::boot() initializes new Settings, and the containerBuild()
  // method sets additional settings.
  new Settings($settings + Settings::getAll());

  // Create and set new configuration directories.
  $this
    ->prepareConfigDirectories();

  // Set the request scope.
  $this->container = $this->kernel
    ->getContainer();
  $this->container
    ->get('request_stack')
    ->push($request);

  // Re-inject extension file listings into state, unless the key/value
  // service was overridden (in which case its storage does not exist yet).
  if ($this->container
    ->get('keyvalue') instanceof KeyValueMemoryFactory) {
    $this->container
      ->get('state')
      ->set('system.module.files', $this->moduleFiles);
    $this->container
      ->get('state')
      ->set('system.theme.files', $this->themeFiles);
  }

  // Create a minimal core.extension configuration object so that the list of
  // enabled modules can be maintained allowing
  // \Drupal\Core\Config\ConfigInstaller::installDefaultConfig() to work.
  // Write directly to active storage to avoid early instantiation of
  // the event dispatcher which can prevent modules from registering events.
  \Drupal::service('config.storage')
    ->write('core.extension', array(
    'module' => array(),
    'theme' => array(),
  ));

  // Collect and set a fixed module list.
  $class = get_class($this);
  $modules = array();
  while ($class) {
    if (property_exists($class, 'modules')) {

      // Only add the modules, if the $modules property was not inherited.
      $rp = new \ReflectionProperty($class, 'modules');
      if ($rp->class == $class) {
        $modules[$class] = $class::$modules;
      }
    }
    $class = get_parent_class($class);
  }

  // Modules have been collected in reverse class hierarchy order; modules
  // defined by base classes should be sorted first. Then, merge the results
  // together.
  $modules = array_reverse($modules);
  $modules = call_user_func_array('array_merge_recursive', $modules);
  if ($modules) {
    $this
      ->enableModules($modules);
  }

  // Tests based on this class are entitled to use Drupal's File and
  // StreamWrapper APIs.
  // @todo Move StreamWrapper management into DrupalKernel.
  // @see https://www.drupal.org/node/2028109
  file_prepare_directory($this->publicFilesDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
  $this
    ->settingsSet('file_public_path', $this->publicFilesDirectory);
  $this->streamWrappers = array();
  $this
    ->registerStreamWrapper('public', 'Drupal\\Core\\StreamWrapper\\PublicStream');

  // The temporary stream wrapper is able to operate both with and without
  // configuration.
  $this
    ->registerStreamWrapper('temporary', 'Drupal\\Core\\StreamWrapper\\TemporaryStream');
}