From 75f121b0069b29d460bd3d899e5fff6dd16d93e8 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 14 Sep 2023 11:47:38 +0300 Subject: [PATCH] added dtt test base --- .../src/ExistingSite/ExistingSiteTestBase.php | 34 +++++++ .../ExistingSiteJavascriptTestBase.php | 34 +++++++ .../src/Traits/DefaultConfigurationTrait.php | 92 +++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 tests/src/ExistingSite/ExistingSiteTestBase.php create mode 100644 tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php create mode 100644 tests/src/Traits/DefaultConfigurationTrait.php diff --git a/tests/src/ExistingSite/ExistingSiteTestBase.php b/tests/src/ExistingSite/ExistingSiteTestBase.php new file mode 100644 index 00000000..d6fedd8a --- /dev/null +++ b/tests/src/ExistingSite/ExistingSiteTestBase.php @@ -0,0 +1,34 @@ +setupDefaultConfiguration(); + } + + /** + * {@inheritdoc} + */ + public function tearDown() : void { + parent::tearDown(); + $this->tearDownDefaultConfiguration(); + } + +} diff --git a/tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php b/tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php new file mode 100644 index 00000000..e1a907f9 --- /dev/null +++ b/tests/src/ExistingSiteJavascript/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(); + } + +}