Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / field_layout / tests / src / Unit / FieldLayoutBuilderTest.php
1 <?php
2
3 namespace Drupal\Tests\field_layout\Unit;
4
5 use Drupal\Core\Entity\EntityFieldManagerInterface;
6 use Drupal\Core\Field\FieldDefinitionInterface;
7 use Drupal\field_layout\Display\EntityDisplayWithLayoutInterface;
8 use Drupal\field_layout\FieldLayoutBuilder;
9 use Drupal\Core\Layout\LayoutPluginManagerInterface;
10 use Drupal\Core\Layout\LayoutDefault;
11 use Drupal\Core\Layout\LayoutDefinition;
12 use Drupal\Tests\UnitTestCase;
13 use Prophecy\Argument;
14
15 /**
16  * @coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder
17  * @group field_layout
18  */
19 class FieldLayoutBuilderTest extends UnitTestCase {
20
21   /**
22    * @var \Drupal\Core\Layout\LayoutPluginManager|\Prophecy\Prophecy\ProphecyInterface
23    */
24   protected $layoutPluginManager;
25
26   /**
27    * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
28    */
29   protected $entityFieldManager;
30
31   /**
32    * @var \Drupal\field_layout\FieldLayoutBuilder
33    */
34   protected $fieldLayoutBuilder;
35
36   /**
37    * @var \Drupal\Core\Layout\LayoutInterface
38    */
39   protected $layoutPlugin;
40
41   /**
42    * @var \Drupal\Core\Layout\LayoutDefinition
43    */
44   protected $pluginDefinition;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->pluginDefinition = new LayoutDefinition([
53       'library' => 'field_layout/drupal.layout.twocol',
54       'theme_hook' => 'layout__twocol',
55       'regions' => [
56         'left' => [
57           'label' => 'Left',
58         ],
59         'right' => [
60           'label' => 'Right',
61         ],
62       ],
63     ]);
64     $this->layoutPlugin = new LayoutDefault([], 'two_column', $this->pluginDefinition);
65
66     $this->layoutPluginManager = $this->prophesize(LayoutPluginManagerInterface::class);
67     $this->layoutPluginManager->getDefinition('unknown', FALSE)->willReturn(NULL);
68     $this->layoutPluginManager->getDefinition('two_column', FALSE)->willReturn($this->pluginDefinition);
69
70     $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
71
72     $this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal(), $this->entityFieldManager->reveal());
73   }
74
75   /**
76    * @covers ::buildView
77    * @covers ::getFields
78    */
79   public function testBuildView() {
80     $definitions = [];
81     $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
82     $non_configurable_field_definition->isDisplayConfigurable('view')->willReturn(FALSE);
83     $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
84     $definitions['non_configurable_field_with_extra_field'] = $non_configurable_field_definition->reveal();
85     $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
86     $extra_fields = [];
87     $extra_fields['non_configurable_field_with_extra_field'] = [
88       'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
89     ];
90     $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn($extra_fields);
91
92     $build = [
93       'test1' => [
94         '#markup' => 'Test1',
95       ],
96       'test2' => [
97         '#markup' => 'Test2',
98       ],
99       'non_configurable_field' => [
100         '#markup' => 'Non-configurable',
101       ],
102       'non_configurable_field_with_extra_field' => [
103         '#markup' => 'Non-configurable with extra field',
104       ],
105     ];
106
107     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
108     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
109     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
110     $display->getLayout()->willReturn($this->layoutPlugin);
111     $display->getLayoutId()->willReturn('two_column');
112     $display->getLayoutSettings()->willReturn([]);
113     $display->getComponents()->willReturn([
114       'test1' => [
115         'region' => 'right',
116       ],
117       'test2' => [
118         'region' => 'unknown_region',
119       ],
120       'non_configurable_field' => [
121         'region' => 'left',
122       ],
123       'non_configurable_field_with_extra_field' => [
124         'region' => 'left',
125       ],
126     ]);
127
128     $expected = [
129       'test2' => [
130         '#markup' => 'Test2',
131       ],
132       'non_configurable_field' => [
133         '#markup' => 'Non-configurable',
134       ],
135       '_field_layout' => [
136         'left' => [
137           'non_configurable_field_with_extra_field' => [
138             '#markup' => 'Non-configurable with extra field',
139           ],
140         ],
141         'right' => [
142           'test1' => [
143             '#markup' => 'Test1',
144           ],
145         ],
146         '#settings' => [],
147         '#layout' => $this->pluginDefinition,
148         '#theme' => 'layout__twocol',
149         '#attached' => [
150           'library' => [
151             'field_layout/drupal.layout.twocol',
152           ],
153         ],
154       ],
155     ];
156     $this->fieldLayoutBuilder->buildView($build, $display->reveal());
157     $this->assertEquals($expected, $build);
158     $this->assertSame($expected, $build);
159   }
160
161   /**
162    * @covers ::buildForm
163    * @covers ::getFields
164    */
165   public function testBuildForm() {
166     $definitions = [];
167     $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
168     $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
169     $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
170     $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
171     $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
172
173     $build = [
174       'test1' => [
175         '#markup' => 'Test1',
176       ],
177       'test2' => [
178         '#markup' => 'Test2',
179         '#group' => 'existing_group',
180       ],
181       'test3' => [
182         '#markup' => 'Test3',
183       ],
184       'field_layout' => [
185         '#markup' => 'Field created through the UI happens to be named "Layout"',
186       ],
187       'non_configurable_field' => [
188         '#markup' => 'Non-configurable',
189       ],
190     ];
191
192     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
193     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
194     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
195     $display->getLayout()->willReturn($this->layoutPlugin);
196     $display->getLayoutId()->willReturn('two_column');
197     $display->getLayoutSettings()->willReturn([]);
198     $display->getComponents()->willReturn([
199       'test1' => [
200         'region' => 'right',
201       ],
202       'test2' => [
203         'region' => 'left',
204       ],
205       'test3' => [
206         'region' => 'unknown_region',
207       ],
208       'field_layout' => [
209         'region' => 'right',
210       ],
211       'non_configurable_field' => [
212         'region' => 'left',
213       ],
214     ]);
215
216     $expected = [
217       'test1' => [
218         '#markup' => 'Test1',
219         '#group' => 'right',
220       ],
221       'test2' => [
222         '#markup' => 'Test2',
223         '#group' => 'existing_group',
224       ],
225       'test3' => [
226         '#markup' => 'Test3',
227       ],
228       'field_layout' => [
229         '#markup' => 'Field created through the UI happens to be named "Layout"',
230         '#group' => 'right',
231       ],
232       'non_configurable_field' => [
233         '#markup' => 'Non-configurable',
234       ],
235       '_field_layout' => [
236         'left' => [
237           '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
238           '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
239         ],
240         'right' => [
241           '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
242           '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
243         ],
244         '#settings' => [],
245         '#layout' => $this->pluginDefinition,
246         '#theme' => 'layout__twocol',
247         '#attached' => [
248           'library' => [
249             'field_layout/drupal.layout.twocol',
250           ],
251         ],
252       ],
253     ];
254     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
255     $this->assertEquals($expected, $build);
256     $this->assertSame($expected, $build);
257   }
258
259   /**
260    * @covers ::buildForm
261    */
262   public function testBuildFormEmpty() {
263     $definitions = [];
264     $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
265     $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
266     $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
267     $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
268     $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
269
270     $build = [
271       'non_configurable_field' => [
272         '#markup' => 'Non-configurable',
273       ],
274     ];
275
276     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
277     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
278     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
279     $display->getLayout()->willReturn($this->layoutPlugin);
280     $display->getLayoutId()->willReturn('two_column');
281     $display->getLayoutSettings()->willReturn([]);
282     $display->getComponents()->willReturn([
283       'test1' => [
284         'region' => 'right',
285       ],
286       'non_configurable_field' => [
287         'region' => 'left',
288       ],
289     ]);
290
291     $expected = [
292       'non_configurable_field' => [
293         '#markup' => 'Non-configurable',
294       ],
295     ];
296     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
297     $this->assertSame($expected, $build);
298   }
299
300   /**
301    * @covers ::buildForm
302    */
303   public function testBuildFormNoLayout() {
304     $this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
305
306     $build = [
307       'test1' => [
308         '#markup' => 'Test1',
309       ],
310     ];
311
312     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
313     $display->getLayoutId()->willReturn('unknown');
314     $display->getLayoutSettings()->willReturn([]);
315     $display->getComponents()->shouldNotBeCalled();
316
317     $expected = [
318       'test1' => [
319         '#markup' => 'Test1',
320       ],
321     ];
322     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
323     $this->assertSame($expected, $build);
324   }
325
326 }