From fd58fbe7ee802e1ec93830bdd28e7e4e0448dd38 Mon Sep 17 00:00:00 2001 From: Lee Rowlands <lee.rowlands@previousnext.com.au> Date: Wed, 28 Jul 2021 16:53:57 +1000 Subject: [PATCH] Issue #3222577 by alexpott, podarok, daffie: ServiceNotFoundException You have requested a non-existent service "language_negotiator" - hook_modules_installed() --- core/lib/Drupal/Core/DrupalKernel.php | 1 + .../Core/Test/FunctionalTestSetupTrait.php | 6 +- .../DrupalFlushAllCachesInInstallerTest.php | 61 +++++++++++++++++++ .../Installer/InstallerTranslationTest.php | 4 +- 4 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php index c8d76030624a..afb27395431d 100644 --- a/core/lib/Drupal/Core/DrupalKernel.php +++ b/core/lib/Drupal/Core/DrupalKernel.php @@ -515,6 +515,7 @@ public function shutdown() { } $this->container->get('stream_wrapper_manager')->unregister(); $this->booted = FALSE; + $this->configStorage = NULL; $this->container = NULL; $this->moduleList = NULL; $this->moduleData = []; diff --git a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php index 5027566db550..829c226950db 100644 --- a/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php +++ b/core/lib/Drupal/Core/Test/FunctionalTestSetupTrait.php @@ -292,7 +292,11 @@ protected function prepareRequestForGenerator($clean_urls = TRUE, $override_serv */ protected function doInstall() { require_once DRUPAL_ROOT . '/core/includes/install.core.inc'; - install_drupal($this->classLoader, $this->installParameters()); + $parameters = $this->installParameters(); + // Simulate a real install which does not start with the any connections set + // in \Drupal\Core\Database\Database::$connections. + Database::removeConnection('default'); + install_drupal($this->classLoader, $parameters); } /** diff --git a/core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php b/core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php new file mode 100644 index 000000000000..e9d0493de8d5 --- /dev/null +++ b/core/tests/Drupal/FunctionalTests/Installer/DrupalFlushAllCachesInInstallerTest.php @@ -0,0 +1,61 @@ +<?php + +namespace Drupal\FunctionalTests\Installer; + +use Drupal\Core\Serialization\Yaml; +use Drupal\Tests\BrowserTestBase; + +/** + * Tests drupal_flush_all_caches() during an install. + * + * @group Installer + */ +class DrupalFlushAllCachesInInstallerTest extends BrowserTestBase { + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * {@inheritdoc} + */ + protected $profile = 'cache_flush_test'; + + /** + * {@inheritdoc} + */ + protected function prepareEnvironment() { + parent::prepareEnvironment(); + $info = [ + 'type' => 'profile', + 'core_version_requirement' => '*', + 'name' => 'Cache flush test', + 'install' => ['language'], + ]; + // 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')); +} +EOF; + + file_put_contents("$path/cache_flush_test.install", $php_code); + } + + /** + * Confirms that the installation succeeded. + */ + public function testInstalled() { + $this->assertTrue(\Drupal::state()->get('cache_flush_test')); + } + +} diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php index f0b91b773915..40af4c935c43 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php @@ -92,9 +92,9 @@ public function testInstaller() { $this->rebuildContainer(); /** @var \Drupal\user\Entity\User $account */ $account = User::load(0); - $this->assertEquals('en', $account->language()->getId(), 'Anonymous user is English.'); + $this->assertEquals('de', $account->language()->getId(), 'Anonymous user is German.'); $account = User::load(1); - $this->assertEquals('en', $account->language()->getId(), 'Administrator user is English.'); + $this->assertEquals('de', $account->language()->getId(), 'Administrator user is German.'); $account = $this->drupalCreateUser(); $this->assertEquals('de', $account->language()->getId(), 'New user is German.'); -- GitLab