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