Upgraded drupal core with security updates
[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
67     // An initially visible field, with a submitted region change.
68     $entity->getComponent('field_start_visible_change_region')
69       ->willReturn([
70         'weight' => 0,
71         'type' => 'textfield',
72         'region' => 'content',
73       ]);
74     $field_values['field_start_visible_change_region'] = [
75       'weight' => 0,
76       'type' => 'textfield',
77       'region' => 'hidden',
78     ];
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([]);
83       })
84       ->shouldBeCalled();
85
86     // A field that is flagged for plugin settings update on the second build.
87     $entity->getComponent('field_plugin_settings_update')
88       ->willReturn([
89         'weight' => 0,
90         'type' => 'textfield',
91         'region' => 'content',
92       ]);
93     $field_values['field_plugin_settings_update'] = [
94       'weight' => 0,
95       'type' => 'textfield',
96       'region' => 'content',
97       'settings_edit_form' => [
98         'third_party_settings' => [
99           'foo' => 'bar',
100         ],
101       ],
102     ];
103     $entity
104       ->setComponent('field_plugin_settings_update', [
105         'weight' => 0,
106         'type' => 'textfield',
107         'region' => 'content',
108       ])
109       ->will(function ($args) {
110         // On subsequent calls, getComponent() will return the newly set values.
111         $this->getComponent($args[0])->willReturn($args[1]);
112         $args[1] += [
113           'settings' => [],
114           'third_party_settings' => [
115             'foo' => 'bar',
116           ],
117         ];
118         $this->setComponent($args[0], $args[1])->shouldBeCalled();
119       })
120       ->shouldBeCalled();
121
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());
125
126     $form = [
127       '#fields' => array_keys($field_values),
128       '#extra' => [],
129     ];
130     $form_state = new FormState();
131     $form_state->setValues(['fields' => $field_values]);
132     $form_state->setProcessInput();
133
134     $form_object->buildEntity($form, $form_state);
135     $form_state->setSubmitted();
136
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);
142   }
143
144 }