PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/user/tests/src/Functional/UserAdminLanguageTest.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 204 lines | 105 code | 24 blank | 75 comment | 0 complexity | 0214161bcf33b280e0b4b2005a8d6a26 MD5 | raw file
  1. <?php
  2. namespace Drupal\Tests\user\Functional;
  3. use Drupal\Core\Language\LanguageInterface;
  4. use Drupal\Tests\BrowserTestBase;
  5. /**
  6. * Tests users' ability to change their own administration language.
  7. *
  8. * @group user
  9. */
  10. class UserAdminLanguageTest extends BrowserTestBase {
  11. /**
  12. * A user with permission to access admin pages and administer languages.
  13. *
  14. * @var \Drupal\user\UserInterface
  15. */
  16. protected $adminUser;
  17. /**
  18. * A non-administrator user for this test.
  19. *
  20. * @var \Drupal\user\UserInterface
  21. */
  22. protected $regularUser;
  23. /**
  24. * Modules to enable.
  25. *
  26. * @var array
  27. */
  28. protected static $modules = ['user', 'language', 'language_test'];
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected $defaultTheme = 'stark';
  33. protected function setUp(): void {
  34. parent::setUp();
  35. // User to add and remove language.
  36. $this->adminUser = $this->drupalCreateUser([
  37. 'administer languages',
  38. 'access administration pages',
  39. ]);
  40. // User to check non-admin access.
  41. $this->regularUser = $this->drupalCreateUser();
  42. }
  43. /**
  44. * Tests that admin language is not configurable in single language sites.
  45. */
  46. public function testUserAdminLanguageConfigurationNotAvailableWithOnlyOneLanguage() {
  47. $this->drupalLogin($this->adminUser);
  48. $this->setLanguageNegotiation();
  49. $path = 'user/' . $this->adminUser->id() . '/edit';
  50. $this->drupalGet($path);
  51. // Ensure administration pages language settings widget is not available.
  52. $this->assertSession()->fieldNotExists('edit-preferred-admin-langcode');
  53. }
  54. /**
  55. * Tests that admin language negotiation is configurable only if enabled.
  56. */
  57. public function testUserAdminLanguageConfigurationAvailableWithAdminLanguageNegotiation() {
  58. $this->drupalLogin($this->adminUser);
  59. $this->addCustomLanguage();
  60. $path = 'user/' . $this->adminUser->id() . '/edit';
  61. // Checks with user administration pages language negotiation disabled.
  62. $this->drupalGet($path);
  63. // Ensure administration pages language settings widget is not available.
  64. $this->assertSession()->fieldNotExists('edit-preferred-admin-langcode');
  65. // Checks with user administration pages language negotiation enabled.
  66. $this->setLanguageNegotiation();
  67. $this->drupalGet($path);
  68. // Ensure administration pages language settings widget is available.
  69. $this->assertSession()->fieldExists('edit-preferred-admin-langcode');
  70. }
  71. /**
  72. * Tests that the admin language is configurable only for administrators.
  73. *
  74. * If a user has the permission "access administration pages" or
  75. * "view the administration theme", they should be able to see the setting to
  76. * pick the language they want those pages in.
  77. *
  78. * If a user does not have that permission, it would confusing for them to
  79. * have a setting for pages they cannot access, so they should not be able to
  80. * set a language for those pages.
  81. */
  82. public function testUserAdminLanguageConfigurationAvailableIfAdminLanguageNegotiationIsEnabled() {
  83. $this->drupalLogin($this->adminUser);
  84. // Adds a new language, because with only one language, setting won't show.
  85. $this->addCustomLanguage();
  86. $this->setLanguageNegotiation();
  87. $path = 'user/' . $this->adminUser->id() . '/edit';
  88. $this->drupalGet($path);
  89. // Ensure administration pages language setting is visible for admin.
  90. $this->assertSession()->fieldExists('edit-preferred-admin-langcode');
  91. // Ensure administration pages language setting is visible for editors.
  92. $editor = $this->drupalCreateUser(['view the administration theme']);
  93. $this->drupalLogin($editor);
  94. $path = 'user/' . $editor->id() . '/edit';
  95. $this->drupalGet($path);
  96. $this->assertSession()->fieldExists('edit-preferred-admin-langcode');
  97. // Ensure administration pages language setting is hidden for non-admins.
  98. $this->drupalLogin($this->regularUser);
  99. $path = 'user/' . $this->regularUser->id() . '/edit';
  100. $this->drupalGet($path);
  101. $this->assertSession()->fieldNotExists('edit-preferred-admin-langcode');
  102. }
  103. /**
  104. * Tests the actual language negotiation.
  105. */
  106. public function testActualNegotiation() {
  107. $this->drupalLogin($this->adminUser);
  108. $this->addCustomLanguage();
  109. $this->setLanguageNegotiation();
  110. // Even though we have admin language negotiation, so long as the user has
  111. // no preference set, negotiation will fall back further.
  112. $path = 'user/' . $this->adminUser->id() . '/edit';
  113. $this->drupalGet($path);
  114. $this->assertSession()->pageTextContains('Language negotiation method: language-default');
  115. $this->drupalGet('xx/' . $path);
  116. $this->assertSession()->pageTextContains('Language negotiation method: language-url');
  117. // Set a preferred language code for the user.
  118. $edit = [];
  119. $edit['preferred_admin_langcode'] = 'xx';
  120. $this->drupalGet($path);
  121. $this->submitForm($edit, 'Save');
  122. // Test negotiation with the URL method first. The admin method will only
  123. // be used if the URL method did not match.
  124. $this->drupalGet($path);
  125. $this->assertSession()->pageTextContains('Language negotiation method: language-user-admin');
  126. $this->drupalGet('xx/' . $path);
  127. $this->assertSession()->pageTextContains('Language negotiation method: language-url');
  128. // Test negotiation with the admin language method first. The admin method
  129. // will be used at all times.
  130. $this->setLanguageNegotiation(TRUE);
  131. $this->drupalGet($path);
  132. $this->assertSession()->pageTextContains('Language negotiation method: language-user-admin');
  133. $this->drupalGet('xx/' . $path);
  134. $this->assertSession()->pageTextContains('Language negotiation method: language-user-admin');
  135. // Unset the preferred language code for the user.
  136. $edit = [];
  137. $edit['preferred_admin_langcode'] = '';
  138. $this->drupalGet($path);
  139. $this->submitForm($edit, 'Save');
  140. $this->drupalGet($path);
  141. $this->assertSession()->pageTextContains('Language negotiation method: language-default');
  142. $this->drupalGet('xx/' . $path);
  143. $this->assertSession()->pageTextContains('Language negotiation method: language-url');
  144. }
  145. /**
  146. * Sets the User interface negotiation detection method.
  147. *
  148. * Enables the "Account preference for administration pages" language
  149. * detection method for the User interface language negotiation type.
  150. *
  151. * @param bool $admin_first
  152. * Whether the admin negotiation should be first.
  153. */
  154. public function setLanguageNegotiation($admin_first = FALSE) {
  155. $edit = [
  156. 'language_interface[enabled][language-user-admin]' => TRUE,
  157. 'language_interface[enabled][language-url]' => TRUE,
  158. 'language_interface[weight][language-user-admin]' => ($admin_first ? -12 : -8),
  159. 'language_interface[weight][language-url]' => -10,
  160. ];
  161. $this->drupalGet('admin/config/regional/language/detection');
  162. $this->submitForm($edit, 'Save settings');
  163. }
  164. /**
  165. * Helper method for adding a custom language.
  166. */
  167. public function addCustomLanguage() {
  168. $langcode = 'xx';
  169. // The English name for the language.
  170. $name = $this->randomMachineName(16);
  171. $edit = [
  172. 'predefined_langcode' => 'custom',
  173. 'langcode' => $langcode,
  174. 'label' => $name,
  175. 'direction' => LanguageInterface::DIRECTION_LTR,
  176. ];
  177. $this->drupalGet('admin/config/regional/language/add');
  178. $this->submitForm($edit, 'Add custom language');
  179. }
  180. }