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',
67 // An initially visible field, with a submitted region change.
68 $entity->getComponent('field_start_visible_change_region')
71 'type' => 'textfield',
72 'region' => 'content',
74 $field_values['field_start_visible_change_region'] = [
76 'type' => 'textfield',
79 $entity->removeComponent('field_start_visible_change_region')
80 ->will(function ($args) {
81 // On subsequent calls, getComponent() will return an empty array.
82 $this->getComponent($args[0])->willReturn([]);
86 // A field that is flagged for plugin settings update on the second build.
87 $entity->getComponent('field_plugin_settings_update')
90 'type' => 'textfield',
91 'region' => 'content',
93 $field_values['field_plugin_settings_update'] = [
95 'type' => 'textfield',
96 'region' => 'content',
97 'settings_edit_form' => [
98 'third_party_settings' => [
104 ->setComponent('field_plugin_settings_update', [
106 'type' => 'textfield',
107 'region' => 'content',
109 ->will(function ($args) {
110 // On subsequent calls, getComponent() will return the newly set values.
111 $this->getComponent($args[0])->willReturn($args[1]);
114 'third_party_settings' => [
118 $this->setComponent($args[0], $args[1])->shouldBeCalled();
122 $form_object = new EntityViewDisplayEditForm($this->container->get('plugin.manager.field.field_type'), $this->container->get('plugin.manager.field.formatter'));
123 $form_object->setEntityManager($this->container->get('entity.manager'));
124 $form_object->setEntity($entity->reveal());
127 '#fields' => array_keys($field_values),
130 $form_state = new FormState();
131 $form_state->setValues(['fields' => $field_values]);
132 $form_state->setProcessInput();
134 $form_object->buildEntity($form, $form_state);
135 $form_state->setSubmitted();
137 // Flag one field for updating plugin settings.
138 $form_state->set('plugin_settings_update', 'field_plugin_settings_update');
139 // During form submission, buildEntity() will be called twice. Simulate that
140 // here to prove copyFormValuesToEntity() is idempotent.
141 $form_object->buildEntity($form, $form_state);