You are here

public function FormTest::testFieldFormMultivalueWithRequiredRadio in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Functional/FormTest.php \Drupal\Tests\field\Functional\FormTest::testFieldFormMultivalueWithRequiredRadio()
  2. 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 373

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
    ->submitForm([], 'Add another item');

  // Verify that no error is thrown by the radio element.
  $this
    ->assertSession()
    ->elementNotExists('xpath', '//div[contains(@class, "error")]');

  // Verify that the widget is added.
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[0][value]", '');
  $this
    ->assertSession()
    ->fieldValueEquals("{$field_name}[1][value]", '');

  // Verify that no extraneous widget is displayed.
  $this
    ->assertSession()
    ->fieldNotExists("{$field_name}[2][value]");
}