public function UserRegistrationTest::testRegistrationWithEmailVerification in Drupal 8
Same name and namespace in other branches
- 9 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testRegistrationWithEmailVerification()
File
- core/modules/user/tests/src/Functional/UserRegistrationTest.php, line 32
Class
- UserRegistrationTest
- Tests registration of user under different configurations.
Namespace
Drupal\Tests\user\Functional
Code
public function testRegistrationWithEmailVerification() {
$config = $this
->config('user.settings');
$config
->set('verify_mail', TRUE)
->save();
$config
->set('register', UserInterface::REGISTER_ADMINISTRATORS_ONLY)
->save();
$this
->drupalGet('user/register');
$this
->assertSession()
->statusCodeEquals(403);
$config
->set('register', UserInterface::REGISTER_VISITORS)
->save();
$edit = [];
$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.');
$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
->assertTitle('Set password | Drupal');
$config
->set('register', UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL)
->save();
$edit = [];
$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_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.');
}