You are here

function FieldFormTestCase::testFieldFormMultipleWidget in Drupal 7

Tests widgets handling multiple values.

File

modules/field/tests/field.test, line 1936
Tests for field.module.

Class

FieldFormTestCase

Code

function testFieldFormMultipleWidget() {

  // Create a field with fixed cardinality and an instance using a multiple
  // widget.
  $this->field = $this->field_multiple;
  $this->field_name = $this->field['field_name'];
  $this->instance['field_name'] = $this->field_name;
  $this->instance['widget']['type'] = 'test_field_widget_multiple';
  field_create_field($this->field);
  field_create_instance($this->instance);
  $langcode = LANGUAGE_NONE;

  // Display creation form.
  $this
    ->drupalGet('test-entity/add/test-bundle');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}]", '', 'Widget is displayed.');

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

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

  // Display the form, check that the values are correctly filled in.
  $this
    ->drupalGet('test-entity/manage/' . $id . '/edit');
  $this
    ->assertFieldByName("{$this->field_name}[{$langcode}]", '1, 2, 3', 'Widget is displayed.');

  // Submit the form with more values than the field accepts.
  $edit = array(
    "{$this->field_name}[{$langcode}]" => '1, 2, 3, 4, 5',
  );
  $this
    ->drupalPost(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, $this->field_name, $langcode, array(
    1,
    2,
    3,
  ));
}