diff --git a/tests/src/Functional/ExistingSiteTestBase.php b/tests/src/Functional/ExistingSiteTestBase.php new file mode 100644 index 00000000..31a9a827 --- /dev/null +++ b/tests/src/Functional/ExistingSiteTestBase.php @@ -0,0 +1,34 @@ +setupDefaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function tearDown() : void { + parent::tearDown(); + $this->tearDownDefaultConfiguration(); + } + +} diff --git a/tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php b/tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php new file mode 100644 index 00000000..c31fec53 --- /dev/null +++ b/tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php @@ -0,0 +1,34 @@ +setupDefaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function tearDown() : void { + parent::tearDown(); + $this->tearDownDefaultConfiguration(); + } + +} diff --git a/tests/src/Traits/DefaultConfigurationTrait.php b/tests/src/Traits/DefaultConfigurationTrait.php new file mode 100644 index 00000000..54375654 --- /dev/null +++ b/tests/src/Traits/DefaultConfigurationTrait.php @@ -0,0 +1,92 @@ +getLanguage($langcode); + } + + /** + * Wrapper for drupalGet() to always set language code. + * + * @param string|\Drupal\Core\Url $url + * The url. + * @param string $langcode + * The langcode. + * @param array $options + * The options. + * @param array $headers + * The headers. + */ + protected function drupalGetWithLanguage(string|Url $url, string $langcode = 'en', array $options = [], array $headers = []) : void { + $options['language'] = $this->getLanguage($langcode); + $this->drupalGet($url, $options, $headers); + } + + /** + * Set up the default configuration. + */ + protected function setupDefaultConfiguration() : void { + $this->defaultLanguage = $this->getDefaultLanguageConfiguration() + ->get('selected_langcode'); + $this->setDefaultLanguage('en'); + } + + /** + * Restores the default configuration. + */ + protected function tearDownDefaultConfiguration() : void { + $this->setDefaultLanguage($this->defaultLanguage); + } + + /** + * Gets the configuration. + * + * @return \Drupal\Core\Config\Config + * The default configuration. + */ + protected function getDefaultLanguageConfiguration() : Config { + return \Drupal::configFactory() + ->getEditable('language.negotiation'); + } + + /** + * Sets the default language. + * + * @param string $langcode + * The langcode to set as default. + */ + protected function setDefaultLanguage(string $langcode) : void { + $this->getDefaultLanguageConfiguration() + ->set('selected_langcode', $langcode) + ->save(); + } + +}