Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / layout_builder / tests / src / Unit / BlockComponentRenderArrayTest.php
1 <?php
2
3 namespace Drupal\Tests\layout_builder\Unit;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockManagerInterface;
7 use Drupal\Core\Block\BlockPluginInterface;
8 use Drupal\Core\Cache\Cache;
9 use Drupal\Core\DependencyInjection\ContainerBuilder;
10 use Drupal\Core\Session\AccountInterface;
11 use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent;
12 use Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray;
13 use Drupal\layout_builder\SectionComponent;
14 use Drupal\Tests\UnitTestCase;
15
16 /**
17  * @coversDefaultClass \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray
18  * @group layout_builder
19  */
20 class BlockComponentRenderArrayTest extends UnitTestCase {
21
22   /**
23    * The current user.
24    *
25    * @var \Drupal\Core\Session\AccountInterface
26    */
27   protected $account;
28
29   /**
30    * The block plugin manager.
31    *
32    * @var \Drupal\Core\Block\BlockManagerInterface
33    */
34   protected $blockManager;
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41
42     $this->blockManager = $this->prophesize(BlockManagerInterface::class);
43     $this->account = $this->prophesize(AccountInterface::class);
44
45     $container = new ContainerBuilder();
46     $container->set('plugin.manager.block', $this->blockManager->reveal());
47     \Drupal::setContainer($container);
48   }
49
50   /**
51    * @covers ::onBuildRender
52    */
53   public function testOnBuildRender() {
54     $block = $this->prophesize(BlockPluginInterface::class);
55     $access_result = AccessResult::allowed();
56     $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
57     $block->getCacheContexts()->willReturn([]);
58     $block->getCacheTags()->willReturn(['test']);
59     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
60     $block->getConfiguration()->willReturn([]);
61     $block->getPluginId()->willReturn('block_plugin_id');
62     $block->getBaseId()->willReturn('block_plugin_id');
63     $block->getDerivativeId()->willReturn(NULL);
64
65     $block_content = ['#markup' => 'The block content.'];
66     $block->build()->willReturn($block_content);
67     $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
68
69     $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
70     $contexts = [];
71     $in_preview = FALSE;
72     $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
73
74     $subscriber = new BlockComponentRenderArray($this->account->reveal());
75
76     $expected_build = [
77       '#theme' => 'block',
78       '#weight' => 0,
79       '#configuration' => [],
80       '#plugin_id' => 'block_plugin_id',
81       '#base_plugin_id' => 'block_plugin_id',
82       '#derivative_plugin_id' => NULL,
83       'content' => $block_content,
84     ];
85
86     $expected_cache = $expected_build + [
87       '#cache' => [
88         'contexts' => [],
89         'tags' => ['test'],
90         'max-age' => -1,
91       ],
92     ];
93
94     $subscriber->onBuildRender($event);
95     $result = $event->getBuild();
96     $this->assertEquals($expected_build, $result);
97     $event->getCacheableMetadata()->applyTo($result);
98     $this->assertEquals($expected_cache, $result);
99   }
100
101   /**
102    * @covers ::onBuildRender
103    */
104   public function testOnBuildRenderDenied() {
105     $block = $this->prophesize(BlockPluginInterface::class);
106     $access_result = AccessResult::forbidden();
107     $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
108     $block->getCacheContexts()->shouldNotBeCalled();
109     $block->getCacheTags()->shouldNotBeCalled();
110     $block->getCacheMaxAge()->shouldNotBeCalled();
111     $block->getConfiguration()->shouldNotBeCalled();
112     $block->getPluginId()->shouldNotBeCalled();
113     $block->getBaseId()->shouldNotBeCalled();
114     $block->getDerivativeId()->shouldNotBeCalled();
115
116     $block_content = ['#markup' => 'The block content.'];
117     $block->build()->willReturn($block_content);
118     $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
119
120     $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
121     $contexts = [];
122     $in_preview = FALSE;
123     $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
124
125     $subscriber = new BlockComponentRenderArray($this->account->reveal());
126
127     $expected_build = [];
128     $expected_cache = [
129       '#cache' => [
130         'contexts' => [],
131         'tags' => [],
132         'max-age' => -1,
133       ],
134     ];
135
136     $subscriber->onBuildRender($event);
137     $result = $event->getBuild();
138     $this->assertEquals($expected_build, $result);
139     $event->getCacheableMetadata()->applyTo($result);
140     $this->assertEquals($expected_cache, $result);
141   }
142
143   /**
144    * @covers ::onBuildRender
145    */
146   public function testOnBuildRenderInPreview() {
147     $block = $this->prophesize(BlockPluginInterface::class);
148     $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled();
149     $block->getCacheContexts()->willReturn([]);
150     $block->getCacheTags()->willReturn(['test']);
151     $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
152     $block->getConfiguration()->willReturn([]);
153     $block->getPluginId()->willReturn('block_plugin_id');
154     $block->getBaseId()->willReturn('block_plugin_id');
155     $block->getDerivativeId()->willReturn(NULL);
156
157     $block_content = ['#markup' => 'The block content.'];
158     $block->build()->willReturn($block_content);
159     $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
160
161     $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
162     $contexts = [];
163     $in_preview = TRUE;
164     $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
165
166     $subscriber = new BlockComponentRenderArray($this->account->reveal());
167
168     $expected_build = [
169       '#theme' => 'block',
170       '#weight' => 0,
171       '#configuration' => [],
172       '#plugin_id' => 'block_plugin_id',
173       '#base_plugin_id' => 'block_plugin_id',
174       '#derivative_plugin_id' => NULL,
175       'content' => $block_content,
176     ];
177
178     $expected_cache = $expected_build + [
179       '#cache' => [
180         'contexts' => [],
181         'tags' => ['test'],
182         'max-age' => 0,
183       ],
184     ];
185
186     $subscriber->onBuildRender($event);
187     $result = $event->getBuild();
188     $this->assertEquals($expected_build, $result);
189     $event->getCacheableMetadata()->applyTo($result);
190     $this->assertEquals($expected_cache, $result);
191   }
192
193   /**
194    * @covers ::onBuildRender
195    */
196   public function testOnBuildRenderNoBlock() {
197     $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn(NULL);
198
199     $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
200     $contexts = [];
201     $in_preview = FALSE;
202     $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
203
204     $subscriber = new BlockComponentRenderArray($this->account->reveal());
205
206     $expected_build = [];
207     $expected_cache = [
208       '#cache' => [
209         'contexts' => [],
210         'tags' => [],
211         'max-age' => -1,
212       ],
213     ];
214
215     $subscriber->onBuildRender($event);
216     $result = $event->getBuild();
217     $this->assertEquals($expected_build, $result);
218     $event->getCacheableMetadata()->applyTo($result);
219     $this->assertEquals($expected_cache, $result);
220   }
221
222 }