You are here

function FormTest::testFieldFormMultivalueWithRequiredRadio in Zircon Profile 8

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

Tests widget handling of multiple required radios.

File

core/modules/field/src/Tests/FormTest.php, line 356
Contains \Drupal\field\Tests\FormTest.

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testFieldFormMultivalueWithRequiredRadio() {

  // Create a multivalue test field.
  $field_storage = $this->fieldStorageUnlimited;
  $field_name = $field_storage['field_name'];
  $this->field['field_name'] = $field_name;
  entity_create('field_storage_config', $field_storage)
    ->save();
  entity_create('field_config', $this->field)
    ->save();
  entity_get_form_display($this->field['entity_type'], $this->field['bundle'], 'default')
    ->setComponent($field_name)
    ->save();

  // Add a required radio field.
  entity_create('field_storage_config', array(
    'field_name' => 'required_radio_test',
    'entity_type' => 'entity_test',
    'type' => 'list_string',
    'settings' => array(
      'allowed_values' => array(
        'yes' => 'yes',
        'no' => 'no',
      ),
    ),
  ))
    ->save();
  $field = array(
    'field_name' => 'required_radio_test',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'required' => TRUE,
  );
  entity_create('field_config', $field)
    ->save();
  entity_get_form_display($field['entity_type'], $field['bundle'], 'default')
    ->setComponent($field['field_name'], array(
    'type' => 'options_buttons',
  ))
    ->save();

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

  // Press the 'Add more' button.
  $this
    ->drupalPostForm(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("{$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');
}