Chris@0: drupalPlaceBlock('system_menu_block:account'); Chris@0: Chris@0: // Create a user. Chris@0: $account = $this->drupalCreateUser(); Chris@0: Chris@0: // Activate user by logging in. Chris@0: $this->drupalLogin($account); Chris@0: Chris@0: $this->account = User::load($account->id()); Chris@0: $this->account->pass_raw = $account->pass_raw; Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Set the last login time that is used to generate the one-time link so Chris@0: // that it is definitely over a second ago. Chris@0: $account->login = REQUEST_TIME - mt_rand(10, 100000); Chris@0: db_update('users_field_data') Chris@0: ->fields(['login' => $account->getLastLoginTime()]) Chris@0: ->condition('uid', $account->id()) Chris@0: ->execute(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests password reset functionality. Chris@0: */ Chris@0: public function testUserPasswordReset() { Chris@0: // Verify that accessing the password reset form without having the session Chris@0: // variables set results in an access denied message. Chris@0: $this->drupalGet(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()])); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Try to reset the password for an invalid account. Chris@0: $this->drupalGet('user/password'); Chris@0: Chris@0: $edit = ['name' => $this->randomMachineName(32)]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: Chris@0: $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.'); Chris@0: $this->assertEqual(count($this->drupalGetMails(['id' => 'user_password_reset'])), 0, 'No email was sent when requesting a password for an invalid account.'); Chris@0: Chris@0: // Reset the password by username via the password reset page. Chris@0: $edit['name'] = $this->account->getUsername(); Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: Chris@0: // Verify that the user was sent an email. Chris@0: $this->assertMail('to', $this->account->getEmail(), 'Password email sent to user.'); Chris@0: $subject = t('Replacement login information for @username at @site', ['@username' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]); Chris@0: $this->assertMail('subject', $subject, 'Password reset email subject is correct.'); Chris@0: Chris@0: $resetURL = $this->getResetURL(); Chris@0: $this->drupalGet($resetURL); Chris@0: // Ensure that the current url does not contain the hash and timestamp. Chris@0: $this->assertUrl(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()])); Chris@0: Chris@0: $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache')); Chris@0: Chris@0: // Ensure the password reset URL is not cached. Chris@0: $this->drupalGet($resetURL); Chris@0: $this->assertFalse($this->drupalGetHeader('X-Drupal-Cache')); Chris@0: Chris@0: // Check the one-time login page. Chris@0: $this->assertText($this->account->getUsername(), 'One-time login page contains the correct username.'); Chris@0: $this->assertText(t('This login can be used only once.'), 'Found warning about one-time login.'); Chris@0: $this->assertTitle(t('Reset password | Drupal'), 'Page title is "Reset password".'); Chris@0: Chris@0: // Check successful login. Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $this->assertLink(t('Log out')); Chris@0: $this->assertTitle(t('@name | @site', ['@name' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]), 'Logged in using password reset link.'); Chris@0: Chris@0: // Make sure the ajax request from uploading a user picture does not Chris@0: // invalidate the reset token. Chris@0: $image = current($this->drupalGetTestFiles('image')); Chris@0: $edit = [ Chris@14: 'files[user_picture_0]' => \Drupal::service('file_system')->realpath($image->uri), Chris@0: ]; Chris@0: $this->drupalPostAjaxForm(NULL, $edit, 'user_picture_0_upload_button'); Chris@0: Chris@0: // Change the forgotten password. Chris@0: $password = user_password(); Chris@0: $edit = ['pass[pass1]' => $password, 'pass[pass2]' => $password]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertText(t('The changes have been saved.'), 'Forgotten password changed.'); Chris@0: Chris@0: // Verify that the password reset session has been destroyed. Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertText(t("Your current password is missing or incorrect; it's required to change the Password."), 'Password needed to make profile changes.'); Chris@0: Chris@0: // Log out, and try to log in again using the same one-time link. Chris@0: $this->drupalLogout(); Chris@0: $this->drupalGet($resetURL); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $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.'); Chris@0: Chris@0: // Request a new password again, this time using the email address. Chris@0: $this->drupalGet('user/password'); Chris@0: // Count email messages before to compare with after. Chris@0: $before = count($this->drupalGetMails(['id' => 'user_password_reset'])); Chris@0: $edit = ['name' => $this->account->getEmail()]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: $this->assertTrue(count($this->drupalGetMails(['id' => 'user_password_reset'])) === $before + 1, 'Email sent when requesting password reset using email address.'); Chris@0: Chris@0: // Visit the user edit page without pass-reset-token and make sure it does Chris@0: // not cause an error. Chris@0: $resetURL = $this->getResetURL(); Chris@0: $this->drupalGet($resetURL); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $this->drupalGet('user/' . $this->account->id() . '/edit'); Chris@0: $this->assertNoText('Expected user_string to be a string, NULL given'); Chris@0: $this->drupalLogout(); Chris@0: Chris@0: // Create a password reset link as if the request time was 60 seconds older than the allowed limit. Chris@0: $timeout = $this->config('user.settings')->get('password_reset_timeout'); Chris@0: $bogus_timestamp = REQUEST_TIME - $timeout - 60; Chris@0: $_uid = $this->account->id(); Chris@0: $this->drupalGet("user/reset/$_uid/$bogus_timestamp/" . user_pass_rehash($this->account, $bogus_timestamp)); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $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.'); Chris@0: Chris@0: // Create a user, block the account, and verify that a login link is denied. Chris@0: $timestamp = REQUEST_TIME - 1; Chris@0: $blocked_account = $this->drupalCreateUser()->block(); Chris@0: $blocked_account->save(); Chris@0: $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp)); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: // Verify a blocked user can not request a new password. Chris@0: $this->drupalGet('user/password'); Chris@0: // Count email messages before to compare with after. Chris@0: $before = count($this->drupalGetMails(['id' => 'user_password_reset'])); Chris@0: $edit = ['name' => $blocked_account->getUsername()]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: $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'); Chris@0: $this->assertTrue(count($this->drupalGetMails(['id' => 'user_password_reset'])) === $before, 'No email was sent when requesting password reset for a blocked account'); Chris@0: Chris@0: // Verify a password reset link is invalidated when the user's email address changes. Chris@0: $this->drupalGet('user/password'); Chris@0: $edit = ['name' => $this->account->getUsername()]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: $old_email_reset_link = $this->getResetURL(); Chris@0: $this->account->setEmail("1" . $this->account->getEmail()); Chris@0: $this->account->save(); Chris@0: $this->drupalGet($old_email_reset_link); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $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.'); Chris@0: Chris@0: // Verify a password reset link will automatically log a user when /login is Chris@0: // appended. Chris@0: $this->drupalGet('user/password'); Chris@0: $edit = ['name' => $this->account->getUsername()]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Submit')); Chris@0: $reset_url = $this->getResetURL(); Chris@0: $this->drupalGet($reset_url . '/login'); Chris@0: $this->assertLink(t('Log out')); Chris@0: $this->assertTitle(t('@name | @site', ['@name' => $this->account->getUsername(), '@site' => $this->config('system.site')->get('name')]), 'Logged in using password reset link.'); Chris@0: Chris@0: // Ensure blocked and deleted accounts can't access the user.reset.login Chris@0: // route. Chris@0: $this->drupalLogout(); Chris@0: $timestamp = REQUEST_TIME - 1; Chris@0: $blocked_account = $this->drupalCreateUser()->block(); Chris@0: $blocked_account->save(); Chris@0: $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp) . '/login'); Chris@0: $this->assertResponse(403); Chris@0: Chris@0: $blocked_account->delete(); Chris@0: $this->drupalGet("user/reset/" . $blocked_account->id() . "/$timestamp/" . user_pass_rehash($blocked_account, $timestamp) . '/login'); Chris@0: $this->assertResponse(403); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Retrieves password reset email and extracts the login link. Chris@0: */ Chris@0: public function getResetURL() { Chris@0: // Assume the most recent email. Chris@0: $_emails = $this->drupalGetMails(); Chris@0: $email = end($_emails); Chris@0: $urls = []; Chris@0: preg_match('#.+user/reset/.+#', $email['body'], $urls); Chris@0: Chris@0: return $urls[0]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test user password reset while logged in. Chris@0: */ Chris@0: public function testUserPasswordResetLoggedIn() { Chris@0: $another_account = $this->drupalCreateUser(); Chris@0: $this->drupalLogin($another_account); Chris@0: $this->drupalGet('user/password'); Chris@0: $this->drupalPostForm(NULL, NULL, t('Submit')); Chris@0: Chris@0: // Click the reset URL while logged and change our password. Chris@0: $resetURL = $this->getResetURL(); Chris@0: // Log in as a different user. Chris@0: $this->drupalLogin($this->account); Chris@0: $this->drupalGet($resetURL); Chris@0: $this->assertRaw(new FormattableMarkup( Chris@0: '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 log out and try using the link again.', Chris@0: ['%other_user' => $this->account->getUsername(), '%resetting_user' => $another_account->getUsername(), ':logout' => Url::fromRoute('user.logout')->toString()] Chris@0: )); Chris@0: Chris@0: $another_account->delete(); Chris@0: $this->drupalGet($resetURL); Chris@0: $this->assertText('The one-time login link you clicked is invalid.'); Chris@0: Chris@0: // Log in. Chris@0: $this->drupalLogin($this->account); Chris@0: Chris@0: // Reset the password by username via the password reset page. Chris@0: $this->drupalGet('user/password'); Chris@0: $this->drupalPostForm(NULL, NULL, t('Submit')); Chris@0: Chris@0: // Click the reset URL while logged and change our password. Chris@0: $resetURL = $this->getResetURL(); Chris@0: $this->drupalGet($resetURL); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: Chris@0: // Change the password. Chris@0: $password = user_password(); Chris@0: $edit = ['pass[pass1]' => $password, 'pass[pass2]' => $password]; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save')); Chris@0: $this->assertText(t('The changes have been saved.'), 'Password changed.'); Chris@0: Chris@0: // Logged in users should not be able to access the user.reset.login or the Chris@0: // user.reset.form routes. Chris@0: $timestamp = REQUEST_TIME - 1; Chris@0: $this->drupalGet("user/reset/" . $this->account->id() . "/$timestamp/" . user_pass_rehash($this->account, $timestamp) . '/login'); Chris@0: $this->assertResponse(403); Chris@0: $this->drupalGet("user/reset/" . $this->account->id()); Chris@0: $this->assertResponse(403); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Prefill the text box on incorrect login via link to password reset page. Chris@0: */ Chris@0: public function testUserResetPasswordTextboxFilled() { Chris@0: $this->drupalGet('user/login'); Chris@0: $edit = [ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'pass' => $this->randomMachineName(), Chris@0: ]; Chris@0: $this->drupalPostForm('user/login', $edit, t('Log in')); Chris@0: $this->assertRaw(t('Unrecognized username or password. Forgot your password?', Chris@0: [':password' => \Drupal::url('user.pass', [], ['query' => ['name' => $edit['name']]])])); Chris@0: unset($edit['pass']); Chris@0: $this->drupalGet('user/password', ['query' => ['name' => $edit['name']]]); Chris@0: $this->assertFieldByName('name', $edit['name'], 'User name found.'); Chris@0: // Ensure the name field value is not cached. Chris@0: $this->drupalGet('user/password'); Chris@0: $this->assertNoFieldByName('name', $edit['name'], 'User name not found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Make sure that users cannot forge password reset URLs of other users. Chris@0: */ Chris@0: public function testResetImpersonation() { Chris@0: // Create two identical user accounts except for the user name. They must Chris@0: // have the same empty password, so we can't use $this->drupalCreateUser(). Chris@0: $edit = []; Chris@0: $edit['name'] = $this->randomMachineName(); Chris@0: $edit['mail'] = $edit['name'] . '@example.com'; Chris@0: $edit['status'] = 1; Chris@0: $user1 = User::create($edit); Chris@0: $user1->save(); Chris@0: Chris@0: $edit['name'] = $this->randomMachineName(); Chris@0: $user2 = User::create($edit); Chris@0: $user2->save(); Chris@0: Chris@0: // Unique password hashes are automatically generated, the only way to Chris@0: // change that is to update it directly in the database. Chris@0: db_update('users_field_data') Chris@0: ->fields(['pass' => NULL]) Chris@0: ->condition('uid', [$user1->id(), $user2->id()], 'IN') Chris@0: ->execute(); Chris@0: \Drupal::entityManager()->getStorage('user')->resetCache(); Chris@0: $user1 = User::load($user1->id()); Chris@0: $user2 = User::load($user2->id()); Chris@0: Chris@0: $this->assertEqual($user1->getPassword(), $user2->getPassword(), 'Both users have the same password hash.'); Chris@0: Chris@0: // The password reset URL must not be valid for the second user when only Chris@0: // the user ID is changed in the URL. Chris@0: $reset_url = user_pass_reset_url($user1); Chris@0: $attack_reset_url = str_replace("user/reset/{$user1->id()}", "user/reset/{$user2->id()}", $reset_url); Chris@0: $this->drupalGet($attack_reset_url); Chris@0: $this->drupalPostForm(NULL, NULL, t('Log in')); Chris@0: $this->assertNoText($user2->getUsername(), 'The invalid password reset page does not show the user name.'); Chris@0: $this->assertUrl('user/password', [], 'The user is redirected to the password reset request page.'); Chris@0: $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.'); Chris@0: } Chris@0: Chris@0: }