Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / DefaultsSectionStorageTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
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;
17
18 /**
19  * @coversDefaultClass \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage
20  *
21  * @group layout_builder
22  */
23 class DefaultsSectionStorageTest extends UnitTestCase {
24
25   /**
26    * The plugin.
27    *
28    * @var \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage
29    */
30   protected $plugin;
31
32   /**
33    * The entity manager.
34    *
35    * @var \Drupal\Core\Entity\EntityManagerInterface
36    */
37   protected $entityTypeManager;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class);
46     $entity_type_bundle_info = $this->prophesize(EntityTypeBundleInfoInterface::class);
47     $sample_entity_generator = $this->prophesize(LayoutBuilderSampleEntityGenerator::class);
48
49     $definition = new SectionStorageDefinition([
50       'id' => 'defaults',
51       'class' => DefaultsSectionStorage::class,
52     ]);
53     $this->plugin = new DefaultsSectionStorage([], '', $definition, $this->entityTypeManager->reveal(), $entity_type_bundle_info->reveal(), $sample_entity_generator->reveal());
54   }
55
56   /**
57    * @covers ::extractIdFromRoute
58    *
59    * @dataProvider providerTestExtractIdFromRoute
60    */
61   public function testExtractIdFromRoute($expected, $value, array $defaults) {
62     $result = $this->plugin->extractIdFromRoute($value, [], 'the_parameter_name', $defaults);
63     $this->assertSame($expected, $result);
64   }
65
66   /**
67    * Provides data for ::testExtractIdFromRoute().
68    */
69   public function providerTestExtractIdFromRoute() {
70     $data = [];
71     $data['with value'] = [
72       'foo.bar.baz',
73       'foo.bar.baz',
74       [],
75     ];
76     $data['empty value, without bundle'] = [
77       'my_entity_type.bundle_name.default',
78       '',
79       [
80         'entity_type_id' => 'my_entity_type',
81         'view_mode_name' => 'default',
82         'bundle_key' => 'my_bundle',
83         'my_bundle' => 'bundle_name',
84       ],
85     ];
86     $data['empty value, with bundle'] = [
87       'my_entity_type.bundle_name.default',
88       '',
89       [
90         'entity_type_id' => 'my_entity_type',
91         'view_mode_name' => 'default',
92         'bundle' => 'bundle_name',
93       ],
94     ];
95     $data['without value, empty defaults'] = [
96       NULL,
97       '',
98       [],
99     ];
100     return $data;
101   }
102
103   /**
104    * @covers ::getSectionListFromId
105    *
106    * @dataProvider providerTestGetSectionListFromId
107    */
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');
112
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());
115     }
116     else {
117       $this->entityTypeManager->getDefinition('entity_view_display')->shouldNotBeCalled();
118       $this->entityTypeManager->getStorage('entity_view_display')->shouldNotBeCalled();
119     }
120
121     if (!$success) {
122       $this->setExpectedException(\InvalidArgumentException::class);
123     }
124
125     $result = $this->plugin->getSectionListFromId($value);
126     if ($success) {
127       $this->assertEquals('the_return_value', $result);
128     }
129   }
130
131   /**
132    * Provides data for ::testGetSectionListFromId().
133    */
134   public function providerTestGetSectionListFromId() {
135     $data = [];
136     $data['with value'] = [
137       TRUE,
138       'foo.bar.baz',
139       'foo.bar.baz',
140     ];
141     $data['without value, empty defaults'] = [
142       FALSE,
143       NULL,
144       '',
145     ];
146     return $data;
147   }
148
149   /**
150    * @covers ::getSectionListFromId
151    */
152   public function testGetSectionListFromIdCreate() {
153     $expected = 'the_return_value';
154     $value = 'foo.bar.baz';
155     $expected_create_values = [
156       'targetEntityType' => 'foo',
157       'bundle' => 'bar',
158       'mode' => 'baz',
159       'status' => TRUE,
160     ];
161     $entity_storage = $this->prophesize(EntityStorageInterface::class);
162     $entity_storage->load($value)->willReturn(NULL);
163     $entity_storage->create($expected_create_values)->willReturn($expected);
164
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());
167
168     $result = $this->plugin->getSectionListFromId($value);
169     $this->assertSame($expected, $result);
170   }
171
172   /**
173    * @covers ::buildRoutes
174    * @covers ::getEntityTypes
175    */
176   public function testBuildRoutes() {
177     $entity_types = [];
178
179     $not_fieldable = $this->prophesize(EntityTypeInterface::class);
180     $not_fieldable->entityClassImplements(FieldableEntityInterface::class)->willReturn(FALSE);
181     $entity_types['not_fieldable'] = $not_fieldable->reveal();
182
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();
187
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();
193
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();
199
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();
207
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);
214
215     $expected = [
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}',
220         [
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',
228         ],
229         [
230           '_field_ui_view_mode_access' => 'administer with_bundle_key display',
231           '_has_layout_section' => 'true',
232         ],
233         [
234           'parameters' => [
235             'section_storage' => ['layout_builder_tempstore' => TRUE],
236           ],
237           '_layout_builder' => TRUE,
238           '_admin_route' => FALSE,
239         ]
240       ),
241       'layout_builder.defaults.with_bundle_key.save' => new Route(
242         '/admin/entity/whatever/display-layout/{view_mode_name}/save',
243         [
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',
249         ],
250         [
251           '_field_ui_view_mode_access' => 'administer with_bundle_key display',
252           '_has_layout_section' => 'true',
253         ],
254         [
255           'parameters' => [
256             'section_storage' => ['layout_builder_tempstore' => TRUE],
257           ],
258           '_layout_builder' => TRUE,
259           '_admin_route' => FALSE,
260         ]
261       ),
262       'layout_builder.defaults.with_bundle_key.cancel' => new Route(
263         '/admin/entity/whatever/display-layout/{view_mode_name}/cancel',
264         [
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',
270         ],
271         [
272           '_field_ui_view_mode_access' => 'administer with_bundle_key display',
273           '_has_layout_section' => 'true',
274         ],
275         [
276           'parameters' => [
277             'section_storage' => ['layout_builder_tempstore' => TRUE],
278           ],
279           '_layout_builder' => TRUE,
280           '_admin_route' => FALSE,
281         ]
282       ),
283       'layout_builder.defaults.with_bundle_parameter.view' => new Route(
284         '/admin/entity/{bundle}/display-layout/{view_mode_name}',
285         [
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',
292         ],
293         [
294           '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
295           '_has_layout_section' => 'true',
296         ],
297         [
298           'parameters' => [
299             'section_storage' => ['layout_builder_tempstore' => TRUE],
300           ],
301           '_layout_builder' => TRUE,
302           '_admin_route' => FALSE,
303         ]
304       ),
305       'layout_builder.defaults.with_bundle_parameter.save' => new Route(
306         '/admin/entity/{bundle}/display-layout/{view_mode_name}/save',
307         [
308           'entity_type_id' => 'with_bundle_parameter',
309           'section_storage_type' => 'defaults',
310           'section_storage' => '',
311           '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::saveLayout',
312         ],
313         [
314           '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
315           '_has_layout_section' => 'true',
316         ],
317         [
318           'parameters' => [
319             'section_storage' => ['layout_builder_tempstore' => TRUE],
320           ],
321           '_layout_builder' => TRUE,
322           '_admin_route' => FALSE,
323         ]
324       ),
325       'layout_builder.defaults.with_bundle_parameter.cancel' => new Route(
326         '/admin/entity/{bundle}/display-layout/{view_mode_name}/cancel',
327         [
328           'entity_type_id' => 'with_bundle_parameter',
329           'section_storage_type' => 'defaults',
330           'section_storage' => '',
331           '_controller' => '\Drupal\layout_builder\Controller\LayoutBuilderController::cancelLayout',
332         ],
333         [
334           '_field_ui_view_mode_access' => 'administer with_bundle_parameter display',
335           '_has_layout_section' => 'true',
336         ],
337         [
338           'parameters' => [
339             'section_storage' => ['layout_builder_tempstore' => TRUE],
340           ],
341           '_layout_builder' => TRUE,
342           '_admin_route' => FALSE,
343         ]
344       ),
345     ];
346
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}'));
350
351     $this->plugin->buildRoutes($collection);
352     $this->assertEquals($expected, $collection->all());
353     $this->assertSame(array_keys($expected), array_keys($collection->all()));
354   }
355
356 }