3 namespace Drupal\Tests\layout_builder\Unit;
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;
18 * @coversDefaultClass \Drupal\layout_builder\SectionComponent
19 * @group layout_builder
21 class SectionComponentTest extends UnitTestCase {
24 * @covers ::toRenderArray
26 public function testToRenderArray() {
27 $existing_block = $this->prophesize(BlockPluginInterface::class);
28 $existing_block->getPluginId()->willReturn('block_plugin_id');
30 $block_manager = $this->prophesize(BlockManagerInterface::class);
31 $block_manager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($existing_block->reveal());
33 // Imitate an event subscriber by setting a resulting build on the event.
34 $event_dispatcher = $this->prophesize(EventDispatcherInterface::class);
36 ->dispatch(LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY, Argument::type(SectionComponentBuildRenderArrayEvent::class))
38 ->will(function ($args) {
39 /** @var \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event */
41 $event->setBuild(['#markup' => $event->getPlugin()->getPluginId()]);
45 $layout_plugin = $this->prophesize(LayoutInterface::class);
46 $layout_plugin->build(Argument::type('array'))->willReturnArgument(0);
48 $layout_manager = $this->prophesize(LayoutPluginManagerInterface::class);
49 $layout_manager->createInstance('layout_onecol', [])->willReturn($layout_plugin->reveal());
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);
63 '#markup' => 'block_plugin_id',
66 $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
67 $result = $component->toRenderArray();
68 $this->assertEquals($expected, $result);