TestRunnerKernel.php 3.7 KB
Newer Older
1
2
3
4
5
6
<?php

namespace Drupal\Core\Test;

use Drupal\Core\DrupalKernel;
use Drupal\Core\Extension\Extension;
7
use Drupal\Core\Extension\ExtensionDiscovery;
8
9
10
11
12
13
14
15
16
17
18
use Drupal\Core\Site\Settings;
use Symfony\Component\HttpFoundation\Request;

/**
 * Kernel for run-tests.sh.
 */
class TestRunnerKernel extends DrupalKernel {

  /**
   * {@inheritdoc}
   */
19
20
  public static function createFromRequest(Request $request, $class_loader, $environment = 'test_runner', $allow_dumping = TRUE, $app_root = NULL) {
    return parent::createFromRequest($request, $class_loader, $environment, $allow_dumping, $app_root);
21
22
23
24
25
  }

  /**
   * {@inheritdoc}
   */
26
27
28
29
30
  public function __construct($environment, $class_loader, $allow_dumping = FALSE, $app_root = NULL) {
    // Force $allow_dumping to FALSE, because the test runner kernel should
    // always have to rebuild its container, and potentially avoid isolation
    // issues against the tests.
    parent::__construct($environment, $class_loader, FALSE, $app_root);
31
32

    // Prime the module list and corresponding Extension objects.
33
34
    // @todo Remove System module. Needed because
    //   \Drupal\Core\Datetime\DateFormatter has a (needless) dependency on the
35
36
    //   'date_format' entity, so calls to DateFormatter::format() and
    //   DateFormatter::formatInterval() cause a plugin not found exception.
37
    $this->moduleList = [
38
      'system' => 0,
39
40
    ];
    $this->moduleData = [
41
      'system' => new Extension($this->root, 'module', 'core/modules/system/system.info.yml', 'system.module'),
42
    ];
43
44
45
46
47
48
49
    // In order to support Simpletest in Drupal 9 conditionally include the
    // module.
    $extensions = (new ExtensionDiscovery($this->root, FALSE, [], 'ignore_site_path_does_not_exist'))->scan('module', FALSE);
    if (isset($extensions['simpletest'])) {
      $this->moduleList['simpletest'] = 0;
      $this->moduleData['simpletest'] = $extensions['simpletest'];
    }
50
51
52
53
54
55
56
57
  }

  /**
   * {@inheritdoc}
   */
  public function boot() {
    // Ensure that required Settings exist.
    if (!Settings::getAll()) {
58
      new Settings([
59
        'hash_salt' => 'run-tests',
60
        'container_yamls' => [],
61
62
63
        // If there is no settings.php, then there is no parent site. In turn,
        // there is no public files directory; use a custom public files path.
        'file_public_path' => 'sites/default/files',
64
      ]);
65
66
67
68
69
70
71
72
73
74
75
76
77
78
    }

    // Remove Drupal's error/exception handlers; they are designed for HTML
    // and there is no storage nor a (watchdog) logger here.
    restore_error_handler();
    restore_exception_handler();

    // In addition, ensure that PHP errors are not hidden away in logs.
    ini_set('display_errors', TRUE);

    parent::boot();

    $this->getContainer()->get('module_handler')->loadAll();

79
    $test_discovery = new TestDiscovery(
80
      $this->getContainer()->getParameter('app.root'),
81
82
83
      $this->getContainer()->get('class_loader')
    );
    $test_discovery->registerTestNamespaces();
84

85
86
87
    // Register stream wrappers.
    $this->getContainer()->get('stream_wrapper_manager')->register();

88
    // Create the build/artifacts directory if necessary.
89
    include_once $this->getAppRoot() . '/core/includes/file.inc';
90
91
    if (!is_dir('public://simpletest') && !@mkdir('public://simpletest', 0777, TRUE) && !is_dir('public://simpletest')) {
      throw new \RuntimeException('Unable to create directory: public://simpletest');
92
    }
93
94
95
96
97
98
99
100
101
102
103
104
105
  }

  /**
   * {@inheritdoc}
   */
  public function discoverServiceProviders() {
    parent::discoverServiceProviders();
    // The test runner does not require an installed Drupal site to exist.
    // Therefore, its environment is identical to that of the early installer.
    $this->serviceProviderClasses['app']['Test'] = 'Drupal\Core\Installer\InstallerServiceProvider';
  }

}