Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added dtt test base #132

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions tests/src/Functional/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\Functional;

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 tests/src/FunctionalJavascript/ExistingSiteJavascriptTestBase.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\FunctionalJavascript;

use Drupal\Tests\helfi_api_base\Traits\DefaultConfigurationTrait;
use weitzman\DrupalTestTraits\ExistingSiteSelenium2DriverTestBase;

/**
* Existing site test base.
*/
abstract class ExistingSiteJavascriptTestBase extends ExistingSiteSelenium2DriverTestBase {

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

}
Loading