You are here

public function PageBlockDisplayVariantTest::testBuildEmptyBlock in Page Manager 8

Same name and namespace in other branches
  1. 8.4 tests/src/Unit/PageBlockDisplayVariantTest.php \Drupal\Tests\page_manager\Unit\PageBlockDisplayVariantTest::testBuildEmptyBlock()

Tests the build() method when a block is empty.

@covers ::build @covers ::buildRegions @covers ::buildBlock

File

tests/src/Unit/PageBlockDisplayVariantTest.php, line 44
Contains \Drupal\Tests\page_manager\Unit\PageBlockDisplayVariantTest.

Class

PageBlockDisplayVariantTest
Tests the block variant plugin.

Namespace

Drupal\Tests\page_manager\Unit

Code

public function testBuildEmptyBlock() {
  $account = $this
    ->prophesize(AccountInterface::class);
  $block1 = $this
    ->prophesize(BlockPluginInterface::class);
  $block1
    ->access($account)
    ->willReturn(TRUE);

  // Building a block with empty content.
  $block1
    ->build()
    ->willReturn([
    '#cache' => [
      'tags' => [
        0 => 'tag_to_be_merged',
      ],
    ],
  ]);
  $context_handler = $this
    ->prophesize(ContextHandlerInterface::class);
  $uuid_generator = $this
    ->prophesize(UuidInterface::class);
  $token = $this
    ->prophesize(Token::class);
  $block_manager = $this
    ->prophesize(BlockManager::class);
  $condition_manager = $this
    ->prophesize(ConditionManager::class);
  $module_handler = $this
    ->prophesize(ModuleHandlerInterface::class);
  $variant_plugin = new PageBlockDisplayVariant([], '', [], $context_handler
    ->reveal(), $account
    ->reveal(), $uuid_generator
    ->reveal(), $token
    ->reveal(), $block_manager
    ->reveal(), $condition_manager
    ->reveal(), $module_handler
    ->reveal());

  // Empty block.
  $expected_build = [
    '#markup' => '',
    '#cache' => [
      'tags' => [
        'block_plugin:block_plugin_id',
        'page:page_id',
        'tag_to_be_merged',
      ],
      'contexts' => [],
      'max-age' => -1,
    ],
  ];
  $build = [
    '#block_plugin' => $block1
      ->reveal(),
    '#cache' => [
      'tags' => [
        'page:page_id',
        'block_plugin:block_plugin_id',
      ],
    ],
  ];
  $build = $variant_plugin
    ->buildBlock($build);

  // Assert that cacheability metadata is merged.
  $this
    ->assertSame($expected_build, $build);
}