3 namespace Drupal\Core\Test;
5 use Drupal\Component\FileCache\FileCacheFactory;
6 use Drupal\Component\Render\FormattableMarkup;
7 use Drupal\Core\Config\Development\ConfigSchemaChecker;
8 use Drupal\Core\Database\Database;
9 use Drupal\Core\DrupalKernel;
10 use Drupal\Core\Extension\MissingDependencyException;
11 use Drupal\Core\Serialization\Yaml;
12 use Drupal\Core\Session\UserSession;
13 use Drupal\Core\Site\Settings;
14 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
15 use Drupal\Tests\SessionTestTrait;
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 use RefreshVariablesTrait;
29 * The "#1" admin user.
31 * @var \Drupal\Core\Session\AccountInterface
36 * The class loader to use for installation and initialization of setup.
38 * @var \Symfony\Component\Classloader\Classloader
40 protected $classLoader;
43 * The config directories used in this test.
45 protected $configDirectories = [];
48 * The flag to set 'apcu_ensure_unique_prefix' setting.
50 * Wide use of a unique prefix can lead to problems with memory, if tests are
51 * run with a concurrency higher than 1. Therefore, FALSE by default.
55 * @see \Drupal\Core\Site\Settings::getApcuPrefix().
57 protected $apcuEnsureUniquePrefix = FALSE;
60 * Prepares site settings and services before installation.
62 protected function prepareSettings() {
63 // Prepare installer settings that are not install_drupal() parameters.
64 // Copy and prepare an actual settings.php, so as to resemble a regular
66 // Not using File API; a potential error must trigger a PHP warning.
67 $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
68 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
70 // The public file system path is created during installation. Additionally,
72 // - The temporary directory is set and created by install_base_system().
73 // - The private file directory is created post install by
74 // FunctionalTestSetupTrait::initConfig().
75 // @see system_requirements()
76 // @see TestBase::prepareEnvironment()
77 // @see install_base_system()
78 // @see \Drupal\Core\Test\FunctionalTestSetupTrait::initConfig()
79 $settings['settings']['file_public_path'] = (object) [
80 'value' => $this->publicFilesDirectory,
83 $settings['settings']['file_private_path'] = (object) [
84 'value' => $this->privateFilesDirectory,
87 // Save the original site directory path, so that extensions in the
88 // site-specific directory can still be discovered in the test site
90 // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
91 $settings['settings']['test_parent_site'] = (object) [
92 'value' => $this->originalSite,
95 // Add the parent profile's search path to the child site's search paths.
96 // @see \Drupal\Core\Extension\ExtensionDiscovery::getProfileDirectories()
97 $settings['conf']['simpletest.settings']['parent_profile'] = (object) [
98 'value' => $this->originalProfile,
101 $settings['settings']['apcu_ensure_unique_prefix'] = (object) [
102 'value' => $this->apcuEnsureUniquePrefix,
105 $this->writeSettings($settings);
106 // Allow for test-specific overrides.
107 $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
108 if (file_exists($settings_testing_file)) {
109 // Copy the testing-specific settings.php overrides in place.
110 copy($settings_testing_file, $directory . '/settings.testing.php');
111 // Add the name of the testing class to settings.php and include the
112 // testing specific overrides.
113 file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
115 $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
116 if (!file_exists($settings_services_file)) {
117 // Otherwise, use the default services as a starting point for overrides.
118 $settings_services_file = DRUPAL_ROOT . '/sites/default/default.services.yml';
120 // Copy the testing-specific service overrides in place.
121 copy($settings_services_file, $directory . '/services.yml');
122 if ($this->strictConfigSchema) {
123 // Add a listener to validate configuration schema on save.
124 $yaml = new SymfonyYaml();
125 $content = file_get_contents($directory . '/services.yml');
126 $services = $yaml->parse($content);
127 $services['services']['simpletest.config_schema_checker'] = [
128 'class' => ConfigSchemaChecker::class,
129 'arguments' => ['@config.typed', $this->getConfigSchemaExclusions()],
130 'tags' => [['name' => 'event_subscriber']],
132 file_put_contents($directory . '/services.yml', $yaml->dump($services));
134 // Since Drupal is bootstrapped already, install_begin_request() will not
135 // bootstrap again. Hence, we have to reload the newly written custom
136 // settings.php manually.
137 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
141 * Rewrites the settings.php file of the test site.
143 * @param array $settings
144 * An array of settings to write out, in the format expected by
145 * drupal_rewrite_settings().
147 * @see drupal_rewrite_settings()
149 protected function writeSettings(array $settings) {
150 include_once DRUPAL_ROOT . '/core/includes/install.inc';
151 $filename = $this->siteDirectory . '/settings.php';
152 // system_requirements() removes write permissions from settings.php
153 // whenever it is invoked.
154 // Not using File API; a potential error must trigger a PHP warning.
155 chmod($filename, 0666);
156 drupal_rewrite_settings($settings, $filename);
160 * Changes parameters in the services.yml file.
162 * @param string $name
163 * The name of the parameter.
164 * @param string $value
165 * The value of the parameter.
167 protected function setContainerParameter($name, $value) {
168 $filename = $this->siteDirectory . '/services.yml';
169 chmod($filename, 0666);
171 $services = Yaml::decode(file_get_contents($filename));
172 $services['parameters'][$name] = $value;
173 file_put_contents($filename, Yaml::encode($services));
175 // Ensure that the cache is deleted for the yaml file loader.
176 $file_cache = FileCacheFactory::get('container_yaml_loader');
177 $file_cache->delete($filename);
181 * Rebuilds \Drupal::getContainer().
183 * Use this to update the test process's kernel with a new service container.
184 * For example, when the list of enabled modules is changed via the internal
185 * browser the test process's kernel has a service container with an out of
188 * @see TestBase::prepareEnvironment()
189 * @see TestBase::restoreEnvironment()
191 * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
192 * changes are immediately reflected in \Drupal::getContainer(). Until then,
193 * tests can invoke this workaround when requiring services from newly
194 * enabled modules to be immediately available in the same request.
196 protected function rebuildContainer() {
197 // Rebuild the kernel and bring it back to a fully bootstrapped state.
198 $this->container = $this->kernel->rebuildContainer();
200 // Make sure the url generator has a request object, otherwise calls to
201 // $this->drupalGet() will fail.
202 $this->prepareRequestForGenerator();
206 * Resets all data structures after having enabled new modules.
208 * This method is called by FunctionalTestSetupTrait::rebuildAll() after
209 * enabling the requested modules. It must be called again when additional
210 * modules are enabled later.
212 * @see \Drupal\Core\Test\FunctionalTestSetupTrait::rebuildAll()
213 * @see \Drupal\Tests\BrowserTestBase::installDrupal()
214 * @see \Drupal\simpletest\WebTestBase::setUp()
216 protected function resetAll() {
217 // Clear all database and static caches and rebuild data structures.
218 drupal_flush_all_caches();
219 $this->container = \Drupal::getContainer();
221 // Reset static variables and reload permissions.
222 $this->refreshVariables();
226 * Creates a mock request and sets it on the generator.
228 * This is used to manipulate how the generator generates paths during tests.
229 * It also ensures that calls to $this->drupalGet() will work when running
230 * from run-tests.sh because the url generator no longer looks at the global
231 * variables that are set there but relies on getting this information from a
234 * @param bool $clean_urls
235 * Whether to mock the request using clean urls.
236 * @param array $override_server_vars
237 * An array of server variables to override.
239 * @return \Symfony\Component\HttpFoundation\Request
240 * The mocked request object.
242 protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = []) {
243 $request = Request::createFromGlobals();
244 $server = $request->server->all();
245 if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
246 // We need this for when the test is executed by run-tests.sh.
247 // @todo Remove this once run-tests.sh has been converted to use a Request
250 $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
251 $base_path = rtrim($server['REQUEST_URI'], '/');
254 $base_path = $request->getBasePath();
257 $request_path = $base_path ? $base_path . '/user' : 'user';
260 $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
262 $server = array_merge($server, $override_server_vars);
264 $request = Request::create($request_path, 'GET', [], [], [], $server);
265 // Ensure the request time is REQUEST_TIME to ensure that API calls
266 // in the test use the right timestamp.
267 $request->server->set('REQUEST_TIME', REQUEST_TIME);
268 $this->container->get('request_stack')->push($request);
270 // The request context is normally set by the router_listener from within
271 // its KernelEvents::REQUEST listener. In the simpletest parent site this
272 // event is not fired, therefore it is necessary to updated the request
273 // context manually here.
274 $this->container->get('router.request_context')->fromRequest($request);
280 * Execute the non-interactive installer.
282 * @see install_drupal()
284 protected function doInstall() {
285 require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
286 install_drupal($this->classLoader, $this->installParameters());
290 * Initialize settings created during install.
292 protected function initSettings() {
293 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
294 foreach ($GLOBALS['config_directories'] as $type => $path) {
295 $this->configDirectories[$type] = $path;
298 // After writing settings.php, the installer removes write permissions
299 // from the site directory. To allow drupal_generate_test_ua() to write
300 // a file containing the private key for drupal_valid_test_ua(), the site
301 // directory has to be writable.
302 // TestBase::restoreEnvironment() will delete the entire site directory.
303 // Not using File API; a potential error must trigger a PHP warning.
304 chmod(DRUPAL_ROOT . '/' . $this->siteDirectory, 0777);
306 // During tests, cacheable responses should get the debugging cacheability
307 // headers by default.
308 $this->setContainerParameter('http.response.debug_cacheability_headers', TRUE);
312 * Initialize various configurations post-installation.
314 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
317 protected function initConfig(ContainerInterface $container) {
318 $config = $container->get('config.factory');
320 // Manually create the private directory.
321 file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
323 // Manually configure the test mail collector implementation to prevent
324 // tests from sending out emails and collect them in state instead.
325 // While this should be enforced via settings.php prior to installation,
326 // some tests expect to be able to test mail system implementations.
327 $config->getEditable('system.mail')
328 ->set('interface.default', 'test_mail_collector')
331 // By default, verbosely display all errors and disable all production
332 // environment optimizations for all tests to avoid needless overhead and
333 // ensure a sane default experience for test authors.
334 // @see https://www.drupal.org/node/2259167
335 $config->getEditable('system.logging')
336 ->set('error_level', 'verbose')
338 $config->getEditable('system.performance')
339 ->set('css.preprocess', FALSE)
340 ->set('js.preprocess', FALSE)
343 // Set an explicit time zone to not rely on the system one, which may vary
344 // from setup to setup. The Australia/Sydney time zone is chosen so all
345 // tests are run using an edge case scenario (UTC10 and DST). This choice
346 // is made to prevent time zone related regressions and reduce the
347 // fragility of the testing system in general.
348 $config->getEditable('system.date')
349 ->set('timezone.default', 'Australia/Sydney')
354 * Initializes user 1 for the site to be installed.
356 protected function initUserSession() {
357 $password = $this->randomMachineName();
358 // Define information about the user 1 account.
359 $this->rootUser = new UserSession([
362 'mail' => 'admin@example.com',
363 'pass_raw' => $password,
364 'passRaw' => $password,
365 'timezone' => date_default_timezone_get(),
368 // The child site derives its session name from the database prefix when
369 // running web tests.
370 $this->generateSessionName($this->databasePrefix);
374 * Initializes the kernel after installation.
376 * @param \Symfony\Component\HttpFoundation\Request $request
379 * @return \Symfony\Component\DependencyInjection\ContainerInterface
382 protected function initKernel(Request $request) {
383 $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
384 $this->kernel->prepareLegacyRequest($request);
385 // Force the container to be built from scratch instead of loaded from the
386 // disk. This forces us to not accidentally load the parent site.
387 return $this->kernel->rebuildContainer();
391 * Install modules defined by `static::$modules`.
393 * To install test modules outside of the testing environment, add
395 * $settings['extension_discovery_scan_tests'] = TRUE;
397 * to your settings.php.
399 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
402 protected function installModulesFromClassProperty(ContainerInterface $container) {
403 $class = get_class($this);
406 if (property_exists($class, 'modules')) {
407 $modules = array_merge($modules, $class::$modules);
409 $class = get_parent_class($class);
412 $modules = array_unique($modules);
414 $success = $container->get('module_installer')->install($modules, TRUE);
415 $this->assertTrue($success, new FormattableMarkup('Enabled modules: %modules', ['%modules' => implode(', ', $modules)]));
417 catch (MissingDependencyException $e) {
418 // The exception message has all the details.
419 $this->fail($e->getMessage());
422 $this->rebuildContainer();
427 * Resets and rebuilds the environment after setup.
429 protected function rebuildAll() {
430 // Reset/rebuild all data structures after enabling the modules, primarily
431 // to synchronize all data structures and caches between the test runner and
433 // @see \Drupal\Core\DrupalKernel::bootCode()
434 // @todo Test-specific setUp() methods may set up further fixtures; find a
435 // way to execute this after setUp() is done, or to eliminate it entirely.
437 $this->kernel->prepareLegacyRequest(\Drupal::request());
439 // Explicitly call register() again on the container registered in \Drupal.
440 // @todo This should already be called through
441 // DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that
442 // appears to be calling a different container.
443 $this->container->get('stream_wrapper_manager')->register();
447 * Returns the parameters that will be used when Simpletest installs Drupal.
449 * @see install_drupal()
450 * @see install_state_defaults()
453 * Array of parameters for use in install_drupal().
455 protected function installParameters() {
456 $connection_info = Database::getConnectionInfo();
457 $driver = $connection_info['default']['driver'];
458 $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
459 unset($connection_info['default']['driver']);
460 unset($connection_info['default']['namespace']);
461 unset($connection_info['default']['pdo']);
462 unset($connection_info['default']['init_commands']);
463 // Remove database connection info that is not used by SQLite.
464 if ($driver === 'sqlite') {
465 unset($connection_info['default']['username']);
466 unset($connection_info['default']['password']);
467 unset($connection_info['default']['host']);
468 unset($connection_info['default']['port']);
471 'interactive' => FALSE,
473 'profile' => $this->profile,
477 'install_settings_form' => [
479 $driver => $connection_info['default'],
481 'install_configure_form' => [
482 'site_name' => 'Drupal',
483 'site_mail' => 'simpletest@example.com',
485 'name' => $this->rootUser->name,
486 'mail' => $this->rootUser->getEmail(),
488 'pass1' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
489 'pass2' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
492 // form_type_checkboxes_value() requires NULL instead of FALSE values
493 // for programmatic form submissions to disable a checkbox.
494 'enable_update_status_module' => NULL,
495 'enable_update_status_emails' => NULL,
500 // If we only have one db driver available, we cannot set the driver.
501 include_once DRUPAL_ROOT . '/core/includes/install.inc';
502 if (count($this->getDatabaseTypes()) == 1) {
503 unset($parameters['forms']['install_settings_form']['driver']);
509 * Sets up the base URL based upon the environment variable.
512 * Thrown when no SIMPLETEST_BASE_URL environment variable is provided.
514 protected function setupBaseUrl() {
517 // Get and set the domain of the environment we are running our test
519 $base_url = getenv('SIMPLETEST_BASE_URL');
521 throw new \Exception(
522 'You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.'
526 // Setup $_SERVER variable.
527 $parsed_url = parse_url($base_url);
528 $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
529 $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
530 $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;
532 $this->baseUrl = $base_url;
534 // If the passed URL schema is 'https' then setup the $_SERVER variables
535 // properly so that testing will run under HTTPS.
536 if ($parsed_url['scheme'] === 'https') {
537 $_SERVER['HTTPS'] = 'on';
539 $_SERVER['HTTP_HOST'] = $host;
540 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
541 $_SERVER['SERVER_ADDR'] = '127.0.0.1';
542 $_SERVER['SERVER_PORT'] = $port;
543 $_SERVER['SERVER_SOFTWARE'] = NULL;
544 $_SERVER['SERVER_NAME'] = 'localhost';
545 $_SERVER['REQUEST_URI'] = $path . '/';
546 $_SERVER['REQUEST_METHOD'] = 'GET';
547 $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
548 $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
549 $_SERVER['PHP_SELF'] = $path . '/index.php';
550 $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
554 * Prepares the current environment for running the test.
556 * Also sets up new resources for the testing environment, such as the public
557 * filesystem and configuration directories.
559 * This method is private as it must only be called once by
560 * BrowserTestBase::setUp() (multiple invocations for the same test would have
561 * unpredictable consequences) and it must not be callable or overridable by
564 protected function prepareEnvironment() {
565 // Bootstrap Drupal so we can use Drupal's built in functions.
566 $this->classLoader = require __DIR__ . '/../../../../../autoload.php';
567 $request = Request::createFromGlobals();
568 $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
569 // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
571 $kernel->prepareLegacyRequest($request);
572 $this->prepareDatabasePrefix();
574 $this->originalSite = $kernel->findSitePath($request);
576 // Create test directory ahead of installation so fatal errors and debug
577 // information can be logged during installation process.
578 file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
580 // Prepare filesystem directory paths.
581 $this->publicFilesDirectory = $this->siteDirectory . '/files';
582 $this->privateFilesDirectory = $this->siteDirectory . '/private';
583 $this->tempFilesDirectory = $this->siteDirectory . '/temp';
584 $this->translationFilesDirectory = $this->siteDirectory . '/translations';
586 // Ensure the configImporter is refreshed for each test.
587 $this->configImporter = NULL;
589 // Unregister all custom stream wrappers of the parent site.
590 $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
591 foreach ($wrappers as $scheme => $info) {
592 stream_wrapper_unregister($scheme);
596 drupal_static_reset();
598 $this->container = NULL;
601 unset($GLOBALS['config_directories']);
602 unset($GLOBALS['config']);
603 unset($GLOBALS['conf']);
606 ini_set('log_errors', 1);
607 ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
609 // Change the database prefix.
610 $this->changeDatabasePrefix();
612 // After preparing the environment and changing the database prefix, we are
613 // in a valid test environment.
614 drupal_valid_test_ua($this->databasePrefix);
618 // For performance, simply use the database prefix as hash salt.
619 'hash_salt' => $this->databasePrefix,
622 drupal_set_time_limit($this->timeLimit);
624 // Save and clean the shutdown callbacks array because it is static cached
625 // and will be changed by the test run. Otherwise it will contain callbacks
626 // from both environments and the testing environment will try to call the
627 // handlers defined by the original one.
628 $callbacks = &drupal_register_shutdown_function();
629 $this->originalShutdownCallbacks = $callbacks;
634 * Returns all supported database driver installer objects.
636 * This wraps drupal_get_database_types() for use without a current container.
638 * @return \Drupal\Core\Database\Install\Tasks[]
639 * An array of available database driver installer objects.
641 protected function getDatabaseTypes() {
642 if (isset($this->originalContainer) && $this->originalContainer) {
643 \Drupal::setContainer($this->originalContainer);
645 $database_types = drupal_get_database_types();
646 if (isset($this->originalContainer) && $this->originalContainer) {
647 \Drupal::unsetContainer();
649 return $database_types;