Pull merge.
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / VocabularyLanguageTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional;
4
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\language\Entity\ContentLanguageSettings;
7
8 /**
9  * Tests the language functionality for vocabularies.
10  *
11  * @group taxonomy
12  */
13 class VocabularyLanguageTest extends TaxonomyTestBase {
14
15   public static $modules = ['language'];
16
17   protected function setUp() {
18     parent::setUp();
19
20     // Create an administrative user.
21     $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
22
23     // Add some custom languages.
24     ConfigurableLanguage::create([
25       'id' => 'aa',
26       'label' => $this->randomMachineName(),
27     ])->save();
28
29     ConfigurableLanguage::create([
30       'id' => 'bb',
31       'label' => $this->randomMachineName(),
32     ])->save();
33   }
34
35   /**
36    * Tests language settings for vocabularies.
37    */
38   public function testVocabularyLanguage() {
39     $this->drupalGet('admin/structure/taxonomy/add');
40
41     // Check that we have the language selector available.
42     $this->assertField('edit-langcode', 'The language selector field was found on the page.');
43
44     // Create the vocabulary.
45     $vid = mb_strtolower($this->randomMachineName());
46     $edit['name'] = $this->randomMachineName();
47     $edit['description'] = $this->randomMachineName();
48     $edit['langcode'] = 'aa';
49     $edit['vid'] = $vid;
50     $this->drupalPostForm(NULL, $edit, t('Save'));
51
52     // Check the language on the edit page.
53     $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
54     $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The vocabulary language was correctly selected.');
55
56     // Change the language and save again.
57     $edit['langcode'] = 'bb';
58     unset($edit['vid']);
59     $this->drupalPostForm(NULL, $edit, t('Save'));
60
61     // Check again the language on the edit page.
62     $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
63     $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The vocabulary language was correctly selected.');
64   }
65
66   /**
67    * Tests term language settings for vocabulary terms are saved and updated.
68    */
69   public function testVocabularyDefaultLanguageForTerms() {
70     // Add a new vocabulary and check that the default language settings are for
71     // the terms are saved.
72     $edit = [
73       'name' => $this->randomMachineName(),
74       'vid' => mb_strtolower($this->randomMachineName()),
75       'default_language[langcode]' => 'bb',
76       'default_language[language_alterable]' => TRUE,
77     ];
78     $vid = $edit['vid'];
79     $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
80
81     // Check that the vocabulary was actually created.
82     $this->drupalGet('admin/structure/taxonomy/manage/' . $edit['vid']);
83     $this->assertResponse(200, 'The vocabulary has been created.');
84
85     // Check that the language settings were saved.
86     $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $edit['vid']);
87     $this->assertEqual($language_settings->getDefaultLangcode(), 'bb', 'The langcode was saved.');
88     $this->assertTrue($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
89
90     // Check that the correct options are selected in the interface.
91     $this->assertOptionSelected('edit-default-language-langcode', 'bb', 'The correct default language for the terms of this vocabulary is selected.');
92     $this->assertFieldChecked('edit-default-language-language-alterable', 'Show language selection option is checked.');
93
94     // Edit the vocabulary and check that the new settings are updated.
95     $edit = [
96       'default_language[langcode]' => 'aa',
97       'default_language[language_alterable]' => FALSE,
98     ];
99     $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
100
101     // And check again the settings and also the interface.
102     $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
103     $this->assertEqual($language_settings->getDefaultLangcode(), 'aa', 'The langcode was saved.');
104     $this->assertFalse($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
105
106     $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
107     $this->assertOptionSelected('edit-default-language-langcode', 'aa', 'The correct default language for the terms of this vocabulary is selected.');
108     $this->assertNoFieldChecked('edit-default-language-language-alterable', 'Show language selection option is not checked.');
109
110     // Check that language settings are changed after editing vocabulary.
111     $edit = [
112       'name' => $this->randomMachineName(),
113       'default_language[langcode]' => 'authors_default',
114       'default_language[language_alterable]' => FALSE,
115     ];
116     $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
117
118     // Check that we have the new settings.
119     $new_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
120     $this->assertEqual($new_settings->getDefaultLangcode(), 'authors_default', 'The langcode was saved.');
121     $this->assertFalse($new_settings->isLanguageAlterable(), 'The new visibility setting was saved.');
122   }
123
124 }