ToolbarLanguageSwitcherTest.php in Admin Toolbar Language Switcher 8.1.x
File
tests/src/Functional/ToolbarLanguageSwitcherTest.php
View source
<?php
namespace Drupal\Tests\toolbar_language_switcher\Functional;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
class ToolbarLanguageSwitcherTest extends BrowserTestBase {
use UserCreationTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'toolbar_language_switcher',
];
protected $user;
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'access toolbar',
'use toolbar_language_switcher',
]);
}
public function testSingleLanguageProjectSetup() {
$front_page = Url::fromRoute('<front>');
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($front_page);
$assert_session
->pageTextNotContains('Switch to English');
$assert_session
->pageTextNotContains('English');
}
public function testTwoLanguagesProjectSetup() {
$languages = [
'xx' => 'Lolspeak1',
];
$this
->installLanguages($languages);
$front_page = Url::fromRoute('<front>');
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($front_page);
$assert_session
->linkExists('Switch to Lolspeak1');
$this
->clickLink('Switch to Lolspeak1');
$assert_session
->linkExists('Switch to English');
$this
->clickLink('Switch to English');
$assert_session
->linkExists('Switch to Lolspeak1');
}
public function testMultipleLanguagesProjectSetup() {
$languages = [
'xx' => 'Lolspeak1',
'zz' => 'Lolspeak2',
];
$this
->installLanguages($languages);
$front_page = Url::fromRoute('<front>');
$assert_session = $this
->assertSession();
$this
->drupalLogin($this->user);
$this
->drupalGet($front_page);
foreach ($languages as $title) {
$assert_session
->linkExists($title);
}
}
public function testNotRenderedForTheForbiddenUser() {
$languages = [
'xx' => 'Lolspeak1',
'zz' => 'Lolspeak2',
];
$this
->installLanguages($languages);
$front_page = Url::fromRoute('<front>');
$assert_session = $this
->assertSession();
$this
->drupalLogin($this
->drupalCreateUser([
'access toolbar',
]));
$this
->drupalGet($front_page);
foreach ($languages as $title) {
$assert_session
->linkNotExists($title);
}
}
public function installLanguages($languages) {
foreach ($languages as $langcode) {
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->save();
}
}
}