3 namespace Drupal\Tests\layout_builder\Unit;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockManagerInterface;
7 use Drupal\Core\Block\BlockPluginInterface;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\DependencyInjection\ContainerBuilder;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
12 use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
13 use Drupal\layout_builder\SectionComponent;
14 use Drupal\Tests\UnitTestCase;
17 * @coversDefaultClass \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray
18 * @group layout_builder
20 class BlockComponentRenderArrayTest extends UnitTestCase {
25 * @var \Drupal\Core\Session\AccountInterface
30 * The block plugin manager.
32 * @var \Drupal\Core\Block\BlockManagerInterface
34 protected $blockManager;
39 protected function setUp() {
42 $this->blockManager = $this->prophesize(BlockManagerInterface::class);
43 $this->account = $this->prophesize(AccountInterface::class);
45 $container = new ContainerBuilder();
46 $container->set('plugin.manager.block', $this->blockManager->reveal());
47 \Drupal::setContainer($container);
51 * @covers ::onBuildRender
53 public function testOnBuildRender() {
54 $block = $this->prophesize(BlockPluginInterface::class);
55 $access_result = AccessResult::allowed();
56 $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
57 $block->getCacheContexts()->willReturn([]);
58 $block->getCacheTags()->willReturn(['test']);
59 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
60 $block->getConfiguration()->willReturn([]);
61 $block->getPluginId()->willReturn('block_plugin_id');
62 $block->getBaseId()->willReturn('block_plugin_id');
63 $block->getDerivativeId()->willReturn(NULL);
65 $block_content = ['#markup' => 'The block content.'];
66 $block->build()->willReturn($block_content);
67 $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
69 $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
72 $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
74 $subscriber = new BlockComponentRenderArray($this->account->reveal());
79 '#configuration' => [],
80 '#plugin_id' => 'block_plugin_id',
81 '#base_plugin_id' => 'block_plugin_id',
82 '#derivative_plugin_id' => NULL,
83 'content' => $block_content,
86 $expected_cache = $expected_build + [
94 $subscriber->onBuildRender($event);
95 $result = $event->getBuild();
96 $this->assertEquals($expected_build, $result);
97 $event->getCacheableMetadata()->applyTo($result);
98 $this->assertEquals($expected_cache, $result);
102 * @covers ::onBuildRender
104 public function testOnBuildRenderDenied() {
105 $block = $this->prophesize(BlockPluginInterface::class);
106 $access_result = AccessResult::forbidden();
107 $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
108 $block->getCacheContexts()->shouldNotBeCalled();
109 $block->getCacheTags()->shouldNotBeCalled();
110 $block->getCacheMaxAge()->shouldNotBeCalled();
111 $block->getConfiguration()->shouldNotBeCalled();
112 $block->getPluginId()->shouldNotBeCalled();
113 $block->getBaseId()->shouldNotBeCalled();
114 $block->getDerivativeId()->shouldNotBeCalled();
116 $block_content = ['#markup' => 'The block content.'];
117 $block->build()->willReturn($block_content);
118 $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
120 $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
123 $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
125 $subscriber = new BlockComponentRenderArray($this->account->reveal());
127 $expected_build = [];
136 $subscriber->onBuildRender($event);
137 $result = $event->getBuild();
138 $this->assertEquals($expected_build, $result);
139 $event->getCacheableMetadata()->applyTo($result);
140 $this->assertEquals($expected_cache, $result);
144 * @covers ::onBuildRender
146 public function testOnBuildRenderInPreview() {
147 $block = $this->prophesize(BlockPluginInterface::class);
148 $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
149 $block->getCacheContexts()->willReturn([]);
150 $block->getCacheTags()->willReturn(['test']);
151 $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
152 $block->getConfiguration()->willReturn([]);
153 $block->getPluginId()->willReturn('block_plugin_id');
154 $block->getBaseId()->willReturn('block_plugin_id');
155 $block->getDerivativeId()->willReturn(NULL);
157 $block_content = ['#markup' => 'The block content.'];
158 $block->build()->willReturn($block_content);
159 $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
161 $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
164 $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
166 $subscriber = new BlockComponentRenderArray($this->account->reveal());
171 '#configuration' => [],
172 '#plugin_id' => 'block_plugin_id',
173 '#base_plugin_id' => 'block_plugin_id',
174 '#derivative_plugin_id' => NULL,
175 'content' => $block_content,
178 $expected_cache = $expected_build + [
186 $subscriber->onBuildRender($event);
187 $result = $event->getBuild();
188 $this->assertEquals($expected_build, $result);
189 $event->getCacheableMetadata()->applyTo($result);
190 $this->assertEquals($expected_cache, $result);
194 * @covers ::onBuildRender
196 public function testOnBuildRenderNoBlock() {
197 $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn(NULL);
199 $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
202 $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
204 $subscriber = new BlockComponentRenderArray($this->account->reveal());
206 $expected_build = [];
215 $subscriber->onBuildRender($event);
216 $result = $event->getBuild();
217 $this->assertEquals($expected_build, $result);
218 $event->getCacheableMetadata()->applyTo($result);
219 $this->assertEquals($expected_cache, $result);