Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / language / tests / src / Functional / LanguageConfigurationElementTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Functional;
4
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;
10
11 /**
12  * Tests the features of the language configuration element field.
13  *
14  * @group language
15  */
16 class LanguageConfigurationElementTest extends BrowserTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['taxonomy', 'node', 'language', 'language_elements_test', 'field_ui'];
24
25   protected function setUp() {
26     parent::setUp();
27     $user = $this->drupalCreateUser(['access administration pages', 'administer languages', 'administer content types']);
28     $this->drupalLogin($user);
29   }
30
31   /**
32    * Tests the language settings have been saved.
33    */
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');
40
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');
47
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');
54
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');
61
62     // Test if content type settings have been saved.
63     $edit = [
64       'name' => 'Page',
65       'type' => 'page',
66       'language_configuration[langcode]' => 'authors_default',
67       'language_configuration[language_alterable]' => TRUE,
68     ];
69     $this->drupalPostForm('admin/structure/types/add', $edit, 'Save and manage fields');
70
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');
75
76   }
77
78   /**
79    * Tests that the language_get_default_langcode() returns the correct values.
80    */
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(),
87       ])->save();
88     }
89
90     // Fixed language.
91     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
92       ->setLanguageAlterable(TRUE)
93       ->setDefaultLangcode('bb')
94       ->save();
95
96     $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
97     $this->assertEqual($langcode, 'bb');
98
99     // Current interface.
100     ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
101       ->setLanguageAlterable(TRUE)
102       ->setDefaultLangcode('current_interface')
103       ->save();
104
105     $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
106     $language_interface = \Drupal::languageManager()->getCurrentLanguage();
107     $this->assertEqual($langcode, $language_interface->getId());
108
109     // Site's default.
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.');
114
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)
119       ->save();
120     $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
121     $this->assertEqual($langcode, 'cc');
122
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');
127     // Check calling the
128     // \Drupal\language\ConfigurableLanguageInterface::isDefault() method
129     // directly.
130     $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.');
131
132     // Check the default value of a language field when authors preferred option
133     // is selected.
134     // Create first an user and assign a preferred langcode to him.
135     $some_user = $this->drupalCreateUser();
136     $some_user->preferred_langcode = 'bb';
137     $some_user->save();
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')
144       ->save();
145
146     $this->drupalGet('language-tests/language_configuration_element_test');
147     $this->assertOptionSelected('edit-langcode', 'bb');
148   }
149
150   /**
151    * Tests that the configuration is retained when the node type is updated.
152    */
153   public function testNodeTypeUpdate() {
154     // Create the article content type first if the profile used is not the
155     // standard one.
156     if ($this->profile != 'standard') {
157       $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
158     }
159     $admin_user = $this->drupalCreateUser(['administer content types']);
160     $this->drupalLogin($admin_user);
161     $edit = [
162       'language_configuration[langcode]' => 'current_interface',
163       'language_configuration[language_alterable]' => TRUE,
164     ];
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.
172     $edit = [
173       'title_label' => 'Name',
174     ];
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.');
181   }
182
183   /**
184    * Tests the language settings are deleted on bundle delete.
185    */
186   public function testNodeTypeDelete() {
187     // Create the article content type first if the profile used is not the
188     // standard one.
189     if ($this->profile != 'standard') {
190       $this->drupalCreateContentType([
191         'type' => 'article',
192         'name' => 'Article',
193       ]);
194     }
195     $admin_user = $this->drupalCreateUser(['administer content types']);
196     $this->drupalLogin($admin_user);
197
198     // Create language configuration for the articles.
199     $edit = [
200       'language_configuration[langcode]' => 'authors_default',
201       'language_configuration[language_alterable]' => TRUE,
202     ];
203     $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
204
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.');
208
209     // Delete 'article' bundle.
210     $this->drupalPostForm('admin/structure/types/manage/article/delete', [], t('Delete'));
211
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.');
216   }
217
218   /**
219    * Tests that the configuration is retained when a vocabulary is updated.
220    */
221   public function testTaxonomyVocabularyUpdate() {
222     $vocabulary = Vocabulary::create([
223       'name' => 'Country',
224       'vid' => 'country',
225     ]);
226     $vocabulary->save();
227
228     $admin_user = $this->drupalCreateUser(['administer taxonomy']);
229     $this->drupalLogin($admin_user);
230     $edit = [
231       'default_language[langcode]' => 'current_interface',
232       'default_language[language_alterable]' => TRUE,
233     ];
234     $this->drupalPostForm('admin/structure/taxonomy/manage/country', $edit, t('Save'));
235
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.
242     $edit = [
243       'name' => 'Nation',
244     ];
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.');
251   }
252
253 }