15694113534b8bcff0fd3c4c961740bf29b4b30e
[yaffs-website] / web / core / modules / block / tests / src / Unit / Plugin / DisplayVariant / BlockPageVariantTest.php
1 <?php
2
3 namespace Drupal\Tests\block\Unit\Plugin\DisplayVariant;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\DependencyInjection\Container;
7 use Drupal\Tests\UnitTestCase;
8
9 /**
10  * @coversDefaultClass \Drupal\block\Plugin\DisplayVariant\BlockPageVariant
11  * @group block
12  */
13 class BlockPageVariantTest extends UnitTestCase {
14
15   /**
16    * The block repository.
17    *
18    * @var \Drupal\block\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
19    */
20   protected $blockRepository;
21
22   /**
23    * The block view builder.
24    *
25    * @var \Drupal\Core\Entity\EntityViewBuilderInterface|\PHPUnit_Framework_MockObject_MockObject
26    */
27   protected $blockViewBuilder;
28
29   /**
30    * The plugin context handler.
31    *
32    * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
33    */
34   protected $contextHandler;
35
36   /**
37    * Sets up a display variant plugin for testing.
38    *
39    * @param array $configuration
40    *   An array of plugin configuration.
41    * @param array $definition
42    *   The plugin definition array.
43    *
44    * @return \Drupal\block\Plugin\DisplayVariant\BlockPageVariant|\PHPUnit_Framework_MockObject_MockObject
45    *   A mocked display variant plugin.
46    */
47   public function setUpDisplayVariant($configuration = [], $definition = []) {
48
49     $container = new Container();
50     $cache_context_manager = $this->getMockBuilder('Drupal\Core\Cache\CacheContextsManager')
51       ->disableOriginalConstructor()
52       ->getMock();
53     $container->set('cache_contexts_manager', $cache_context_manager);
54     $cache_context_manager->expects($this->any())
55       ->method('assertValidTokens')
56       ->willReturn(TRUE);
57     \Drupal::setContainer($container);
58
59     $this->blockRepository = $this->getMock('Drupal\block\BlockRepositoryInterface');
60     $this->blockViewBuilder = $this->getMock('Drupal\Core\Entity\EntityViewBuilderInterface');
61
62     return $this->getMockBuilder('Drupal\block\Plugin\DisplayVariant\BlockPageVariant')
63       ->setConstructorArgs([$configuration, 'test', $definition, $this->blockRepository, $this->blockViewBuilder, ['config:block_list']])
64       ->setMethods(['getRegionNames'])
65       ->getMock();
66   }
67
68   public function providerBuild() {
69     $blocks_config = [
70       'block1' => [
71         // region, is main content block, is messages block, is title block
72         'top', FALSE, FALSE, FALSE,
73       ],
74       // Test multiple blocks in the same region.
75       'block2' => [
76         'bottom', FALSE, FALSE, FALSE,
77       ],
78       'block3' => [
79         'bottom', FALSE, FALSE, FALSE,
80       ],
81       // Test a block implementing MainContentBlockPluginInterface.
82       'block4' => [
83         'center', TRUE, FALSE, FALSE,
84       ],
85       // Test a block implementing MessagesBlockPluginInterface.
86       'block5' => [
87         'center', FALSE, TRUE, FALSE,
88       ],
89       // Test a block implementing TitleBlockPluginInterface.
90       'block6' => [
91         'center', FALSE, FALSE, TRUE,
92       ],
93     ];
94
95     $test_cases = [];
96     $test_cases[] = [$blocks_config, 6,
97       [
98         '#cache' => [
99           'tags' => [
100             'config:block_list',
101             'route',
102           ],
103           'contexts' => [],
104           'max-age' => -1,
105         ],
106         'top' => [
107           'block1' => [],
108           '#sorted' => TRUE,
109         ],
110         // The main content was rendered via a block.
111         'center' => [
112           'block4' => [],
113           'block5' => [],
114           'block6' => [],
115           '#sorted' => TRUE,
116         ],
117         'bottom' => [
118           'block2' => [],
119           'block3' => [],
120           '#sorted' => TRUE,
121         ],
122       ],
123     ];
124     unset($blocks_config['block5']);
125     $test_cases[] = [$blocks_config, 5,
126       [
127         '#cache' => [
128           'tags' => [
129             'config:block_list',
130             'route',
131           ],
132           'contexts' => [],
133           'max-age' => -1,
134         ],
135         'top' => [
136           'block1' => [],
137           '#sorted' => TRUE,
138         ],
139         'center' => [
140           'block4' => [],
141           'block6' => [],
142           '#sorted' => TRUE,
143         ],
144         'bottom' => [
145           'block2' => [],
146           'block3' => [],
147           '#sorted' => TRUE,
148         ],
149         // The messages are rendered via the fallback in case there is no block
150         // rendering the main content.
151         'content' => [
152           'messages' => [
153             '#weight' => -1000,
154             '#type' => 'status_messages',
155           ],
156         ],
157       ],
158     ];
159     unset($blocks_config['block4']);
160     unset($blocks_config['block6']);
161     $test_cases[] = [$blocks_config, 3,
162       [
163         '#cache' => [
164           'tags' => [
165             'config:block_list',
166             'route',
167           ],
168           'contexts' => [],
169           'max-age' => -1,
170         ],
171         'top' => [
172           'block1' => [],
173           '#sorted' => TRUE,
174         ],
175         'bottom' => [
176           'block2' => [],
177           'block3' => [],
178           '#sorted' => TRUE,
179         ],
180         // The main content & messages are rendered via the fallback in case
181         // there are no blocks rendering them.
182         'content' => [
183           'system_main' => ['#markup' => 'Hello kittens!'],
184           'messages' => [
185             '#weight' => -1000,
186             '#type' => 'status_messages',
187           ],
188         ],
189       ],
190     ];
191     return $test_cases;
192   }
193
194   /**
195    * Tests the building of a full page variant.
196    *
197    * @covers ::build
198    *
199    * @dataProvider providerBuild
200    */
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!']);
204
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')
214         ->willReturn([]);
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;
219     }
220     $this->blockViewBuilder->expects($this->exactly($visible_block_count))
221       ->method('view')
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']);
227         return $blocks;
228       });
229
230     $value = $display_variant->build();
231     $this->assertSame($expected_render_array, $value);
232   }
233
234   /**
235    * Tests the building of a full page variant with no main content set.
236    *
237    * @covers ::build
238    */
239   public function testBuildWithoutMainContent() {
240     $display_variant = $this->setUpDisplayVariant();
241     $this->blockRepository->expects($this->once())
242       ->method('getVisibleBlocksPerRegion')
243       ->willReturn([]);
244
245     $expected = [
246       '#cache' => [
247         'tags' => [
248           'config:block_list',
249         ],
250         'contexts' => [],
251         'max-age' => -1,
252       ],
253       'content' => [
254         'system_main' => [],
255         'messages' => [
256           '#weight' => -1000,
257           '#type' => 'status_messages',
258         ],
259       ],
260     ];
261     $this->assertSame($expected, $display_variant->build());
262   }
263
264 }