3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\Core\Entity\EntityStorageInterface;
6 use Drupal\Core\Entity\EntityType;
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
8 use Drupal\Core\Entity\EntityTypeInterface;
9 use Drupal\Core\Entity\EntityTypeManagerInterface;
10 use Drupal\Core\Entity\FieldableEntityInterface;
11 use Drupal\layout_builder\Entity\LayoutBuilderSampleEntityGenerator;
12 use Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage;
13 use Drupal\layout_builder\SectionStorage\SectionStorageDefinition;
14 use Drupal\Tests\UnitTestCase;
15 use Symfony\Component\Routing\Route;
16 use Symfony\Component\Routing\RouteCollection;
19 * @coversDefaultClass \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage
21 * @group layout_builder
23 class DefaultsSectionStorageTest extends UnitTestCase {
28 * @var \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage
35 * @var \Drupal\Core\Entity\EntityManagerInterface
37 protected $entityTypeManager;
42 protected function setUp() {
45 $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
46 $entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
47 $sample_entity_generator = $this->prophesize(LayoutBuilderSampleEntityGenerator::class);
49 $definition = new SectionStorageDefinition([
51 'class' => DefaultsSectionStorage::class,
53 $this->plugin = new DefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal());
57 * @covers ::extractIdFromRoute
59 * @dataProvider providerTestExtractIdFromRoute
61 public function testExtractIdFromRoute($expected, $value, array $defaults) {
62 $result = $this->plugin->extractIdFromRoute($value, [], 'the_parameter_name', $defaults);
63 $this->assertSame($expected, $result);
67 * Provides data for ::testExtractIdFromRoute().
69 public function providerTestExtractIdFromRoute() {
71 $data['with value'] = [
76 $data['empty value, without bundle'] = [
77 'my_entity_type.bundle_name.default',
80 'entity_type_id' => 'my_entity_type',
81 'view_mode_name' => 'default',
82 'bundle_key' => 'my_bundle',
83 'my_bundle' => 'bundle_name',
86 $data['empty value, with bundle'] = [
87 'my_entity_type.bundle_name.default',
90 'entity_type_id' => 'my_entity_type',
91 'view_mode_name' => 'default',
92 'bundle' => 'bundle_name',
95 $data['without value, empty defaults'] = [
104 * @covers ::getSectionListFromId
106 * @dataProvider providerTestGetSectionListFromId
108 public function testGetSectionListFromId($success, $expected_entity_id, $value) {
109 if ($expected_entity_id) {
110 $entity_storage = $this->prophesize(EntityStorageInterface::class);
111 $entity_storage->load($expected_entity_id)->willReturn('the_return_value');
113 $this->entityTypeManager->getDefinition('entity_view_display')->willReturn(new EntityType(['id' => 'entity_view_display']));
114 $this->entityTypeManager->getStorage('entity_view_display')->willReturn($entity_storage->reveal());
117 $this->entityTypeManager->getDefinition('entity_view_display')->shouldNotBeCalled();
118 $this->entityTypeManager->getStorage('entity_view_display')->shouldNotBeCalled();
122 $this->setExpectedException(\InvalidArgumentException::class);
125 $result = $this->plugin->getSectionListFromId($value);
127 $this->assertEquals('the_return_value', $result);
132 * Provides data for ::testGetSectionListFromId().
134 public function providerTestGetSectionListFromId() {
136 $data['with value'] = [
141 $data['without value, empty defaults'] = [
150 * @covers ::getSectionListFromId
152 public function testGetSectionListFromIdCreate() {
153 $expected = 'the_return_value';
154 $value = 'foo.bar.baz';
155 $expected_create_values = [
156 'targetEntityType' => 'foo',
161 $entity_storage = $this->prophesize(EntityStorageInterface::class);
162 $entity_storage->load($value)->willReturn(NULL);
163 $entity_storage->create($expected_create_values)->willReturn($expected);
165 $this->entityTypeManager->getDefinition('entity_view_display')->willReturn(new EntityType(['id' => 'entity_view_display']));
166 $this->entityTypeManager->getStorage('entity_view_display')->willReturn($entity_storage->reveal());
168 $result = $this->plugin->getSectionListFromId($value);
169 $this->assertSame($expected, $result);
173 * @covers ::buildRoutes
174 * @covers ::getEntityTypes
176 public function testBuildRoutes() {
179 $not_fieldable = $this->prophesize(EntityTypeInterface::class);
180 $not_fieldable->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
181 $entity_types['not_fieldable'] = $not_fieldable->reveal();
183 $no_view_builder = $this->prophesize(EntityTypeInterface::class);
184 $no_view_builder->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
185 $no_view_builder->hasViewBuilderClass()->willReturn(FALSE);
186 $entity_types['no_view_builder'] = $no_view_builder->reveal();
188 $no_field_ui_route = $this->prophesize(EntityTypeInterface::class);
189 $no_field_ui_route->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
190 $no_field_ui_route->hasViewBuilderClass()->willReturn(TRUE);
191 $no_field_ui_route->get('field_ui_base_route')->willReturn(NULL);
192 $entity_types['no_field_ui_route'] = $no_field_ui_route->reveal();
194 $unknown_field_ui_route = $this->prophesize(EntityTypeInterface::class);
195 $unknown_field_ui_route->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
196 $unknown_field_ui_route->hasViewBuilderClass()->willReturn(TRUE);
197 $unknown_field_ui_route->get('field_ui_base_route')->willReturn('unknown');
198 $entity_types['unknown_field_ui_route'] = $unknown_field_ui_route->reveal();
200 $with_bundle_key = $this->prophesize(EntityTypeInterface::class);
201 $with_bundle_key->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
202 $with_bundle_key->hasViewBuilderClass()->willReturn(TRUE);
203 $with_bundle_key->get('field_ui_base_route')->willReturn('known');
204 $with_bundle_key->hasKey('bundle')->willReturn(TRUE);
205 $with_bundle_key->getBundleEntityType()->willReturn('my_bundle_type');
206 $entity_types['with_bundle_key'] = $with_bundle_key->reveal();
208 $with_bundle_parameter = $this->prophesize(EntityTypeInterface::class);
209 $with_bundle_parameter->entityClassImplements(FieldableEntityInterface::class)->willReturn(TRUE);
210 $with_bundle_parameter->hasViewBuilderClass()->willReturn(TRUE);
211 $with_bundle_parameter->get('field_ui_base_route')->willReturn('with_bundle');
212 $entity_types['with_bundle_parameter'] = $with_bundle_parameter->reveal();
213 $this->entityTypeManager->getDefinitions()->willReturn($entity_types);
216 'known' => new Route('/admin/entity/whatever', [], [], ['_admin_route' => TRUE]),
217 'with_bundle' => new Route('/admin/entity/{bundle}'),
218 'layout_builder.defaults.with_bundle_key.view' => new Route(
219 '/admin/entity/whatever/display-layout/{view_mode_name}',
221 'entity_type_id' => 'with_bundle_key',
222 'bundle_key' => 'my_bundle_type',
223 'section_storage_type' => 'defaults',
224 'section_storage' => '',
225 'is_rebuilding' => FALSE,
226 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::layout',
227 '_title_callback' => '\Drupal\layout_builder\Controller\LayoutBuilderController::title',
230 '_field_ui_view_mode_access' => 'administer with_bundle_key display',
231 '_has_layout_section' => 'true',
235 'section_storage' => ['layout_builder_tempstore' => TRUE],
237 '_layout_builder' => TRUE,
238 '_admin_route' => FALSE,
241 'layout_builder.defaults.with_bundle_key.save' => new Route(
242 '/admin/entity/whatever/display-layout/{view_mode_name}/save',
244 'entity_type_id' => 'with_bundle_key',
245 'bundle_key' => 'my_bundle_type',
246 'section_storage_type' => 'defaults',
247 'section_storage' => '',
248 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::saveLayout',
251 '_field_ui_view_mode_access' => 'administer with_bundle_key display',
252 '_has_layout_section' => 'true',
256 'section_storage' => ['layout_builder_tempstore' => TRUE],
258 '_layout_builder' => TRUE,
259 '_admin_route' => FALSE,
262 'layout_builder.defaults.with_bundle_key.cancel' => new Route(
263 '/admin/entity/whatever/display-layout/{view_mode_name}/cancel',
265 'entity_type_id' => 'with_bundle_key',
266 'bundle_key' => 'my_bundle_type',
267 'section_storage_type' => 'defaults',
268 'section_storage' => '',
269 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::cancelLayout',
272 '_field_ui_view_mode_access' => 'administer with_bundle_key display',
273 '_has_layout_section' => 'true',
277 'section_storage' => ['layout_builder_tempstore' => TRUE],
279 '_layout_builder' => TRUE,
280 '_admin_route' => FALSE,
283 'layout_builder.defaults.with_bundle_parameter.view' => new Route(
284 '/admin/entity/{bundle}/display-layout/{view_mode_name}',
286 'entity_type_id' => 'with_bundle_parameter',
287 'section_storage_type' => 'defaults',
288 'section_storage' => '',
289 'is_rebuilding' => FALSE,
290 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::layout',
291 '_title_callback' => '\Drupal\layout_builder\Controller\LayoutBuilderController::title',
294 '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
295 '_has_layout_section' => 'true',
299 'section_storage' => ['layout_builder_tempstore' => TRUE],
301 '_layout_builder' => TRUE,
302 '_admin_route' => FALSE,
305 'layout_builder.defaults.with_bundle_parameter.save' => new Route(
306 '/admin/entity/{bundle}/display-layout/{view_mode_name}/save',
308 'entity_type_id' => 'with_bundle_parameter',
309 'section_storage_type' => 'defaults',
310 'section_storage' => '',
311 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::saveLayout',
314 '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
315 '_has_layout_section' => 'true',
319 'section_storage' => ['layout_builder_tempstore' => TRUE],
321 '_layout_builder' => TRUE,
322 '_admin_route' => FALSE,
325 'layout_builder.defaults.with_bundle_parameter.cancel' => new Route(
326 '/admin/entity/{bundle}/display-layout/{view_mode_name}/cancel',
328 'entity_type_id' => 'with_bundle_parameter',
329 'section_storage_type' => 'defaults',
330 'section_storage' => '',
331 '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::cancelLayout',
334 '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
335 '_has_layout_section' => 'true',
339 'section_storage' => ['layout_builder_tempstore' => TRUE],
341 '_layout_builder' => TRUE,
342 '_admin_route' => FALSE,
347 $collection = new RouteCollection();
348 $collection->add('known', new Route('/admin/entity/whatever', [], [], ['_admin_route' => TRUE]));
349 $collection->add('with_bundle', new Route('/admin/entity/{bundle}'));
351 $this->plugin->buildRoutes($collection);
352 $this->assertEquals($expected, $collection->all());
353 $this->assertSame(array_keys($expected), array_keys($collection->all()));