annotate core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Core\Test;
Chris@0 4
Chris@0 5 use Drupal\Component\FileCache\FileCacheFactory;
Chris@0 6 use Drupal\Component\Utility\SafeMarkup;
Chris@0 7 use Drupal\Core\Cache\Cache;
Chris@0 8 use Drupal\Core\Config\Development\ConfigSchemaChecker;
Chris@0 9 use Drupal\Core\Database\Database;
Chris@0 10 use Drupal\Core\DrupalKernel;
Chris@0 11 use Drupal\Core\Extension\MissingDependencyException;
Chris@0 12 use Drupal\Core\Serialization\Yaml;
Chris@0 13 use Drupal\Core\Session\UserSession;
Chris@0 14 use Drupal\Core\Site\Settings;
Chris@0 15 use Drupal\Core\StreamWrapper\StreamWrapperInterface;
Chris@0 16 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 17 use Symfony\Component\HttpFoundation\Request;
Chris@0 18 use Symfony\Component\Yaml\Yaml as SymfonyYaml;
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Defines a trait for shared functional test setup functionality.
Chris@0 22 */
Chris@0 23 trait FunctionalTestSetupTrait {
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The "#1" admin user.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Core\Session\AccountInterface
Chris@0 29 */
Chris@0 30 protected $rootUser;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The class loader to use for installation and initialization of setup.
Chris@0 34 *
Chris@0 35 * @var \Symfony\Component\Classloader\Classloader
Chris@0 36 */
Chris@0 37 protected $classLoader;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The config directories used in this test.
Chris@0 41 */
Chris@0 42 protected $configDirectories = [];
Chris@0 43
Chris@0 44 /**
Chris@14 45 * The flag to set 'apcu_ensure_unique_prefix' setting.
Chris@14 46 *
Chris@14 47 * Wide use of a unique prefix can lead to problems with memory, if tests are
Chris@14 48 * run with a concurrency higher than 1. Therefore, FALSE by default.
Chris@14 49 *
Chris@14 50 * @var bool
Chris@14 51 *
Chris@14 52 * @see \Drupal\Core\Site\Settings::getApcuPrefix().
Chris@14 53 */
Chris@14 54 protected $apcuEnsureUniquePrefix = FALSE;
Chris@14 55
Chris@14 56 /**
Chris@0 57 * Prepares site settings and services before installation.
Chris@0 58 */
Chris@0 59 protected function prepareSettings() {
Chris@0 60 // Prepare installer settings that are not install_drupal() parameters.
Chris@0 61 // Copy and prepare an actual settings.php, so as to resemble a regular
Chris@0 62 // installation.
Chris@0 63 // Not using File API; a potential error must trigger a PHP warning.
Chris@0 64 $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
Chris@0 65 copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
Chris@0 66
Chris@0 67 // The public file system path is created during installation. Additionally,
Chris@0 68 // during tests:
Chris@0 69 // - The temporary directory is set and created by install_base_system().
Chris@0 70 // - The private file directory is created post install by
Chris@0 71 // FunctionalTestSetupTrait::initConfig().
Chris@0 72 // @see system_requirements()
Chris@0 73 // @see TestBase::prepareEnvironment()
Chris@0 74 // @see install_base_system()
Chris@0 75 // @see \Drupal\Core\Test\FunctionalTestSetupTrait::initConfig()
Chris@0 76 $settings['settings']['file_public_path'] = (object) [
Chris@0 77 'value' => $this->publicFilesDirectory,
Chris@0 78 'required' => TRUE,
Chris@0 79 ];
Chris@0 80 $settings['settings']['file_private_path'] = (object) [
Chris@0 81 'value' => $this->privateFilesDirectory,
Chris@0 82 'required' => TRUE,
Chris@0 83 ];
Chris@0 84 // Save the original site directory path, so that extensions in the
Chris@0 85 // site-specific directory can still be discovered in the test site
Chris@0 86 // environment.
Chris@0 87 // @see \Drupal\Core\Extension\ExtensionDiscovery::scan()
Chris@0 88 $settings['settings']['test_parent_site'] = (object) [
Chris@0 89 'value' => $this->originalSite,
Chris@0 90 'required' => TRUE,
Chris@0 91 ];
Chris@0 92 // Add the parent profile's search path to the child site's search paths.
Chris@0 93 // @see \Drupal\Core\Extension\ExtensionDiscovery::getProfileDirectories()
Chris@0 94 $settings['conf']['simpletest.settings']['parent_profile'] = (object) [
Chris@0 95 'value' => $this->originalProfile,
Chris@0 96 'required' => TRUE,
Chris@0 97 ];
Chris@14 98 $settings['settings']['apcu_ensure_unique_prefix'] = (object) [
Chris@14 99 'value' => $this->apcuEnsureUniquePrefix,
Chris@14 100 'required' => TRUE,
Chris@14 101 ];
Chris@0 102 $this->writeSettings($settings);
Chris@0 103 // Allow for test-specific overrides.
Chris@0 104 $settings_testing_file = DRUPAL_ROOT . '/' . $this->originalSite . '/settings.testing.php';
Chris@0 105 if (file_exists($settings_testing_file)) {
Chris@0 106 // Copy the testing-specific settings.php overrides in place.
Chris@0 107 copy($settings_testing_file, $directory . '/settings.testing.php');
Chris@0 108 // Add the name of the testing class to settings.php and include the
Chris@0 109 // testing specific overrides.
Chris@0 110 file_put_contents($directory . '/settings.php', "\n\$test_class = '" . get_class($this) . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
Chris@0 111 }
Chris@0 112 $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
Chris@0 113 if (!file_exists($settings_services_file)) {
Chris@0 114 // Otherwise, use the default services as a starting point for overrides.
Chris@0 115 $settings_services_file = DRUPAL_ROOT . '/sites/default/default.services.yml';
Chris@0 116 }
Chris@0 117 // Copy the testing-specific service overrides in place.
Chris@0 118 copy($settings_services_file, $directory . '/services.yml');
Chris@0 119 if ($this->strictConfigSchema) {
Chris@0 120 // Add a listener to validate configuration schema on save.
Chris@0 121 $yaml = new SymfonyYaml();
Chris@0 122 $content = file_get_contents($directory . '/services.yml');
Chris@0 123 $services = $yaml->parse($content);
Chris@0 124 $services['services']['simpletest.config_schema_checker'] = [
Chris@0 125 'class' => ConfigSchemaChecker::class,
Chris@0 126 'arguments' => ['@config.typed', $this->getConfigSchemaExclusions()],
Chris@0 127 'tags' => [['name' => 'event_subscriber']],
Chris@0 128 ];
Chris@0 129 file_put_contents($directory . '/services.yml', $yaml->dump($services));
Chris@0 130 }
Chris@0 131 // Since Drupal is bootstrapped already, install_begin_request() will not
Chris@0 132 // bootstrap again. Hence, we have to reload the newly written custom
Chris@0 133 // settings.php manually.
Chris@0 134 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
Chris@0 135 }
Chris@0 136
Chris@0 137 /**
Chris@0 138 * Rewrites the settings.php file of the test site.
Chris@0 139 *
Chris@0 140 * @param array $settings
Chris@0 141 * An array of settings to write out, in the format expected by
Chris@0 142 * drupal_rewrite_settings().
Chris@0 143 *
Chris@0 144 * @see drupal_rewrite_settings()
Chris@0 145 */
Chris@0 146 protected function writeSettings(array $settings) {
Chris@0 147 include_once DRUPAL_ROOT . '/core/includes/install.inc';
Chris@0 148 $filename = $this->siteDirectory . '/settings.php';
Chris@0 149 // system_requirements() removes write permissions from settings.php
Chris@0 150 // whenever it is invoked.
Chris@0 151 // Not using File API; a potential error must trigger a PHP warning.
Chris@0 152 chmod($filename, 0666);
Chris@0 153 drupal_rewrite_settings($settings, $filename);
Chris@0 154 }
Chris@0 155
Chris@0 156 /**
Chris@0 157 * Changes parameters in the services.yml file.
Chris@0 158 *
Chris@0 159 * @param string $name
Chris@0 160 * The name of the parameter.
Chris@0 161 * @param string $value
Chris@0 162 * The value of the parameter.
Chris@0 163 */
Chris@0 164 protected function setContainerParameter($name, $value) {
Chris@0 165 $filename = $this->siteDirectory . '/services.yml';
Chris@0 166 chmod($filename, 0666);
Chris@0 167
Chris@0 168 $services = Yaml::decode(file_get_contents($filename));
Chris@0 169 $services['parameters'][$name] = $value;
Chris@0 170 file_put_contents($filename, Yaml::encode($services));
Chris@0 171
Chris@0 172 // Ensure that the cache is deleted for the yaml file loader.
Chris@0 173 $file_cache = FileCacheFactory::get('container_yaml_loader');
Chris@0 174 $file_cache->delete($filename);
Chris@0 175 }
Chris@0 176
Chris@0 177 /**
Chris@0 178 * Rebuilds \Drupal::getContainer().
Chris@0 179 *
Chris@0 180 * Use this to update the test process's kernel with a new service container.
Chris@0 181 * For example, when the list of enabled modules is changed via the internal
Chris@0 182 * browser the test process's kernel has a service container with an out of
Chris@0 183 * date module list.
Chris@0 184 *
Chris@0 185 * @see TestBase::prepareEnvironment()
Chris@0 186 * @see TestBase::restoreEnvironment()
Chris@0 187 *
Chris@0 188 * @todo Fix https://www.drupal.org/node/2021959 so that module enable/disable
Chris@0 189 * changes are immediately reflected in \Drupal::getContainer(). Until then,
Chris@0 190 * tests can invoke this workaround when requiring services from newly
Chris@0 191 * enabled modules to be immediately available in the same request.
Chris@0 192 */
Chris@0 193 protected function rebuildContainer() {
Chris@0 194 // Rebuild the kernel and bring it back to a fully bootstrapped state.
Chris@0 195 $this->container = $this->kernel->rebuildContainer();
Chris@0 196
Chris@0 197 // Make sure the url generator has a request object, otherwise calls to
Chris@0 198 // $this->drupalGet() will fail.
Chris@0 199 $this->prepareRequestForGenerator();
Chris@0 200 }
Chris@0 201
Chris@0 202 /**
Chris@0 203 * Resets all data structures after having enabled new modules.
Chris@0 204 *
Chris@0 205 * This method is called by FunctionalTestSetupTrait::rebuildAll() after
Chris@0 206 * enabling the requested modules. It must be called again when additional
Chris@0 207 * modules are enabled later.
Chris@0 208 *
Chris@0 209 * @see \Drupal\Core\Test\FunctionalTestSetupTrait::rebuildAll()
Chris@0 210 * @see \Drupal\Tests\BrowserTestBase::installDrupal()
Chris@0 211 * @see \Drupal\simpletest\WebTestBase::setUp()
Chris@0 212 */
Chris@0 213 protected function resetAll() {
Chris@0 214 // Clear all database and static caches and rebuild data structures.
Chris@0 215 drupal_flush_all_caches();
Chris@0 216 $this->container = \Drupal::getContainer();
Chris@0 217
Chris@0 218 // Reset static variables and reload permissions.
Chris@0 219 $this->refreshVariables();
Chris@0 220 }
Chris@0 221
Chris@0 222 /**
Chris@0 223 * Refreshes in-memory configuration and state information.
Chris@0 224 *
Chris@0 225 * Useful after a page request is made that changes configuration or state in
Chris@0 226 * a different thread.
Chris@0 227 *
Chris@0 228 * In other words calling a settings page with $this->drupalPostForm() with a
Chris@0 229 * changed value would update configuration to reflect that change, but in the
Chris@0 230 * thread that made the call (thread running the test) the changed values
Chris@0 231 * would not be picked up.
Chris@0 232 *
Chris@0 233 * This method clears the cache and loads a fresh copy.
Chris@0 234 */
Chris@0 235 protected function refreshVariables() {
Chris@0 236 // Clear the tag cache.
Chris@0 237 \Drupal::service('cache_tags.invalidator')->resetChecksums();
Chris@0 238 foreach (Cache::getBins() as $backend) {
Chris@0 239 if (is_callable([$backend, 'reset'])) {
Chris@0 240 $backend->reset();
Chris@0 241 }
Chris@0 242 }
Chris@0 243
Chris@0 244 $this->container->get('config.factory')->reset();
Chris@0 245 $this->container->get('state')->resetCache();
Chris@0 246 }
Chris@0 247
Chris@0 248 /**
Chris@0 249 * Creates a mock request and sets it on the generator.
Chris@0 250 *
Chris@0 251 * This is used to manipulate how the generator generates paths during tests.
Chris@0 252 * It also ensures that calls to $this->drupalGet() will work when running
Chris@0 253 * from run-tests.sh because the url generator no longer looks at the global
Chris@0 254 * variables that are set there but relies on getting this information from a
Chris@0 255 * request object.
Chris@0 256 *
Chris@0 257 * @param bool $clean_urls
Chris@0 258 * Whether to mock the request using clean urls.
Chris@0 259 * @param array $override_server_vars
Chris@0 260 * An array of server variables to override.
Chris@0 261 *
Chris@0 262 * @return \Symfony\Component\HttpFoundation\Request
Chris@0 263 * The mocked request object.
Chris@0 264 */
Chris@0 265 protected function prepareRequestForGenerator($clean_urls = TRUE, $override_server_vars = []) {
Chris@0 266 $request = Request::createFromGlobals();
Chris@0 267 $server = $request->server->all();
Chris@0 268 if (basename($server['SCRIPT_FILENAME']) != basename($server['SCRIPT_NAME'])) {
Chris@0 269 // We need this for when the test is executed by run-tests.sh.
Chris@0 270 // @todo Remove this once run-tests.sh has been converted to use a Request
Chris@0 271 // object.
Chris@0 272 $cwd = getcwd();
Chris@0 273 $server['SCRIPT_FILENAME'] = $cwd . '/' . basename($server['SCRIPT_NAME']);
Chris@0 274 $base_path = rtrim($server['REQUEST_URI'], '/');
Chris@0 275 }
Chris@0 276 else {
Chris@0 277 $base_path = $request->getBasePath();
Chris@0 278 }
Chris@0 279 if ($clean_urls) {
Chris@0 280 $request_path = $base_path ? $base_path . '/user' : 'user';
Chris@0 281 }
Chris@0 282 else {
Chris@0 283 $request_path = $base_path ? $base_path . '/index.php/user' : '/index.php/user';
Chris@0 284 }
Chris@0 285 $server = array_merge($server, $override_server_vars);
Chris@0 286
Chris@0 287 $request = Request::create($request_path, 'GET', [], [], [], $server);
Chris@0 288 // Ensure the request time is REQUEST_TIME to ensure that API calls
Chris@0 289 // in the test use the right timestamp.
Chris@0 290 $request->server->set('REQUEST_TIME', REQUEST_TIME);
Chris@0 291 $this->container->get('request_stack')->push($request);
Chris@0 292
Chris@0 293 // The request context is normally set by the router_listener from within
Chris@0 294 // its KernelEvents::REQUEST listener. In the simpletest parent site this
Chris@0 295 // event is not fired, therefore it is necessary to updated the request
Chris@0 296 // context manually here.
Chris@0 297 $this->container->get('router.request_context')->fromRequest($request);
Chris@0 298
Chris@0 299 return $request;
Chris@0 300 }
Chris@0 301
Chris@0 302 /**
Chris@0 303 * Execute the non-interactive installer.
Chris@0 304 *
Chris@0 305 * @see install_drupal()
Chris@0 306 */
Chris@0 307 protected function doInstall() {
Chris@0 308 require_once DRUPAL_ROOT . '/core/includes/install.core.inc';
Chris@0 309 install_drupal($this->classLoader, $this->installParameters());
Chris@0 310 }
Chris@0 311
Chris@0 312 /**
Chris@0 313 * Initialize settings created during install.
Chris@0 314 */
Chris@0 315 protected function initSettings() {
Chris@0 316 Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
Chris@0 317 foreach ($GLOBALS['config_directories'] as $type => $path) {
Chris@0 318 $this->configDirectories[$type] = $path;
Chris@0 319 }
Chris@0 320
Chris@0 321 // After writing settings.php, the installer removes write permissions
Chris@0 322 // from the site directory. To allow drupal_generate_test_ua() to write
Chris@0 323 // a file containing the private key for drupal_valid_test_ua(), the site
Chris@0 324 // directory has to be writable.
Chris@0 325 // TestBase::restoreEnvironment() will delete the entire site directory.
Chris@0 326 // Not using File API; a potential error must trigger a PHP warning.
Chris@0 327 chmod(DRUPAL_ROOT . '/' . $this->siteDirectory, 0777);
Chris@0 328
Chris@0 329 // During tests, cacheable responses should get the debugging cacheability
Chris@0 330 // headers by default.
Chris@0 331 $this->setContainerParameter('http.response.debug_cacheability_headers', TRUE);
Chris@0 332 }
Chris@0 333
Chris@0 334 /**
Chris@0 335 * Initialize various configurations post-installation.
Chris@0 336 *
Chris@0 337 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
Chris@0 338 * The container.
Chris@0 339 */
Chris@0 340 protected function initConfig(ContainerInterface $container) {
Chris@0 341 $config = $container->get('config.factory');
Chris@0 342
Chris@0 343 // Manually create the private directory.
Chris@0 344 file_prepare_directory($this->privateFilesDirectory, FILE_CREATE_DIRECTORY);
Chris@0 345
Chris@0 346 // Manually configure the test mail collector implementation to prevent
Chris@0 347 // tests from sending out emails and collect them in state instead.
Chris@0 348 // While this should be enforced via settings.php prior to installation,
Chris@0 349 // some tests expect to be able to test mail system implementations.
Chris@0 350 $config->getEditable('system.mail')
Chris@0 351 ->set('interface.default', 'test_mail_collector')
Chris@0 352 ->save();
Chris@0 353
Chris@0 354 // By default, verbosely display all errors and disable all production
Chris@0 355 // environment optimizations for all tests to avoid needless overhead and
Chris@0 356 // ensure a sane default experience for test authors.
Chris@0 357 // @see https://www.drupal.org/node/2259167
Chris@0 358 $config->getEditable('system.logging')
Chris@0 359 ->set('error_level', 'verbose')
Chris@0 360 ->save();
Chris@0 361 $config->getEditable('system.performance')
Chris@0 362 ->set('css.preprocess', FALSE)
Chris@0 363 ->set('js.preprocess', FALSE)
Chris@0 364 ->save();
Chris@0 365
Chris@0 366 // Set an explicit time zone to not rely on the system one, which may vary
Chris@0 367 // from setup to setup. The Australia/Sydney time zone is chosen so all
Chris@0 368 // tests are run using an edge case scenario (UTC10 and DST). This choice
Chris@0 369 // is made to prevent time zone related regressions and reduce the
Chris@0 370 // fragility of the testing system in general.
Chris@0 371 $config->getEditable('system.date')
Chris@0 372 ->set('timezone.default', 'Australia/Sydney')
Chris@0 373 ->save();
Chris@0 374 }
Chris@0 375
Chris@0 376 /**
Chris@0 377 * Initializes user 1 for the site to be installed.
Chris@0 378 */
Chris@0 379 protected function initUserSession() {
Chris@0 380 $password = $this->randomMachineName();
Chris@0 381 // Define information about the user 1 account.
Chris@0 382 $this->rootUser = new UserSession([
Chris@0 383 'uid' => 1,
Chris@0 384 'name' => 'admin',
Chris@0 385 'mail' => 'admin@example.com',
Chris@0 386 'pass_raw' => $password,
Chris@0 387 'passRaw' => $password,
Chris@0 388 'timezone' => date_default_timezone_get(),
Chris@0 389 ]);
Chris@0 390
Chris@0 391 // The child site derives its session name from the database prefix when
Chris@0 392 // running web tests.
Chris@0 393 $this->generateSessionName($this->databasePrefix);
Chris@0 394 }
Chris@0 395
Chris@0 396 /**
Chris@0 397 * Initializes the kernel after installation.
Chris@0 398 *
Chris@0 399 * @param \Symfony\Component\HttpFoundation\Request $request
Chris@0 400 * Request object.
Chris@0 401 *
Chris@0 402 * @return \Symfony\Component\DependencyInjection\ContainerInterface
Chris@0 403 * The container.
Chris@0 404 */
Chris@0 405 protected function initKernel(Request $request) {
Chris@0 406 $this->kernel = DrupalKernel::createFromRequest($request, $this->classLoader, 'prod', TRUE);
Chris@0 407 $this->kernel->prepareLegacyRequest($request);
Chris@0 408 // Force the container to be built from scratch instead of loaded from the
Chris@0 409 // disk. This forces us to not accidentally load the parent site.
Chris@0 410 return $this->kernel->rebuildContainer();
Chris@0 411 }
Chris@0 412
Chris@0 413 /**
Chris@0 414 * Install modules defined by `static::$modules`.
Chris@0 415 *
Chris@0 416 * To install test modules outside of the testing environment, add
Chris@0 417 * @code
Chris@0 418 * $settings['extension_discovery_scan_tests'] = TRUE;
Chris@0 419 * @endcode
Chris@0 420 * to your settings.php.
Chris@0 421 *
Chris@0 422 * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
Chris@0 423 * The container.
Chris@0 424 */
Chris@0 425 protected function installModulesFromClassProperty(ContainerInterface $container) {
Chris@0 426 $class = get_class($this);
Chris@0 427 $modules = [];
Chris@0 428 while ($class) {
Chris@0 429 if (property_exists($class, 'modules')) {
Chris@0 430 $modules = array_merge($modules, $class::$modules);
Chris@0 431 }
Chris@0 432 $class = get_parent_class($class);
Chris@0 433 }
Chris@0 434 if ($modules) {
Chris@0 435 $modules = array_unique($modules);
Chris@0 436 try {
Chris@0 437 $success = $container->get('module_installer')->install($modules, TRUE);
Chris@0 438 $this->assertTrue($success, SafeMarkup::format('Enabled modules: %modules', ['%modules' => implode(', ', $modules)]));
Chris@0 439 }
Chris@0 440 catch (MissingDependencyException $e) {
Chris@0 441 // The exception message has all the details.
Chris@0 442 $this->fail($e->getMessage());
Chris@0 443 }
Chris@0 444
Chris@0 445 $this->rebuildContainer();
Chris@0 446 }
Chris@0 447 }
Chris@0 448
Chris@0 449 /**
Chris@0 450 * Resets and rebuilds the environment after setup.
Chris@0 451 */
Chris@0 452 protected function rebuildAll() {
Chris@0 453 // Reset/rebuild all data structures after enabling the modules, primarily
Chris@0 454 // to synchronize all data structures and caches between the test runner and
Chris@0 455 // the child site.
Chris@0 456 // @see \Drupal\Core\DrupalKernel::bootCode()
Chris@0 457 // @todo Test-specific setUp() methods may set up further fixtures; find a
Chris@0 458 // way to execute this after setUp() is done, or to eliminate it entirely.
Chris@0 459 $this->resetAll();
Chris@0 460 $this->kernel->prepareLegacyRequest(\Drupal::request());
Chris@0 461
Chris@0 462 // Explicitly call register() again on the container registered in \Drupal.
Chris@0 463 // @todo This should already be called through
Chris@0 464 // DrupalKernel::prepareLegacyRequest() -> DrupalKernel::boot() but that
Chris@0 465 // appears to be calling a different container.
Chris@0 466 $this->container->get('stream_wrapper_manager')->register();
Chris@0 467 }
Chris@0 468
Chris@0 469 /**
Chris@0 470 * Returns the parameters that will be used when Simpletest installs Drupal.
Chris@0 471 *
Chris@0 472 * @see install_drupal()
Chris@0 473 * @see install_state_defaults()
Chris@0 474 *
Chris@0 475 * @return array
Chris@0 476 * Array of parameters for use in install_drupal().
Chris@0 477 */
Chris@0 478 protected function installParameters() {
Chris@0 479 $connection_info = Database::getConnectionInfo();
Chris@0 480 $driver = $connection_info['default']['driver'];
Chris@0 481 $connection_info['default']['prefix'] = $connection_info['default']['prefix']['default'];
Chris@0 482 unset($connection_info['default']['driver']);
Chris@0 483 unset($connection_info['default']['namespace']);
Chris@0 484 unset($connection_info['default']['pdo']);
Chris@0 485 unset($connection_info['default']['init_commands']);
Chris@0 486 // Remove database connection info that is not used by SQLite.
Chris@0 487 if ($driver === 'sqlite') {
Chris@0 488 unset($connection_info['default']['username']);
Chris@0 489 unset($connection_info['default']['password']);
Chris@0 490 unset($connection_info['default']['host']);
Chris@0 491 unset($connection_info['default']['port']);
Chris@0 492 }
Chris@0 493 $parameters = [
Chris@0 494 'interactive' => FALSE,
Chris@0 495 'parameters' => [
Chris@0 496 'profile' => $this->profile,
Chris@0 497 'langcode' => 'en',
Chris@0 498 ],
Chris@0 499 'forms' => [
Chris@0 500 'install_settings_form' => [
Chris@0 501 'driver' => $driver,
Chris@0 502 $driver => $connection_info['default'],
Chris@0 503 ],
Chris@0 504 'install_configure_form' => [
Chris@0 505 'site_name' => 'Drupal',
Chris@0 506 'site_mail' => 'simpletest@example.com',
Chris@0 507 'account' => [
Chris@0 508 'name' => $this->rootUser->name,
Chris@0 509 'mail' => $this->rootUser->getEmail(),
Chris@0 510 'pass' => [
Chris@0 511 'pass1' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
Chris@0 512 'pass2' => isset($this->rootUser->pass_raw) ? $this->rootUser->pass_raw : $this->rootUser->passRaw,
Chris@0 513 ],
Chris@0 514 ],
Chris@0 515 // form_type_checkboxes_value() requires NULL instead of FALSE values
Chris@0 516 // for programmatic form submissions to disable a checkbox.
Chris@0 517 'enable_update_status_module' => NULL,
Chris@0 518 'enable_update_status_emails' => NULL,
Chris@0 519 ],
Chris@0 520 ],
Chris@0 521 ];
Chris@0 522
Chris@0 523 // If we only have one db driver available, we cannot set the driver.
Chris@0 524 include_once DRUPAL_ROOT . '/core/includes/install.inc';
Chris@0 525 if (count($this->getDatabaseTypes()) == 1) {
Chris@0 526 unset($parameters['forms']['install_settings_form']['driver']);
Chris@0 527 }
Chris@0 528 return $parameters;
Chris@0 529 }
Chris@0 530
Chris@0 531 /**
Chris@0 532 * Sets up the base URL based upon the environment variable.
Chris@0 533 *
Chris@0 534 * @throws \Exception
Chris@0 535 * Thrown when no SIMPLETEST_BASE_URL environment variable is provided.
Chris@0 536 */
Chris@0 537 protected function setupBaseUrl() {
Chris@0 538 global $base_url;
Chris@0 539
Chris@0 540 // Get and set the domain of the environment we are running our test
Chris@0 541 // coverage against.
Chris@0 542 $base_url = getenv('SIMPLETEST_BASE_URL');
Chris@0 543 if (!$base_url) {
Chris@0 544 throw new \Exception(
Chris@0 545 'You must provide a SIMPLETEST_BASE_URL environment variable to run some PHPUnit based functional tests.'
Chris@0 546 );
Chris@0 547 }
Chris@0 548
Chris@0 549 // Setup $_SERVER variable.
Chris@0 550 $parsed_url = parse_url($base_url);
Chris@0 551 $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
Chris@0 552 $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
Chris@0 553 $port = isset($parsed_url['port']) ? $parsed_url['port'] : 80;
Chris@0 554
Chris@0 555 $this->baseUrl = $base_url;
Chris@0 556
Chris@0 557 // If the passed URL schema is 'https' then setup the $_SERVER variables
Chris@0 558 // properly so that testing will run under HTTPS.
Chris@0 559 if ($parsed_url['scheme'] === 'https') {
Chris@0 560 $_SERVER['HTTPS'] = 'on';
Chris@0 561 }
Chris@0 562 $_SERVER['HTTP_HOST'] = $host;
Chris@0 563 $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
Chris@0 564 $_SERVER['SERVER_ADDR'] = '127.0.0.1';
Chris@0 565 $_SERVER['SERVER_PORT'] = $port;
Chris@0 566 $_SERVER['SERVER_SOFTWARE'] = NULL;
Chris@0 567 $_SERVER['SERVER_NAME'] = 'localhost';
Chris@0 568 $_SERVER['REQUEST_URI'] = $path . '/';
Chris@0 569 $_SERVER['REQUEST_METHOD'] = 'GET';
Chris@0 570 $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
Chris@0 571 $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
Chris@0 572 $_SERVER['PHP_SELF'] = $path . '/index.php';
Chris@0 573 $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
Chris@0 574 }
Chris@0 575
Chris@0 576 /**
Chris@0 577 * Prepares the current environment for running the test.
Chris@0 578 *
Chris@0 579 * Also sets up new resources for the testing environment, such as the public
Chris@0 580 * filesystem and configuration directories.
Chris@0 581 *
Chris@0 582 * This method is private as it must only be called once by
Chris@0 583 * BrowserTestBase::setUp() (multiple invocations for the same test would have
Chris@0 584 * unpredictable consequences) and it must not be callable or overridable by
Chris@0 585 * test classes.
Chris@0 586 */
Chris@0 587 protected function prepareEnvironment() {
Chris@0 588 // Bootstrap Drupal so we can use Drupal's built in functions.
Chris@0 589 $this->classLoader = require __DIR__ . '/../../../../../autoload.php';
Chris@0 590 $request = Request::createFromGlobals();
Chris@0 591 $kernel = TestRunnerKernel::createFromRequest($request, $this->classLoader);
Chris@0 592 // TestRunnerKernel expects the working directory to be DRUPAL_ROOT.
Chris@0 593 chdir(DRUPAL_ROOT);
Chris@0 594 $kernel->prepareLegacyRequest($request);
Chris@0 595 $this->prepareDatabasePrefix();
Chris@0 596
Chris@0 597 $this->originalSite = $kernel->findSitePath($request);
Chris@0 598
Chris@0 599 // Create test directory ahead of installation so fatal errors and debug
Chris@0 600 // information can be logged during installation process.
Chris@0 601 file_prepare_directory($this->siteDirectory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
Chris@0 602
Chris@0 603 // Prepare filesystem directory paths.
Chris@0 604 $this->publicFilesDirectory = $this->siteDirectory . '/files';
Chris@0 605 $this->privateFilesDirectory = $this->siteDirectory . '/private';
Chris@0 606 $this->tempFilesDirectory = $this->siteDirectory . '/temp';
Chris@0 607 $this->translationFilesDirectory = $this->siteDirectory . '/translations';
Chris@0 608
Chris@0 609 // Ensure the configImporter is refreshed for each test.
Chris@0 610 $this->configImporter = NULL;
Chris@0 611
Chris@0 612 // Unregister all custom stream wrappers of the parent site.
Chris@0 613 $wrappers = \Drupal::service('stream_wrapper_manager')->getWrappers(StreamWrapperInterface::ALL);
Chris@0 614 foreach ($wrappers as $scheme => $info) {
Chris@0 615 stream_wrapper_unregister($scheme);
Chris@0 616 }
Chris@0 617
Chris@0 618 // Reset statics.
Chris@0 619 drupal_static_reset();
Chris@0 620
Chris@0 621 $this->container = NULL;
Chris@0 622
Chris@0 623 // Unset globals.
Chris@0 624 unset($GLOBALS['config_directories']);
Chris@0 625 unset($GLOBALS['config']);
Chris@0 626 unset($GLOBALS['conf']);
Chris@0 627
Chris@0 628 // Log fatal errors.
Chris@0 629 ini_set('log_errors', 1);
Chris@0 630 ini_set('error_log', DRUPAL_ROOT . '/' . $this->siteDirectory . '/error.log');
Chris@0 631
Chris@0 632 // Change the database prefix.
Chris@0 633 $this->changeDatabasePrefix();
Chris@0 634
Chris@0 635 // After preparing the environment and changing the database prefix, we are
Chris@0 636 // in a valid test environment.
Chris@0 637 drupal_valid_test_ua($this->databasePrefix);
Chris@0 638
Chris@0 639 // Reset settings.
Chris@0 640 new Settings([
Chris@0 641 // For performance, simply use the database prefix as hash salt.
Chris@0 642 'hash_salt' => $this->databasePrefix,
Chris@0 643 ]);
Chris@0 644
Chris@0 645 drupal_set_time_limit($this->timeLimit);
Chris@0 646
Chris@0 647 // Save and clean the shutdown callbacks array because it is static cached
Chris@0 648 // and will be changed by the test run. Otherwise it will contain callbacks
Chris@0 649 // from both environments and the testing environment will try to call the
Chris@0 650 // handlers defined by the original one.
Chris@0 651 $callbacks = &drupal_register_shutdown_function();
Chris@0 652 $this->originalShutdownCallbacks = $callbacks;
Chris@0 653 $callbacks = [];
Chris@0 654 }
Chris@0 655
Chris@0 656 /**
Chris@0 657 * Returns all supported database driver installer objects.
Chris@0 658 *
Chris@0 659 * This wraps drupal_get_database_types() for use without a current container.
Chris@0 660 *
Chris@0 661 * @return \Drupal\Core\Database\Install\Tasks[]
Chris@0 662 * An array of available database driver installer objects.
Chris@0 663 */
Chris@0 664 protected function getDatabaseTypes() {
Chris@0 665 if ($this->originalContainer) {
Chris@0 666 \Drupal::setContainer($this->originalContainer);
Chris@0 667 }
Chris@0 668 $database_types = drupal_get_database_types();
Chris@0 669 if ($this->originalContainer) {
Chris@0 670 \Drupal::unsetContainer();
Chris@0 671 }
Chris@0 672 return $database_types;
Chris@0 673 }
Chris@0 674
Chris@0 675 }