function UserRegistrationTest::testRegistrationWithoutEmailVerification

Same name in other branches
  1. 9 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testRegistrationWithoutEmailVerification()
  2. 8.9.x core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testRegistrationWithoutEmailVerification()
  3. 10 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testRegistrationWithoutEmailVerification()

File

core/modules/user/tests/src/Functional/UserRegistrationTest.php, line 73

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegistrationWithoutEmailVerification() : void {
    $config = $this->config('user.settings');
    // Don't require email verification and allow registration by site visitors
    // without administrator approval.
    $config->set('verify_mail', FALSE)
        ->set('register', UserInterface::REGISTER_VISITORS)
        ->save();
    $edit = [];
    $edit['name'] = $name = $this->randomMachineName();
    $edit['mail'] = $mail = $edit['name'] . '@example.com';
    // Try entering a mismatching password.
    $edit['pass[pass1]'] = '99999.0';
    $edit['pass[pass2]'] = '99999';
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
        ->pageTextContains('The specified passwords do not match.');
    // Enter a correct password.
    $edit['pass[pass1]'] = $new_pass = $this->randomMachineName();
    $edit['pass[pass2]'] = $new_pass;
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $this->container
        ->get('entity_type.manager')
        ->getStorage('user')
        ->resetCache();
    $accounts = $this->container
        ->get('entity_type.manager')
        ->getStorage('user')
        ->loadByProperties([
        'name' => $name,
        'mail' => $mail,
    ]);
    $new_user = reset($accounts);
    $this->assertNotNull($new_user, 'New account successfully created with matching passwords.');
    $this->assertSession()
        ->pageTextContains('Registration successful. You are now logged in.');
    $this->drupalLogout();
    // Allow registration by site visitors, but require administrator approval.
    $config->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
        ->save();
    $edit = [];
    $edit['name'] = $name = $this->randomMachineName();
    $edit['mail'] = $mail = $edit['name'] . '@example.com';
    $edit['pass[pass1]'] = $pass = $this->randomMachineName();
    $edit['pass[pass2]'] = $pass;
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
        ->pageTextContains('Thank you for applying for an account. Your account is currently pending approval by the site administrator.');
    // Try to log in before administrator approval.
    $auth = [
        'name' => $name,
        'pass' => $pass,
    ];
    $this->drupalGet('user/login');
    $this->submitForm($auth, 'Log in');
    $this->assertSession()
        ->pageTextContains('The username ' . $name . ' has not been activated or is blocked.');
    // Activate the new account.
    $accounts = $this->container
        ->get('entity_type.manager')
        ->getStorage('user')
        ->loadByProperties([
        'name' => $name,
        'mail' => $mail,
    ]);
    $new_user = reset($accounts);
    $admin_user = $this->drupalCreateUser([
        'administer users',
    ]);
    $this->drupalLogin($admin_user);
    $edit = [
        'status' => 1,
    ];
    $this->drupalGet('user/' . $new_user->id() . '/edit');
    $this->submitForm($edit, 'Save');
    $this->drupalLogout();
    // Log in after administrator approval.
    $this->drupalGet('user/login');
    $this->submitForm($auth, 'Log in');
    $this->assertSession()
        ->pageTextContains('Member for');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.