Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\Tests\block\Unit\Plugin\DisplayVariant\BlockPageVariantTest.
Angie Byron
committed
*/
Dries Buytaert
committed
namespace Drupal\Tests\block\Unit\Plugin\DisplayVariant;
Angie Byron
committed
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\DependencyInjection\Container;
Angie Byron
committed
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
* @group block
Angie Byron
committed
*/
class BlockPageVariantTest extends UnitTestCase {
Angie Byron
committed
/**
Alex Pott
committed
* The block repository.
Angie Byron
committed
*
Alex Pott
committed
* @var \Drupal\block\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
*/
Alex Pott
committed
protected $blockRepository;
Angie Byron
committed
/**
* The block view builder.
*
* @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $blockViewBuilder;
/**
* The plugin context handler.
Angie Byron
committed
*
* @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
*/
protected $contextHandler;
Angie Byron
committed
/**
* Sets up a display variant plugin for testing.
*
* @param array $configuration
* An array of plugin configuration.
* @param array $definition
* The plugin definition array.
*
* @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant|\PHPUnit_Framework_MockObject_MockObject
Angie Byron
committed
* A mocked display variant plugin.
*/
public function setUpDisplayVariant($configuration = array(), $definition = array()) {
$container = new Container();
$cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
->disableOriginalConstructor()
->getMock();
$container->set('cache_contexts_manager', $cache_context_manager);
$cache_context_manager->expects($this->any())
->method('validateTokens')
->with([])
->willReturn([]);
\Drupal::setContainer($container);
Alex Pott
committed
$this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
Angie Byron
committed
$this->blockViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant')
->setConstructorArgs(array($configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, ['config:block_list']))
Angie Byron
committed
->setMethods(array('getRegionNames'))
->getMock();
}
public function providerBuild() {
$blocks_config = array(
'block1' => array(
// region, is main content block, is messages block, is title block
'top', FALSE, FALSE, FALSE,
Alex Pott
committed
// Test multiple blocks in the same region.
'block2' => array(
'bottom', FALSE, FALSE, FALSE,
),
'block3' => array(
'bottom', FALSE, FALSE, FALSE,
),
// Test a block implementing MainContentBlockPluginInterface.
Alex Pott
committed
'block4' => array(
'center', TRUE, FALSE, FALSE,
),
// Test a block implementing MessagesBlockPluginInterface.
'block5' => array(
'center', FALSE, TRUE, FALSE,
),
// Test a block implementing TitleBlockPluginInterface.
'block6' => array(
'center', FALSE, FALSE, TRUE,
),
);
$test_cases = [];
$test_cases[] = [$blocks_config, 6,
'#cache' => [
'tags' => [
'config:block_list',
'route',
],
'contexts' => [],
'max-age' => -1,
],
'top' => [
'block1' => [],
'#sorted' => TRUE,
],
// The main content was rendered via a block.
'center' => [
Alex Pott
committed
'block4' => [],
'block5' => [],
'block6' => [],
'#sorted' => TRUE,
],
'bottom' => [
Alex Pott
committed
'block2' => [],
'block3' => [],
'#sorted' => TRUE,
],
],
];
unset($blocks_config['block5']);
$test_cases[] = [$blocks_config, 5,
[
'#cache' => [
'tags' => [
'config:block_list',
'route',
],
'contexts' => [],
'max-age' => -1,
],
'top' => [
'block1' => [],
'#sorted' => TRUE,
],
'center' => [
'block4' => [],
'block6' => [],
'#sorted' => TRUE,
],
'bottom' => [
'block2' => [],
'block3' => [],
'#sorted' => TRUE,
],
// The messages are rendered via the fallback in case there is no block
// rendering the main content.
'content' => [
'messages' => [
'#weight' => -1000,
'#type' => 'status_messages',
],
],
],
];
Alex Pott
committed
unset($blocks_config['block4']);
unset($blocks_config['block6']);
$test_cases[] = [$blocks_config, 3,
[
'#cache' => [
'tags' => [
'config:block_list',
'route',
],
'contexts' => [],
'max-age' => -1,
],
'top' => [
'block1' => [],
'#sorted' => TRUE,
],
'bottom' => [
Alex Pott
committed
'block2' => [],
'block3' => [],
'#sorted' => TRUE,
],
// The main content & messages are rendered via the fallback in case
// there are no blocks rendering them.
'content' => [
'system_main' => ['#markup' => 'Hello kittens!'],
'messages' => [
'#weight' => -1000,
'#type' => 'status_messages',
],
],
],
];
return $test_cases;
}
Angie Byron
committed
/**
* Tests the building of a full page variant.
*
* @covers ::build
*
* @dataProvider providerBuild
Angie Byron
committed
*/
public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) {
Angie Byron
committed
$display_variant = $this->setUpDisplayVariant();
$display_variant->setMainContent(['#markup' => 'Hello kittens!']);
Angie Byron
committed
Alex Pott
committed
$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');
Angie Byron
committed
foreach ($blocks_config as $block_id => $block_config) {
$block = $this->getMock('Drupal\block\BlockInterface');
$block->expects($this->any())
->method('getContexts')
->willReturn([]);
Alex Pott
committed
$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)));
Alex Pott
committed
$blocks[$block_config[0]][$block_id] = $block;
Angie Byron
committed
}
$this->blockViewBuilder->expects($this->exactly($visible_block_count))
Angie Byron
committed
->method('view')
->will($this->returnValue(array()));
Alex Pott
committed
$this->blockRepository->expects($this->once())
->method('getVisibleBlocksPerRegion')
->willReturnCallback(function (&$cacheable_metadata) use ($blocks) {
$cacheable_metadata['top'] = (new CacheableMetadata())->addCacheTags(['route']);
return $blocks;
});
Angie Byron
committed
$value = $display_variant->build();
$this->assertSame($expected_render_array, $value);
Angie Byron
committed
}
/**
* Tests the building of a full page variant with no main content set.
*
* @covers ::build
*/
public function testBuildWithoutMainContent() {
$display_variant = $this->setUpDisplayVariant();
$this->blockRepository->expects($this->once())
->method('getVisibleBlocksPerRegion')
->willReturn([]);
$expected = [
'#cache' => [
'tags' => [
'config:block_list',
],
'contexts' => [],
'max-age' => -1,
],
'content' => [
'system_main' => [],
'messages' => [
'#weight' => -1000,
'#type' => 'status_messages',
],
],
];
$this->assertSame($expected, $display_variant->build());
}