From 75f121b0069b29d460bd3d899e5fff6dd16d93e8 Mon Sep 17 00:00:00 2001 From: rpnykanen Date: Thu, 14 Sep 2023 11:47:38 +0300 Subject: [PATCH 1/2] 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(); + } + +} From bc455379e3cd228d1663c1ab3e1e183734881684 Mon Sep 17 00:00:00 2001 From: tuutti Date: Thu, 14 Sep 2023 12:04:41 +0300 Subject: [PATCH 2/2] Moved existing site test bases under regular functional test folder because Drupal does not seem to recognize the namespace otherwise --- .../{ExistingSite => Functional}/ExistingSiteTestBase.php | 2 +- .../ExistingSiteJavascriptTestBase.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename tests/src/{ExistingSite => Functional}/ExistingSiteTestBase.php (91%) rename tests/src/{ExistingSiteJavascript => FunctionalJavascript}/ExistingSiteJavascriptTestBase.php (76%) diff --git a/tests/src/ExistingSite/ExistingSiteTestBase.php b/tests/src/Functional/ExistingSiteTestBase.php similarity index 91% rename from tests/src/ExistingSite/ExistingSiteTestBase.php rename to tests/src/Functional/ExistingSiteTestBase.php index d6fedd8a..31a9a827 100644 --- a/tests/src/ExistingSite/ExistingSiteTestBase.php +++ b/tests/src/Functional/ExistingSiteTestBase.php @@ -2,7 +2,7 @@ declare(strict_types = 1); -namespace Drupal\Tests\helfi_api_base\ExistingSite; +namespace Drupal\Tests\helfi_api_base\Functional; use Drupal\Tests\helfi_api_base\Traits\DefaultConfigurationTrait; use weitzman\DrupalTestTraits\ExistingSiteBase; diff --git a/tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php b/tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php similarity index 76% rename from tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php rename to tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php index e1a907f9..c31fec53 100644 --- a/tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php +++ b/tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.php @@ -2,15 +2,15 @@ declare(strict_types = 1); -namespace Drupal\Tests\helfi_api_base\ExistingSiteJavascript; +namespace Drupal\Tests\helfi_api_base\FunctionalJavascript; use Drupal\Tests\helfi_api_base\Traits\DefaultConfigurationTrait; -use weitzman\DrupalTestTraits\ExistingSiteWebDriverTestBase; +use weitzman\DrupalTestTraits\ExistingSiteSelenium2DriverTestBase; /** * Existing site test base. */ -abstract class ExistingSiteJavascriptTestBase extends ExistingSiteWebDriverTestBase { +abstract class ExistingSiteJavascriptTestBase extends ExistingSiteSelenium2DriverTestBase { use DefaultConfigurationTrait;