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);
96 'non_configurable_field' => [
97 '#markup' => 'Non-configurable',
99 'non_configurable_field_with_extra_field' => [
100 '#markup' => 'Non-configurable with extra field',
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([
114 'non_configurable_field' => [
117 'non_configurable_field_with_extra_field' => [
123 'non_configurable_field' => [
124 '#markup' => 'Non-configurable',
128 'non_configurable_field_with_extra_field' => [
129 '#markup' => 'Non-configurable with extra field',
134 '#markup' => 'Test1',
138 '#layout' => $this->pluginDefinition,
139 '#theme' => 'layout__twocol',
142 'field_layout/drupal.layout.twocol',
147 $this->fieldLayoutBuilder->buildView($build, $display->reveal());
148 $this->assertEquals($expected, $build);
149 $this->assertSame($expected, $build);
153 * @covers ::buildForm
154 * @covers ::getFields
156 public function testBuildForm() {
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([]);
166 '#markup' => 'Test1',
169 '#markup' => 'Test2',
170 '#group' => 'existing_group',
173 '#markup' => 'Field created through the UI happens to be named "Layout"',
175 'non_configurable_field' => [
176 '#markup' => 'Non-configurable',
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([
196 'non_configurable_field' => [
203 '#markup' => 'Test1',
207 '#markup' => 'Test2',
208 '#group' => 'existing_group',
211 '#markup' => 'Field created through the UI happens to be named "Layout"',
214 'non_configurable_field' => [
215 '#markup' => 'Non-configurable',
219 '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
220 '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
223 '#process' => ['\Drupal\Core\Render\Element\RenderElement::processGroup'],
224 '#pre_render' => ['\Drupal\Core\Render\Element\RenderElement::preRenderGroup'],
227 '#layout' => $this->pluginDefinition,
228 '#theme' => 'layout__twocol',
231 'field_layout/drupal.layout.twocol',
236 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
237 $this->assertEquals($expected, $build);
238 $this->assertSame($expected, $build);
242 * @covers ::buildForm
244 public function testBuildFormEmpty() {
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([]);
253 'non_configurable_field' => [
254 '#markup' => 'Non-configurable',
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([
268 'non_configurable_field' => [
274 'non_configurable_field' => [
275 '#markup' => 'Non-configurable',
278 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
279 $this->assertSame($expected, $build);
283 * @covers ::buildForm
285 public function testBuildFormNoLayout() {
286 $this->entityFieldManager->getFieldDefinitions(Argument::any(), Argument::any())->shouldNotBeCalled();
290 '#markup' => 'Test1',
294 $display = $this->prophesize(EntityDisplayWithLayoutInterface::class);
295 $display->getLayoutId()->willReturn('unknown');
296 $display->getLayoutSettings()->willReturn([]);
297 $display->getComponents()->shouldNotBeCalled();
301 '#markup' => 'Test1',
304 $this->fieldLayoutBuilder->buildForm($build, $display->reveal());
305 $this->assertSame($expected, $build);