-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\ExistingSite; | ||
|
||
use Drupal\Tests\helfi_api_base\Traits\DefaultConfigurationTrait; | ||
use weitzman\DrupalTestTraits\ExistingSiteBase; | ||
|
||
/** | ||
* Existing site test base. | ||
*/ | ||
abstract class ExistingSiteTestBase extends ExistingSiteBase { | ||
|
||
use DefaultConfigurationTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() : void { | ||
parent::setUp(); | ||
|
||
$this->setupDefaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function tearDown() : void { | ||
parent::tearDown(); | ||
$this->tearDownDefaultConfiguration(); | ||
} | ||
|
||
} |
34 changes: 34 additions & 0 deletions
34
tests/src/ExistingSiteJavascript/ExistingSiteJavascriptTestBase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\ExistingSiteJavascript; | ||
|
||
use Drupal\Tests\helfi_api_base\Traits\DefaultConfigurationTrait; | ||
use weitzman\DrupalTestTraits\ExistingSiteWebDriverTestBase; | ||
|
||
/** | ||
* Existing site test base. | ||
*/ | ||
abstract class ExistingSiteJavascriptTestBase extends ExistingSiteWebDriverTestBase { | ||
|
||
use DefaultConfigurationTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp() : void { | ||
parent::setUp(); | ||
|
||
$this->setupDefaultConfiguration(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function tearDown() : void { | ||
parent::tearDown(); | ||
$this->tearDownDefaultConfiguration(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Drupal\Tests\helfi_api_base\Traits; | ||
|
||
use Drupal\Core\Config\Config; | ||
use Drupal\Core\Language\LanguageInterface; | ||
use Drupal\Core\Url; | ||
|
||
/** | ||
* Basic configuration for each test. | ||
*/ | ||
trait DefaultConfigurationTrait { | ||
|
||
/** | ||
* The default language. | ||
* | ||
* @var string | ||
*/ | ||
protected string $defaultLanguage = ''; | ||
|
||
/** | ||
* Gets the language object for given langcode. | ||
* | ||
* @param string $langcode | ||
* The langcode. | ||
* | ||
* @return \Drupal\Core\Language\LanguageInterface | ||
* The language. | ||
*/ | ||
protected function getLanguage(string $langcode) : LanguageInterface { | ||
return \Drupal::languageManager()->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(); | ||
} | ||
|
||
} |