3 namespace Drupal\Tests\taxonomy\Functional;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\language\Entity\ContentLanguageSettings;
9 * Tests the language functionality for vocabularies.
13 class VocabularyLanguageTest extends TaxonomyTestBase {
15 public static $modules = ['language'];
17 protected function setUp() {
20 // Create an administrative user.
21 $this->drupalLogin($this->drupalCreateUser(['administer taxonomy']));
23 // Add some custom languages.
24 ConfigurableLanguage::create([
26 'label' => $this->randomMachineName(),
29 ConfigurableLanguage::create([
31 'label' => $this->randomMachineName(),
36 * Tests language settings for vocabularies.
38 public function testVocabularyLanguage() {
39 $this->drupalGet('admin/structure/taxonomy/add');
41 // Check that we have the language selector available.
42 $this->assertField('edit-langcode', 'The language selector field was found on the page.');
44 // Create the vocabulary.
45 $vid = mb_strtolower($this->randomMachineName());
46 $edit['name'] = $this->randomMachineName();
47 $edit['description'] = $this->randomMachineName();
48 $edit['langcode'] = 'aa';
50 $this->drupalPostForm(NULL, $edit, t('Save'));
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.');
56 // Change the language and save again.
57 $edit['langcode'] = 'bb';
59 $this->drupalPostForm(NULL, $edit, t('Save'));
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.');
67 * Tests term language settings for vocabulary terms are saved and updated.
69 public function testVocabularyDefaultLanguageForTerms() {
70 // Add a new vocabulary and check that the default language settings are for
71 // the terms are saved.
73 'name' => $this->randomMachineName(),
74 'vid' => mb_strtolower($this->randomMachineName()),
75 'default_language[langcode]' => 'bb',
76 'default_language[language_alterable]' => TRUE,
79 $this->drupalPostForm('admin/structure/taxonomy/add', $edit, t('Save'));
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.');
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.');
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.');
94 // Edit the vocabulary and check that the new settings are updated.
96 'default_language[langcode]' => 'aa',
97 'default_language[language_alterable]' => FALSE,
99 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
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.');
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.');
110 // Check that language settings are changed after editing vocabulary.
112 'name' => $this->randomMachineName(),
113 'default_language[langcode]' => 'authors_default',
114 'default_language[language_alterable]' => FALSE,
116 $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vid, $edit, t('Save'));
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.');