You are here

function FieldAttachOtherTest::testEntityFormDisplayBuildForm in Zircon Profile 8

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

Tests \Drupal\Core\Entity\Display\EntityFormDisplayInterface::buildForm().

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

File

core/modules/field/src/Tests/FieldAttachOtherTest.php, line 246
Contains \Drupal\field\Tests\FieldAttachOtherTest.

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\field\Tests

Code

function testEntityFormDisplayBuildForm() {
  $this
    ->createFieldWithStorage('_2');
  $entity_type = 'entity_test';
  $entity = entity_create($entity_type, array(
    'id' => 1,
    'revision_id' => 1,
    'type' => $this->fieldTestData->field
      ->getTargetBundle(),
  ));

  // Test generating widgets for all fields.
  $display = entity_get_form_display($entity_type, $this->fieldTestData->field
    ->getTargetBundle(), 'default');
  $form = array();
  $form_state = new FormState();
  $display
    ->buildForm($entity, $form, $form_state);
  $this
    ->assertEqual($form[$this->fieldTestData->field_name]['widget']['#title'], $this->fieldTestData->field
    ->getLabel(), "First field's form title is {$this->fieldTestData->field->getLabel()}");
  $this
    ->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->field_2
    ->getLabel(), "Second field's form title is {$this->fieldTestData->field_2->getLabel()}");
  for ($delta = 0; $delta < $this->fieldTestData->field_storage
    ->getCardinality(); $delta++) {

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

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

  // Test generating widgets for all fields.
  $display = entity_get_form_display($entity_type, $this->fieldTestData->field
    ->getTargetBundle(), 'default');
  foreach ($display
    ->getComponents() as $name => $options) {
    if ($name != $this->fieldTestData->field_name_2) {
      $display
        ->removeComponent($name);
    }
  }
  $form = array();
  $form_state = new FormState();
  $display
    ->buildForm($entity, $form, $form_state);
  $this
    ->assertFalse(isset($form[$this->fieldTestData->field_name]), 'The first field does not exist in the form');
  $this
    ->assertEqual($form[$this->fieldTestData->field_name_2]['widget']['#title'], $this->fieldTestData->field_2
    ->getLabel(), "Second field's form title is {$this->fieldTestData->field_2->getLabel()}");
  for ($delta = 0; $delta < $this->fieldTestData->field_storage_2
    ->getCardinality(); $delta++) {

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