You are here

public function FormTest::testFieldFormMultivalueWithRequiredRadio in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormMultivalueWithRequiredRadio()

Tests widget handling of multiple required radios.

File

core/modules/field/tests/src/Functional/FormTest.php, line 367

Class

FormTest
Tests field form handling.

Namespace

Drupal\Tests\field\Functional

Code

public function testFieldFormMultivalueWithRequiredRadio() {

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');

  // Create a multivalue test field.
  $field_storage = $this->fieldStorageUnlimited;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  FieldStorageConfig::create($field_storage)
    ->save();
  FieldConfig::create($this->field)
    ->save();
  $display_repository
    ->getFormDisplay($this->field['entity_type'], $this->field['bundle'])
    ->setComponent($field_name)
    ->save();

  // Add a required radio field.
  FieldStorageConfig::create([
    'field_name' => 'required_radio_test',
    'entity_type' => 'entity_test',
    'type' => 'list_string',
    'settings' => [
      'allowed_values' => [
        'yes' => 'yes',
        'no' => 'no',
      ],
    ],
  ])
    ->save();
  $field = [
    'field_name' => 'required_radio_test',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'required' => TRUE,
  ];
  FieldConfig::create($field)
    ->save();
  $display_repository
    ->getFormDisplay($field['entity_type'], $field['bundle'])
    ->setComponent($field['field_name'], [
    'type' => 'options_buttons',
  ])
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');

  // Press the 'Add more' button.
  $this
    ->drupalPostForm(NULL, [], t('Add another item'));

  // Verify that no error is thrown by the radio element.
  $this
    ->assertNoFieldByXpath('//div[contains(@class, "error")]', FALSE, 'No error message is displayed.');

  // Verify that the widget is added.
  $this
    ->assertFieldByName("{$field_name}[0][value]", '', 'Widget 1 is displayed');
  $this
    ->assertFieldByName("{$field_name}[1][value]", '', 'New widget is displayed');
  $this
    ->assertNoField("{$field_name}[2][value]", 'No extraneous widget is displayed');
}