function FunctionalTestSetupTrait::prepareSettings

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::prepareSettings()
  2. 8.9.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::prepareSettings()
  3. 11.x core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php \Drupal\Core\Test\FunctionalTestSetupTrait::prepareSettings()

Prepares site settings and services before installation.

2 calls to FunctionalTestSetupTrait::prepareSettings()
BrowserTestBase::installDrupal in core/tests/Drupal/Tests/BrowserTestBase.php
Installs Drupal into the test site.
TestSiteInstallCommand::installDrupal in core/tests/Drupal/TestSite/Commands/TestSiteInstallCommand.php
Installs Drupal into the test site.
4 methods override FunctionalTestSetupTrait::prepareSettings()
InstallerPerformanceTest::prepareSettings in core/tests/Drupal/FunctionalTests/Installer/InstallerPerformanceTest.php
Prepares site settings and services before installation.
InstallerTranslationNonStandardFilenamesTest::prepareSettings in core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationNonStandardFilenamesTest.php
Prepares site settings and services before installation.
MaintenanceThemeUpdateRegistryTest::prepareSettings in core/modules/system/tests/src/Functional/Theme/MaintenanceThemeUpdateRegistryTest.php
Prepares site settings and services before installation.
UpdatePathTestBase::prepareSettings in core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBase.php
Add settings that are missed since the installer isn't run.

File

core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php, line 75

Class

FunctionalTestSetupTrait
Defines a trait for shared functional test setup functionality.

Namespace

Drupal\Core\Test

Code

protected function prepareSettings() {
  // Prepare installer settings that are not install_drupal() parameters.
  // Copy and prepare an actual settings.php, so as to resemble a regular
  // installation.
  // Not using File API; a potential error must trigger a PHP warning.
  $directory = DRUPAL_ROOT . '/' . $this->siteDirectory;
  copy(DRUPAL_ROOT . '/sites/default/default.settings.php', $directory . '/settings.php');
  // The public file system path is created during installation. Additionally,
  // during tests:
  // - The temporary directory is set and created by install_base_system().
  // - The private file directory is created post install by
  //   FunctionalTestSetupTrait::initConfig().
  // @see system_requirements()
  // @see TestBase::prepareEnvironment()
  // @see install_base_system()
  // @see \Drupal\Core\Test\FunctionalTestSetupTrait::initConfig()
  $settings['settings']['file_public_path'] = (object) [
    'value' => $this->publicFilesDirectory,
    'required' => TRUE,
  ];
  $settings['settings']['file_private_path'] = (object) [
    'value' => $this->privateFilesDirectory,
    'required' => TRUE,
  ];
  $settings['settings']['file_temp_path'] = (object) [
    'value' => $this->tempFilesDirectory,
    'required' => TRUE,
  ];
  // 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['settings']['test_parent_site'] = (object) [
    'value' => $this->originalSite,
    'required' => TRUE,
  ];
  $settings['settings']['apcu_ensure_unique_prefix'] = (object) [
    'value' => $this->apcuEnsureUniquePrefix,
    'required' => TRUE,
  ];
  // Disable fetching of advisories during tests to avoid outbound calls. This
  // cannot be set in ::initConfig() because it would not stop these calls
  // during install. Tests that need to have the security advisories
  // functionality enabled should override this method and unset this
  // variable.
  // @see \Drupal\Tests\system\Functional\SecurityAdvisories\SecurityAdvisoryTest::writeSettings()
  $settings['config']['system.advisories']['enabled'] = (object) [
    'value' => FALSE,
    'required' => TRUE,
  ];
  $this->writeSettings($settings);
  // Allow for test-specific overrides.
  $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');
    // Add the name of the testing class to settings.php and include the
    // testing specific overrides.
    file_put_contents($directory . '/settings.php', "\n\$test_class = '" . static::class . "';\n" . 'include DRUPAL_ROOT . \'/\' . $site_path . \'/settings.testing.php\';' . "\n", FILE_APPEND);
  }
  $settings_services_file = DRUPAL_ROOT . '/' . $this->originalSite . '/testing.services.yml';
  if (!file_exists($settings_services_file)) {
    // Otherwise, use the default services as a starting point for overrides.
    $settings_services_file = DRUPAL_ROOT . '/sites/default/default.services.yml';
  }
  // Put the testing-specific service overrides in place.
  $yaml = new SymfonyYaml();
  $content = file_get_contents($settings_services_file);
  // Disable session garbage collection since test environments do not last
  // long enough to have stale sessions. This prevents random delete queries
  // from running during tests.
  $services = $yaml->parse($content);
  $services['parameters']['session.storage.options']['gc_probability'] = 0;
  // Disable the super user access policy so that we are sure our tests check
  // for the right permissions.
  if (!isset($this->usesSuperUserAccessPolicy)) {
    $test_file_name = (new \ReflectionClass($this))->getFileName();
    // @todo Decide in https://www.drupal.org/project/drupal/issues/3437926
    //   how to remove this fallback behavior.
    $this->usesSuperUserAccessPolicy = !str_starts_with($test_file_name, $this->root . DIRECTORY_SEPARATOR . 'core');
  }
  $services['parameters']['security.enable_super_user'] = $this->usesSuperUserAccessPolicy;
  if ($this->strictConfigSchema) {
    // Add a listener to validate configuration schema on save.
    $test_file_name = (new \ReflectionClass($this))->getFileName();
    // @todo Decide in https://www.drupal.org/project/drupal/issues/3395099 when/how to trigger deprecation errors or even failures for contrib modules.
    $is_core_test = str_starts_with($test_file_name, DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'core');
    $services['services']['testing.config_schema_checker'] = [
      'class' => ConfigSchemaChecker::class,
      'arguments' => [
        '@config.typed',
        $this->getConfigSchemaExclusions(),
        $is_core_test,
      ],
      'tags' => [
        [
          'name' => 'event_subscriber',
        ],
      ],
    ];
  }
  file_put_contents($directory . '/services.yml', $yaml->dump($services));
  // Since Drupal is bootstrapped already, install_begin_request() will not
  // bootstrap again. Hence, we have to reload the newly written custom
  // settings.php manually.
  Settings::initialize(DRUPAL_ROOT, $this->siteDirectory, $this->classLoader);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.