function EntityDisplayFormBaseTest::testCopyFormValuesToEntity

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayFormBaseTest.php \Drupal\KernelTests\Core\Entity\EntityDisplayFormBaseTest::testCopyFormValuesToEntity()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayFormBaseTest.php \Drupal\KernelTests\Core\Entity\EntityDisplayFormBaseTest::testCopyFormValuesToEntity()
  3. 11.x core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayFormBaseTest.php \Drupal\KernelTests\Core\Entity\EntityDisplayFormBaseTest::testCopyFormValuesToEntity()

@covers ::copyFormValuesToEntity

File

core/tests/Drupal/KernelTests/Core/Entity/EntityDisplayFormBaseTest.php, line 27

Class

EntityDisplayFormBaseTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21field_ui%21src%21Form%21EntityDisplayFormBase.php/class/EntityDisplayFormBase/10" title="Base class for EntityDisplay edit forms." class="local">\Drupal\field_ui\Form\EntityDisplayFormBase</a>

Namespace

Drupal\KernelTests\Core\Entity

Code

public function testCopyFormValuesToEntity() {
    $field_values = [];
    $entity = $this->prophesize(EntityDisplayInterface::class);
    $entity->getPluginCollections()
        ->willReturn([]);
    $entity->getTargetEntityTypeId()
        ->willReturn('entity_test_with_bundle');
    $entity->getTargetBundle()
        ->willReturn('target_bundle');
    // An initially hidden field, with a submitted region change.
    $entity->getComponent('new_field_mismatch_type_visible')
        ->willReturn([]);
    $field_values['new_field_mismatch_type_visible'] = [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'hidden',
    ];
    $entity->removeComponent('new_field_mismatch_type_visible')
        ->will(function (array $args) use ($entity) {
        // On subsequent calls, getComponent() will return an empty array.
        $entity->getComponent($args[0])
            ->willReturn([]);
    })
        ->shouldBeCalled();
    // An initially visible field, with identical submitted values.
    $entity->getComponent('field_visible_no_changes')
        ->willReturn([
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ]);
    $field_values['field_visible_no_changes'] = [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ];
    $entity->setComponent('field_visible_no_changes', [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ])
        ->shouldBeCalled();
    // An initially visible field, with a submitted region change.
    $entity->getComponent('field_start_visible_change_region')
        ->willReturn([
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ]);
    $field_values['field_start_visible_change_region'] = [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'hidden',
    ];
    $entity->removeComponent('field_start_visible_change_region')
        ->will(function (array $args) use ($entity) {
        // On subsequent calls, getComponent() will return an empty array.
        $entity->getComponent($args[0])
            ->willReturn([]);
    })
        ->shouldBeCalled();
    // A field that is flagged for plugin settings update on the second build.
    $entity->getComponent('field_plugin_settings_update')
        ->willReturn([
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ]);
    $field_values['field_plugin_settings_update'] = [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
        'settings_edit_form' => [
            'third_party_settings' => [
                'foo' => 'bar',
            ],
        ],
    ];
    $entity->setComponent('field_plugin_settings_update', [
        'weight' => 0,
        'type' => 'textfield',
        'region' => 'content',
    ])
        ->will(function (array $args) use ($entity) {
        // On subsequent calls, getComponent() will return the newly set values.
        $entity->getComponent($args[0])
            ->willReturn($args[1]);
        $args[1] += [
            'settings' => [],
            'third_party_settings' => [
                'foo' => 'bar',
            ],
        ];
        $entity->setComponent($args[0], $args[1])
            ->shouldBeCalled();
    })
        ->shouldBeCalled();
    $form_object = new EntityViewDisplayEditForm($this->container
        ->get('plugin.manager.field.field_type'), $this->container
        ->get('plugin.manager.field.formatter'), $this->container
        ->get('entity_display.repository'), $this->container
        ->get('entity_field.manager'));
    $form_object->setEntity($entity->reveal());
    $form = [
        '#fields' => array_keys($field_values),
        '#extra' => [],
    ];
    $form_state = new FormState();
    $form_state->setValues([
        'fields' => $field_values,
    ]);
    $form_state->setProcessInput();
    $form_object->buildEntity($form, $form_state);
    $form_state->setSubmitted();
    // Flag one field for updating plugin settings.
    $form_state->set('plugin_settings_update', 'field_plugin_settings_update');
    // During form submission, buildEntity() will be called twice. Simulate that
    // here to prove copyFormValuesToEntity() is idempotent.
    $form_object->buildEntity($form, $form_state);
}

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