function FieldAttachOtherTest::testFieldAttachExtractFormValues

Test field_attach_extract_form_values().

File

drupal/core/modules/field/lib/Drupal/field/Tests/FieldAttachOtherTest.php, line 473
Definition of Drupal\field\Tests\FieldAttachOtherTest.

Class

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

Namespace

Drupal\field\Tests

Code

function testFieldAttachExtractFormValues() {
  $this
    ->createFieldWithInstance('_2');
  $entity_type = 'test_entity';
  $entity_init = field_test_create_entity(0, 0, $this->instance['bundle']);
  $langcode = Language::LANGCODE_NOT_SPECIFIED;

  // Build the form for all fields.
  $form = array();
  $form_state = form_state_defaults();
  $form_state['form_display'] = entity_get_form_display($entity_type, $this->instance['bundle'], 'default');
  field_attach_form($entity_init, $form, $form_state);

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

    // Assign random weight.
    do {
      $weight = mt_rand(0, $this->field['cardinality']);
    } 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->field_2['cardinality']; $delta++) {
    $values_2[$delta]['value'] = mt_rand(1, 127);

    // Assign random weight.
    do {
      $weight = mt_rand(0, $this->field_2['cardinality']);
    } 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.
  drupal_prepare_form('field_test_entity_form', $form, $form_state);
  drupal_process_form('field_test_entity_form', $form, $form_state);
  $form_state['values'][$this->field_name][$langcode] = $values;
  $form_state['values'][$this->field_name_2][$langcode] = $values_2;

  // Call field_attach_extract_form_values() for all fields.
  $entity = clone $entity_init;
  field_attach_extract_form_values($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->field_name}[$langcode], $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->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values');

  // Call field_attach_extract_form_values() for a single field (the second field).
  $options = array(
    'field_name' => $this->field_name_2,
  );
  $entity = clone $entity_init;
  field_attach_extract_form_values($entity, $form, $form_state, $options);
  $expected_values_2 = array();
  foreach ($weights_2 as $key => $value) {
    if ($key != 1) {
      $expected_values_2[] = array(
        'value' => $values_2[$key]['value'],
      );
    }
  }
  $this
    ->assertFalse(isset($entity->{$this->field_name}), 'The first field does not exist in the entity object');
  $this
    ->assertIdentical($entity->{$this->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values');
}