View source
<?php
namespace Drupal\Tests\user_registrationpassword\Functional;
use Drupal\Tests\BrowserTestBase;
class UserRegistrationPassword extends BrowserTestBase {
protected $defaultTheme = 'stark';
public static $modules = [
'user_registrationpassword',
];
public function testRegistrationWithEmailVerificationAndPassword() {
$edit = [];
$edit['name'] = $name = $this
->randomMachineName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['pass[pass1]'] = $new_pass = $this
->randomMachineName();
$edit['pass[pass2]'] = $new_pass;
$pass = $new_pass;
$this
->drupalPostForm('user/register', $edit, 'Create new account');
$this
->assertSession()
->pageTextContains('A welcome message with further instructions has been sent to your email address.');
$accounts = \Drupal::entityQuery('user')
->condition('name', $name)
->condition('mail', $mail)
->condition('status', 0)
->execute();
$account = \Drupal::entityTypeManager()
->getStorage('user')
->load(reset($accounts));
$requestTime = \Drupal::time()
->getRequestTime();
$timestamp = $requestTime + 5000;
$test_timestamp = $requestTime;
$bogus_timestamp = $requestTime - 86500;
$this
->assertFalse($account
->isActive(), 'New account is blocked until approved via email confirmation. status check.');
$this
->assertEquals(0, $account
->getLastLoginTime(), 'New account is blocked until approved via email confirmation. login check.');
$this
->assertEquals(0, $account
->getLastAccessedTime(), 'New account is blocked until approved via email confirmation. access check.');
$auth = [
'name' => $name,
'pass' => $pass,
];
$this
->drupalPostForm('user/login', $auth, 'Log in');
$this
->assertSession()
->pageTextContains('The username ' . $name . ' has not been activated or is blocked.');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$test_timestamp}/" . user_pass_rehash($account, $test_timestamp));
$this
->assertSession()
->pageTextContains('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.');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $bogus_timestamp));
$this
->assertSession()
->pageTextContains('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.');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$bogus_timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('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.');
$account_cloned = clone $account;
$account_cloned
->setPassword('boguspass');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account_cloned, $timestamp));
$this
->assertSession()
->pageTextContains('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.');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('You have just used your one-time login link. Your account is now active and you are authenticated.');
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('You are currently authenticated as user ' . $account
->getAccountName() . '.');
$this
->drupalLogout();
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('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.');
$auth = [
'name' => $name,
'pass' => $pass,
];
$this
->drupalPostForm('user/login', $auth, 'Log in');
$this
->assertSession()
->pageTextContains('Member for');
}
public function testPasswordResetFormResendActivation() {
$edit1 = [];
$edit1['name'] = $this
->randomMachineName();
$edit1['mail'] = $edit1['name'] . '@example.com';
$edit1['pass[pass1]'] = $new_pass = $this
->randomMachineName();
$edit1['pass[pass2]'] = $new_pass;
$this
->drupalPostForm('user/register', $edit1, 'Create new account');
$this
->assertSession()
->pageTextContains('A welcome message with further instructions has been sent to your email address.');
$edit2 = [];
$edit2['name'] = $edit1['name'];
$this
->drupalPostForm('user/password', $edit2, 'Submit');
$this
->assertSession()
->pageTextContains('Further instructions have been sent to your email address.');
$edit3 = [];
$edit3['name'] = $this
->randomMachineName();
$this
->drupalPostForm('user/password', $edit3, 'Submit');
$this
->assertSession()
->pageTextContains($edit3['name'] . ' is not recognized as a username or an email address.');
$edit4 = [];
$edit4['name'] = $this
->randomMachineName() . '@example.com';
$this
->drupalPostForm('user/password', $edit4, 'Submit');
$this
->assertSession()
->pageTextContains($edit4['name'] . ' is not recognized as a username or an email address.');
}
public function testLoginWithUrpLinkWhileBlocked() {
$timestamp = \Drupal::time()
->getRequestTime() + 5000;
$edit = [];
$edit['name'] = $name = $this
->randomMachineName();
$edit['mail'] = $mail = $edit['name'] . '@example.com';
$edit['pass[pass1]'] = $new_pass = $this
->randomMachineName();
$edit['pass[pass2]'] = $new_pass;
$this
->drupalPostForm('user/register', $edit, 'Create new account');
$accounts = \Drupal::entityQuery('user')
->condition('name', $name)
->condition('mail', $mail)
->condition('status', 0)
->execute();
$account = \Drupal::entityTypeManager()
->getStorage('user')
->load(reset($accounts));
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('You have just used your one-time login link. Your account is now active and you are authenticated.');
$this
->drupalLogout();
$account
->setLastLoginTime(\Drupal::time()
->getRequestTime())
->block()
->save();
$this
->drupalGet("user/registrationpassword/" . $account
->id() . "/{$timestamp}/" . user_pass_rehash($account, $timestamp));
$this
->assertSession()
->pageTextContains('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.');
$edit2['name'] = $edit['name'];
$this
->drupalPostForm('user/password', $edit2, 'Submit');
$this
->assertSession()
->pageTextContains($edit2['name'] . ' is blocked or has not been activated yet.');
}
}