You are here

public function BlockPageVariantTest::testBuild in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php \Drupal\Tests\block\Unit\Plugin\DisplayVariant\BlockPageVariantTest::testBuild()

Tests the building of a full page variant.

@covers ::build

@dataProvider providerBuild

File

core/modules/block/tests/src/Unit/Plugin/DisplayVariant/BlockPageVariantTest.php, line 206
Contains \Drupal\Tests\block\Unit\Plugin\DisplayVariant\BlockPageVariantTest.

Class

BlockPageVariantTest
@coversDefaultClass \Drupal\block\Plugin\DisplayVariant\BlockPageVariant @group block

Namespace

Drupal\Tests\block\Unit\Plugin\DisplayVariant

Code

public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) {
  $display_variant = $this
    ->setUpDisplayVariant();
  $display_variant
    ->setMainContent([
    '#markup' => 'Hello kittens!',
  ]);
  $blocks = [
    'top' => [],
    'center' => [],
    'bottom' => [],
  ];
  $block_plugin = $this
    ->getMock('Drupal\\Core\\Block\\BlockPluginInterface');
  $main_content_block_plugin = $this
    ->getMock('Drupal\\Core\\Block\\MainContentBlockPluginInterface');
  $messages_block_plugin = $this
    ->getMock('Drupal\\Core\\Block\\MessagesBlockPluginInterface');
  $title_block_plugin = $this
    ->getMock('Drupal\\Core\\Block\\TitleBlockPluginInterface');
  foreach ($blocks_config as $block_id => $block_config) {
    $block = $this
      ->getMock('Drupal\\block\\BlockInterface');
    $block
      ->expects($this
      ->any())
      ->method('getContexts')
      ->willReturn([]);
    $block
      ->expects($this
      ->atLeastOnce())
      ->method('getPlugin')
      ->willReturn($block_config[1] ? $main_content_block_plugin : ($block_config[2] ? $messages_block_plugin : ($block_config[3] ? $title_block_plugin : $block_plugin)));
    $blocks[$block_config[0]][$block_id] = $block;
  }
  $this->blockViewBuilder
    ->expects($this
    ->exactly($visible_block_count))
    ->method('view')
    ->will($this
    ->returnValue(array()));
  $this->blockRepository
    ->expects($this
    ->once())
    ->method('getVisibleBlocksPerRegion')
    ->willReturnCallback(function (&$cacheable_metadata) use ($blocks) {
    $cacheable_metadata['top'] = (new CacheableMetadata())
      ->addCacheTags([
      'route',
    ]);
    return $blocks;
  });
  $value = $display_variant
    ->build();
  $this
    ->assertSame($expected_render_array, $value);
}