PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/src_8/core/modules/language/tests/src/Functional/LanguageListTest.php

https://bitbucket.org/razum-io/ns-hub
PHP | 220 lines | 139 code | 29 blank | 52 comment | 0 complexity | 28d8545899bebe8d2e4e02303babaf3f MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, MIT, BSD-3-Clause
  1. <?php
  2. namespace Drupal\Tests\language\Functional;
  3. use Drupal\language\Entity\ConfigurableLanguage;
  4. use Drupal\Core\Language\Language;
  5. use Drupal\Core\Language\LanguageInterface;
  6. use Drupal\Tests\BrowserTestBase;
  7. /**
  8. * Adds a new language and tests changing its status and the default language.
  9. *
  10. * @group language
  11. */
  12. class LanguageListTest extends BrowserTestBase {
  13. /**
  14. * Modules to enable.
  15. *
  16. * @var array
  17. */
  18. public static $modules = ['language'];
  19. /**
  20. * Functional tests for adding, editing and deleting languages.
  21. */
  22. public function testLanguageList() {
  23. // User to add and remove language.
  24. $admin_user = $this->drupalCreateUser(['administer languages', 'access administration pages']);
  25. $this->drupalLogin($admin_user);
  26. // Get the weight of the last language.
  27. $languages = \Drupal::service('language_manager')->getLanguages();
  28. $last_language_weight = end($languages)->getWeight();
  29. // Add predefined language.
  30. $edit = [
  31. 'predefined_langcode' => 'fr',
  32. ];
  33. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
  34. $this->assertText('French', 'Language added successfully.');
  35. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
  36. // Get the weight of the last language and check that the weight is one unit
  37. // heavier than the last configurable language.
  38. $this->rebuildContainer();
  39. $languages = \Drupal::service('language_manager')->getLanguages();
  40. $last_language = end($languages);
  41. $this->assertEqual($last_language->getWeight(), $last_language_weight + 1);
  42. $this->assertEqual($last_language->getId(), $edit['predefined_langcode']);
  43. // Add custom language.
  44. $langcode = 'xx';
  45. $name = $this->randomMachineName(16);
  46. $edit = [
  47. 'predefined_langcode' => 'custom',
  48. 'langcode' => $langcode,
  49. 'label' => $name,
  50. 'direction' => Language::DIRECTION_LTR,
  51. ];
  52. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
  53. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
  54. $this->assertRaw('"edit-languages-' . $langcode . '-weight"', 'Language code found.');
  55. $this->assertText(t($name), 'Test language added.');
  56. $language = \Drupal::service('language_manager')->getLanguage($langcode);
  57. $english = \Drupal::service('language_manager')->getLanguage('en');
  58. // Check if we can change the default language.
  59. $path = 'admin/config/regional/language';
  60. $this->drupalGet($path);
  61. $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
  62. // Change the default language.
  63. $edit = [
  64. 'site_default_language' => $langcode,
  65. ];
  66. $this->drupalPostForm(NULL, $edit, t('Save configuration'));
  67. $this->rebuildContainer();
  68. $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
  69. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
  70. // Ensure we can't delete the default language.
  71. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  72. $this->assertResponse(403, 'Failed to delete the default language.');
  73. // Ensure 'Edit' link works.
  74. $this->drupalGet('admin/config/regional/language');
  75. $this->clickLink(t('Edit'));
  76. $this->assertTitle(t('Edit language | Drupal'), 'Page title is "Edit language".');
  77. // Edit a language.
  78. $name = $this->randomMachineName(16);
  79. $edit = [
  80. 'label' => $name,
  81. ];
  82. $this->drupalPostForm('admin/config/regional/language/edit/' . $langcode, $edit, t('Save language'));
  83. $this->assertRaw($name, 'The language has been updated.');
  84. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
  85. // Change back the default language.
  86. $edit = [
  87. 'site_default_language' => 'en',
  88. ];
  89. $this->drupalPostForm($path, $edit, t('Save configuration'));
  90. $this->rebuildContainer();
  91. // Ensure 'delete' link works.
  92. $this->drupalGet('admin/config/regional/language');
  93. $this->clickLink(t('Delete'));
  94. $this->assertText(t('Are you sure you want to delete the language'), '"Delete" link is correct.');
  95. // Delete a language.
  96. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  97. // First test the 'cancel' link.
  98. $this->clickLink(t('Cancel'));
  99. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $english]));
  100. $this->assertRaw($name, 'The language was not deleted.');
  101. // Delete the language for real. This a confirm form, we do not need any
  102. // fields changed.
  103. $this->drupalPostForm('admin/config/regional/language/delete/' . $langcode, [], t('Delete'));
  104. // We need raw here because %language and %langcode will add HTML.
  105. $t_args = ['%language' => $name, '%langcode' => $langcode];
  106. $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The test language has been removed.');
  107. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $english]));
  108. // Verify that language is no longer found.
  109. $this->drupalGet('admin/config/regional/language/delete/' . $langcode);
  110. $this->assertResponse(404, 'Language no longer found.');
  111. // Delete French.
  112. $this->drupalPostForm('admin/config/regional/language/delete/fr', [], t('Delete'));
  113. // Make sure the "language_count" state has been updated correctly.
  114. $this->rebuildContainer();
  115. // We need raw here because %language and %langcode will add HTML.
  116. $t_args = ['%language' => 'French', '%langcode' => 'fr'];
  117. $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The French language has been removed.');
  118. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
  119. // Verify that language is no longer found.
  120. $this->drupalGet('admin/config/regional/language/delete/fr');
  121. $this->assertResponse(404, 'Language no longer found.');
  122. // Make sure the "language_count" state has not changed.
  123. // Ensure we can delete the English language. Right now English is the only
  124. // language so we must add a new language and make it the default before
  125. // deleting English.
  126. $langcode = 'xx';
  127. $name = $this->randomMachineName(16);
  128. $edit = [
  129. 'predefined_langcode' => 'custom',
  130. 'langcode' => $langcode,
  131. 'label' => $name,
  132. 'direction' => Language::DIRECTION_LTR,
  133. ];
  134. $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
  135. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE]));
  136. $this->assertText($name, 'Name found.');
  137. // Check if we can change the default language.
  138. $path = 'admin/config/regional/language';
  139. $this->drupalGet($path);
  140. $this->assertFieldChecked('edit-site-default-language-en', 'English is the default language.');
  141. // Change the default language.
  142. $edit = [
  143. 'site_default_language' => $langcode,
  144. ];
  145. $this->drupalPostForm(NULL, $edit, t('Save configuration'));
  146. $this->rebuildContainer();
  147. $this->assertNoFieldChecked('edit-site-default-language-en', 'Default language updated.');
  148. $this->assertUrl(\Drupal::url('entity.configurable_language.collection', [], ['absolute' => TRUE, 'language' => $language]));
  149. $this->drupalPostForm('admin/config/regional/language/delete/en', [], t('Delete'));
  150. // We need raw here because %language and %langcode will add HTML.
  151. $t_args = ['%language' => 'English', '%langcode' => 'en'];
  152. $this->assertRaw(t('The %language (%langcode) language has been removed.', $t_args), 'The English language has been removed.');
  153. $this->rebuildContainer();
  154. // Ensure we can't delete a locked language.
  155. $this->drupalGet('admin/config/regional/language/delete/und');
  156. $this->assertResponse(403, 'Can not delete locked language');
  157. // Ensure that NL cannot be set default when it's not available.
  158. // First create the NL language.
  159. $edit = [
  160. 'predefined_langcode' => 'nl',
  161. ];
  162. $this->drupalPostForm('admin/config/regional/language/add', $edit, 'Add language');
  163. // Load the form which has now the additional NL language option.
  164. $this->drupalGet('admin/config/regional/language');
  165. // Delete the NL language in the background.
  166. $language_storage = $this->container->get('entity_type.manager')->getStorage('configurable_language');
  167. $language_storage->load('nl')->delete();
  168. $this->drupalPostForm(NULL, ['site_default_language' => 'nl'], 'Save configuration');
  169. $this->assertText(t('Selected default language no longer exists.'));
  170. $this->assertNoFieldChecked('edit-site-default-language-xx', 'The previous default language got deselected.');
  171. }
  172. /**
  173. * Functional tests for the language states (locked or configurable).
  174. */
  175. public function testLanguageStates() {
  176. // Add some languages, and also lock some of them.
  177. ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l1'])->save();
  178. ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l2', 'locked' => TRUE])->save();
  179. ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l3'])->save();
  180. ConfigurableLanguage::create(['label' => $this->randomMachineName(), 'id' => 'l4', 'locked' => TRUE])->save();
  181. $expected_locked_languages = ['l4' => 'l4', 'l2' => 'l2', 'und' => 'und', 'zxx' => 'zxx'];
  182. $expected_all_languages = ['l4' => 'l4', 'l3' => 'l3', 'l2' => 'l2', 'l1' => 'l1', 'en' => 'en', 'und' => 'und', 'zxx' => 'zxx'];
  183. $expected_conf_languages = ['l3' => 'l3', 'l1' => 'l1', 'en' => 'en'];
  184. $locked_languages = $this->container->get('language_manager')->getLanguages(LanguageInterface::STATE_LOCKED);
  185. $this->assertEqual(array_diff_key($expected_locked_languages, $locked_languages), [], 'Locked languages loaded correctly.');
  186. $all_languages = $this->container->get('language_manager')->getLanguages(LanguageInterface::STATE_ALL);
  187. $this->assertEqual(array_diff_key($expected_all_languages, $all_languages), [], 'All languages loaded correctly.');
  188. $conf_languages = $this->container->get('language_manager')->getLanguages();
  189. $this->assertEqual(array_diff_key($expected_conf_languages, $conf_languages), [], 'Configurable languages loaded correctly.');
  190. }
  191. }