1.20.x user.test UserRegistrationTestCase::testRegistrationEmailAsUsername()

Tests new users username matches their email if username is an email.

File

modules/user/tests/user.test, line 321
Tests for user.module.

Class

UserRegistrationTestCase

Code

function testRegistrationEmailAsUsername() {
  // Don't require email verification.
  // Allow registration by site visitors without administrator approval.
  config('system.core')
    ->set('user_email_verification', FALSE)
    ->set('user_register', USER_REGISTER_VISITORS)
    ->save();

  $mail = $this->randomName() . '@example.com';
  $different = $this->randomName() . $mail;

  // Set up edit array.
  $edit = array();
  $edit['mail'] = $mail;
  $edit['name'] = $different;
  $edit['pass'] = $this->randomName();

  // Attempt to create an account using an email that doesn't match the name.
  $this->backdropPost('user/register', $edit, t('Create new account'));
  $this->assertText(t('An email address was provided as a username, but does not match the account email address.'), 'Email username does not match user email - error message found.');
  $this->assertNoText(t('Registration successful. You are now logged in.'), 'The user was not created and logged in.');

  // Attempt to create new account using matching email address.
  $edit['name'] = $edit['mail'] = $this->randomName() . '@example.com';

  $this->backdropPost('user/register', $edit, t('Create new account'));
  $this->assertText(t('Registration successful. You are now logged in.'), 'The user was created and logged in with matching email.');

  $new_user = user_load_by_name($edit['name']);
  $this->assertTrue(($new_user->name === $edit['name']) && ($new_user->mail === $edit['mail']), 'Created user with matching username and email address.');
}