You are here

public function UserRegistrationPasswordTestCase::testRegistrationWithEmailVerificationAndPassword in User registration password 7

Implements testRegistrationWithEmailVerificationAndPassword().

File

tests/user_registrationpassword.test, line 40
Functionality tests for user_registrationpassword.module.

Class

UserRegistrationPasswordTestCase
Class UserRegistrationPasswordTestCase.

Code

public function testRegistrationWithEmailVerificationAndPassword() {

  // Allow registration by site visitors without administrator
  // approval and set password during registration.
  variable_set('user_register', USER_REGISTER_VISITORS);

  // Disable e-mail verification.
  variable_set('user_email_verification', FALSE);

  // Prevent standard notification email to administrators and to user.
  variable_set('user_mail_register_pending_approval_notify', FALSE);

  // Set the registration variable to 2, register with pass, but require
  // confirmation.
  variable_set('user_registrationpassword_registration', USER_REGISTRATIONPASSWORD_VERIFICATION_PASS);

  // Register a new account.
  $edit = array();
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $new_pass;
  $pass = $new_pass;
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $this
    ->assertText(t('A welcome message with further instructions has been sent to your e-mail address.'), t('User registered successfully.'));

  // Load the new user.
  $accounts = user_load_multiple(array(), array(
    'name' => $name,
    'mail' => $mail,
    'status' => 0,
  ));
  $account = reset($accounts);

  // Configure some timestamps.
  // We up the timestamp a bit, else the check will fail.
  // The function that checks this uses the execution time
  // and that's always larger in real-life situations
  // (and it fails correctly when you remove the + 5000).
  $timestamp = REQUEST_TIME + 5000;
  $test_timestamp = REQUEST_TIME;
  $bogus_timestamp = REQUEST_TIME - 86500;

  // Check if the account has not been activated.
  $this
    ->assertFalse($account->status, t('New account is blocked until approved via e-mail confirmation. status check.'));
  $this
    ->assertFalse($account->login, t('New account is blocked until approved via e-mail confirmation. login check.'));
  $this
    ->assertFalse($account->access, t('New account is blocked until approved via e-mail confirmation. access check.'));

  // Login before activation.
  $auth = array(
    'name' => $name,
    'pass' => $pass,
  );
  $this
    ->drupalPost('user/login', $auth, t('Log in'));
  $this
    ->assertText(t('The username @name has not been activated or is blocked.', array(
    '@name' => $name,
  )), t('User cannot login yet.'));

  // Timestamp can not be smaller then current. (== registration time).
  // If this is the case, something is really wrong.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$test_timestamp}/" . user_pass_rehash($account->pass, $test_timestamp, $account->login, $account->uid));
  $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.'));

  // Fake key combi.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $bogus_timestamp, $account->login, $account->uid));
  $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.'));

  // Fake timestamp.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$bogus_timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
  $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.'));

  // Wrong password.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$bogus_timestamp}/" . user_pass_rehash($this
    ->randomName(), $timestamp, $account->login, $account->uid));
  $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.'));

  // Attempt to use the activation link.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
  $this
    ->assertText(t('You have just used your one-time login link. Your account is now active and you are authenticated.'));

  // Attempt to use the activation link again.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
  $this
    ->assertText(t('You are currently authenticated as user !user.', array(
    '!user' => $account->name,
  )));

  // Logout the user.
  $this
    ->drupalLogout();

  // Then attempt to use the activation link yet again.
  $this
    ->drupalGet("user/registrationpassword/{$account->uid}/{$timestamp}/" . user_pass_rehash($account->pass, $timestamp, $account->login, $account->uid));
  $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.'));

  // And then try to do normal login.
  $auth = array(
    'name' => $name,
    'pass' => $pass,
  );
  $this
    ->drupalPost('user/login', $auth, t('Log in'));
  $this
    ->assertText(t('Member for'), t('User logged in.'));
}