Newer
Older
Alex Bronstein
committed
<?php
namespace Drupal\Tests\layout_builder\Unit;
Angie Byron
committed
use Drupal\block_content\Access\RefinableDependentAccessInterface;
use Drupal\Component\Plugin\Context\ContextInterface;
Alex Bronstein
committed
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockManagerInterface;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder;
Angie Byron
committed
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
Alex Bronstein
committed
use Drupal\Core\Session\AccountInterface;
Angie Byron
committed
use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
Alex Bronstein
committed
use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
use Drupal\layout_builder\SectionComponent;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray
* @group layout_builder
*/
class BlockComponentRenderArrayTest extends UnitTestCase {
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $account;
/**
* The block plugin manager.
*
* @var \Drupal\Core\Block\BlockManagerInterface
*/
protected $blockManager;
Angie Byron
committed
/**
* Dataprovider for test functions that should test block types.
*/
public function providerBlockTypes() {
return [
[TRUE],
[FALSE],
];
}
Alex Bronstein
committed
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->blockManager = $this->prophesize(BlockManagerInterface::class);
$this->account = $this->prophesize(AccountInterface::class);
$container = new ContainerBuilder();
$container->set('plugin.manager.block', $this->blockManager->reveal());
Angie Byron
committed
$container->set('context.handler', $this->prophesize(ContextHandlerInterface::class));
Alex Bronstein
committed
\Drupal::setContainer($container);
}
/**
* @covers ::onBuildRender
Angie Byron
committed
*
* @dataProvider providerBlockTypes
Alex Bronstein
committed
*/
Angie Byron
committed
public function testOnBuildRender($refinable_dependent_access) {
$contexts = [];
if ($refinable_dependent_access) {
$block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class);
$layout_entity = $this->prophesize(EntityInterface::class);
$layout_entity = $layout_entity->reveal();
$context = $this->prophesize(ContextInterface::class);
$context->getContextValue()->willReturn($layout_entity);
$contexts['layout_builder.entity'] = $context->reveal();
$block->setAccessDependency($layout_entity)->shouldBeCalled();
}
else {
$block = $this->prophesize(BlockPluginInterface::class);
}
Alex Bronstein
committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
$access_result = AccessResult::allowed();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn(['test']);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$block->getConfiguration()->willReturn([]);
$block->getPluginId()->willReturn('block_plugin_id');
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$block_content = ['#markup' => 'The block content.'];
$block->build()->willReturn($block_content);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
$in_preview = FALSE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
$subscriber = new BlockComponentRenderArray($this->account->reveal());
$expected_build = [
'#theme' => 'block',
'#weight' => 0,
'#configuration' => [],
'#plugin_id' => 'block_plugin_id',
'#base_plugin_id' => 'block_plugin_id',
'#derivative_plugin_id' => NULL,
'content' => $block_content,
];
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'max-age' => -1,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
Angie Byron
committed
*
* @dataProvider providerBlockTypes
Alex Bronstein
committed
*/
Angie Byron
committed
public function testOnBuildRenderDenied($refinable_dependent_access) {
$contexts = [];
if ($refinable_dependent_access) {
$block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class);
$layout_entity = $this->prophesize(EntityInterface::class);
$layout_entity = $layout_entity->reveal();
$context = $this->prophesize(ContextInterface::class);
$context->getContextValue()->willReturn($layout_entity);
$contexts['layout_builder.entity'] = $context->reveal();
$block->setAccessDependency($layout_entity)->shouldBeCalled();
}
else {
$block = $this->prophesize(BlockPluginInterface::class);
}
Alex Bronstein
committed
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
$access_result = AccessResult::forbidden();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
$block->getCacheContexts()->shouldNotBeCalled();
$block->getCacheTags()->shouldNotBeCalled();
$block->getCacheMaxAge()->shouldNotBeCalled();
$block->getConfiguration()->shouldNotBeCalled();
$block->getPluginId()->shouldNotBeCalled();
$block->getBaseId()->shouldNotBeCalled();
$block->getDerivativeId()->shouldNotBeCalled();
$block_content = ['#markup' => 'The block content.'];
$block->build()->willReturn($block_content);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
$in_preview = FALSE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
$subscriber = new BlockComponentRenderArray($this->account->reveal());
$expected_build = [];
$expected_cache = [
'#cache' => [
'contexts' => [],
'tags' => [],
'max-age' => -1,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
Angie Byron
committed
*
* @dataProvider providerBlockTypes
Alex Bronstein
committed
*/
Angie Byron
committed
public function testOnBuildRenderInPreview($refinable_dependent_access) {
$contexts = [];
if ($refinable_dependent_access) {
$block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class);
$block->setAccessDependency(new LayoutPreviewAccessAllowed())->shouldBeCalled();
$layout_entity = $this->prophesize(EntityInterface::class);
$layout_entity = $layout_entity->reveal();
$layout_entity->in_preview = TRUE;
$context = $this->prophesize(ContextInterface::class);
$context->getContextValue()->willReturn($layout_entity);
$contexts['layout_builder.entity'] = $context->reveal();
}
else {
$block = $this->prophesize(BlockPluginInterface::class);
}
Alex Bronstein
committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
$block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn(['test']);
$block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
$block->getConfiguration()->willReturn([]);
$block->getPluginId()->willReturn('block_plugin_id');
$block->getBaseId()->willReturn('block_plugin_id');
$block->getDerivativeId()->willReturn(NULL);
$block_content = ['#markup' => 'The block content.'];
$block->build()->willReturn($block_content);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
$in_preview = TRUE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
$subscriber = new BlockComponentRenderArray($this->account->reveal());
$expected_build = [
'#theme' => 'block',
'#weight' => 0,
'#configuration' => [],
'#plugin_id' => 'block_plugin_id',
'#base_plugin_id' => 'block_plugin_id',
'#derivative_plugin_id' => NULL,
'content' => $block_content,
];
$expected_cache = $expected_build + [
'#cache' => [
'contexts' => [],
'tags' => ['test'],
'max-age' => 0,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
/**
* @covers ::onBuildRender
*/
public function testOnBuildRenderNoBlock() {
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn(NULL);
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
$contexts = [];
$in_preview = FALSE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
$subscriber = new BlockComponentRenderArray($this->account->reveal());
$expected_build = [];
$expected_cache = [
'#cache' => [
'contexts' => [],
'tags' => [],
'max-age' => -1,
],
];
$subscriber->onBuildRender($event);
$result = $event->getBuild();
$this->assertEquals($expected_build, $result);
$event->getCacheableMetadata()->applyTo($result);
$this->assertEquals($expected_cache, $result);
}
}
Angie Byron
committed
/**
* Test interface for dependent access block plugins.
*/
interface TestBlockPluginWithRefinableDependentAccessInterface extends BlockPluginInterface, RefinableDependentAccessInterface {
}