3 namespace Drupal\Core\Test;
5 use Drupal\Component\FileCache\FileCacheFactory;
6 use Drupal\Component\Utility\SafeMarkup;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Config\Development\ConfigSchemaChecker;
9 use Drupal\Core\Database\Database;
10 use Drupal\Core\DrupalKernel;
11 use Drupal\Core\Extension\MissingDependencyException;
12 use Drupal\Core\Serialization\Yaml;
13 use Drupal\Core\Session\UserSession;
14 use Drupal\Core\Site\Settings;
15 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
16 use Symfony\Component\DependencyInjection\ContainerInterface;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\Yaml\Yaml as SymfonyYaml;
21 * Defines a trait for shared functional test setup functionality.
23 trait FunctionalTestSetupTrait {
26 * The "#1" admin user.
28 * @var \Drupal\Core\Session\AccountInterface
33 * The class loader to use for installation and initialization of setup.
35 * @var \Symfony\Component\Classloader\Classloader
37 protected $classLoader;
40 * The config directories used in this test.
42 protected $configDirectories = [];
45 * Prepares site settings and services before installation.
47 protected function prepareSettings() {
48 // Prepare installer settings that are not install_drupal() parameters.
49 // Copy and prepare an actual settings.php, so as to resemble a regular
51 // Not using File API; a potential error must trigger a PHP warning.
52 $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
53 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
55 // The public file system path is created during installation. Additionally,
57 // - The temporary directory is set and created by install_base_system().
58 // - The private file directory is created post install by
59 // FunctionalTestSetupTrait::initConfig().
60 // @see system_requirements()
61 // @see TestBase::prepareEnvironment()
62 // @see install_base_system()
63 // @see \Drupal\Core\Test\FunctionalTestSetupTrait::initConfig()
64 $settings['settings']['file_public_path'] = (object) [
65 'value' => $this->publicFilesDirectory,
68 $settings['settings']['file_private_path'] = (object) [
69 'value' => $this->privateFilesDirectory,
72 // Save the original site directory path, so that extensions in the
73 // site-specific directory can still be discovered in the test site
75 // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
76 $settings['settings']['test_parent_site'] = (object) [
77 'value' => $this->originalSite,
80 // Add the parent profile's search path to the child site's search paths.
81 // @see \Drupal\Core\Extension\ExtensionDiscovery::getProfileDirectories()
82 $settings['conf']['simpletest.settings']['parent_profile'] = (object) [
83 'value' => $this->originalProfile,
86 $this->writeSettings($settings);
87 // Allow for test-specific overrides.
88 $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
89 if (file_exists($settings_testing_file)) {
90 // Copy the testing-specific settings.php overrides in place.
91 copy($settings_testing_file, $directory . '/settings.testing.php');
92 // Add the name of the testing class to settings.php and include the
93 // testing specific overrides.
94 file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
96 $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
97 if (!file_exists($settings_services_file)) {
98 // Otherwise, use the default services as a starting point for overrides.
99 $settings_services_file = DRUPAL_ROOT . '/sites/default/default.services.yml';
101 // Copy the testing-specific service overrides in place.
102 copy($settings_services_file, $directory . '/services.yml');
103 if ($this->strictConfigSchema) {
104 // Add a listener to validate configuration schema on save.
105 $yaml = new SymfonyYaml();
106 $content = file_get_contents($directory . '/services.yml');
107 $services = $yaml->parse($content);
108 $services['services']['simpletest.config_schema_checker'] = [
109 'class' => ConfigSchemaChecker::class,
110 'arguments' => ['@config.typed', $this->getConfigSchemaExclusions()],
111 'tags' => [['name' => 'event_subscriber']],
113 file_put_contents($directory . '/services.yml', $yaml->dump($services));
115 // Since Drupal is bootstrapped already, install_begin_request() will not
116 // bootstrap again. Hence, we have to reload the newly written custom
117 // settings.php manually.
118 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
122 * Rewrites the settings.php file of the test site.
124 * @param array $settings
125 * An array of settings to write out, in the format expected by
126 * drupal_rewrite_settings().
128 * @see drupal_rewrite_settings()
130 protected function writeSettings(array $settings) {
131 include_once DRUPAL_ROOT . '/core/includes/install.inc';
132 $filename = $this->siteDirectory . '/settings.php';
133 // system_requirements() removes write permissions from settings.php
134 // whenever it is invoked.
135 // Not using File API; a potential error must trigger a PHP warning.
136 chmod($filename, 0666);
137 drupal_rewrite_settings($settings, $filename);
141 * Changes parameters in the services.yml file.
143 * @param string $name
144 * The name of the parameter.
145 * @param string $value
146 * The value of the parameter.
148 protected function setContainerParameter($name, $value) {
149 $filename = $this->siteDirectory . '/services.yml';
150 chmod($filename, 0666);
152 $services = Yaml::decode(file_get_contents($filename));
153 $services['parameters'][$name] = $value;
154 file_put_contents($filename, Yaml::encode($services));
156 // Ensure that the cache is deleted for the yaml file loader.
157 $file_cache = FileCacheFactory::get('container_yaml_loader');
158 $file_cache->delete($filename);
162 * Rebuilds \Drupal::getContainer().
164 * Use this to update the test process's kernel with a new service container.
165 * For example, when the list of enabled modules is changed via the internal
166 * browser the test process's kernel has a service container with an out of
169 * @see TestBase::prepareEnvironment()
170 * @see TestBase::restoreEnvironment()
172 * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
173 * changes are immediately reflected in \Drupal::getContainer(). Until then,
174 * tests can invoke this workaround when requiring services from newly
175 * enabled modules to be immediately available in the same request.
177 protected function rebuildContainer() {
178 // Rebuild the kernel and bring it back to a fully bootstrapped state.
179 $this->container = $this->kernel->rebuildContainer();
181 // Make sure the url generator has a request object, otherwise calls to
182 // $this->drupalGet() will fail.
183 $this->prepareRequestForGenerator();
187 * Resets all data structures after having enabled new modules.
189 * This method is called by FunctionalTestSetupTrait::rebuildAll() after
190 * enabling the requested modules. It must be called again when additional
191 * modules are enabled later.
193 * @see \Drupal\Core\Test\FunctionalTestSetupTrait::rebuildAll()
194 * @see \Drupal\Tests\BrowserTestBase::installDrupal()
195 * @see \Drupal\simpletest\WebTestBase::setUp()
197 protected function resetAll() {
198 // Clear all database and static caches and rebuild data structures.
199 drupal_flush_all_caches();
200 $this->container = \Drupal::getContainer();
202 // Reset static variables and reload permissions.
203 $this->refreshVariables();
207 * Refreshes in-memory configuration and state information.
209 * Useful after a page request is made that changes configuration or state in
210 * a different thread.
212 * In other words calling a settings page with $this->drupalPostForm() with a
213 * changed value would update configuration to reflect that change, but in the
214 * thread that made the call (thread running the test) the changed values
215 * would not be picked up.
217 * This method clears the cache and loads a fresh copy.
219 protected function refreshVariables() {
220 // Clear the tag cache.
221 \Drupal::service('cache_tags.invalidator')->resetChecksums();
222 foreach (Cache::getBins() as $backend) {
223 if (is_callable([$backend, 'reset'])) {
228 $this->container->get('config.factory')->reset();
229 $this->container->get('state')->resetCache();
233 * Creates a mock request and sets it on the generator.
235 * This is used to manipulate how the generator generates paths during tests.
236 * It also ensures that calls to $this->drupalGet() will work when running
237 * from run-tests.sh because the url generator no longer looks at the global
238 * variables that are set there but relies on getting this information from a
241 * @param bool $clean_urls
242 * Whether to mock the request using clean urls.
243 * @param array $override_server_vars
244 * An array of server variables to override.
246 * @return \Symfony\Component\HttpFoundation\Request
247 * The mocked request object.
249 protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = []) {
250 $request = Request::createFromGlobals();
251 $server = $request->server->all();
252 if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
253 // We need this for when the test is executed by run-tests.sh.
254 // @todo Remove this once run-tests.sh has been converted to use a Request
257 $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
258 $base_path = rtrim($server['REQUEST_URI'], '/');
261 $base_path = $request->getBasePath();
264 $request_path = $base_path ? $base_path . '/user' : 'user';
267 $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
269 $server = array_merge($server, $override_server_vars);
271 $request = Request::create($request_path, 'GET', [], [], [], $server);
272 // Ensure the request time is REQUEST_TIME to ensure that API calls
273 // in the test use the right timestamp.
274 $request->server->set('REQUEST_TIME', REQUEST_TIME);
275 $this->container->get('request_stack')->push($request);
277 // The request context is normally set by the router_listener from within
278 // its KernelEvents::REQUEST listener. In the simpletest parent site this
279 // event is not fired, therefore it is necessary to updated the request
280 // context manually here.
281 $this->container->get('router.request_context')->fromRequest($request);
287 * Execute the non-interactive installer.
289 * @see install_drupal()
291 protected function doInstall() {
292 require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
293 install_drupal($this->classLoader, $this->installParameters());
297 * Initialize settings created during install.
299 protected function initSettings() {
300 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
301 foreach ($GLOBALS['config_directories'] as $type => $path) {
302 $this->configDirectories[$type] = $path;
305 // After writing settings.php, the installer removes write permissions
306 // from the site directory. To allow drupal_generate_test_ua() to write
307 // a file containing the private key for drupal_valid_test_ua(), the site
308 // directory has to be writable.
309 // TestBase::restoreEnvironment() will delete the entire site directory.
310 // Not using File API; a potential error must trigger a PHP warning.
311 chmod(DRUPAL_ROOT . '/' . $this->siteDirectory, 0777);
313 // During tests, cacheable responses should get the debugging cacheability
314 // headers by default.
315 $this->setContainerParameter('http.response.debug_cacheability_headers', TRUE);
319 * Initialize various configurations post-installation.
321 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
324 protected function initConfig(ContainerInterface $container) {
325 $config = $container->get('config.factory');
327 // Manually create the private directory.
328 file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
330 // Manually configure the test mail collector implementation to prevent
331 // tests from sending out emails and collect them in state instead.
332 // While this should be enforced via settings.php prior to installation,
333 // some tests expect to be able to test mail system implementations.
334 $config->getEditable('system.mail')
335 ->set('interface.default', 'test_mail_collector')
338 // By default, verbosely display all errors and disable all production
339 // environment optimizations for all tests to avoid needless overhead and
340 // ensure a sane default experience for test authors.
341 // @see https://www.drupal.org/node/2259167
342 $config->getEditable('system.logging')
343 ->set('error_level', 'verbose')
345 $config->getEditable('system.performance')
346 ->set('css.preprocess', FALSE)
347 ->set('js.preprocess', FALSE)
350 // Set an explicit time zone to not rely on the system one, which may vary
351 // from setup to setup. The Australia/Sydney time zone is chosen so all
352 // tests are run using an edge case scenario (UTC10 and DST). This choice
353 // is made to prevent time zone related regressions and reduce the
354 // fragility of the testing system in general.
355 $config->getEditable('system.date')
356 ->set('timezone.default', 'Australia/Sydney')
361 * Initializes user 1 for the site to be installed.
363 protected function initUserSession() {
364 $password = $this->randomMachineName();
365 // Define information about the user 1 account.
366 $this->rootUser = new UserSession([
369 'mail' => 'admin@example.com',
370 'pass_raw' => $password,
371 'passRaw' => $password,
372 'timezone' => date_default_timezone_get(),
375 // The child site derives its session name from the database prefix when
376 // running web tests.
377 $this->generateSessionName($this->databasePrefix);
381 * Initializes the kernel after installation.
383 * @param \Symfony\Component\HttpFoundation\Request $request
386 * @return \Symfony\Component\DependencyInjection\ContainerInterface
389 protected function initKernel(Request $request) {
390 $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
391 $this->kernel->prepareLegacyRequest($request);
392 // Force the container to be built from scratch instead of loaded from the
393 // disk. This forces us to not accidentally load the parent site.
394 return $this->kernel->rebuildContainer();
398 * Install modules defined by `static::$modules`.
400 * To install test modules outside of the testing environment, add
402 * $settings['extension_discovery_scan_tests'] = TRUE;
404 * to your settings.php.
406 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
409 protected function installModulesFromClassProperty(ContainerInterface $container) {
410 $class = get_class($this);
413 if (property_exists($class, 'modules')) {
414 $modules = array_merge($modules, $class::$modules);
416 $class = get_parent_class($class);
419 $modules = array_unique($modules);
421 $success = $container->get('module_installer')->install($modules, TRUE);
422 $this->assertTrue($success, SafeMarkup::format('Enabled modules: %modules', ['%modules' => implode(', ', $modules)]));
424 catch (MissingDependencyException $e) {
425 // The exception message has all the details.
426 $this->fail($e->getMessage());
429 $this->rebuildContainer();
434 * Resets and rebuilds the environment after setup.
436 protected function rebuildAll() {
437 // Reset/rebuild all data structures after enabling the modules, primarily
438 // to synchronize all data structures and caches between the test runner and
440 // @see \Drupal\Core\DrupalKernel::bootCode()
441 // @todo Test-specific setUp() methods may set up further fixtures; find a
442 // way to execute this after setUp() is done, or to eliminate it entirely.
444 $this->kernel->prepareLegacyRequest(\Drupal::request());
446 // Explicitly call register() again on the container registered in \Drupal.
447 // @todo This should already be called through
448 // DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that
449 // appears to be calling a different container.
450 $this->container->get('stream_wrapper_manager')->register();
454 * Returns the parameters that will be used when Simpletest installs Drupal.
456 * @see install_drupal()
457 * @see install_state_defaults()
460 * Array of parameters for use in install_drupal().
462 protected function installParameters() {
463 $connection_info = Database::getConnectionInfo();
464 $driver = $connection_info['default']['driver'];
465 $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
466 unset($connection_info['default']['driver']);
467 unset($connection_info['default']['namespace']);
468 unset($connection_info['default']['pdo']);
469 unset($connection_info['default']['init_commands']);
470 // Remove database connection info that is not used by SQLite.
471 if ($driver === 'sqlite') {
472 unset($connection_info['default']['username']);
473 unset($connection_info['default']['password']);
474 unset($connection_info['default']['host']);
475 unset($connection_info['default']['port']);
478 'interactive' => FALSE,
480 'profile' => $this->profile,
484 'install_settings_form' => [
486 $driver => $connection_info['default'],
488 'install_configure_form' => [
489 'site_name' => 'Drupal',
490 'site_mail' => 'simpletest@example.com',
492 'name' => $this->rootUser->name,
493 'mail' => $this->rootUser->getEmail(),
495 'pass1' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
496 'pass2' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
499 // form_type_checkboxes_value() requires NULL instead of FALSE values
500 // for programmatic form submissions to disable a checkbox.
501 'enable_update_status_module' => NULL,
502 'enable_update_status_emails' => NULL,
507 // If we only have one db driver available, we cannot set the driver.
508 include_once DRUPAL_ROOT . '/core/includes/install.inc';
509 if (count($this->getDatabaseTypes()) == 1) {
510 unset($parameters['forms']['install_settings_form']['driver']);
516 * Sets up the base URL based upon the environment variable.
519 * Thrown when no SIMPLETEST_BASE_URL environment variable is provided.
521 protected function setupBaseUrl() {
524 // Get and set the domain of the environment we are running our test
526 $base_url = getenv('SIMPLETEST_BASE_URL');
528 throw new \Exception(
529 'You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.'
533 // Setup $_SERVER variable.
534 $parsed_url = parse_url($base_url);
535 $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
536 $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
537 $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;
539 $this->baseUrl = $base_url;
541 // If the passed URL schema is 'https' then setup the $_SERVER variables
542 // properly so that testing will run under HTTPS.
543 if ($parsed_url['scheme'] === 'https') {
544 $_SERVER['HTTPS'] = 'on';
546 $_SERVER['HTTP_HOST'] = $host;
547 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
548 $_SERVER['SERVER_ADDR'] = '127.0.0.1';
549 $_SERVER['SERVER_PORT'] = $port;
550 $_SERVER['SERVER_SOFTWARE'] = NULL;
551 $_SERVER['SERVER_NAME'] = 'localhost';
552 $_SERVER['REQUEST_URI'] = $path . '/';
553 $_SERVER['REQUEST_METHOD'] = 'GET';
554 $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
555 $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
556 $_SERVER['PHP_SELF'] = $path . '/index.php';
557 $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
561 * Prepares the current environment for running the test.
563 * Also sets up new resources for the testing environment, such as the public
564 * filesystem and configuration directories.
566 * This method is private as it must only be called once by
567 * BrowserTestBase::setUp() (multiple invocations for the same test would have
568 * unpredictable consequences) and it must not be callable or overridable by
571 protected function prepareEnvironment() {
572 // Bootstrap Drupal so we can use Drupal's built in functions.
573 $this->classLoader = require __DIR__ . '/../../../../../autoload.php';
574 $request = Request::createFromGlobals();
575 $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
576 // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
578 $kernel->prepareLegacyRequest($request);
579 $this->prepareDatabasePrefix();
581 $this->originalSite = $kernel->findSitePath($request);
583 // Create test directory ahead of installation so fatal errors and debug
584 // information can be logged during installation process.
585 file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
587 // Prepare filesystem directory paths.
588 $this->publicFilesDirectory = $this->siteDirectory . '/files';
589 $this->privateFilesDirectory = $this->siteDirectory . '/private';
590 $this->tempFilesDirectory = $this->siteDirectory . '/temp';
591 $this->translationFilesDirectory = $this->siteDirectory . '/translations';
593 // Ensure the configImporter is refreshed for each test.
594 $this->configImporter = NULL;
596 // Unregister all custom stream wrappers of the parent site.
597 $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
598 foreach ($wrappers as $scheme => $info) {
599 stream_wrapper_unregister($scheme);
603 drupal_static_reset();
605 $this->container = NULL;
608 unset($GLOBALS['config_directories']);
609 unset($GLOBALS['config']);
610 unset($GLOBALS['conf']);
613 ini_set('log_errors', 1);
614 ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
616 // Change the database prefix.
617 $this->changeDatabasePrefix();
619 // After preparing the environment and changing the database prefix, we are
620 // in a valid test environment.
621 drupal_valid_test_ua($this->databasePrefix);
625 // For performance, simply use the database prefix as hash salt.
626 'hash_salt' => $this->databasePrefix,
629 drupal_set_time_limit($this->timeLimit);
631 // Save and clean the shutdown callbacks array because it is static cached
632 // and will be changed by the test run. Otherwise it will contain callbacks
633 // from both environments and the testing environment will try to call the
634 // handlers defined by the original one.
635 $callbacks = &drupal_register_shutdown_function();
636 $this->originalShutdownCallbacks = $callbacks;
641 * Returns all supported database driver installer objects.
643 * This wraps drupal_get_database_types() for use without a current container.
645 * @return \Drupal\Core\Database\Install\Tasks[]
646 * An array of available database driver installer objects.
648 protected function getDatabaseTypes() {
649 if ($this->originalContainer) {
650 \Drupal::setContainer($this->originalContainer);
652 $database_types = drupal_get_database_types();
653 if ($this->originalContainer) {
654 \Drupal::unsetContainer();
656 return $database_types;