You are here

function UserRegistrationTest::testRegistrationWithUserFields in Zircon Profile 8.0

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

Tests Field API fields on user registration forms.

File

core/modules/user/src/Tests/UserRegistrationTest.php, line 289
Contains \Drupal\user\Tests\UserRegistrationTest.

Class

UserRegistrationTest
Tests registration of user under different configurations.

Namespace

Drupal\user\Tests

Code

function testRegistrationWithUserFields() {

  // Create a field on 'user' entity type.
  $field_storage = entity_create('field_storage_config', array(
    'field_name' => 'test_user_field',
    'entity_type' => 'user',
    'type' => 'test_field',
    'cardinality' => 1,
  ));
  $field_storage
    ->save();
  $field = entity_create('field_config', array(
    'field_storage' => $field_storage,
    'label' => 'Some user field',
    'bundle' => 'user',
    'required' => TRUE,
  ));
  $field
    ->save();
  entity_get_form_display('user', 'user', 'default')
    ->setComponent('test_user_field', array(
    'type' => 'test_field_widget',
  ))
    ->save();
  entity_get_form_display('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.
  entity_get_form_display('user', 'user', 'register')
    ->setComponent('test_user_field', array(
    '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 = array();
  $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.', array(
    '@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.', array(
    '%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 = entity_load_multiple_by_properties('user', array(
    '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();
  foreach (array(
    'js',
    'nojs',
  ) as $js) {
    $this
      ->drupalGet('user/register');
    $this
      ->assertRegistrationFormCacheTagsWithUserFields();

    // Add two inputs.
    $value = rand(1, 255);
    $edit = array();
    $edit['test_user_field[0][value]'] = $value;
    if ($js == 'js') {
      $this
        ->drupalPostAjaxForm(NULL, $edit, 'test_user_field_add_more');
      $this
        ->drupalPostAjaxForm(NULL, $edit, 'test_user_field_add_more');
    }
    else {
      $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 = entity_load_multiple_by_properties('user', array(
      'name' => $name,
      'mail' => $mail,
    ));
    $new_user = reset($accounts);
    $this
      ->assertEqual($new_user->test_user_field[0]->value, $value, format_string('@js : The field value was correctly saved.', array(
      '@js' => $js,
    )));
    $this
      ->assertEqual($new_user->test_user_field[1]->value, $value + 1, format_string('@js : The field value was correctly saved.', array(
      '@js' => $js,
    )));
    $this
      ->assertEqual($new_user->test_user_field[2]->value, $value + 2, format_string('@js : The field value was correctly saved.', array(
      '@js' => $js,
    )));
  }
}