You are here

public function UserRegistrationTest::testUuidFormState 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::testUuidFormState()

Tests that UUID isn't cached in form state on register form.

This is a regression test for https://www.drupal.org/node/2500527 to ensure that the form is not cached on GET requests.

File

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

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testUuidFormState() {
  \Drupal::service('module_installer')
    ->install([
    'image',
  ]);

  // Add a picture field in order to ensure that no form cache is written,
  // which breaks registration of more than 1 user every 6 hours.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'user_picture',
    'entity_type' => 'user',
    'type' => 'image',
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_name' => 'user_picture',
    'entity_type' => 'user',
    'bundle' => 'user',
  ]);
  $field
    ->save();
  $form_display = EntityFormDisplay::create([
    'targetEntityType' => 'user',
    'bundle' => 'user',
    'mode' => 'default',
    'status' => TRUE,
  ]);
  $form_display
    ->setComponent('user_picture', [
    'type' => 'image_image',
  ]);
  $form_display
    ->save();

  // Don't require email verification and allow registration by site visitors
  // without administrator approval.
  $this
    ->config('user.settings')
    ->set('verify_mail', FALSE)
    ->set('register', UserInterface::REGISTER_VISITORS)
    ->save();
  $edit = [];
  $edit['name'] = $this
    ->randomMachineName();
  $edit['mail'] = $edit['name'] . '@example.com';
  $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this
    ->randomMachineName();

  // Create one account.
  $this
    ->drupalGet('user/register');
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $user_storage = \Drupal::entityTypeManager()
    ->getStorage('user');
  $this
    ->assertNotEmpty($user_storage
    ->loadByProperties([
    'name' => $edit['name'],
  ]));
  $this
    ->drupalLogout();

  // Create a second account.
  $edit['name'] = $this
    ->randomMachineName();
  $edit['mail'] = $edit['name'] . '@example.com';
  $edit['pass[pass2]'] = $edit['pass[pass1]'] = $this
    ->randomMachineName();
  $this
    ->drupalGet('user/register');
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertNotEmpty($user_storage
    ->loadByProperties([
    'name' => $edit['name'],
  ]));
}