public function UserRegistrationTest::testRegistrationWithUserFields in Drupal 8
Same name and namespace in other branches
- 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 293
Class
- UserRegistrationTest
- Tests registration of user under different configurations.
Namespace
Drupal\Tests\user\FunctionalCode
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
->assertNoText($field
->label(), 'The field does not appear on user registration form');
$this
->assertCacheTag('config:core.entity_form_display.user.user.register');
$this
->assertCacheTag('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
->assertText($field
->label(), 'The field appears on user registration form');
$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
->drupalPostForm(NULL, $edit, t('Create new account'));
$this
->assertRegistrationFormCacheTagsWithUserFields();
$this
->assertRaw(t('@name field is required.', [
'@name' => $field
->label(),
]), 'Field validation error was correctly reported.');
// Invalid input.
$edit['test_user_field[0][value]'] = '-1';
$this
->drupalPostForm(NULL, $edit, t('Create new account'));
$this
->assertRegistrationFormCacheTagsWithUserFields();
$this
->assertRaw(t('%name does not accept the value -1.', [
'%name' => $field
->label(),
]), 'Field validation error was correctly reported.');
// Submit with valid data.
$value = rand(1, 255);
$edit['test_user_field[0][value]'] = $value;
$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->test_user_field->value, $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
->drupalPostForm(NULL, $edit, t('Add another item'));
$this
->drupalPostForm(NULL, $edit, t('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
->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->test_user_field[0]->value, $value, 'The field value was correctly saved.');
$this
->assertEqual($new_user->test_user_field[1]->value, $value + 1, 'The field value was correctly saved.');
$this
->assertEqual($new_user->test_user_field[2]->value, $value + 2, 'The field value was correctly saved.');
}