8e6bf956c4bfde3c00b4ee0f36ebb79ff3099cfb
[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       'non_configurable_field' => [
97         '#markup' => 'Non-configurable',
98       ],
99       'non_configurable_field_with_extra_field' => [
100         '#markup' => 'Non-configurable with extra field',
101       ],
102     ];
103
104     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
105     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
106     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
107     $display->getLayout()->willReturn($this->layoutPlugin);
108     $display->getLayoutId()->willReturn('two_column');
109     $display->getLayoutSettings()->willReturn([]);
110     $display->getComponents()->willReturn([
111       'test1' => [
112         'region' => 'right',
113       ],
114       'non_configurable_field' => [
115         'region' => 'left',
116       ],
117       'non_configurable_field_with_extra_field' => [
118         'region' => 'left',
119       ],
120     ]);
121
122     $expected = [
123       'non_configurable_field' => [
124         '#markup' => 'Non-configurable',
125       ],
126       '_field_layout' => [
127         'left' => [
128           'non_configurable_field_with_extra_field' => [
129             '#markup' => 'Non-configurable with extra field',
130           ],
131         ],
132         'right' => [
133           'test1' => [
134             '#markup' => 'Test1',
135           ],
136         ],
137         '#settings' => [],
138         '#layout' => $this->pluginDefinition,
139         '#theme' => 'layout__twocol',
140         '#attached' => [
141           'library' => [
142             'field_layout/drupal.layout.twocol',
143           ],
144         ],
145       ],
146     ];
147     $this->fieldLayoutBuilder->buildView($build, $display->reveal());
148     $this->assertEquals($expected, $build);
149     $this->assertSame($expected, $build);
150   }
151
152   /**
153    * @covers ::buildForm
154    * @covers ::getFields
155    */
156   public function testBuildForm() {
157     $definitions = [];
158     $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
159     $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
160     $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
161     $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
162     $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
163
164     $build = [
165       'test1' => [
166         '#markup' => 'Test1',
167       ],
168       'test2' => [
169         '#markup' => 'Test2',
170         '#group' => 'existing_group',
171       ],
172       'field_layout' => [
173         '#markup' => 'Field created through the UI happens to be named "Layout"',
174       ],
175       'non_configurable_field' => [
176         '#markup' => 'Non-configurable',
177       ],
178     ];
179
180     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
181     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
182     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
183     $display->getLayout()->willReturn($this->layoutPlugin);
184     $display->getLayoutId()->willReturn('two_column');
185     $display->getLayoutSettings()->willReturn([]);
186     $display->getComponents()->willReturn([
187       'test1' => [
188         'region' => 'right',
189       ],
190       'test2' => [
191         'region' => 'left',
192       ],
193       'field_layout' => [
194         'region' => 'right',
195       ],
196       'non_configurable_field' => [
197         'region' => 'left',
198       ],
199     ]);
200
201     $expected = [
202       'test1' => [
203         '#markup' => 'Test1',
204         '#group' => 'right',
205       ],
206       'test2' => [
207         '#markup' => 'Test2',
208         '#group' => 'existing_group',
209       ],
210       'field_layout' => [
211         '#markup' => 'Field created through the UI happens to be named "Layout"',
212         '#group' => 'right',
213       ],
214       'non_configurable_field' => [
215         '#markup' => 'Non-configurable',
216       ],
217       '_field_layout' => [
218         'left' => [
219           '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
220           '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
221         ],
222         'right' => [
223           '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
224           '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
225         ],
226         '#settings' => [],
227         '#layout' => $this->pluginDefinition,
228         '#theme' => 'layout__twocol',
229         '#attached' => [
230           'library' => [
231             'field_layout/drupal.layout.twocol',
232           ],
233         ],
234       ],
235     ];
236     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
237     $this->assertEquals($expected, $build);
238     $this->assertSame($expected, $build);
239   }
240
241   /**
242    * @covers ::buildForm
243    */
244   public function testBuildFormEmpty() {
245     $definitions = [];
246     $non_configurable_field_definition = $this->prophesize(FieldDefinitionInterface::class);
247     $non_configurable_field_definition->isDisplayConfigurable('form')->willReturn(FALSE);
248     $definitions['non_configurable_field'] = $non_configurable_field_definition->reveal();
249     $this->entityFieldManager->getFieldDefinitions('the_entity_type_id', 'the_entity_type_bundle')->willReturn($definitions);
250     $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn([]);
251
252     $build = [
253       'non_configurable_field' => [
254         '#markup' => 'Non-configurable',
255       ],
256     ];
257
258     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
259     $display->getTargetEntityTypeId()->willReturn('the_entity_type_id');
260     $display->getTargetBundle()->willReturn('the_entity_type_bundle');
261     $display->getLayout()->willReturn($this->layoutPlugin);
262     $display->getLayoutId()->willReturn('two_column');
263     $display->getLayoutSettings()->willReturn([]);
264     $display->getComponents()->willReturn([
265       'test1' => [
266         'region' => 'right',
267       ],
268       'non_configurable_field' => [
269         'region' => 'left',
270       ],
271     ]);
272
273     $expected = [
274       'non_configurable_field' => [
275         '#markup' => 'Non-configurable',
276       ],
277     ];
278     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
279     $this->assertSame($expected, $build);
280   }
281
282   /**
283    * @covers ::buildForm
284    */
285   public function testBuildFormNoLayout() {
286     $this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
287
288     $build = [
289       'test1' => [
290         '#markup' => 'Test1',
291       ],
292     ];
293
294     $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
295     $display->getLayoutId()->willReturn('unknown');
296     $display->getLayoutSettings()->willReturn([]);
297     $display->getComponents()->shouldNotBeCalled();
298
299     $expected = [
300       'test1' => [
301         '#markup' => 'Test1',
302       ],
303     ];
304     $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
305     $this->assertSame($expected, $build);
306   }
307
308 }