You are here

function FormTest::testFieldFormMultipleWidget 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::testFieldFormMultipleWidget()

Tests widgets handling multiple values.

File

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

Class

FormTest
Tests field form handling.

Namespace

Drupal\field\Tests

Code

function testFieldFormMultipleWidget() {

  // Create a field with fixed cardinality, configure the form to use a
  // "multiple" widget.
  $field_storage = $this->fieldStorageMultiple;
  $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, array(
    'type' => 'test_field_widget_multiple',
  ))
    ->save();

  // Display creation form.
  $this
    ->drupalGet('entity_test/add');
  $this
    ->assertFieldByName($field_name, '', 'Widget is displayed.');

  // Create entity with three values.
  $edit = array(
    $field_name => '1, 2, 3',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  preg_match('|entity_test/manage/(\\d+)|', $this->url, $match);
  $id = $match[1];

  // Check that the values were saved.
  $entity_init = entity_load('entity_test', $id);
  $this
    ->assertFieldValues($entity_init, $field_name, array(
    1,
    2,
    3,
  ));

  // Display the form, check that the values are correctly filled in.
  $this
    ->drupalGet('entity_test/manage/' . $id . '/edit');
  $this
    ->assertFieldByName($field_name, '1, 2, 3', 'Widget is displayed.');

  // Submit the form with more values than the field accepts.
  $edit = array(
    $field_name => '1, 2, 3, 4, 5',
  );
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertRaw('this field cannot hold more than 4 values', 'Form validation failed.');

  // Check that the field values were not submitted.
  $this
    ->assertFieldValues($entity_init, $field_name, array(
    1,
    2,
    3,
  ));
}