UserPasswordResetTest.php in Drupal 9
File
core/modules/user/tests/src/FunctionalJavascript/UserPasswordResetTest.php
View source
<?php
namespace Drupal\Tests\user\FunctionalJavascript;
use Drupal\Core\Database\Database;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\TestFileCreationTrait;
use Drupal\user\Entity\User;
class UserPasswordResetTest extends WebDriverTestBase {
use AssertMailTrait {
getMails as drupalGetMails;
}
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected $profile = 'standard';
protected $account;
protected static $modules = [
'block',
];
protected function setUp() : void {
parent::setUp();
$account = $this
->drupalCreateUser();
$this
->drupalLogin($account);
$this->account = User::load($account
->id());
$this->account->pass_raw = $account->pass_raw;
$this
->drupalLogout();
$account->login = REQUEST_TIME - mt_rand(10, 100000);
Database::getConnection()
->update('users_field_data')
->fields([
'login' => $account
->getLastLoginTime(),
])
->condition('uid', $account
->id())
->execute();
}
public function testUserPasswordResetWithAdditionalAjaxForm() {
$this
->drupalGet(Url::fromRoute('user.reset.form', [
'uid' => $this->account
->id(),
]));
$this
->drupalGet('user/password');
$edit['name'] = $this->account
->getAccountName();
$this
->submitForm($edit, 'Submit');
$resetURL = $this
->getResetURL();
$this
->drupalGet($resetURL);
$this
->submitForm([], 'Log in');
$image_file = current($this
->drupalGetTestFiles('image'));
$image_path = \Drupal::service('file_system')
->realpath($image_file->uri);
$this
->getSession()
->getPage()
->attachFileToField('Picture', $image_path);
$this
->assertSession()
->waitForButton('Remove');
$password = \Drupal::service('password_generator')
->generate();
$edit = [
'pass[pass1]' => $password,
'pass[pass2]' => $password,
];
$this
->submitForm($edit, 'Save');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains("Your current password is missing or incorrect; it's required to change the Password.");
}
public function getResetURL() {
$_emails = $this
->drupalGetMails();
$email = end($_emails);
$urls = [];
preg_match('#.+user/reset/.+#', $email['body'], $urls);
return $urls[0];
}
}