function FormTest::testFieldFormMultivalueWithRequiredRadio

Tests widget handling of multiple required radios.

File

drupal/core/modules/field/lib/Drupal/field/Tests/FormTest.php, line 271
Definition of Drupal\field\Tests\FormTest.

Class

FormTest

Namespace

Drupal\field\Tests

Code

function testFieldFormMultivalueWithRequiredRadio() {

  // Create a multivalue test field.
  $this->field = $this->field_unlimited;
  $this->field_name = $this->field['field_name'];
  $this->instance['field_name'] = $this->field_name;
  field_create_field($this->field);
  field_create_instance($this->instance);
  entity_get_form_display($this->instance['entity_type'], $this->instance['bundle'], 'default')
    ->setComponent($this->field_name)
    ->save();
  $langcode = Language::LANGCODE_NOT_SPECIFIED;

  // Add a required radio field.
  field_create_field(array(
    'field_name' => 'required_radio_test',
    'type' => 'list_text',
    'settings' => array(
      'allowed_values' => array(
        'yes' => 'yes',
        'no' => 'no',
      ),
    ),
  ));
  $instance = array(
    'field_name' => 'required_radio_test',
    'entity_type' => 'test_entity',
    'bundle' => 'test_bundle',
    'required' => TRUE,
  );
  field_create_instance($instance);
  entity_get_form_display($instance['entity_type'], $instance['bundle'], 'default')
    ->setComponent($instance['field_name'], array(
    'type' => 'options_buttons',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('test-entity/add/test_bundle');

  // Press the 'Add more' button.
  $this
    ->drupalPost(NULL, array(), 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("{$this->field_name}[{$langcode}][0][value]", '', 'Widget 1 is displayed');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}][1][value]", '', 'New widget is displayed');
  $this
    ->assertNoField("{$this->field_name}[{$langcode}][2][value]", 'No extraneous widget is displayed');
}