LanguageListTest.php 10.4 KB
Newer Older
1
2
<?php

3
namespace Drupal\Tests\language\Functional;
4

5
use Drupal\language\Entity\ConfigurableLanguage;
6
use Drupal\Core\Language\Language;
7
use Drupal\Core\Language\LanguageInterface;
8
use Drupal\Tests\BrowserTestBase;
9
10

/**
11
12
13
 * Adds a new language and tests changing its status and the default language.
 *
 * @group language
14
 */
15
class LanguageListTest extends BrowserTestBase {
16
17
18
19
20
21

  /**
   * Modules to enable.
   *
   * @var array
   */
22
  public static $modules = ['language'];
23

24
25
26
  /**
   * Functional tests for adding, editing and deleting languages.
   */
27
  public function testLanguageList() {
28
29

    // User to add and remove language.
30
    $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
31
32
    $this->drupalLogin($admin_user);

33
34
35
36
    // Get the weight of the last language.
    $languages = \Drupal::service('language_manager')->getLanguages();
    $last_language_weight = end($languages)->getWeight();

37
    // Add predefined language.
38
    $edit = [
39
      'predefined_langcode' => 'fr',
40
    ];
41
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
42
    $this->assertText('French', 'Language added successfully.');
43
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
44

45
46
47
48
49
50
51
52
    // Get the weight of the last language and check that the weight is one unit
    // heavier than the last configurable language.
    $this->rebuildContainer();
    $languages = \Drupal::service('language_manager')->getLanguages();
    $last_language = end($languages);
    $this->assertEqual($last_language->getWeight(), $last_language_weight + 1);
    $this->assertEqual($last_language->getId(), $edit['predefined_langcode']);

53
54
    // Add custom language.
    $langcode = 'xx';
55
    $name = $this->randomMachineName(16);
56
    $edit = [
57
58
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
59
      'label' => $name,
60
      'direction' => Language::DIRECTION_LTR,
61
    ];
62
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
63
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
64
    $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
65
    $this->assertText(t($name), 'Test language added.');
66

67
68
69
    $language = \Drupal::service('language_manager')->getLanguage($langcode);
    $english = \Drupal::service('language_manager')->getLanguage('en');

70
    // Check if we can change the default language.
71
    $path = 'admin/config/regional/language';
72
    $this->drupalGet($path);
73
    $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
74
    // Change the default language.
75
    $edit = [
76
      'site_default_language' => $langcode,
77
    ];
78
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
79
    $this->rebuildContainer();
80
81
    $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
82
83
84

    // Ensure we can't delete the default language.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
85
    $this->assertResponse(403, 'Failed to delete the default language.');
86

87
    // Ensure 'Edit' link works.
88
    $this->drupalGet('admin/config/regional/language');
89
    $this->clickLink(t('Edit'));
90
    $this->assertTitle(t('Edit language | Drupal'), 'Page title is "Edit language".');
91
    // Edit a language.
92
    $name = $this->randomMachineName(16);
93
    $edit = [
94
      'label' => $name,
95
    ];
96
    $this->drupalPostForm('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
97
    $this->assertRaw($name, 'The language has been updated.');
98
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
99
100

    // Change back the default language.
101
    $edit = [
102
      'site_default_language' => 'en',
103
    ];
104
    $this->drupalPostForm($path, $edit, t('Save configuration'));
105
    $this->rebuildContainer();
106
107
    // Ensure 'delete' link works.
    $this->drupalGet('admin/config/regional/language');
108
109
    $this->clickLink(t('Delete'));
    $this->assertText(t('Are you sure you want to delete the language'), '"Delete" link is correct.');
110
111
112
113
    // Delete a language.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
    // First test the 'cancel' link.
    $this->clickLink(t('Cancel'));
114
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $english]));
115
    $this->assertRaw($name, 'The language was not deleted.');
116
117
    // Delete the language for real. This a confirm form, we do not need any
    // fields changed.
118
    $this->drupalPostForm('admin/config/regional/language/delete/' . $langcode, [], t('Delete'));
119
    // We need raw here because %language and %langcode will add HTML.
