function FieldAttachOtherTest::testEntityFormDisplayExtractFormValues

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayExtractFormValues()
  2. 10 core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayExtractFormValues()
  3. 11.x core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php \Drupal\Tests\field\Kernel\FieldAttachOtherTest::testEntityFormDisplayExtractFormValues()

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

File

core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php, line 300

Class

FieldAttachOtherTest
Tests other Field API functions.

Namespace

Drupal\Tests\field\Kernel

Code

public function testEntityFormDisplayExtractFormValues() {
    $this->createFieldWithStorage('_2');
    $entity_type = 'entity_test';
    $entity_init = $this->container
        ->get('entity_type.manager')
        ->getStorage($entity_type)
        ->create([
        'id' => 1,
        'revision_id' => 1,
        'type' => $this->fieldTestData->field
            ->getTargetBundle(),
    ]);
    // Build the form for all fields.
    $display = \Drupal::service('entity_display.repository')->getFormDisplay($entity_type, $this->fieldTestData->field
        ->getTargetBundle());
    $form = [];
    $form_state = new FormState();
    $display->buildForm($entity_init, $form, $form_state);
    // Simulate incoming values.
    // First field.
    $values = [];
    $weights = [];
    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 = [];
    $weights_2 = [];
    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::entityTypeManager()->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 = [];
    $expected_values_2 = [];
    foreach ($weights as $key => $value) {
        if ($key != 1) {
            $expected_values[] = [
                '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[] = [
                '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 = [];
    foreach ($weights_2 as $key => $value) {
        if ($key != 1) {
            $expected_values_2[] = [
                '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');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.