You are here

private function MenuLinkFormatterTest::bootKernel in Menu Link (Field) 8

Bootstraps a kernel for a test.

Overrides KernelTestBase::bootKernel

1 call to MenuLinkFormatterTest::bootKernel()
MenuLinkFormatterTest::setUp in tests/src/Kernel/MenuLinkFormatterTest.php

File

tests/src/Kernel/MenuLinkFormatterTest.php, line 218

Class

MenuLinkFormatterTest
Tests the menu link field formatters.

Namespace

Drupal\Tests\menu_link\Kernel

Code

private function bootKernel() : void {
  $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(get_class($this));

  // 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__ . '/../../../../../../core/includes/install.inc';
  $connection_info = Database::getConnectionInfo();
  $driver = $connection_info['default']['driver'];
  $namespace = $connection_info['default']['namespace'] ?? NULL;
  $errors = db_installer_object($driver, $namespace)
    ->runTasks();
  if (!empty($errors)) {
    self::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::class',
  ];
  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';
}