You are here

private function KernelTestBase::bootKernel in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::bootKernel()

Bootstraps a kernel for a test.

1 call to KernelTestBase::bootKernel()
KernelTestBase::setUp in core/tests/Drupal/KernelTests/KernelTestBase.php

File

core/tests/Drupal/KernelTests/KernelTestBase.php, line 328

Class

KernelTestBase
Base class for functional integration tests.

Namespace

Drupal\KernelTests

Code

private function bootKernel() {
  $this
    ->setSetting('container_yamls', []);

  // Allow for test-specific overrides.
  $settings_services_file = $this->root . '/sites/default/testing.services.yml';
  if (file_exists($settings_services_file)) {

    // Copy the testing-specific service overrides in place.
    $testing_services_file = $this->siteDirectory . '/services.yml';
    copy($settings_services_file, $testing_services_file);
    $this
      ->setSetting('container_yamls', [
      $testing_services_file,
    ]);
  }

  // Allow for global test environment overrides.
  if (file_exists($test_env = $this->root . '/sites/default/testing.services.yml')) {
    $GLOBALS['conf']['container_yamls']['testing'] = $test_env;
  }

  // Add this test class as a service provider.
  $GLOBALS['conf']['container_service_providers']['test'] = $this;
  $modules = self::getModulesToEnable(static::class);

  // When a module is providing the database driver, then enable that module.
  $connection_info = Database::getConnectionInfo();
  $driver = $connection_info['default']['driver'];
  $namespace = $connection_info['default']['namespace'] ?? NULL;
  $autoload = $connection_info['default']['autoload'] ?? NULL;
  if (strpos($autoload, 'src/Driver/Database/') !== FALSE) {
    [
      $first,
      $second,
    ] = explode('\\', $namespace, 3);
    if ($first === 'Drupal' && strtolower($second) === $second) {

      // Add the module that provides the database driver to the list of
      // modules as the first to be enabled.
      array_unshift($modules, $second);
    }
  }

  // Bootstrap the kernel. Do not use createFromRequest() to retain Settings.
  $kernel = new DrupalKernel('testing', $this->classLoader, FALSE);
  $kernel
    ->setSitePath($this->siteDirectory);

  // Boot a new one-time container from scratch. Set the module list upfront
  // to avoid a subsequent rebuild or setting the kernel into the
  // pre-installer mode.
  $extensions = $modules ? $this
    ->getExtensionsForModules($modules) : [];
  $kernel
    ->updateModules($extensions, $extensions);

  // DrupalKernel::boot() is not sufficient as it does not invoke preHandle(),
  // which is required to initialize legacy global variables.
  $request = Request::create('/');
  $kernel
    ->boot();
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_OBJECT, new Route('<none>'));
  $request->attributes
    ->set(RouteObjectInterface::ROUTE_NAME, '<none>');
  $kernel
    ->preHandle($request);
  $this->container = $kernel
    ->getContainer();

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

  // Setup the destination to the be frontpage by default.
  \Drupal::destination()
    ->set('/');

  // Write the core.extension configuration.
  // Required for ConfigInstaller::installDefaultConfig() to work.
  $this->container
    ->get('config.storage')
    ->write('core.extension', [
    'module' => array_fill_keys($modules, 0),
    'theme' => [],
    'profile' => '',
  ]);
  $settings = Settings::getAll();
  $settings['php_storage']['default'] = [
    'class' => '\\Drupal\\Component\\PhpStorage\\FileStorage',
  ];
  new Settings($settings);

  // 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.
  $GLOBALS['config']['system.mail']['interface']['default'] = 'test_mail_collector';

  // Manually configure the default file scheme so that modules that use file
  // functions don't have to install system and its configuration.
  // @see file_default_scheme()
  $GLOBALS['config']['system.file']['default_scheme'] = 'public';
}