3 namespace Drupal\Tests\field_layout\Unit;
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;
16 * @coversDefaultClass \Drupal\field_layout\FieldLayoutBuilder
19 class FieldLayoutBuilderTest extends UnitTestCase {
22 * @var \Drupal\Core\Layout\LayoutPluginManager|\Prophecy\Prophecy\ProphecyInterface
24 protected $layoutPluginManager;
27 * @var \Drupal\Core\Entity\EntityFieldManagerInterface|\Prophecy\Prophecy\ProphecyInterface
29 protected $entityFieldManager;
32 * @var \Drupal\field_layout\FieldLayoutBuilder
34 protected $fieldLayoutBuilder;
37 * @var \Drupal\Core\Layout\LayoutInterface
39 protected $layoutPlugin;
42 * @var \Drupal\Core\Layout\LayoutDefinition
44 protected $pluginDefinition;
49 protected function setUp() {
52 $this->pluginDefinition = new LayoutDefinition([
53 'library' => 'field_layout/drupal.layout.twocol',
54 'theme_hook' => 'layout__twocol',
64 $this->layoutPlugin = new LayoutDefault([], 'two_column', $this->pluginDefinition);
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);
70 $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class);
72 $this->fieldLayoutBuilder = new FieldLayoutBuilder($this->layoutPluginManager->reveal(), $this->entityFieldManager->reveal());
79 public function testBuildView() {
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);
87 $extra_fields['non_configurable_field_with_extra_field'] = [
88 'label' => 'This non-configurable field is also defined in hook_entity_extra_field_info()',
90 $this->entityFieldManager->getExtraFields('the_entity_type_id', 'the_entity_type_bundle')->willReturn($extra_fields);
99 'non_configurable_field' => [
100 '#markup' => 'Non-configurable',
102 'non_configurable_field_with_extra_field' => [
103 '#markup' => 'Non-configurable with extra field',
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([
118 'region' => 'unknown_region',
120 'non_configurable_field' => [
123 'non_configurable_field_with_extra_field' => [
130 '#markup' => 'Test2',
132 'non_configurable_field' => [
133 '#markup' => 'Non-configurable',
137 'non_configurable_field_with_extra_field' => [
138 '#markup' => 'Non-configurable with extra field',
143 '#markup' => 'Test1',
147 '#layout' => $this->pluginDefinition,
148 '#theme' => 'layout__twocol',
151 'field_layout/drupal.layout.twocol',
156 $this->fieldLayoutBuilder->buildView($build, $display->reveal());
157 $this->assertEquals($expected, $build);
158 $this->assertSame($expected, $build);
162 * @covers ::buildForm
163 * @covers ::getFields
165 public function testBuildForm() {
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([]);
175 '#markup' => 'Test1',
178 '#markup' => 'Test2',
179 '#group' => 'existing_group',
182 '#markup' => 'Test3',
185 '#markup' => 'Field created through the UI happens to be named "Layout"',
187 'non_configurable_field' => [
188 '#markup' => 'Non-configurable',
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([
206 'region' => 'unknown_region',
211 'non_configurable_field' => [
218 '#markup' => 'Test1',
222 '#markup' => 'Test2',
223 '#group' => 'existing_group',
226 '#markup' => 'Test3',
229 '#markup' => 'Field created through the UI happens to be named "Layout"',
232 'non_configurable_field' => [
233 '#markup' => 'Non-configurable',
237 '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
238 '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
241 '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
242 '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
245 '#layout' => $this->pluginDefinition,
246 '#theme' => 'layout__twocol',
249 'field_layout/drupal.layout.twocol',
254 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
255 $this->assertEquals($expected, $build);
256 $this->assertSame($expected, $build);
260 * @covers ::buildForm
262 public function testBuildFormEmpty() {
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([]);
271 'non_configurable_field' => [
272 '#markup' => 'Non-configurable',
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([
286 'non_configurable_field' => [
292 'non_configurable_field' => [
293 '#markup' => 'Non-configurable',
296 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
297 $this->assertSame($expected, $build);
301 * @covers ::buildForm
303 public function testBuildFormNoLayout() {
304 $this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
308 '#markup' => 'Test1',
312 $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
313 $display->getLayoutId()->willReturn('unknown');
314 $display->getLayoutSettings()->willReturn([]);
315 $display->getComponents()->shouldNotBeCalled();
319 '#markup' => 'Test1',
322 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
323 $this->assertSame($expected, $build);