You are here

function FieldAttachOtherTestCase::testFieldAttachForm in Drupal 7

Test field_attach_form().

This could be much more thorough, but it does verify that the correct widgets show up.

File

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

Class

FieldAttachOtherTestCase
Unit test class for non-storage related field_attach_* functions.

Code

function testFieldAttachForm() {
  $this
    ->createFieldWithInstance('_2');
  $entity_type = 'test_entity';
  $entity = field_test_create_stub_entity(0, 0, $this->instance['bundle']);
  $langcode = LANGUAGE_NONE;

  // When generating form for all fields.
  $form = array();
  $form_state = form_state_defaults();
  field_attach_form($entity_type, $entity, $form, $form_state);
  $this
    ->assertEqual($form[$this->field_name][$langcode]['#title'], $this->instance['label'], "First field's form title is {$this->instance['label']}");
  $this
    ->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}");
  for ($delta = 0; $delta < $this->field['cardinality']; $delta++) {

    // field_test_widget uses 'textfield'
    $this
      ->assertEqual($form[$this->field_name][$langcode][$delta]['value']['#type'], 'textfield', "First field's form delta {$delta} widget is textfield");
  }
  for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {

    // field_test_widget uses 'textfield'
    $this
      ->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta {$delta} widget is textfield");
  }

  // When generating form for a single field (the second field).
  $options = array(
    'field_name' => $this->field_name_2,
  );
  $form = array();
  $form_state = form_state_defaults();
  field_attach_form($entity_type, $entity, $form, $form_state, NULL, $options);
  $this
    ->assertFalse(isset($form[$this->field_name]), 'The first field does not exist in the form');
  $this
    ->assertEqual($form[$this->field_name_2][$langcode]['#title'], $this->instance_2['label'], "Second field's form title is {$this->instance_2['label']}");
  for ($delta = 0; $delta < $this->field_2['cardinality']; $delta++) {

    // field_test_widget uses 'textfield'
    $this
      ->assertEqual($form[$this->field_name_2][$langcode][$delta]['value']['#type'], 'textfield', "Second field's form delta {$delta} widget is textfield");
  }
}