You are here

class BlockComponentRenderArrayTest in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php \Drupal\Tests\layout_builder\Unit\BlockComponentRenderArrayTest
  2. 10 core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php \Drupal\Tests\layout_builder\Unit\BlockComponentRenderArrayTest

@coversDefaultClass \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray @group layout_builder

Hierarchy

Expanded class hierarchy of BlockComponentRenderArrayTest

File

core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php, line 29

Namespace

Drupal\Tests\layout_builder\Unit
View source
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;

  /**
   * Dataprovider for test functions that should test block types.
   */
  public function providerBlockTypes() {
    return [
      [
        TRUE,
      ],
      [
        FALSE,
      ],
    ];
  }

  /**
   * {@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());
    $container
      ->set('context.handler', $this
      ->prophesize(ContextHandlerInterface::class));
    \Drupal::setContainer($container);
  }

  /**
   * @covers ::onBuildRender
   *
   * @dataProvider providerBlockTypes
   */
  public function testOnBuildRender($refinable_dependent_access) {
    $contexts = [];
    if ($refinable_dependent_access) {
      $block = $this
        ->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class)
        ->willImplement(PreviewFallbackInterface::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)
        ->willImplement(PreviewFallbackInterface::class);
    }
    $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);
    $placeholder_label = 'Placeholder Label';
    $block
      ->getPreviewFallbackString()
      ->willReturn($placeholder_label);
    $block_content = [
      '#markup' => 'The block content.',
      '#cache' => [
        'tags' => [
          'build-tag',
        ],
      ],
    ];
    $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' => [
          'build-tag',
          '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
   *
   * @dataProvider providerBlockTypes
   */
  public function testOnBuildRenderWithoutPreviewFallbackString($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([]);
    $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);
    $placeholder_label = 'Placeholder Label';
    $block
      ->label()
      ->willReturn($placeholder_label);
    $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());
    $translation = $this
      ->prophesize(TranslationInterface::class);
    $translation
      ->translateString(Argument::type(TranslatableMarkup::class))
      ->willReturn($placeholder_label);
    $subscriber
      ->setStringTranslation($translation
      ->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
   *
   * @dataProvider providerBlockTypes
   */
  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();
    $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
   *
   * @dataProvider providerBlockTypes
   */
  public function testOnBuildRenderInPreview($refinable_dependent_access) {
    $contexts = [];
    if ($refinable_dependent_access) {
      $block = $this
        ->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class)
        ->willImplement(PreviewFallbackInterface::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)
        ->willImplement(PreviewFallbackInterface::class);
    }
    $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);
    $placeholder_label = 'Placeholder Label';
    $block
      ->getPreviewFallbackString()
      ->willReturn($placeholder_label);
    $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,
      '#attributes' => [
        'data-layout-content-preview-placeholder-label' => $placeholder_label,
      ],
    ];
    $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 testOnBuildRenderInPreviewEmptyBuild() {
    $block = $this
      ->prophesize(BlockPluginInterface::class)
      ->willImplement(PreviewFallbackInterface::class);
    $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
      ->getPluginDefinition()
      ->willReturn([
      'admin_label' => 'adminlabel',
    ]);
    $placeholder_string = 'The placeholder string';
    $block
      ->getPreviewFallbackString()
      ->willReturn($placeholder_string);
    $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',
    ]);
    $event = new SectionComponentBuildRenderArrayEvent($component, [], TRUE);
    $subscriber = new BlockComponentRenderArray($this->account
      ->reveal());
    $translation = $this
      ->prophesize(TranslationInterface::class);
    $translation
      ->translateString(Argument::type(TranslatableMarkup::class))
      ->willReturn($placeholder_string);
    $subscriber
      ->setStringTranslation($translation
      ->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,
      '#attributes' => [
        'data-layout-content-preview-placeholder-label' => $placeholder_string,
      ],
    ];
    $expected_build['content']['#markup'] = $placeholder_string;
    $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 testOnBuildRenderEmptyBuild() {
    $block = $this
      ->prophesize(BlockPluginInterface::class);
    $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
      ->build()
      ->willReturn([
      '#cache' => [
        'tags' => [
          'build-tag',
        ],
      ],
    ]);
    $this->blockManager
      ->createInstance('some_block_id', [
      'id' => 'some_block_id',
    ])
      ->willReturn($block
      ->reveal());
    $component = new SectionComponent('some-uuid', 'some-region', [
      'id' => 'some_block_id',
    ]);
    $event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE);
    $subscriber = new BlockComponentRenderArray($this->account
      ->reveal());
    $expected_build = [];
    $expected_cache = $expected_build + [
      '#cache' => [
        'contexts' => [],
        'tags' => [
          'build-tag',
          '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
   */
  public function testOnBuildRenderEmptyBuildWithCacheTags() {
    $block = $this
      ->prophesize(BlockPluginInterface::class);
    $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 = [
      '#cache' => [
        'tags' => [
          'empty_build_cache_test',
        ],
      ],
    ];
    $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',
    ]);
    $event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE);
    $subscriber = new BlockComponentRenderArray($this->account
      ->reveal());
    $expected_build = [];
    $expected_cache = $expected_build + [
      '#cache' => [
        'contexts' => [],
        'tags' => [
          'empty_build_cache_test',
          '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
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockComponentRenderArrayTest::$account protected property The current user.
BlockComponentRenderArrayTest::$blockManager protected property The block plugin manager.
BlockComponentRenderArrayTest::providerBlockTypes public function Dataprovider for test functions that should test block types.
BlockComponentRenderArrayTest::setUp protected function Overrides UnitTestCase::setUp
BlockComponentRenderArrayTest::testOnBuildRender public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderDenied public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderEmptyBuild public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderEmptyBuildWithCacheTags public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderInPreview public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderInPreviewEmptyBuild public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderNoBlock public function @covers ::onBuildRender
BlockComponentRenderArrayTest::testOnBuildRenderWithoutPreviewFallbackString public function @covers ::onBuildRender
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.