3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Core\Entity\Display\EntityDisplayInterface;
6 use Drupal\Core\Form\FormState;
7 use Drupal\field_ui\Form\EntityViewDisplayEditForm;
8 use Drupal\KernelTests\KernelTestBase;
11 * @coversDefaultClass \Drupal\field_ui\Form\EntityDisplayFormBase
15 class EntityDisplayFormBaseTest extends KernelTestBase {
20 public static $modules = ['entity_test'];
23 * @covers ::copyFormValuesToEntity
25 public function testCopyFormValuesToEntity() {
27 $entity = $this->prophesize(EntityDisplayInterface::class);
28 $entity->getPluginCollections()->willReturn([]);
29 $entity->getTargetEntityTypeId()->willReturn('entity_test_with_bundle');
30 $entity->getTargetBundle()->willReturn('target_bundle');
32 // An initially hidden field, with a submitted region change.
33 $entity->getComponent('new_field_mismatch_type_visible')->willReturn([]);
34 $field_values['new_field_mismatch_type_visible'] = [
36 'type' => 'textfield',
39 $entity->removeComponent('new_field_mismatch_type_visible')
40 ->will(function ($args) {
41 // On subsequent calls, getComponent() will return an empty array.
42 $this->getComponent($args[0])->willReturn([]);
46 // An initially visible field, with identical submitted values.
47 $entity->getComponent('field_visible_no_changes')
50 'type' => 'textfield',
51 'region' => 'content',
53 $field_values['field_visible_no_changes'] = [
55 'type' => 'textfield',
56 'region' => 'content',
59 ->setComponent('field_visible_no_changes', [
61 'type' => 'textfield',
62 'region' => 'content',
66 // An initially visible field, with a submitted region change.
67 $entity->getComponent('field_start_visible_change_region')
70 'type' => 'textfield',
71 'region' => 'content',
73 $field_values['field_start_visible_change_region'] = [
75 'type' => 'textfield',
78 $entity->removeComponent('field_start_visible_change_region')
79 ->will(function ($args) {
80 // On subsequent calls, getComponent() will return an empty array.
81 $this->getComponent($args[0])->willReturn([]);
85 // A field that is flagged for plugin settings update on the second build.
86 $entity->getComponent('field_plugin_settings_update')
89 'type' => 'textfield',
90 'region' => 'content',
92 $field_values['field_plugin_settings_update'] = [
94 'type' => 'textfield',
95 'region' => 'content',
96 'settings_edit_form' => [
97 'third_party_settings' => [
103 ->setComponent('field_plugin_settings_update', [
105 'type' => 'textfield',
106 'region' => 'content',
108 ->will(function ($args) {
109 // On subsequent calls, getComponent() will return the newly set values.
110 $this->getComponent($args[0])->willReturn($args[1]);
113 'third_party_settings' => [
117 $this->setComponent($args[0], $args[1])->shouldBeCalled();
121 $form_object = new EntityViewDisplayEditForm($this->container->get('plugin.manager.field.field_type'), $this->container->get('plugin.manager.field.formatter'));
122 $form_object->setEntityManager($this->container->get('entity.manager'));
123 $form_object->setEntity($entity->reveal());
126 '#fields' => array_keys($field_values),
129 $form_state = new FormState();
130 $form_state->setValues(['fields' => $field_values]);
131 $form_state->setProcessInput();
133 $form_object->buildEntity($form, $form_state);
134 $form_state->setSubmitted();
136 // Flag one field for updating plugin settings.
137 $form_state->set('plugin_settings_update', 'field_plugin_settings_update');
138 // During form submission, buildEntity() will be called twice. Simulate that
139 // here to prove copyFormValuesToEntity() is idempotent.
140 $form_object->buildEntity($form, $form_state);