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

3
namespace Drupal\Tests\language\Functional;
4

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

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

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

25 26 27 28 29
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

30 31 32
  /**
   * Functional tests for adding, editing and deleting languages.
   */
33
  public function testLanguageList() {
34 35

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

39 40 41 42
    // Get the weight of the last language.
    $languages = \Drupal::service('language_manager')->getLanguages();
    $last_language_weight = end($languages)->getWeight();

43
    // Add predefined language.
44
    $edit = [
45
      'predefined_langcode' => 'fr',
46
    ];
47
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
48
    $this->assertText('French', 'Language added successfully.');
49
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE])->toString());
50

51 52 53 54 55 56 57 58
    // 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']);

59 60
    // Add custom language.
    $langcode = 'xx';
61
    $name = $this->randomMachineName(16);
62
    $edit = [
63 64
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
65
      'label' => $name,
66
      'direction' => Language::DIRECTION_LTR,
67
    ];
68
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
69
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE])->toString());
70
    $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
71
    $this->assertText(t($name), 'Test language added.');
72

73 74 75
    $language = \Drupal::service('language_manager')->getLanguage($langcode);
    $english = \Drupal::service('language_manager')->getLanguage('en');

76
    // Check if we can change the default language.
77
    $path = 'admin/config/regional/language';
78
    $this->drupalGet($path);
79
    $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
80
    // Change the default language.
81
    $edit = [
82
      'site_default_language' => $langcode,
83
    ];
84
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
85
    $this->rebuildContainer();
86
    $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
87
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language])->toString());
88 89 90

    // Ensure we can't delete the default language.
    $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
91
    $this->assertSession()->statusCodeEquals(403);
92

93
    // Ensure 'Edit' link works.
94
    $this->drupalGet('admin/config/regional/language');
95
    $this->clickLink(t('Edit'));
96
    $this->assertTitle('Edit language | Drupal');
97
    // Edit a language.
98
    $name = $this->randomMachineName(16);
99
    $edit = [
100
      'label' => $name,
101
    ];
102
    $this->drupalPostForm('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
103
    $this->assertRaw($name, 'The language has been updated.');
104
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language])->toString());
105 106

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

133
    // Delete French.
134
    $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
135 136
    // Make sure the "language_count" state has been updated correctly.
    $this->rebuildContainer();
137
    // We need raw here because %language and %langcode will add HTML.
138
    $t_args = ['%language' => 'French', '%langcode' => 'fr'];
139
    $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The French language has been removed.');
140
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE])->toString());
141 142
    // Verify that language is no longer found.
    $this->drupalGet('admin/config/regional/language/delete/fr');
143
    $this->assertSession()->statusCodeEquals(404);
144
    // Make sure the "language_count" state has not changed.
145 146 147 148 149

    // 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';
150
    $name = $this->randomMachineName(16);
151
    $edit = [
152 153
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
154
      'label' => $name,
155
      'direction' => Language::DIRECTION_LTR,
156
    ];
157
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
158
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE])->toString());
159
    $this->assertText($name, 'Name found.');
160 161

    // Check if we can change the default language.
162
    $path = 'admin/config/regional/language';
163
    $this->drupalGet($path);
164
    $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
165
    // Change the default language.
166
    $edit = [
167
      'site_default_language' => $langcode,
168
    ];
169
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
170
    $this->rebuildContainer();
171
    $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
172
    $this->assertUrl(Url::fromRoute('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language])->toString());
173

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

    // Ensure we can't delete a locked language.
    $this->drupalGet('admin/config/regional/language/delete/und');
182
    $this->assertSession()->statusCodeEquals(403);
183 184

    // Ensure that NL cannot be set default when it's not available.
185 186 187 188 189 190 191
    // 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.
192
    $this->drupalGet('admin/config/regional/language');
193 194 195 196 197 198

    // 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');
199 200
    $this->assertText(t('Selected default language no longer exists.'));
    $this->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.');
201
  }
202 203 204 205

  /**
   * Functional tests for the language states (locked or configurable).
   */
206
  public function testLanguageStates() {
207
    // Add some languages, and also lock some of them.
208 209 210 211 212 213 214
    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'];
215

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

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

222
    $conf_languages = $this->container->get('language_manager')->getLanguages();
223
    $this->assertEqual(array_diff_key($expected_conf_languages, $conf_languages), [], 'Configurable languages loaded correctly.');
224
  }
225

226
}