3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Entity\ConfigurableLanguage;
9 * Tests the language functionality for the taxonomy terms.
13 class TermLanguageTest extends TaxonomyTestBase {
15 public static $modules = ['language'];
18 * Vocabulary for testing.
20 * @var \Drupal\taxonomy\VocabularyInterface
22 protected $vocabulary;
24 protected function setUp() {
27 // Create an administrative user.
28 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
30 // Create a vocabulary to which the terms will be assigned.
31 $this->vocabulary = $this->createVocabulary();
33 // Add some custom languages.
34 foreach (['aa', 'bb', 'cc'] as $language_code) {
35 ConfigurableLanguage::create([
36 'id' => $language_code,
37 'label' => $this->randomMachineName(),
42 public function testTermLanguage() {
43 // Configure the vocabulary to not hide the language selector.
45 'default_language[language_alterable]' => TRUE,
47 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
50 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
51 // Check that we have the language selector.
52 $this->assertField('edit-langcode-0-value', t('The language selector field was found on the page.'));
55 'name[0][value]' => $this->randomMachineName(),
56 'langcode[0][value]' => 'aa',
58 $this->drupalPostForm(NULL, $edit, t('Save'));
59 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
60 $term = reset($terms);
61 $this->assertEqual($term->language()->getId(), $edit['langcode[0][value]'], 'The term contains the correct langcode.');
63 // Check if on the edit page the language is correct.
64 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
65 $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
67 // Change the language of the term.
68 $edit['langcode[0][value]'] = 'bb';
69 $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
71 // Check again that on the edit page the language is correct.
72 $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
73 $this->assertOptionSelected('edit-langcode-0-value', $edit['langcode[0][value]'], 'The term language was correctly selected.');
76 public function testDefaultTermLanguage() {
77 // Configure the vocabulary to not hide the language selector, and make the
78 // default language of the terms fixed.
80 'default_language[langcode]' => 'bb',
81 'default_language[language_alterable]' => TRUE,
83 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
84 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
85 $this->assertOptionSelected('edit-langcode-0-value', 'bb', 'The expected langcode was selected.');
87 // Make the default language of the terms to be the current interface.
89 'default_language[langcode]' => 'current_interface',
90 'default_language[language_alterable]' => TRUE,
92 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
93 $this->drupalGet('aa/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
94 $this->assertOptionSelected('edit-langcode-0-value', 'aa', "The expected langcode, 'aa', was selected.");
95 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
96 $this->assertOptionSelected('edit-langcode-0-value', 'bb', "The expected langcode, 'bb', was selected.");
98 // Change the default language of the site and check if the default terms
99 // language is still correctly selected.
100 $this->config('system.site')->set('default_langcode', 'cc')->save();
102 'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
103 'default_language[language_alterable]' => TRUE,
105 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
106 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
107 $this->assertOptionSelected('edit-langcode-0-value', 'cc', "The expected langcode, 'cc', was selected.");
111 * Tests that translated terms are displayed correctly on the term overview.
113 public function testTermTranslatedOnOverviewPage() {
114 // Configure the vocabulary to not hide the language selector.
116 'default_language[language_alterable]' => TRUE,
118 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
121 $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
124 'name[0][value]' => $this->randomMachineName(),
125 'langcode[0][value]' => 'aa',
127 $this->drupalPostForm(NULL, $edit, t('Save'));
128 $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
129 $term = reset($terms);
131 // Add a translation for that term.
132 $translated_title = $this->randomMachineName();
133 $term->addTranslation('bb', [
134 'name' => $translated_title,
138 // Overview page in the other language shows the translated term
139 $this->drupalGet('bb/admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/overview');
140 $this->assertPattern('|<a[^>]*>' . $translated_title . '</a>|', 'The term language is correct');