function UserRegistrationTest::testRegistrationWithEmailVerification

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

File

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

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegistrationWithEmailVerification() : void {
    $config = $this->config('user.settings');
    // Require email verification.
    $config->set('verify_mail', TRUE)
        ->save();
    // Set registration to administrator only and ensure the user registration
    // page is inaccessible.
    $config->set('register', UserInterface::REGISTER_ADMINISTRATORS_ONLY)
        ->save();
    $this->drupalGet('user/register');
    $this->assertSession()
        ->statusCodeEquals(403);
    // Allow registration by site visitors without administrator approval.
    $config->set('register', UserInterface::REGISTER_VISITORS)
        ->save();
    $edit = [];
    $edit['name'] = $name = $this->randomMachineName();
    $edit['mail'] = $mail = $edit['name'] . '@example.com';
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $this->assertSession()
        ->pageTextContains('A welcome message with further instructions has been sent to your email address.');
    
    /** @var EntityStorageInterface $storage */
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('user');
    $accounts = $storage->loadByProperties([
        '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->assertSession()
        ->titleEquals('Set password | Drupal');
    // 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';
    $this->drupalGet('user/register');
    $this->submitForm($edit, 'Create new account');
    $this->container
        ->get('entity_type.manager')
        ->getStorage('user')
        ->resetCache();
    $accounts = $storage->loadByProperties([
        'name' => $name,
        'mail' => $mail,
    ]);
    $new_user = reset($accounts);
    $this->assertFalse($new_user->isActive(), 'New account is blocked until approved by an administrator.');
}

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