120
    $t_args = ['%language' => $name, '%langcode' => $langcode];
121
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
122
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $english]));
123
124
    // Verify that language is no longer found.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
125
    $this->assertResponse(404, 'Language no longer found.');
126

127
    // Delete French.
128
    $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
129
130
    // Make sure the "language_count" state has been updated correctly.
    $this->rebuildContainer();
131
    // We need raw here because %language and %langcode will add HTML.
132
    $t_args = ['%language' => 'French', '%langcode' => 'fr'];
133
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The French language has been removed.');
134
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
135
136
    // Verify that language is no longer found.
    $this->drupalGet('admin/config/regional/language/delete/fr');
137
    $this->assertResponse(404, 'Language no longer found.');
138
    // Make sure the "language_count" state has not changed.
139
140
141
142
143

    // Ensure we can delete the English language. Right now English is the only
    // language so we must add a new language and make it the default before
    // deleting English.
    $langcode = 'xx';
144
    $name = $this->randomMachineName(16);
145
    $edit = [
146
147
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
148
      'label' => $name,
149
      'direction' => Language::DIRECTION_LTR,
150
    ];
151
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
152
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
153
    $this->assertText($name, 'Name found.');
154
155

    // Check if we can change the default language.
156
    $path = 'admin/config/regional/language';
157
    $this->drupalGet($path);
158
    $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
159
    // Change the default language.
160
    $edit = [
161
      'site_default_language' => $langcode,
162
    ];
163
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
164
    $this->rebuildContainer();
165
166
    $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
167

168
    $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
169
    // We need raw here because %language and %langcode will add HTML.
170
    $t_args = ['%language' => 'English', '%langcode' => 'en'];
171
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The English language has been removed.');
172
    $this->rebuildContainer();
173
174
175
176

    // Ensure we can't delete a locked language.
    $this->drupalGet('admin/config/regional/language/delete/und');
    $this->assertResponse(403, 'Can not delete locked language');
177
178

    // Ensure that NL cannot be set default when it's not available.
179
180
181
182
183
184
185
    // First create the NL language.
    $edit = [
      'predefined_langcode' => 'nl',
    ];
    $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');

    // Load the form which has now the additional NL language option.
186
    $this->drupalGet('admin/config/regional/language');
187
188
189
190
191
192

    // Delete the NL language in the background.
    $language_storage = $this->container->get('entity_type.manager')->getStorage('configurable_language');
    $language_storage->load('nl')->delete();

    $this->drupalPostForm(NULL, ['site_default_language' => 'nl'], 'Save configuration');
193
194
    $this->assertText(t('Selected default language no longer exists.'));
    $this->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.');
195
  }
196
197
198
199

  /**
   * Functional tests for the language states (locked or configurable).
   */
200
  public function testLanguageStates() {
201
    // Add some languages, and also lock some of them.
202
203
204
205
206
207
208
    ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l1'])->save();
    ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l2', 'locked' => TRUE])->save();
    ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l3'])->save();
    ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l4', 'locked' => TRUE])->save();
    $expected_locked_languages = ['l4' => 'l4', 'l2' => 'l2', 'und' => 'und', 'zxx' => 'zxx'];
    $expected_all_languages = ['l4' => 'l4', 'l3' => 'l3', 'l2' => 'l2', 'l1' => 'l1', 'en' => 'en', 'und' => 'und', 'zxx' => 'zxx'];
    $expected_conf_languages = ['l3' => 'l3', 'l1' => 'l1', 'en' => 'en'];
209

210
    $locked_languages = $this->container->get('language_manager')->getLanguages(LanguageInterface::STATE_LOCKED);
211
    $this->assertEqual(array_diff_key($expected_locked_languages, $locked_languages), [], 'Locked languages loaded correctly.');
212

213
    $all_languages = $this->container->get('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
214
    $this->assertEqual(array_diff_key($expected_all_languages, $all_languages), [], 'All languages loaded correctly.');
215

216
    $conf_languages = $this->container->get('language_manager')->getLanguages();
217
    $this->assertEqual(array_diff_key($expected_conf_languages, $conf_languages), [], 'Configurable languages loaded correctly.');
218
  }
219

220
}