3 namespace Drupal\Tests\language\Functional;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\language\Entity\ContentLanguageSettings;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests the features of the language configuration element field.
16 class LanguageConfigurationElementTest extends BrowserTestBase {
23 public static $modules = ['taxonomy', 'node', 'language', 'language_elements_test', 'field_ui'];
25 protected function setUp() {
27 $user = $this->drupalCreateUser(['access administration pages', 'administer languages', 'administer content types']);
28 $this->drupalLogin($user);
32 * Tests the language settings have been saved.
34 public function testLanguageConfigurationElement() {
35 $this->drupalGet('language-tests/language_configuration_element');
36 $edit['lang_configuration[langcode]'] = 'current_interface';
37 $edit['lang_configuration[language_alterable]'] = FALSE;
38 $this->drupalPostForm(NULL, $edit, 'Save');
39 $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
41 // Check that the settings have been saved.
42 $this->assertEqual($lang_conf->getDefaultLangcode(), 'current_interface');
43 $this->assertFalse($lang_conf->isLanguageAlterable());
44 $this->drupalGet('language-tests/language_configuration_element');
45 $this->assertOptionSelected('edit-lang-configuration-langcode', 'current_interface');
46 $this->assertNoFieldChecked('edit-lang-configuration-language-alterable');
48 // Reload the page and save again.
49 $this->drupalGet('language-tests/language_configuration_element');
50 $edit['lang_configuration[langcode]'] = 'authors_default';
51 $edit['lang_configuration[language_alterable]'] = TRUE;
52 $this->drupalPostForm(NULL, $edit, 'Save');
53 $lang_conf = ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'some_bundle');
55 // Check that the settings have been saved.
56 $this->assertEqual($lang_conf->getDefaultLangcode(), 'authors_default');
57 $this->assertTrue($lang_conf->isLanguageAlterable());
58 $this->drupalGet('language-tests/language_configuration_element');
59 $this->assertOptionSelected('edit-lang-configuration-langcode', 'authors_default');
60 $this->assertFieldChecked('edit-lang-configuration-language-alterable');
62 // Test if content type settings have been saved.
66 'language_configuration[langcode]' => 'authors_default',
67 'language_configuration[language_alterable]' => TRUE,
69 $this->drupalPostForm('admin/structure/types/add', $edit, 'Save and manage fields');
71 // Make sure the settings are saved when creating the content type.
72 $this->drupalGet('admin/structure/types/manage/page');
73 $this->assertOptionSelected('edit-language-configuration-langcode', 'authors_default');
74 $this->assertFieldChecked('edit-language-configuration-language-alterable');
79 * Tests that the language_get_default_langcode() returns the correct values.
81 public function testDefaultLangcode() {
82 // Add some custom languages.
83 foreach (['aa', 'bb', 'cc'] as $language_code) {
84 ConfigurableLanguage::create([
85 'id' => $language_code,
86 'label' => $this->randomMachineName(),
91 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
92 ->setLanguageAlterable(TRUE)
93 ->setDefaultLangcode('bb')
96 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
97 $this->assertEqual($langcode, 'bb');
100 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
101 ->setLanguageAlterable(TRUE)
102 ->setDefaultLangcode('current_interface')
105 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
106 $language_interface = \Drupal::languageManager()->getCurrentLanguage();
107 $this->assertEqual($langcode, $language_interface->getId());
110 $old_default = \Drupal::languageManager()->getDefaultLanguage();
111 // Ensure the language entity default value is correct.
112 $configurable_language = ConfigurableLanguage::load($old_default->getId());
113 $this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.');
115 $this->config('system.site')->set('default_langcode', 'cc')->save();
116 ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
117 ->setLanguageAlterable(TRUE)
118 ->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT)
120 $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
121 $this->assertEqual($langcode, 'cc');
123 // Ensure the language entity default value is correct.
124 $configurable_language = ConfigurableLanguage::load($old_default->getId());
125 $this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.');
126 $configurable_language = ConfigurableLanguage::load('cc');
128 // \Drupal\language\ConfigurableLanguageInterface::isDefault() method
130 $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.');
132 // Check the default value of a language field when authors preferred option
134 // Create first an user and assign a preferred langcode to him.
135 $some_user = $this->drupalCreateUser();
136 $some_user->preferred_langcode = 'bb';
138 $this->drupalLogin($some_user);
139 ContentLanguageSettings::create([
140 'target_entity_type_id' => 'entity_test',
141 'target_bundle' => 'some_bundle',
142 ])->setLanguageAlterable(TRUE)
143 ->setDefaultLangcode('authors_default')
146 $this->drupalGet('language-tests/language_configuration_element_test');
147 $this->assertOptionSelected('edit-langcode', 'bb');
151 * Tests that the configuration is retained when the node type is updated.
153 public function testNodeTypeUpdate() {
154 // Create the article content type first if the profile used is not the
156 if ($this->profile != 'standard') {
157 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
159 $admin_user = $this->drupalCreateUser(['administer content types']);
160 $this->drupalLogin($admin_user);
162 'language_configuration[langcode]' => 'current_interface',
163 'language_configuration[language_alterable]' => TRUE,
165 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
166 // Check the language default configuration for the articles.
167 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
168 $uuid = $configuration->uuid();
169 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Article content type.');
170 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Article content type.');
171 // Update the article content type by changing the title label.
173 'title_label' => 'Name',
175 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
176 // Check that we still have the settings for the updated node type.
177 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('node', 'article');
178 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Article content type.');
179 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Article content type.');
180 $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Article content type.');
184 * Tests the language settings are deleted on bundle delete.
186 public function testNodeTypeDelete() {
187 // Create the article content type first if the profile used is not the
189 if ($this->profile != 'standard') {
190 $this->drupalCreateContentType([
195 $admin_user = $this->drupalCreateUser(['administer content types']);
196 $this->drupalLogin($admin_user);
198 // Create language configuration for the articles.
200 'language_configuration[langcode]' => 'authors_default',
201 'language_configuration[language_alterable]' => TRUE,
203 $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
205 // Check the language default configuration for articles is present.
206 $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article');
207 $this->assertTrue($configuration, 'The language configuration is present.');
209 // Delete 'article' bundle.
210 $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete'));
212 // Check that the language configuration has been deleted.
213 \Drupal::entityManager()->getStorage('language_content_settings')->resetCache();
214 $configuration = \Drupal::entityManager()->getStorage('language_content_settings')->load('node.article');
215 $this->assertFalse($configuration, 'The language configuration was deleted after bundle was deleted.');
219 * Tests that the configuration is retained when a vocabulary is updated.
221 public function testTaxonomyVocabularyUpdate() {
222 $vocabulary = Vocabulary::create([
228 $admin_user = $this->drupalCreateUser(['administer taxonomy']);
229 $this->drupalLogin($admin_user);
231 'default_language[langcode]' => 'current_interface',
232 'default_language[language_alterable]' => TRUE,
234 $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save'));
236 // Check the language default configuration.
237 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
238 $uuid = $configuration->uuid();
239 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been saved on the Country vocabulary.');
240 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been saved on the Country vocabulary.');
241 // Update the vocabulary.
245 $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save'));
246 // Check that we still have the settings for the updated vocabulary.
247 $configuration = ContentLanguageSettings::loadByEntityTypeBundle('taxonomy_term', 'country');
248 $this->assertEqual($configuration->getDefaultLangcode(), 'current_interface', 'The default language configuration has been kept on the updated Country vocabulary.');
249 $this->assertTrue($configuration->isLanguageAlterable(), 'The alterable language configuration has been kept on the updated Country vocabulary.');
250 $this->assertEqual($configuration->uuid(), $uuid, 'The language configuration uuid has been kept on the updated Country vocabulary.');