Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / SectionRenderTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
5 use Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher;
6 use Drupal\Component\Plugin\Exception\PluginException;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Block\BlockManagerInterface;
9 use Drupal\Core\Block\BlockPluginInterface;
10 use Drupal\Core\Cache\Cache;
11 use Drupal\Core\DependencyInjection\ContainerBuilder;
12 use Drupal\Core\Layout\LayoutDefinition;
13 use Drupal\Core\Layout\LayoutInterface;
14 use Drupal\Core\Layout\LayoutPluginManagerInterface;
15 use Drupal\Core\Plugin\Context\ContextHandlerInterface;
16 use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
17 use Drupal\Core\Plugin\ContextAwarePluginInterface;
18 use Drupal\Core\Session\AccountInterface;
19 use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
20 use Drupal\layout_builder\Section;
21 use Drupal\layout_builder\SectionComponent;
22 use Drupal\Tests\UnitTestCase;
23 use Prophecy\Argument;
24
25 /**
26  * @coversDefaultClass \Drupal\layout_builder\Section
27  * @group layout_builder
28  */
29 class SectionRenderTest extends UnitTestCase {
30
31   /**
32    * The current user.
33    *
34    * @var \Drupal\Core\Session\AccountInterface
35    */
36   protected $account;
37
38   /**
39    * The block plugin manager.
40    *
41    * @var \Drupal\Core\Block\BlockManagerInterface
42    */
43   protected $blockManager;
44
45   /**
46    * The plugin context handler.
47    *
48    * @var \Drupal\Core\Plugin\Context\ContextHandlerInterface
49    */
50   protected $contextHandler;
51
52   /**
53    * The context manager service.
54    *
55    * @var \Drupal\Core\Plugin\Context\ContextRepositoryInterface
56    */
57   protected $contextRepository;
58
59   /**
60    * The event dispatcher.
61    *
62    * @var \Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher
63    */
64   protected $eventDispatcher;
65
66   /**
67    * {@inheritdoc}
68    */
69   protected function setUp() {
70     parent::setUp();
71
72     $layout_plugin_manager = $this->prophesize(LayoutPluginManagerInterface::class);
73     $this->blockManager = $this->prophesize(BlockManagerInterface::class);
74     $this->contextHandler = $this->prophesize(ContextHandlerInterface::class);
75     $this->contextRepository = $this->prophesize(ContextRepositoryInterface::class);
76     // @todo Refactor this into some better tests in https://www.drupal.org/node/2942605.
77     $this->eventDispatcher = (new \ReflectionClass(ContainerAwareEventDispatcher::class))->newInstanceWithoutConstructor();
78
79     $this->account = $this->prophesize(AccountInterface::class);
80     $subscriber = new BlockComponentRenderArray($this->account->reveal());
81     $this->eventDispatcher->addSubscriber($subscriber);
82
83     $layout = $this->prophesize(LayoutInterface::class);
84     $layout->getPluginDefinition()->willReturn(new LayoutDefinition([]));
85     $layout->build(Argument::type('array'))->willReturnArgument(0);
86     $layout_plugin_manager->createInstance('layout_onecol', [])->willReturn($layout->reveal());
87
88     $container = new ContainerBuilder();
89     $container->set('current_user', $this->account->reveal());
90     $container->set('plugin.manager.block', $this->blockManager->reveal());
91     $container->set('plugin.manager.core.layout', $layout_plugin_manager->reveal());
92     $container->set('context.handler', $this->contextHandler->reveal());
93     $container->set('context.repository', $this->contextRepository->reveal());
94     $container->set('event_dispatcher', $this->eventDispatcher);
95     \Drupal::setContainer($container);
96   }
97
98   /**
99    * @covers ::toRenderArray
100    */
101   public function testToRenderArray() {
102     $block_content = ['#markup' => 'The block content.'];
103     $render_array = [
104       '#theme' => 'block',
105       '#weight' => 0,
106       '#configuration' => [],
107       '#plugin_id' => 'block_plugin_id',
108       '#base_plugin_id' => 'block_plugin_id',
109       '#derivative_plugin_id' => NULL,
110       'content' => $block_content,
111       '#cache' => [
112         'contexts' => [],
113         'tags' => [],
114         'max-age' => -1,
115       ],
116     ];
117
118     $block = $this->prophesize(BlockPluginInterface::class);
119     $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
120
121     $access_result = AccessResult::allowed();
122     $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
123     $block->build()->willReturn($block_content);
124     $block->getCacheContexts()->willReturn([]);
125     $block->getCacheTags()->willReturn([]);
126     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
127     $block->getPluginId()->willReturn('block_plugin_id');
128     $block->getBaseId()->willReturn('block_plugin_id');
129     $block->getDerivativeId()->willReturn(NULL);
130     $block->getConfiguration()->willReturn([]);
131
132     $section = [
133       new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
134     ];
135     $expected = [
136       'content' => [
137         'some_uuid' => $render_array,
138       ],
139     ];
140     $result = (new Section('layout_onecol', [], $section))->toRenderArray();
141     $this->assertEquals($expected, $result);
142   }
143
144   /**
145    * @covers ::toRenderArray
146    */
147   public function testToRenderArrayAccessDenied() {
148     $block = $this->prophesize(BlockPluginInterface::class);
149     $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
150
151     $access_result = AccessResult::forbidden();
152     $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
153     $block->build()->shouldNotBeCalled();
154     $block->getCacheContexts()->willReturn([]);
155     $block->getCacheTags()->willReturn([]);
156     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
157
158     $section = [
159       new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
160     ];
161     $expected = [
162       'content' => [
163         'some_uuid' => [
164           '#cache' => [
165             'contexts' => [],
166             'tags' => [],
167             'max-age' => -1,
168           ],
169         ],
170       ],
171     ];
172     $result = (new Section('layout_onecol', [], $section))->toRenderArray();
173     $this->assertEquals($expected, $result);
174   }
175
176   /**
177    * @covers ::toRenderArray
178    */
179   public function testToRenderArrayPreview() {
180     $block_content = ['#markup' => 'The block content.'];
181     $render_array = [
182       '#theme' => 'block',
183       '#weight' => 0,
184       '#configuration' => [],
185       '#plugin_id' => 'block_plugin_id',
186       '#base_plugin_id' => 'block_plugin_id',
187       '#derivative_plugin_id' => NULL,
188       'content' => $block_content,
189       '#cache' => [
190         'contexts' => [],
191         'tags' => [],
192         'max-age' => 0,
193       ],
194     ];
195     $block = $this->prophesize(BlockPluginInterface::class);
196     $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
197
198     $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
199     $block->build()->willReturn($block_content);
200     $block->getCacheContexts()->willReturn([]);
201     $block->getCacheTags()->willReturn([]);
202     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
203     $block->getConfiguration()->willReturn([]);
204     $block->getPluginId()->willReturn('block_plugin_id');
205     $block->getBaseId()->willReturn('block_plugin_id');
206     $block->getDerivativeId()->willReturn(NULL);
207
208     $section = [
209       new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
210     ];
211     $expected = [
212       'content' => [
213         'some_uuid' => $render_array,
214       ],
215     ];
216     $result = (new Section('layout_onecol', [], $section))->toRenderArray([], TRUE);
217     $this->assertEquals($expected, $result);
218   }
219
220   /**
221    * @covers ::toRenderArray
222    */
223   public function testToRenderArrayEmpty() {
224     $section = [];
225     $expected = [];
226     $result = (new Section('layout_onecol', [], $section))->toRenderArray();
227     $this->assertEquals($expected, $result);
228   }
229
230   /**
231    * @covers ::toRenderArray
232    */
233   public function testContextAwareBlock() {
234     $render_array = [
235       '#theme' => 'block',
236       '#weight' => 0,
237       '#configuration' => [],
238       '#plugin_id' => 'block_plugin_id',
239       '#base_plugin_id' => 'block_plugin_id',
240       '#derivative_plugin_id' => NULL,
241       'content' => [],
242       '#cache' => [
243         'contexts' => [],
244         'tags' => [],
245         'max-age' => -1,
246       ],
247     ];
248
249     $block = $this->prophesize(BlockPluginInterface::class)->willImplement(ContextAwarePluginInterface::class);
250     $this->blockManager->createInstance('block_plugin_id', ['id' => 'block_plugin_id'])->willReturn($block->reveal());
251
252     $access_result = AccessResult::allowed();
253     $block->access($this->account->reveal(), TRUE)->willReturn($access_result);
254     $block->build()->willReturn([]);
255     $block->getCacheContexts()->willReturn([]);
256     $block->getCacheTags()->willReturn([]);
257     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
258     $block->getContextMapping()->willReturn([]);
259     $block->getPluginId()->willReturn('block_plugin_id');
260     $block->getBaseId()->willReturn('block_plugin_id');
261     $block->getDerivativeId()->willReturn(NULL);
262     $block->getConfiguration()->willReturn([]);
263
264     $section = [
265       new SectionComponent('some_uuid', 'content', ['id' => 'block_plugin_id']),
266     ];
267     $expected = [
268       'content' => [
269         'some_uuid' => $render_array,
270       ],
271     ];
272     $result = (new Section('layout_onecol', [], $section))->toRenderArray();
273     $this->assertEquals($expected, $result);
274   }
275
276   /**
277    * @covers ::toRenderArray
278    */
279   public function testToRenderArrayMissingPluginId() {
280     $this->setExpectedException(PluginException::class, 'No plugin ID specified for component with "some_uuid" UUID');
281     (new Section('layout_onecol', [], [new SectionComponent('some_uuid', 'content')]))->toRenderArray();
282   }
283
284 }