Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/user/src/Tests/UserPasswordResetTest.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Tests; | |
4 | |
5 use Drupal\Component\Render\FormattableMarkup; | |
6 use Drupal\Core\Url; | |
7 use Drupal\system\Tests\Cache\PageCacheTagsTestBase; | |
8 use Drupal\user\Entity\User; | |
9 | |
10 /** | |
11 * Ensure that password reset methods work as expected. | |
12 * | |
13 * @group user | |
14 */ | |
15 class UserPasswordResetTest extends PageCacheTagsTestBase { | |
16 | |
17 /** | |
18 * The profile to install as a basis for testing. | |
19 * | |
20 * This test uses the standard profile to test the password reset in | |
21 * combination with an ajax request provided by the user picture configuration | |
22 * in the standard profile. | |
23 * | |
24 * @var string | |
25 */ | |
26 protected $profile = 'standard'; | |
27 | |
28 /** | |
29 * The user object to test password resetting. | |
30 * | |
31 * @var \Drupal\user\UserInterface | |
32 */ | |
33 protected $account; | |
34 | |
35 /** | |
36 * Modules to enable. | |
37 * | |
38 * @var array | |
39 */ | |
40 public static $modules = ['block']; | |
41 | |
42 /** | |
43 * {@inheritdoc} | |
44 */ | |
45 protected function setUp() { | |
46 parent::setUp(); | |
47 | |
48 $this->drupalPlaceBlock('system_menu_block:account'); | |
49 | |
50 // Create a user. | |
51 $account = $this->drupalCreateUser(); | |
52 | |
53 // Activate user by logging in. | |
54 $this->drupalLogin($account); | |
55 | |
56 $this->account = User::load($account->id()); | |
57 $this->account->pass_raw = $account->pass_raw; | |
58 $this->drupalLogout(); | |
59 | |
60 // Set the last login time that is used to generate the one-time link so | |
61 // that it is definitely over a second ago. | |
62 $account->login = REQUEST_TIME - mt_rand(10, 100000); | |
63 db_update('users_field_data') | |
64 ->fields(['login' => $account->getLastLoginTime()]) | |
65 ->condition('uid', $account->id()) | |
66 ->execute(); | |
67 } | |
68 | |
69 /** | |
70 * Tests password reset functionality. | |
71 */ | |
72 public function testUserPasswordReset() { | |
73 // Verify that accessing the password reset form without having the session | |
74 // variables set results in an access denied message. | |
75 $this->drupalGet(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()])); | |
76 $this->assertResponse(403); | |
77 | |
78 // Try to reset the password for an invalid account. | |
79 $this->drupalGet('user/password'); | |
80 | |
81 $edit = ['name' => $this->randomMachineName(32)]; | |
82 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
83 | |
84 $this->assertText(t('@name is not recognized as a username or an email address.', ['@name' => $edit['name']]), 'Validation error message shown when trying to request password for invalid account.'); | |
85 $this->assertEqual(count($this->drupalGetMails(['id' => 'user_password_reset'])), 0, 'No email was sent when requesting a password for an invalid account.'); | |
86 | |
87 // Reset the password by username via the password reset page. | |
88 $edit['name'] = $this->account->getUsername(); | |
89 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
90 | |
91 // Verify that the user was sent an email. | |
92 $this->assertMail('to', $this->account->getEmail(), 'Password email sent to user.'); | |
93 $subject = t('Replacement login information for @username at @site', ['@username' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]); | |
94 $this->assertMail('subject', $subject, 'Password reset email subject is correct.'); | |
95 | |
96 $resetURL = $this->getResetURL(); | |
97 $this->drupalGet($resetURL); | |
98 // Ensure that the current url does not contain the hash and timestamp. | |
99 $this->assertUrl(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()])); | |
100 | |
101 $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache')); | |
102 | |
103 // Ensure the password reset URL is not cached. | |
104 $this->drupalGet($resetURL); | |
105 $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache')); | |
106 | |
107 // Check the one-time login page. | |
108 $this->assertText($this->account->getUsername(), 'One-time login page contains the correct username.'); | |
109 $this->assertText(t('This login can be used only once.'), 'Found warning about one-time login.'); | |
110 $this->assertTitle(t('Reset password | Drupal'), 'Page title is "Reset password".'); | |
111 | |
112 // Check successful login. | |
113 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
114 $this->assertLink(t('Log out')); | |
115 $this->assertTitle(t('@name | @site', ['@name' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]), 'Logged in using password reset link.'); | |
116 | |
117 // Make sure the ajax request from uploading a user picture does not | |
118 // invalidate the reset token. | |
119 $image = current($this->drupalGetTestFiles('image')); | |
120 $edit = [ | |
121 'files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri), | |
122 ]; | |
123 $this->drupalPostAjaxForm(NULL, $edit, 'user_picture_0_upload_button'); | |
124 | |
125 // Change the forgotten password. | |
126 $password = user_password(); | |
127 $edit = ['pass[pass1]' => $password, 'pass[pass2]' => $password]; | |
128 $this->drupalPostForm(NULL, $edit, t('Save')); | |
129 $this->assertText(t('The changes have been saved.'), 'Forgotten password changed.'); | |
130 | |
131 // Verify that the password reset session has been destroyed. | |
132 $this->drupalPostForm(NULL, $edit, t('Save')); | |
133 $this->assertText(t("Your current password is missing or incorrect; it's required to change the Password."), 'Password needed to make profile changes.'); | |
134 | |
135 // Log out, and try to log in again using the same one-time link. | |
136 $this->drupalLogout(); | |
137 $this->drupalGet($resetURL); | |
138 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
139 $this->assertText(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'One-time link is no longer valid.'); | |
140 | |
141 // Request a new password again, this time using the email address. | |
142 $this->drupalGet('user/password'); | |
143 // Count email messages before to compare with after. | |
144 $before = count($this->drupalGetMails(['id' => 'user_password_reset'])); | |
145 $edit = ['name' => $this->account->getEmail()]; | |
146 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
147 $this->assertTrue(count($this->drupalGetMails(['id' => 'user_password_reset'])) === $before + 1, 'Email sent when requesting password reset using email address.'); | |
148 | |
149 // Visit the user edit page without pass-reset-token and make sure it does | |
150 // not cause an error. | |
151 $resetURL = $this->getResetURL(); | |
152 $this->drupalGet($resetURL); | |
153 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
154 $this->drupalGet('user/' . $this->account->id() . '/edit'); | |
155 $this->assertNoText('Expected user_string to be a string, NULL given'); | |
156 $this->drupalLogout(); | |
157 | |
158 // Create a password reset link as if the request time was 60 seconds older than the allowed limit. | |
159 $timeout = $this->config('user.settings')->get('password_reset_timeout'); | |
160 $bogus_timestamp = REQUEST_TIME - $timeout - 60; | |
161 $_uid = $this->account->id(); | |
162 $this->drupalGet("user/reset/$_uid/$bogus_timestamp/" . user_pass_rehash($this->account, $bogus_timestamp)); | |
163 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
164 $this->assertText(t('You have tried to use a one-time login link that has expired. Please request a new one using the form below.'), 'Expired password reset request rejected.'); | |
165 | |
166 // Create a user, block the account, and verify that a login link is denied. | |
167 $timestamp = REQUEST_TIME - 1; | |
168 $blocked_account = $this->drupalCreateUser()->block(); | |
169 $blocked_account->save(); | |
170 $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp)); | |
171 $this->assertResponse(403); | |
172 | |
173 // Verify a blocked user can not request a new password. | |
174 $this->drupalGet('user/password'); | |
175 // Count email messages before to compare with after. | |
176 $before = count($this->drupalGetMails(['id' => 'user_password_reset'])); | |
177 $edit = ['name' => $blocked_account->getUsername()]; | |
178 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
179 $this->assertRaw(t('%name is blocked or has not been activated yet.', ['%name' => $blocked_account->getUsername()]), 'Notified user blocked accounts can not request a new password'); | |
180 $this->assertTrue(count($this->drupalGetMails(['id' => 'user_password_reset'])) === $before, 'No email was sent when requesting password reset for a blocked account'); | |
181 | |
182 // Verify a password reset link is invalidated when the user's email address changes. | |
183 $this->drupalGet('user/password'); | |
184 $edit = ['name' => $this->account->getUsername()]; | |
185 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
186 $old_email_reset_link = $this->getResetURL(); | |
187 $this->account->setEmail("1" . $this->account->getEmail()); | |
188 $this->account->save(); | |
189 $this->drupalGet($old_email_reset_link); | |
190 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
191 $this->assertText(t('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'), 'One-time link is no longer valid.'); | |
192 | |
193 // Verify a password reset link will automatically log a user when /login is | |
194 // appended. | |
195 $this->drupalGet('user/password'); | |
196 $edit = ['name' => $this->account->getUsername()]; | |
197 $this->drupalPostForm(NULL, $edit, t('Submit')); | |
198 $reset_url = $this->getResetURL(); | |
199 $this->drupalGet($reset_url . '/login'); | |
200 $this->assertLink(t('Log out')); | |
201 $this->assertTitle(t('@name | @site', ['@name' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]), 'Logged in using password reset link.'); | |
202 | |
203 // Ensure blocked and deleted accounts can't access the user.reset.login | |
204 // route. | |
205 $this->drupalLogout(); | |
206 $timestamp = REQUEST_TIME - 1; | |
207 $blocked_account = $this->drupalCreateUser()->block(); | |
208 $blocked_account->save(); | |
209 $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp) . '/login'); | |
210 $this->assertResponse(403); | |
211 | |
212 $blocked_account->delete(); | |
213 $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp) . '/login'); | |
214 $this->assertResponse(403); | |
215 } | |
216 | |
217 /** | |
218 * Retrieves password reset email and extracts the login link. | |
219 */ | |
220 public function getResetURL() { | |
221 // Assume the most recent email. | |
222 $_emails = $this->drupalGetMails(); | |
223 $email = end($_emails); | |
224 $urls = []; | |
225 preg_match('#.+user/reset/.+#', $email['body'], $urls); | |
226 | |
227 return $urls[0]; | |
228 } | |
229 | |
230 /** | |
231 * Test user password reset while logged in. | |
232 */ | |
233 public function testUserPasswordResetLoggedIn() { | |
234 $another_account = $this->drupalCreateUser(); | |
235 $this->drupalLogin($another_account); | |
236 $this->drupalGet('user/password'); | |
237 $this->drupalPostForm(NULL, NULL, t('Submit')); | |
238 | |
239 // Click the reset URL while logged and change our password. | |
240 $resetURL = $this->getResetURL(); | |
241 // Log in as a different user. | |
242 $this->drupalLogin($this->account); | |
243 $this->drupalGet($resetURL); | |
244 $this->assertRaw(new FormattableMarkup( | |
245 'Another user (%other_user) is already logged into the site on this computer, but you tried to use a one-time link for user %resetting_user. Please <a href=":logout">log out</a> and try using the link again.', | |
246 ['%other_user' => $this->account->getUsername(), '%resetting_user' => $another_account->getUsername(), ':logout' => Url::fromRoute('user.logout')->toString()] | |
247 )); | |
248 | |
249 $another_account->delete(); | |
250 $this->drupalGet($resetURL); | |
251 $this->assertText('The one-time login link you clicked is invalid.'); | |
252 | |
253 // Log in. | |
254 $this->drupalLogin($this->account); | |
255 | |
256 // Reset the password by username via the password reset page. | |
257 $this->drupalGet('user/password'); | |
258 $this->drupalPostForm(NULL, NULL, t('Submit')); | |
259 | |
260 // Click the reset URL while logged and change our password. | |
261 $resetURL = $this->getResetURL(); | |
262 $this->drupalGet($resetURL); | |
263 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
264 | |
265 // Change the password. | |
266 $password = user_password(); | |
267 $edit = ['pass[pass1]' => $password, 'pass[pass2]' => $password]; | |
268 $this->drupalPostForm(NULL, $edit, t('Save')); | |
269 $this->assertText(t('The changes have been saved.'), 'Password changed.'); | |
270 | |
271 // Logged in users should not be able to access the user.reset.login or the | |
272 // user.reset.form routes. | |
273 $timestamp = REQUEST_TIME - 1; | |
274 $this->drupalGet("user/reset/" . $this->account->id() . "/$timestamp/" . user_pass_rehash($this->account, $timestamp) . '/login'); | |
275 $this->assertResponse(403); | |
276 $this->drupalGet("user/reset/" . $this->account->id()); | |
277 $this->assertResponse(403); | |
278 } | |
279 | |
280 /** | |
281 * Prefill the text box on incorrect login via link to password reset page. | |
282 */ | |
283 public function testUserResetPasswordTextboxFilled() { | |
284 $this->drupalGet('user/login'); | |
285 $edit = [ | |
286 'name' => $this->randomMachineName(), | |
287 'pass' => $this->randomMachineName(), | |
288 ]; | |
289 $this->drupalPostForm('user/login', $edit, t('Log in')); | |
290 $this->assertRaw(t('Unrecognized username or password. <a href=":password">Forgot your password?</a>', | |
291 [':password' => \Drupal::url('user.pass', [], ['query' => ['name' => $edit['name']]])])); | |
292 unset($edit['pass']); | |
293 $this->drupalGet('user/password', ['query' => ['name' => $edit['name']]]); | |
294 $this->assertFieldByName('name', $edit['name'], 'User name found.'); | |
295 // Ensure the name field value is not cached. | |
296 $this->drupalGet('user/password'); | |
297 $this->assertNoFieldByName('name', $edit['name'], 'User name not found.'); | |
298 } | |
299 | |
300 /** | |
301 * Make sure that users cannot forge password reset URLs of other users. | |
302 */ | |
303 public function testResetImpersonation() { | |
304 // Create two identical user accounts except for the user name. They must | |
305 // have the same empty password, so we can't use $this->drupalCreateUser(). | |
306 $edit = []; | |
307 $edit['name'] = $this->randomMachineName(); | |
308 $edit['mail'] = $edit['name'] . '@example.com'; | |
309 $edit['status'] = 1; | |
310 $user1 = User::create($edit); | |
311 $user1->save(); | |
312 | |
313 $edit['name'] = $this->randomMachineName(); | |
314 $user2 = User::create($edit); | |
315 $user2->save(); | |
316 | |
317 // Unique password hashes are automatically generated, the only way to | |
318 // change that is to update it directly in the database. | |
319 db_update('users_field_data') | |
320 ->fields(['pass' => NULL]) | |
321 ->condition('uid', [$user1->id(), $user2->id()], 'IN') | |
322 ->execute(); | |
323 \Drupal::entityManager()->getStorage('user')->resetCache(); | |
324 $user1 = User::load($user1->id()); | |
325 $user2 = User::load($user2->id()); | |
326 | |
327 $this->assertEqual($user1->getPassword(), $user2->getPassword(), 'Both users have the same password hash.'); | |
328 | |
329 // The password reset URL must not be valid for the second user when only | |
330 // the user ID is changed in the URL. | |
331 $reset_url = user_pass_reset_url($user1); | |
332 $attack_reset_url = str_replace("user/reset/{$user1->id()}", "user/reset/{$user2->id()}", $reset_url); | |
333 $this->drupalGet($attack_reset_url); | |
334 $this->drupalPostForm(NULL, NULL, t('Log in')); | |
335 $this->assertNoText($user2->getUsername(), 'The invalid password reset page does not show the user name.'); | |
336 $this->assertUrl('user/password', [], 'The user is redirected to the password reset request page.'); | |
337 $this->assertText('You have tried to use a one-time login link that has either been used or is no longer valid. Please request a new one using the form below.'); | |
338 } | |
339 | |
340 } |