function DrupalFlushAllCachesInInstallerTest::prepareEnvironment

Same name and namespace in other branches
  1. 11.x core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php \Drupal\FunctionalTests\Installer\DrupalFlushAllCachesInInstallerTest::prepareEnvironment()
  2. 10 core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php \Drupal\FunctionalTests\Installer\DrupalFlushAllCachesInInstallerTest::prepareEnvironment()
  3. 9 core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php \Drupal\FunctionalTests\Installer\DrupalFlushAllCachesInInstallerTest::prepareEnvironment()

Prepares the current environment for running the test.

Also sets up new resources for the testing environment, such as the public filesystem and configuration directories.

Overrides FunctionalTestSetupTrait::prepareEnvironment

File

core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php, line 33

Class

DrupalFlushAllCachesInInstallerTest
Tests drupal_flush_all_caches() and container rebuilds during an install.

Namespace

Drupal\FunctionalTests\Installer

Code

protected function prepareEnvironment() : void {
  parent::prepareEnvironment();
  $info = [
    'type' => 'profile',
    'core_version_requirement' => '*',
    'name' => 'Cache flush test',
    'install' => [
      'language',
      'cache_flush_test_module',
    ],
  ];
  // File API functions are not available yet.
  $path = $this->siteDirectory . '/profiles/cache_flush_test';
  mkdir($path, 0777, TRUE);
  file_put_contents("{$path}/cache_flush_test.info.yml", Yaml::encode($info));
  $php_code = <<<'EOF'
  <?php
  function cache_flush_test_install() {
    // Note it is bad practice to call this method during hook_install() as it
    // results in an additional expensive container rebuild.
    drupal_flush_all_caches();
    // Ensure services are available after calling drupal_flush_all_caches().
    \Drupal::state()->set('cache_flush_test', \Drupal::hasService('language_negotiator'));
    // Create configuration the way a profile creates default content and
    // configuration. Its langcode comes from the language the site is being
    // installed in.
    \Drupal\Core\Datetime\Entity\DateFormat::create([
      'id' => 'cache_flush_test',
      'label' => 'Cache flush test',
      'pattern' => 'Y-m-d',
    ])->save();
  }
  EOF;
  file_put_contents("{$path}/cache_flush_test.install", $php_code);
  // Create a module which rebuilds the container in its hook_install().
  // Because the profile is small, all of its modules are installed in the
  // same install group as the system module.
  $module_path = "{$path}/modules/cache_flush_test_module";
  mkdir($module_path, 0777, TRUE);
  $module_info = [
    'type' => 'module',
    'core_version_requirement' => '*',
    'name' => 'Cache flush test module',
  ];
  file_put_contents("{$module_path}/cache_flush_test_module.info.yml", Yaml::encode($module_info));
  $module_php_code = <<<'EOF'
  <?php
  function cache_flush_test_module_install(): void {
    // Installing another module changes the module list, so this rebuilds
    // the container while the install group containing the system module is
    // still being processed.
    \Drupal::service('module_installer')->install(['dblog']);
  }
  EOF;
  file_put_contents("{$module_path}/cache_flush_test_module.install", $module_php_code);
}

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