Pull merge.
[yaffs-website] / web / core / modules / user / tests / src / FunctionalJavascript / UserPasswordResetTest.php
1 <?php
2
3 namespace Drupal\Tests\user\FunctionalJavascript;
4
5 use Drupal\Core\Test\AssertMailTrait;
6 use Drupal\Core\Url;
7 use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
8 use Drupal\Tests\TestFileCreationTrait;
9 use Drupal\user\Entity\User;
10
11 /**
12  * Ensure that password reset methods work as expected.
13  *
14  * @group user
15  */
16 class UserPasswordResetTest extends WebDriverTestBase {
17
18   use AssertMailTrait {
19     getMails as drupalGetMails;
20   }
21
22   use TestFileCreationTrait {
23     getTestFiles as drupalGetTestFiles;
24   }
25
26   /**
27    * The profile to install as a basis for testing.
28    *
29    * This test uses the standard profile to test the password reset in
30    * combination with an ajax request provided by the user picture configuration
31    * in the standard profile.
32    *
33    * @var string
34    */
35   protected $profile = 'standard';
36
37   /**
38    * The user object to test password resetting.
39    *
40    * @var \Drupal\user\UserInterface
41    */
42   protected $account;
43
44   /**
45    * {@inheritdoc}
46    */
47   public static $modules = ['block'];
48
49   /**
50    * {@inheritdoc}
51    */
52   protected function setUp() {
53     parent::setUp();
54
55     // Create a user.
56     $account = $this->drupalCreateUser();
57
58     // Activate user by logging in.
59     $this->drupalLogin($account);
60
61     $this->account = User::load($account->id());
62     $this->account->pass_raw = $account->pass_raw;
63     $this->drupalLogout();
64
65     // Set the last login time that is used to generate the one-time link so
66     // that it is definitely over a second ago.
67     $account->login = REQUEST_TIME - mt_rand(10, 100000);
68     db_update('users_field_data')
69       ->fields(['login' => $account->getLastLoginTime()])
70       ->condition('uid', $account->id())
71       ->execute();
72   }
73
74   /**
75    * Tests password reset functionality with an AJAX form.
76    *
77    * Make sure the ajax request from uploading a user picture does not
78    * invalidate the reset token.
79    */
80   public function testUserPasswordResetWithAdditionalAjaxForm() {
81     $this->drupalGet(Url::fromRoute('user.reset.form', ['uid' => $this->account->id()]));
82
83     // Try to reset the password for an invalid account.
84     $this->drupalGet('user/password');
85
86     // Reset the password by username via the password reset page.
87     $edit['name'] = $this->account->getUsername();
88     $this->drupalPostForm(NULL, $edit, t('Submit'));
89
90     $resetURL = $this->getResetURL();
91     $this->drupalGet($resetURL);
92
93     // Login
94     $this->drupalPostForm(NULL, NULL, t('Log in'));
95
96     // Generate file.
97     $image_file = current($this->drupalGetTestFiles('image'));
98     $image_path = \Drupal::service('file_system')->realpath($image_file->uri);
99
100     // Upload file.
101     $this->getSession()->getPage()->attachFileToField('Picture', $image_path);
102     $this->assertSession()->waitForButton('Remove');
103
104     // Change the forgotten password.
105     $password = user_password();
106     $edit = ['pass[pass1]' => $password, 'pass[pass2]' => $password];
107     $this->drupalPostForm(NULL, $edit, t('Save'));
108
109     // Verify that the password reset session has been destroyed.
110     $this->drupalPostForm(NULL, $edit, t('Save'));
111     // Password needed to make profile changes.
112     $this->assertSession()->pageTextContains("Your current password is missing or incorrect; it's required to change the Password.");
113   }
114
115   /**
116    * Retrieves password reset email and extracts the login link.
117    */
118   public function getResetURL() {
119     // Assume the most recent email.
120     $_emails = $this->drupalGetMails();
121     $email = end($_emails);
122     $urls = [];
123     preg_match('#.+user/reset/.+#', $email['body'], $urls);
124
125     return $urls[0];
126   }
127
128 }