48a24778c1ab83160649f1298188432f1a0352a3
[yaffs-website] / Unit / SectionComponentTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
5 use Drupal\Core\Block\BlockManagerInterface;
6 use Drupal\Core\Block\BlockPluginInterface;
7 use Drupal\Core\DependencyInjection\ContainerBuilder;
8 use Drupal\Core\Layout\LayoutInterface;
9 use Drupal\Core\Layout\LayoutPluginManagerInterface;
10 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
11 use Drupal\layout_builder\LayoutBuilderEvents;
12 use Drupal\layout_builder\SectionComponent;
13 use Drupal\Tests\UnitTestCase;
14 use Prophecy\Argument;
15 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16
17 /**
18  * @coversDefaultClass \Drupal\layout_builder\SectionComponent
19  * @group layout_builder
20  */
21 class SectionComponentTest extends UnitTestCase {
22
23   /**
24    * @covers ::toRenderArray
25    */
26   public function testToRenderArray() {
27     $existing_block = $this->prophesize(BlockPluginInterface::class);
28     $existing_block->getPluginId()->willReturn('block_plugin_id');
29
30     $block_manager = $this->prophesize(BlockManagerInterface::class);
31     $block_manager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($existing_block->reveal());
32
33     // Imitate an event subscriber by setting a resulting build on the event.
34     $event_dispatcher = $this->prophesize(EventDispatcherInterface::class);
35     $event_dispatcher
36       ->dispatch(LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY, Argument::type(SectionComponentBuildRenderArrayEvent::class))
37       ->shouldBeCalled()
38       ->will(function ($args) {
39         /** @var \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event */
40         $event = $args[1];
41         $event->setBuild(['#markup' => $event->getPlugin()->getPluginId()]);
42         return;
43       });
44
45     $layout_plugin = $this->prophesize(LayoutInterface::class);
46     $layout_plugin->build(Argument::type('array'))->willReturnArgument(0);
47
48     $layout_manager = $this->prophesize(LayoutPluginManagerInterface::class);
49     $layout_manager->createInstance('layout_onecol', [])->willReturn($layout_plugin->reveal());
50
51     $container = new ContainerBuilder();
52     $container->set('plugin.manager.block', $block_manager->reveal());
53     $container->set('event_dispatcher', $event_dispatcher->reveal());
54     $container->set('plugin.manager.core.layout', $layout_manager->reveal());
55     \Drupal::setContainer($container);
56
57     $expected = [
58       '#cache' => [
59         'contexts' => [],
60         'tags' => [],
61         'max-age' => -1,
62       ],
63       '#markup' => 'block_plugin_id',
64     ];
65
66     $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
67     $result = $component->toRenderArray();
68     $this->assertEquals($expected, $result);
69   }
70
71 }