protected function KernelTestBase::setUp in Drupal 8
Same name in this branch
- 8 core/tests/Drupal/KernelTests/KernelTestBase.php \Drupal\KernelTests\KernelTestBase::setUp()
- 8 core/modules/simpletest/src/KernelTestBase.php \Drupal\simpletest\KernelTestBase::setUp()
Performs setup tasks before each individual test method is run.
Overrides TestBase::setUp
4 calls to KernelTestBase::setUp()
- EntityUnitTestBase::setUp in core/
modules/ system/ src/ Tests/ Entity/ EntityUnitTestBase.php - Performs setup tasks before each individual test method is run.
- GenericCacheBackendUnitTestBase::setUp in core/
modules/ system/ src/ Tests/ Cache/ GenericCacheBackendUnitTestBase.php - Performs setup tasks before each individual test method is run.
- KernelTestBaseTest::setUp in core/
modules/ simpletest/ src/ Tests/ KernelTestBaseTest.php - Performs setup tasks before each individual test method is run.
- ViewKernelTestBase::setUp in core/
modules/ views/ src/ Tests/ ViewKernelTestBase.php
4 methods override KernelTestBase::setUp()
- EntityUnitTestBase::setUp in core/
modules/ system/ src/ Tests/ Entity/ EntityUnitTestBase.php - Performs setup tasks before each individual test method is run.
- GenericCacheBackendUnitTestBase::setUp in core/
modules/ system/ src/ Tests/ Cache/ GenericCacheBackendUnitTestBase.php - Performs setup tasks before each individual test method is run.
- KernelTestBaseTest::setUp in core/
modules/ simpletest/ src/ Tests/ KernelTestBaseTest.php - Performs setup tasks before each individual test method is run.
- ViewKernelTestBase::setUp in core/
modules/ views/ src/ Tests/ ViewKernelTestBase.php
File
- core/
modules/ simpletest/ src/ KernelTestBase.php, line 166
Class
- KernelTestBase
- Base class for functional integration tests.
Namespace
Drupal\simpletestCode
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'] = TestServiceProvider::class;
// 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);
}
// Set the module list upfront to avoid setting the kernel into the
// pre-installer mode.
$this->kernel
->updateModules([], []);
$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();
// Set the module list upfront to avoid setting the kernel into the
// pre-installer mode.
$this->kernel
->updateModules([], []);
$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;
// Create and set new configuration directories.
$settings['config_sync_directory'] = $this
->prepareConfigDirectories();
// Restore and merge settings.
// DrupalKernel::boot() initializes new Settings, and the containerBuild()
// method sets additional settings.
new Settings($settings + Settings::getAll());
// 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', [
'module' => [],
'theme' => [],
'profile' => '',
]);
// Collect and set a fixed module list.
$class = get_class($this);
$modules = [];
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.
\Drupal::service('file_system')
->prepareDirectory($this->publicFilesDirectory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
$this
->settingsSet('file_public_path', $this->publicFilesDirectory);
$this->streamWrappers = [];
$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');
// 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';
}