Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / BlockComponentRenderArrayTest.php
index 6571a8ff1a6d2130d27aa3bdfdfa98fa2e1cd91b..30820846cd534be9bc1b622f4a6a6f78a60990aa 100644 (file)
@@ -2,12 +2,17 @@
 
 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;
@@ -33,6 +38,16 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
    */
   protected $blockManager;
 
+  /**
+   * Dataprovider for test functions that should test block types.
+   */
+  public function providerBlockTypes() {
+    return [
+      [TRUE],
+      [FALSE],
+    ];
+  }
+
   /**
    * {@inheritdoc}
    */
@@ -44,14 +59,30 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
 
     $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([]);
@@ -67,7 +98,6 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
     $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);
 
@@ -100,9 +130,26 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
 
   /**
    * @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();
@@ -118,7 +165,6 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
     $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);
 
@@ -142,9 +188,26 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
 
   /**
    * @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']);
@@ -159,7 +222,6 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
     $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);
 
@@ -220,3 +282,10 @@ class BlockComponentRenderArrayTest extends UnitTestCase {
   }
 
 }
+
+/**
+ * Test interface for dependent access block plugins.
+ */
+interface TestBlockPluginWithRefinableDependentAccessInterface extends BlockPluginInterface, RefinableDependentAccessInterface {
+
+}