179c63e9f16a61fc7e896e1f96ceb4cac722b60a
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Entity / EntityDisplayFormBaseTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Entity;
4
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;
9
10 /**
11  * @coversDefaultClass \Drupal\field_ui\Form\EntityDisplayFormBase
12  *
13  * @group Entity
14  */
15 class EntityDisplayFormBaseTest extends KernelTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['entity_test'];
21
22   /**
23    * @covers ::copyFormValuesToEntity
24    */
25   public function testCopyFormValuesToEntity() {
26     $field_values = [];
27     $entity = $this->prophesize(EntityDisplayInterface::class);
28     $entity->getPluginCollections()->willReturn([]);
29     $entity->getTargetEntityTypeId()->willReturn('entity_test_with_bundle');
30     $entity->getTargetBundle()->willReturn('target_bundle');
31
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'] = [
35       'weight' => 0,
36       'type' => 'textfield',
37       'region' => 'hidden',
38     ];
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([]);
43       })
44       ->shouldBeCalled();
45
46     // An initially visible field, with identical submitted values.
47     $entity->getComponent('field_visible_no_changes')
48       ->willReturn([
49         'weight' => 0,
50         'type' => 'textfield',
51         'region' => 'content',
52       ]);
53     $field_values['field_visible_no_changes'] = [
54       'weight' => 0,
55       'type' => 'textfield',
56       'region' => 'content',
57     ];
58     $entity
59       ->setComponent('field_visible_no_changes', [
60         'weight' => 0,
61         'type' => 'textfield',
62         'region' => 'content',
63       ])
64       ->shouldBeCalled();
65
66     // An initially visible field, with a submitted region change.
67     $entity->getComponent('field_start_visible_change_region')
68       ->willReturn([
69         'weight' => 0,
70         'type' => 'textfield',
71         'region' => 'content',
72       ]);
73     $field_values['field_start_visible_change_region'] = [
74       'weight' => 0,
75       'type' => 'textfield',
76       'region' => 'hidden',
77     ];
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([]);
82       })
83       ->shouldBeCalled();
84
85     // A field that is flagged for plugin settings update on the second build.
86     $entity->getComponent('field_plugin_settings_update')
87       ->willReturn([
88         'weight' => 0,
89         'type' => 'textfield',
90         'region' => 'content',
91       ]);
92     $field_values['field_plugin_settings_update'] = [
93       'weight' => 0,
94       'type' => 'textfield',
95       'region' => 'content',
96       'settings_edit_form' => [
97         'third_party_settings' => [
98           'foo' => 'bar',
99         ],
100       ],
101     ];
102     $entity
103       ->setComponent('field_plugin_settings_update', [
104         'weight' => 0,
105         'type' => 'textfield',
106         'region' => 'content',
107       ])
108       ->will(function ($args) {
109         // On subsequent calls, getComponent() will return the newly set values.
110         $this->getComponent($args[0])->willReturn($args[1]);
111         $args[1] += [
112           'settings' => [],
113           'third_party_settings' => [
114             'foo' => 'bar',
115           ],
116         ];
117         $this->setComponent($args[0], $args[1])->shouldBeCalled();
118       })
119       ->shouldBeCalled();
120
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());
124
125     $form = [
126       '#fields' => array_keys($field_values),
127       '#extra' => [],
128     ];
129     $form_state = new FormState();
130     $form_state->setValues(['fields' => $field_values]);
131     $form_state->setProcessInput();
132
133     $form_object->buildEntity($form, $form_state);
134     $form_state->setSubmitted();
135
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);
141   }
142
143 }