Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / TermLanguageTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Entity\ConfigurableLanguage;
7
8 /**
9  * Tests the language functionality for the taxonomy terms.
10  *
11  * @group taxonomy
12  */
13 class TermLanguageTest extends TaxonomyTestBase {
14
15   public static $modules = ['language'];
16
17   /**
18    * Vocabulary for testing.
19    *
20    * @var \Drupal\taxonomy\VocabularyInterface
21    */
22   protected $vocabulary;
23
24   protected function setUp() {
25     parent::setUp();
26
27     // Create an administrative user.
28     $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
29
30     // Create a vocabulary to which the terms will be assigned.
31     $this->vocabulary = $this->createVocabulary();
32
33     // Add some custom languages.
34     foreach (['aa', 'bb', 'cc'] as $language_code) {
35       ConfigurableLanguage::create([
36         'id' => $language_code,
37         'label' => $this->randomMachineName(),
38       ])->save();
39     }
40   }
41
42   public function testTermLanguage() {
43     // Configure the vocabulary to not hide the language selector.
44     $edit = [
45       'default_language[language_alterable]' => TRUE,
46     ];
47     $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
48
49     // Add a term.
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.'));
53     // Submit the term.
54     $edit = [
55       'name[0][value]' => $this->randomMachineName(),
56       'langcode[0][value]' => 'aa',
57     ];
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.');
62
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.');
66
67     // Change the language of the term.
68     $edit['langcode[0][value]'] = 'bb';
69     $this->drupalPostForm('taxonomy/term/' . $term->id() . '/edit', $edit, t('Save'));
70
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.');
74   }
75
76   public function testDefaultTermLanguage() {
77     // Configure the vocabulary to not hide the language selector, and make the
78     // default language of the terms fixed.
79     $edit = [
80       'default_language[langcode]' => 'bb',
81       'default_language[language_alterable]' => TRUE,
82     ];
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.');
86
87     // Make the default language of the terms to be the current interface.
88     $edit = [
89       'default_language[langcode]' => 'current_interface',
90       'default_language[language_alterable]' => TRUE,
91     ];
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.");
97
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();
101     $edit = [
102       'default_language[langcode]' => LanguageInterface::LANGCODE_SITE_DEFAULT,
103       'default_language[language_alterable]' => TRUE,
104     ];
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.");
108   }
109
110   /**
111    * Tests that translated terms are displayed correctly on the term overview.
112    */
113   public function testTermTranslatedOnOverviewPage() {
114     // Configure the vocabulary to not hide the language selector.
115     $edit = [
116       'default_language[language_alterable]' => TRUE,
117     ];
118     $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id(), $edit, t('Save'));
119
120     // Add a term.
121     $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add');
122     // Submit the term.
123     $edit = [
124       'name[0][value]' => $this->randomMachineName(),
125       'langcode[0][value]' => 'aa',
126     ];
127     $this->drupalPostForm(NULL, $edit, t('Save'));
128     $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
129     $term = reset($terms);
130
131     // Add a translation for that term.
132     $translated_title = $this->randomMachineName();
133     $term->addTranslation('bb', [
134       'name' => $translated_title,
135     ]);
136     $term->save();
137
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');
141   }
142
143 }