You are here

public function UserRegistrationTest::testRegistrationDefaultValues in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserRegistrationTest.php \Drupal\Tests\user\Functional\UserRegistrationTest::testRegistrationDefaultValues()

File

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

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegistrationDefaultValues() {

  // Don't require email verification and allow registration by site visitors
  // without administrator approval.
  $config_user_settings = $this
    ->config('user.settings')
    ->set('verify_mail', FALSE)
    ->set('register', UserInterface::REGISTER_VISITORS)
    ->save();

  // Set the default timezone to Brussels.
  $config_system_date = $this
    ->config('system.date')
    ->set('timezone.user.configurable', 1)
    ->set('timezone.default', 'Europe/Brussels')
    ->save();

  // Check the presence of expected cache tags.
  $this
    ->drupalGet('user/register');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings');
  $edit = [];
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomMachineName();
  $edit['pass[pass2]'] = $new_pass;
  $this
    ->submitForm($edit, 'Create new account');

  // Check user fields.
  $accounts = $this->container
    ->get('entity_type.manager')
    ->getStorage('user')
    ->loadByProperties([
    'name' => $name,
    'mail' => $mail,
  ]);
  $new_user = reset($accounts);
  $this
    ->assertEquals($name, $new_user
    ->getAccountName(), 'Username matches.');
  $this
    ->assertEquals($mail, $new_user
    ->getEmail(), 'Email address matches.');

  // Verify that the creation time is correct.
  $this
    ->assertGreaterThan(REQUEST_TIME - 20, $new_user
    ->getCreatedTime());
  $this
    ->assertEquals($config_user_settings
    ->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, $new_user
    ->isActive(), 'Correct status field.');
  $this
    ->assertEquals($config_system_date
    ->get('timezone.default'), $new_user
    ->getTimezone(), 'Correct time zone field.');
  $this
    ->assertEquals(\Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId(), $new_user->langcode->value, 'Correct language field.');
  $this
    ->assertEquals(\Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId(), $new_user->preferred_langcode->value, 'Correct preferred language field.');
  $this
    ->assertEquals($mail, $new_user->init->value, 'Correct init field.');
}