function UserRegistrationTest::testRegistrationWithoutEmailVerification

File

drupal/core/modules/user/lib/Drupal/user/Tests/UserRegistrationTest.php, line 62
Definition of Drupal\user\Tests\UserRegistrationTest.

Class

UserRegistrationTest

Namespace

Drupal\user\Tests

Code

function testRegistrationWithoutEmailVerification() {
  $config = config('user.settings');

  // Don't require e-mail verification and allow registration by site visitors
  // without administrator approval.
  $config
    ->set('verify_mail', FALSE)
    ->set('register', USER_REGISTER_VISITORS)
    ->save();
  $edit = array();
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';

  // Try entering a mismatching password.
  $edit['pass[pass1]'] = '99999.0';
  $edit['pass[pass2]'] = '99999';
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $this
    ->assertText(t('The specified passwords do not match.'), 'Typing mismatched passwords displays an error message.');

  // Enter a correct password.
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $new_pass;
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  entity_get_controller('user')
    ->resetCache();
  $accounts = entity_load_multiple_by_properties('user', array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $this
    ->assertText(t('Registration successful. You are now logged in.'), 'Users are logged in after registering.');
  $this
    ->drupalLogout();

  // Allow registration by site visitors, but require administrator approval.
  $config
    ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
    ->save();
  $edit = array();
  $edit['name'] = $name = $this
    ->randomName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $pass;
  $this
    ->drupalPost('user/register', $edit, t('Create new account'));
  $this
    ->assertText(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.'), 'Users are notified of pending approval');

  // Try to login before administrator approval.
  $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,
  )), 'User cannot login yet.');

  // Activate the new account.
  $accounts = entity_load_multiple_by_properties('user', array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer users',
  ));
  $this
    ->drupalLogin($admin_user);
  $edit = array(
    'status' => 1,
  );
  $this
    ->drupalPost('user/' . $new_user->uid . '/edit', $edit, t('Save'));
  $this
    ->drupalLogout();

  // Login after administrator approval.
  $this
    ->drupalPost('user/login', $auth, t('Log in'));
  $this
    ->assertText(t('Member for'), 'User can log in after administrator approval.');
}