You are here

public function UserRegistrationTest::testRegistrationDefaultValues in Drupal 8

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

File

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

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
    ->assertCacheTag('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
    ->drupalPostForm(NULL, $edit, t('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
    ->assertEqual($new_user
    ->getAccountName(), $name, 'Username matches.');
  $this
    ->assertEqual($new_user
    ->getEmail(), $mail, 'Email address matches.');
  $this
    ->assertTrue($new_user
    ->getCreatedTime() > REQUEST_TIME - 20, 'Correct creation time.');
  $this
    ->assertEqual($new_user
    ->isActive(), $config_user_settings
    ->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0, 'Correct status field.');
  $this
    ->assertEqual($new_user
    ->getTimezone(), $config_system_date
    ->get('timezone.default'), 'Correct time zone field.');
  $this
    ->assertEqual($new_user->langcode->value, \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId(), 'Correct language field.');
  $this
    ->assertEqual($new_user->preferred_langcode->value, \Drupal::languageManager()
    ->getDefaultLanguage()
    ->getId(), 'Correct preferred language field.');
  $this
    ->assertEqual($new_user->init->value, $mail, 'Correct init field.');
}