3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Block\BlockManagerInterface;
9 use Drupal\Core\Block\BlockPluginInterface;
10 use Drupal\Core\Cache\Cache;
11 use Drupal\Core\DependencyInjection\ContainerBuilder;
12 use Drupal\Core\Layout\LayoutDefinition;
13 use Drupal\Core\Layout\LayoutInterface;
14 use Drupal\Core\Layout\LayoutPluginManagerInterface;
15 use Drupal\Core\Plugin\Context\ContextHandlerInterface;
16 use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
17 use Drupal\Core\Plugin\ContextAwarePluginInterface;
18 use Drupal\Core\Session\AccountInterface;
19 use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
20 use Drupal\layout_builder\Section;
21 use Drupal\layout_builder\SectionComponent;
22 use Drupal\Tests\UnitTestCase;
23 use Prophecy\Argument;
26 * @coversDefaultClass \Drupal\layout_builder\Section
27 * @group layout_builder
29 class SectionRenderTest extends UnitTestCase {
34 * @var \Drupal\Core\Session\AccountInterface
39 * The block plugin manager.
41 * @var \Drupal\Core\Block\BlockManagerInterface
43 protected $blockManager;
46 * The plugin context handler.
48 * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
50 protected $contextHandler;
53 * The context manager service.
55 * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
57 protected $contextRepository;
60 * The event dispatcher.
62 * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
64 protected $eventDispatcher;
69 protected function setUp() {
72 $layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
73 $this->blockManager = $this->prophesize(BlockManagerInterface::class);
74 $this->contextHandler = $this->prophesize(ContextHandlerInterface::class);
75 $this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);
76 // @todo Refactor this into some better tests in https://www.drupal.org/node/2942605.
77 $this->eventDispatcher = (new \ReflectionClass(ContainerAwareEventDispatcher::class))->newInstanceWithoutConstructor();
79 $this->account = $this->prophesize(AccountInterface::class);
80 $subscriber = new BlockComponentRenderArray($this->account->reveal());
81 $this->eventDispatcher->addSubscriber($subscriber);
83 $layout = $this->prophesize(LayoutInterface::class);
84 $layout->getPluginDefinition()->willReturn(new LayoutDefinition([]));
85 $layout->build(Argument::type('array'))->willReturnArgument(0);
86 $layout_plugin_manager->createInstance('layout_onecol', [])->willReturn($layout->reveal());
88 $container = new ContainerBuilder();
89 $container->set('current_user', $this->account->reveal());
90 $container->set('plugin.manager.block', $this->blockManager->reveal());
91 $container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
92 $container->set('context.handler', $this->contextHandler->reveal());
93 $container->set('context.repository', $this->contextRepository->reveal());
94 $container->set('event_dispatcher', $this->eventDispatcher);
95 \Drupal::setContainer($container);
99 * @covers ::toRenderArray
101 public function testToRenderArray() {
102 $block_content = ['#markup' => 'The block content.'];
106 '#configuration' => [],
107 '#plugin_id' => 'block_plugin_id',
108 '#base_plugin_id' => 'block_plugin_id',
109 '#derivative_plugin_id' => NULL,
110 'content' => $block_content,
118 $block = $this->prophesize(BlockPluginInterface::class);
119 $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
121 $access_result = AccessResult::allowed();
122 $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
123 $block->build()->willReturn($block_content);
124 $block->getCacheContexts()->willReturn([]);
125 $block->getCacheTags()->willReturn([]);
126 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
127 $block->getPluginId()->willReturn('block_plugin_id');
128 $block->getBaseId()->willReturn('block_plugin_id');
129 $block->getDerivativeId()->willReturn(NULL);
130 $block->getConfiguration()->willReturn([]);
133 new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
137 'some_uuid' => $render_array,
140 $result = (new Section('layout_onecol', [], $section))->toRenderArray();
141 $this->assertEquals($expected, $result);
145 * @covers ::toRenderArray
147 public function testToRenderArrayAccessDenied() {
148 $block = $this->prophesize(BlockPluginInterface::class);
149 $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
151 $access_result = AccessResult::forbidden();
152 $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
153 $block->build()->shouldNotBeCalled();
154 $block->getCacheContexts()->willReturn([]);
155 $block->getCacheTags()->willReturn([]);
156 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
159 new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
172 $result = (new Section('layout_onecol', [], $section))->toRenderArray();
173 $this->assertEquals($expected, $result);
177 * @covers ::toRenderArray
179 public function testToRenderArrayPreview() {
180 $block_content = ['#markup' => 'The block content.'];
184 '#configuration' => [],
185 '#plugin_id' => 'block_plugin_id',
186 '#base_plugin_id' => 'block_plugin_id',
187 '#derivative_plugin_id' => NULL,
188 'content' => $block_content,
195 $block = $this->prophesize(BlockPluginInterface::class);
196 $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
198 $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
199 $block->build()->willReturn($block_content);
200 $block->getCacheContexts()->willReturn([]);
201 $block->getCacheTags()->willReturn([]);
202 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
203 $block->getConfiguration()->willReturn([]);
204 $block->getPluginId()->willReturn('block_plugin_id');
205 $block->getBaseId()->willReturn('block_plugin_id');
206 $block->getDerivativeId()->willReturn(NULL);
209 new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
213 'some_uuid' => $render_array,
216 $result = (new Section('layout_onecol', [], $section))->toRenderArray([], TRUE);
217 $this->assertEquals($expected, $result);
221 * @covers ::toRenderArray
223 public function testToRenderArrayEmpty() {
226 $result = (new Section('layout_onecol', [], $section))->toRenderArray();
227 $this->assertEquals($expected, $result);
231 * @covers ::toRenderArray
233 public function testContextAwareBlock() {
237 '#configuration' => [],
238 '#plugin_id' => 'block_plugin_id',
239 '#base_plugin_id' => 'block_plugin_id',
240 '#derivative_plugin_id' => NULL,
249 $block = $this->prophesize(BlockPluginInterface::class)->willImplement(ContextAwarePluginInterface::class);
250 $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
252 $access_result = AccessResult::allowed();
253 $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
254 $block->build()->willReturn([]);
255 $block->getCacheContexts()->willReturn([]);
256 $block->getCacheTags()->willReturn([]);
257 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
258 $block->getContextMapping()->willReturn([]);
259 $block->getPluginId()->willReturn('block_plugin_id');
260 $block->getBaseId()->willReturn('block_plugin_id');
261 $block->getDerivativeId()->willReturn(NULL);
262 $block->getConfiguration()->willReturn([]);
265 new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
269 'some_uuid' => $render_array,
272 $result = (new Section('layout_onecol', [], $section))->toRenderArray();
273 $this->assertEquals($expected, $result);
277 * @covers ::toRenderArray
279 public function testToRenderArrayMissingPluginId() {
280 $this->setExpectedException(PluginException::class, 'No plugin ID specified for component with "some_uuid" UUID');
281 (new Section('layout_onecol', [], [new SectionComponent('some_uuid', 'content')]))->toRenderArray();