View source
<?php
namespace Drupal\Tests\language_switcher_extended\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\BrowserTestBase;
class LanguageSwitcherExtendedTest extends BrowserTestBase {
public static $modules = [
'locale',
'language',
'node',
'content_translation',
'block',
'language_switcher_extended',
];
protected $defaultTheme = 'stable';
protected $node;
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->rootUser);
$edit = [
'predefined_langcode' => 'de',
];
$this
->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
$edit = [
'language_interface[enabled][language-url]' => '1',
];
$this
->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings'));
$block = $this
->drupalPlaceBlock('language_block:' . LanguageInterface::TYPE_INTERFACE, [
'id' => 'test_language_block',
]);
$this
->createContentType([
'name' => 'Article',
'type' => 'article',
]);
$this
->drupalGet('admin/config/regional/content-language');
$edit = [
'entity_types[node]' => TRUE,
'settings[node][article][translatable]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, t('Save configuration'));
$this->node = $this
->createNode([
'type' => 'article',
'title' => 'Test article (en)',
'langcode' => 'en',
]);
$this->node
->save();
}
public function testLinkAlwaysToFrontpage() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'always_link_to_front',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="en"] a', 'data-drupal-link-system-path', '<front>');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'data-drupal-link-system-path', '<front>');
}
public function testLinkUntranslatedEntityLanguageToFront() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'link_to_front',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="en"] a', 'data-drupal-link-system-path', 'node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'data-drupal-link-system-path', '<front>');
}
public function testHideUntranslatedEntityLanguage() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'hide_link',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="en"] a', 'data-drupal-link-system-path', 'node/1');
$this
->assertSession()
->elementNotExists('css', '#block-test-language-block li[hreflang="de"]');
$this
->assertSession()
->elementsCount('css', '#block-test-language-block li', 1);
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'hide_link',
'hide_single_link' => 1,
'show_langcode' => 0,
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementsCount('css', '#block-test-language-block li', 0);
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'hide_link',
'hide_single_link' => 1,
'hide_single_link_block' => 1,
'show_langcode' => 0,
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementsCount('css', '#block-test-language-block', 0);
}
public function testTranslatedLanguageSwitcherItemIsVisible() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'hide_link',
];
$this
->submitForm($edit, 'Save configuration');
$translation = $this->node
->addTranslation('de', $this->node
->toArray());
$translation
->save();
$this
->drupalGet('node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="en"] a', 'data-drupal-link-system-path', 'node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'data-drupal-link-system-path', 'node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'href', 'de/node/1');
}
public function testShowButNotLinkUntranslatedEntityLanguage() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'process_untranslated',
'untranslated_handler' => 'no_link',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="en"] a', 'data-drupal-link-system-path', 'node/1');
$this
->assertSession()
->elementNotExists('css', '#block-test-language-block li.de a');
$this
->assertSession()
->elementTextContains('css', '#block-test-language-block li.de', 'German');
}
public function testAlwaysHideCurrentLanguage() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'always_link_to_front',
'current_language_mode' => 'hide_link',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('en/node/1');
$this
->assertSession()
->elementNotExists('css', '#block-test-language-block li[hreflang="en"]');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'data-drupal-link-system-path', '<front>');
}
public function testShowCurrentLanguageWithoutLink() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'mode' => 'always_link_to_front',
'current_language_mode' => 'no_link',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('en/node/1');
$this
->assertSession()
->elementExists('css', '#block-test-language-block li.en .is-active');
$this
->assertSession()
->elementNotExists('css', '#block-test-language-block li.en a');
$this
->assertSession()
->elementAttributeContains('css', '#block-test-language-block li[hreflang="de"] a', 'data-drupal-link-system-path', '<front>');
}
public function testShowLangcode() {
$this
->drupalGet('admin/config/regional/language/language-switcher-extended');
$edit = [
'show_langcode' => 1,
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('node/1');
$this
->assertSession()
->elementTextContains('css', '#block-test-language-block li.en', 'en');
$this
->assertSession()
->elementTextNotContains('css', '#block-test-language-block li.en', 'English');
$this
->drupalLogout();
$this
->drupalGet('user');
$this
->assertSession()
->elementTextContains('css', '#block-test-language-block li.en', 'en');
$this
->assertSession()
->elementTextNotContains('css', '#block-test-language-block li.en', 'English');
}
}