You are here

public function UserRegistrationTest::testRegistrationWithUserFields in Drupal 10

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

Tests Field API fields on user registration forms.

File

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

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\Tests\user\Functional

Code

public function testRegistrationWithUserFields() {

  // Create a field on 'user' entity type.
  $field_storage = FieldStorageConfig::create([
    'field_name' => 'test_user_field',
    'entity_type' => 'user',
    'type' => 'test_field',
    'cardinality' => 1,
  ]);
  $field_storage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $field_storage,
    'label' => 'Some user field',
    'bundle' => 'user',
    'required' => TRUE,
  ]);
  $field
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay('user', 'user')
    ->setComponent('test_user_field', [
    'type' => 'test_field_widget',
  ])
    ->save();
  $display_repository
    ->getFormDisplay('user', 'user', 'register')
    ->save();

  // Check that the field does not appear on the registration form.
  $this
    ->drupalGet('user/register');
  $this
    ->assertSession()
    ->pageTextNotContains($field
    ->label());
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:core.entity_form_display.user.user.register');
  $this
    ->assertSession()
    ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:user.settings');

  // Have the field appear on the registration form.
  $display_repository
    ->getFormDisplay('user', 'user', 'register')
    ->setComponent('test_user_field', [
    'type' => 'test_field_widget',
  ])
    ->save();
  $this
    ->drupalGet('user/register');
  $this
    ->assertSession()
    ->pageTextContains($field
    ->label());
  $this
    ->assertRegistrationFormCacheTagsWithUserFields();

  // Check that validation errors are correctly reported.
  $edit = [];
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';

  // Missing input in required field.
  $edit['test_user_field[0][value]'] = '';
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertRegistrationFormCacheTagsWithUserFields();
  $this
    ->assertSession()
    ->pageTextContains("{$field->label()} field is required.");

  // Invalid input.
  $edit['test_user_field[0][value]'] = '-1';
  $this
    ->submitForm($edit, 'Create new account');
  $this
    ->assertRegistrationFormCacheTagsWithUserFields();
  $this
    ->assertSession()
    ->pageTextContains("{$field->label()} does not accept the value -1.");

  // Submit with valid data.
  $value = rand(1, 255);
  $edit['test_user_field[0][value]'] = $value;
  $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($value, $new_user->test_user_field->value, 'The field value was correctly saved.');

  // Check that the 'add more' button works.
  $field_storage
    ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
  $field_storage
    ->save();
  $this
    ->drupalGet('user/register');
  $this
    ->assertRegistrationFormCacheTagsWithUserFields();

  // Add two inputs.
  $value = rand(1, 255);
  $edit = [];
  $edit['test_user_field[0][value]'] = $value;
  $this
    ->submitForm($edit, 'Add another item');
  $this
    ->submitForm($edit, 'Add another item');

  // Submit with three values.
  $edit['test_user_field[1][value]'] = $value + 1;
  $edit['test_user_field[2][value]'] = $value + 2;
  $edit['name'] = $name = $this
    ->randomMachineName();
  $edit['mail'] = $mail = $edit['name'] . '@example.com';
  $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($value, $new_user->test_user_field[0]->value, 'The field value was correctly saved.');
  $this
    ->assertEquals($value + 1, $new_user->test_user_field[1]->value, 'The field value was correctly saved.');
  $this
    ->assertEquals($value + 2, $new_user->test_user_field[2]->value, 'The field value was correctly saved.');
}