You are here

function UserRegistrationTest::testRegistrationWithEmailVerification in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/user/src/Tests/UserRegistrationTest.php \Drupal\user\Tests\UserRegistrationTest::testRegistrationWithEmailVerification()

File

core/modules/user/src/Tests/UserRegistrationTest.php, line 31
Contains \Drupal\user\Tests\UserRegistrationTest.

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\user\Tests

Code

function testRegistrationWithEmailVerification() {
  $config = $this
    ->config('user.settings');

  // Require email verification.
  $config
    ->set('verify_mail', TRUE)
    ->save();

  // Set registration to administrator only.
  $config
    ->set('register', USER_REGISTER_ADMINISTRATORS_ONLY)
    ->save();
  $this
    ->drupalGet('user/register');
  $this
    ->assertResponse(403, 'Registration page is inaccessible when only administrators can create accounts.');

  // Allow registration by site visitors without administrator approval.
  $config
    ->set('register', USER_REGISTER_VISITORS)
    ->save();
  $edit = array();
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $this
    ->drupalPostForm('user/register', $edit, t('Create new account'));
  $this
    ->assertText(t('A welcome message with further instructions has been sent to your email address.'), 'User registered successfully.');
  $accounts = entity_load_multiple_by_properties('user', array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $this
    ->assertTrue($new_user
    ->isActive(), 'New account is active after registration.');
  $resetURL = user_pass_reset_url($new_user);
  $this
    ->drupalGet($resetURL);
  $this
    ->assertTitle(t('Set password | Drupal'), 'Page title is "Set password".');

  // Allow registration by site visitors, but require administrator approval.
  $config
    ->set('register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
    ->save();
  $edit = array();
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $this
    ->drupalPostForm('user/register', $edit, t('Create new account'));
  $this->container
    ->get('entity.manager')
    ->getStorage('user')
    ->resetCache();
  $accounts = entity_load_multiple_by_properties('user', array(
    'name' => $name,
    'mail' => $mail,
  ));
  $new_user = reset($accounts);
  $this
    ->assertFalse($new_user
    ->isActive(), 'New account is blocked until approved by an administrator.');
}