You are here

function FieldAttachOtherTest::testEntityFormDisplayExtractFormValues in Zircon Profile 8.0

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

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

File

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

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\field\Tests

Code

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

  // Build the form for all fields.
  $display = entity_get_form_display($entity_type, $this->fieldTestData->field
    ->getTargetBundle(), 'default');
  $form = array();
  $form_state = new FormState();
  $display
    ->buildForm($entity_init, $form, $form_state);

  // Simulate incoming values.
  // First field.
  $values = array();
  $weights = array();
  for ($delta = 0; $delta < $this->fieldTestData->field_storage
    ->getCardinality(); $delta++) {
    $values[$delta]['value'] = mt_rand(1, 127);

    // Assign random weight.
    do {
      $weight = mt_rand(0, $this->fieldTestData->field_storage
        ->getCardinality());
    } while (in_array($weight, $weights));
    $weights[$delta] = $weight;
    $values[$delta]['_weight'] = $weight;
  }

  // Leave an empty value. 'field_test' fields are empty if empty().
  $values[1]['value'] = 0;

  // Second field.
  $values_2 = array();
  $weights_2 = array();
  for ($delta = 0; $delta < $this->fieldTestData->field_storage_2
    ->getCardinality(); $delta++) {
    $values_2[$delta]['value'] = mt_rand(1, 127);

    // Assign random weight.
    do {
      $weight = mt_rand(0, $this->fieldTestData->field_storage_2
        ->getCardinality());
    } while (in_array($weight, $weights_2));
    $weights_2[$delta] = $weight;
    $values_2[$delta]['_weight'] = $weight;
  }

  // Leave an empty value. 'field_test' fields are empty if empty().
  $values_2[1]['value'] = 0;

  // Pretend the form has been built.
  $form_state
    ->setFormObject(\Drupal::entityManager()
    ->getFormObject($entity_type, 'default'));
  \Drupal::formBuilder()
    ->prepareForm('field_test_entity_form', $form, $form_state);
  \Drupal::formBuilder()
    ->processForm('field_test_entity_form', $form, $form_state);
  $form_state
    ->setValue($this->fieldTestData->field_name, $values);
  $form_state
    ->setValue($this->fieldTestData->field_name_2, $values_2);

  // Extract values for all fields.
  $entity = clone $entity_init;
  $display
    ->extractFormValues($entity, $form, $form_state);
  asort($weights);
  asort($weights_2);
  $expected_values = array();
  $expected_values_2 = array();
  foreach ($weights as $key => $value) {
    if ($key != 1) {
      $expected_values[] = array(
        'value' => $values[$key]['value'],
      );
    }
  }
  $this
    ->assertIdentical($entity->{$this->fieldTestData->field_name}
    ->getValue(), $expected_values, 'Submit filters empty values');
  foreach ($weights_2 as $key => $value) {
    if ($key != 1) {
      $expected_values_2[] = array(
        'value' => $values_2[$key]['value'],
      );
    }
  }
  $this
    ->assertIdentical($entity->{$this->fieldTestData->field_name_2}
    ->getValue(), $expected_values_2, 'Submit filters empty values');

  // Call EntityFormDisplayInterface::extractFormValues() for a single field (the second field).
  foreach ($display
    ->getComponents() as $name => $options) {
    if ($name != $this->fieldTestData->field_name_2) {
      $display
        ->removeComponent($name);
    }
  }
  $entity = clone $entity_init;
  $display
    ->extractFormValues($entity, $form, $form_state);
  $expected_values_2 = array();
  foreach ($weights_2 as $key => $value) {
    if ($key != 1) {
      $expected_values_2[] = array(
        'value' => $values_2[$key]['value'],
      );
    }
  }
  $this
    ->assertTrue($entity->{$this->fieldTestData->field_name}
    ->isEmpty(), 'The first field is empty in the entity object');
  $this
    ->assertIdentical($entity->{$this->fieldTestData->field_name_2}
    ->getValue(), $expected_values_2, 'Submit filters empty values');
}