3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\language\Entity\ContentLanguageSettings;
10 * Tests the language functionality for vocabularies.
14 class VocabularyLanguageTest extends TaxonomyTestBase {
16 public static $modules = ['language'];
18 protected function setUp() {
21 // Create an administrative user.
22 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
24 // Add some custom languages.
25 ConfigurableLanguage::create([
27 'label' => $this->randomMachineName(),
30 ConfigurableLanguage::create([
32 'label' => $this->randomMachineName(),
37 * Tests language settings for vocabularies.
39 public function testVocabularyLanguage() {
40 $this->drupalGet('admin/structure/taxonomy/add');
42 // Check that we have the language selector available.
43 $this->assertField('edit-langcode', 'The language selector field was found on the page.');
45 // Create the vocabulary.
46 $vid = Unicode::strtolower($this->randomMachineName());
47 $edit['name'] = $this->randomMachineName();
48 $edit['description'] = $this->randomMachineName();
49 $edit['langcode'] = 'aa';
51 $this->drupalPostForm(NULL, $edit, t('Save'));
53 // Check the language on the edit page.
54 $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
55 $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The vocabulary language was correctly selected.');
57 // Change the language and save again.
58 $edit['langcode'] = 'bb';
60 $this->drupalPostForm(NULL, $edit, t('Save'));
62 // Check again the language on the edit page.
63 $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
64 $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The vocabulary language was correctly selected.');
68 * Tests term language settings for vocabulary terms are saved and updated.
70 public function testVocabularyDefaultLanguageForTerms() {
71 // Add a new vocabulary and check that the default language settings are for
72 // the terms are saved.
74 'name' => $this->randomMachineName(),
75 'vid' => Unicode::strtolower($this->randomMachineName()),
76 'default_language[langcode]' => 'bb',
77 'default_language[language_alterable]' => TRUE,
80 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
82 // Check that the vocabulary was actually created.
83 $this->drupalGet('admin/structure/taxonomy/manage/' . $edit['vid']);
84 $this->assertResponse(200, 'The vocabulary has been created.');
86 // Check that the language settings were saved.
87 $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $edit['vid']);
88 $this->assertEqual($language_settings->getDefaultLangcode(), 'bb', 'The langcode was saved.');
89 $this->assertTrue($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
91 // Check that the correct options are selected in the interface.
92 $this->assertOptionSelected('edit-default-language-langcode', 'bb', 'The correct default language for the terms of this vocabulary is selected.');
93 $this->assertFieldChecked('edit-default-language-language-alterable', 'Show language selection option is checked.');
95 // Edit the vocabulary and check that the new settings are updated.
97 'default_language[langcode]' => 'aa',
98 'default_language[language_alterable]' => FALSE,
100 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
102 // And check again the settings and also the interface.
103 $language_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
104 $this->assertEqual($language_settings->getDefaultLangcode(), 'aa', 'The langcode was saved.');
105 $this->assertFalse($language_settings->isLanguageAlterable(), 'The visibility setting was saved.');
107 $this->drupalGet('admin/structure/taxonomy/manage/' . $vid);
108 $this->assertOptionSelected('edit-default-language-langcode', 'aa', 'The correct default language for the terms of this vocabulary is selected.');
109 $this->assertNoFieldChecked('edit-default-language-language-alterable', 'Show language selection option is not checked.');
111 // Check that language settings are changed after editing vocabulary.
113 'name' => $this->randomMachineName(),
114 'default_language[langcode]' => 'authors_default',
115 'default_language[language_alterable]' => FALSE,
117 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
119 // Check that we have the new settings.
120 $new_settings = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', $vid);
121 $this->assertEqual($new_settings->getDefaultLangcode(), 'authors_default', 'The langcode was saved.');
122 $this->assertFalse($new_settings->isLanguageAlterable(), 'The new visibility setting was saved.');