namespace Drupal\Tests\layout_builder\Unit;
+use Drupal\block_content\Access\RefinableDependentAccessInterface;
+use Drupal\Component\Plugin\Context\ContextInterface;
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;
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Session\AccountInterface;
+use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed;
use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
use Drupal\layout_builder\SectionComponent;
*/
protected $blockManager;
+ /**
+ * Dataprovider for test functions that should test block types.
+ */
+ public function providerBlockTypes() {
+ return [
+ [TRUE],
+ [FALSE],
+ ];
+ }
+
/**
* {@inheritdoc}
*/
$container = new ContainerBuilder();
$container->set('plugin.manager.block', $this->blockManager->reveal());
+ $container->set('context.handler', $this->prophesize(ContextHandlerInterface::class));
\Drupal::setContainer($container);
}
/**
* @covers ::onBuildRender
+ *
+ * @dataProvider providerBlockTypes
*/
- public function testOnBuildRender() {
- $block = $this->prophesize(BlockPluginInterface::class);
+ 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);
+ }
$access_result = AccessResult::allowed();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
$block->getCacheContexts()->willReturn([]);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
- $contexts = [];
$in_preview = FALSE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
/**
* @covers ::onBuildRender
+ *
+ * @dataProvider providerBlockTypes
*/
- public function testOnBuildRenderDenied() {
- $block = $this->prophesize(BlockPluginInterface::class);
+ 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);
+ }
+
$access_result = AccessResult::forbidden();
$block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
$block->getCacheContexts()->shouldNotBeCalled();
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
- $contexts = [];
$in_preview = FALSE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
/**
* @covers ::onBuildRender
+ *
+ * @dataProvider providerBlockTypes
*/
- public function testOnBuildRenderInPreview() {
- $block = $this->prophesize(BlockPluginInterface::class);
+ 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);
+ }
+
$block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
$block->getCacheContexts()->willReturn([]);
$block->getCacheTags()->willReturn(['test']);
$this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
$component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
- $contexts = [];
$in_preview = TRUE;
$event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
}
}
+
+/**
+ * Test interface for dependent access block plugins.
+ */
+interface TestBlockPluginWithRefinableDependentAccessInterface extends BlockPluginInterface, RefinableDependentAccessInterface {
+
+}