3 namespace Drupal\Tests\block\Unit\Plugin\DisplayVariant;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\DependencyInjection\Container;
7 use Drupal\Tests\UnitTestCase;
10 * @coversDefaultClass \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
13 class BlockPageVariantTest extends UnitTestCase {
16 * The block repository.
18 * @var \Drupal\block\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
20 protected $blockRepository;
23 * The block view builder.
25 * @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
27 protected $blockViewBuilder;
30 * The plugin context handler.
32 * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
34 protected $contextHandler;
37 * Sets up a display variant plugin for testing.
39 * @param array $configuration
40 * An array of plugin configuration.
41 * @param array $definition
42 * The plugin definition array.
44 * @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant|\PHPUnit_Framework_MockObject_MockObject
45 * A mocked display variant plugin.
47 public function setUpDisplayVariant($configuration = [], $definition = []) {
49 $container = new Container();
50 $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
51 ->disableOriginalConstructor()
53 $container->set('cache_contexts_manager', $cache_context_manager);
54 $cache_context_manager->expects($this->any())
55 ->method('assertValidTokens')
57 \Drupal::setContainer($container);
59 $this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
60 $this->blockViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
62 return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant')
63 ->setConstructorArgs([$configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, ['config:block_list']])
64 ->setMethods(['getRegionNames'])
68 public function providerBuild() {
71 // region, is main content block, is messages block, is title block
72 'top', FALSE, FALSE, FALSE,
74 // Test multiple blocks in the same region.
76 'bottom', FALSE, FALSE, FALSE,
79 'bottom', FALSE, FALSE, FALSE,
81 // Test a block implementing MainContentBlockPluginInterface.
83 'center', TRUE, FALSE, FALSE,
85 // Test a block implementing MessagesBlockPluginInterface.
87 'center', FALSE, TRUE, FALSE,
89 // Test a block implementing TitleBlockPluginInterface.
91 'center', FALSE, FALSE, TRUE,
96 $test_cases[] = [$blocks_config, 6,
110 // The main content was rendered via a block.
124 unset($blocks_config['block5']);
125 $test_cases[] = [$blocks_config, 5,
149 // The messages are rendered via the fallback in case there is no block
150 // rendering the main content.
154 '#type' => 'status_messages',
159 unset($blocks_config['block4']);
160 unset($blocks_config['block6']);
161 $test_cases[] = [$blocks_config, 3,
180 // The main content & messages are rendered via the fallback in case
181 // there are no blocks rendering them.
183 'system_main' => ['#markup' => 'Hello kittens!'],
186 '#type' => 'status_messages',
195 * Tests the building of a full page variant.
199 * @dataProvider providerBuild
201 public function testBuild(array $blocks_config, $visible_block_count, array $expected_render_array) {
202 $display_variant = $this->setUpDisplayVariant();
203 $display_variant->setMainContent(['#markup' => 'Hello kittens!']);
205 $blocks = ['top' => [], 'center' => [], 'bottom' => []];
206 $block_plugin = $this->getMock('Drupal\Core\Block\BlockPluginInterface');
207 $main_content_block_plugin = $this->getMock('Drupal\Core\Block\MainContentBlockPluginInterface');
208 $messages_block_plugin = $this->getMock('Drupal\Core\Block\MessagesBlockPluginInterface');
209 $title_block_plugin = $this->getMock('Drupal\Core\Block\TitleBlockPluginInterface');
210 foreach ($blocks_config as $block_id => $block_config) {
211 $block = $this->getMock('Drupal\block\BlockInterface');
212 $block->expects($this->any())
213 ->method('getContexts')
215 $block->expects($this->atLeastOnce())
216 ->method('getPlugin')
217 ->willReturn($block_config[1] ? $main_content_block_plugin : ($block_config[2] ? $messages_block_plugin : ($block_config[3] ? $title_block_plugin : $block_plugin)));
218 $blocks[$block_config[0]][$block_id] = $block;
220 $this->blockViewBuilder->expects($this->exactly($visible_block_count))
222 ->will($this->returnValue([]));
223 $this->blockRepository->expects($this->once())
224 ->method('getVisibleBlocksPerRegion')
225 ->willReturnCallback(function (&$cacheable_metadata) use ($blocks) {
226 $cacheable_metadata['top'] = (new CacheableMetadata())->addCacheTags(['route']);
230 $value = $display_variant->build();
231 $this->assertSame($expected_render_array, $value);
235 * Tests the building of a full page variant with no main content set.
239 public function testBuildWithoutMainContent() {
240 $display_variant = $this->setUpDisplayVariant();
241 $this->blockRepository->expects($this->once())
242 ->method('getVisibleBlocksPerRegion')
257 '#type' => 'status_messages',
261 $this->assertSame($expected, $display_variant->build());