Skip to content

Commit

Permalink
added dtt test base
Browse files Browse the repository at this point in the history
  • Loading branch information
rpnykanen committed Sep 14, 2023
1 parent f5fab16 commit 75f121b
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/src/ExistingSite/ExistingSiteTestBase.php
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();
}

}
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();
}

}
92 changes: 92 additions & 0 deletions tests/src/Traits/DefaultConfigurationTrait.php
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();
}

}

0 comments on commit 75f121b

Please sign in to comment.