|
11 | 11 | use Drupal\Core\DependencyInjection\ContainerBuilder; |
12 | 12 | use Drupal\Core\Entity\EntityInterface; |
13 | 13 | use Drupal\Core\Plugin\Context\ContextHandlerInterface; |
| 14 | +use Drupal\Core\Render\PreviewFallbackInterface; |
14 | 15 | use Drupal\Core\Session\AccountInterface; |
15 | 16 | use Drupal\layout_builder\Access\LayoutPreviewAccessAllowed; |
16 | 17 | use Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent; |
@@ -252,6 +253,98 @@ public function testOnBuildRenderInPreview($refinable_dependent_access) { |
252 | 253 | $this->assertEquals($expected_cache, $result); |
253 | 254 | } |
254 | 255 |
|
| 256 | + /** |
| 257 | + * @covers ::onBuildRender |
| 258 | + */ |
| 259 | + public function testOnBuildRenderInPreviewEmptyBuild() { |
| 260 | + $block = $this->prophesize(BlockPluginInterface::class)->willImplement(PreviewFallbackInterface::class); |
| 261 | + |
| 262 | + $block->access($this->account->reveal(), TRUE)->shouldNotBeCalled(); |
| 263 | + $block->getCacheContexts()->willReturn([]); |
| 264 | + $block->getCacheTags()->willReturn(['test']); |
| 265 | + $block->getCacheMaxAge()->willReturn(Cache::PERMANENT); |
| 266 | + $block->getConfiguration()->willReturn([]); |
| 267 | + $block->getPluginId()->willReturn('block_plugin_id'); |
| 268 | + $block->getBaseId()->willReturn('block_plugin_id'); |
| 269 | + $block->getDerivativeId()->willReturn(NULL); |
| 270 | + $placeholder_string = 'The placeholder string'; |
| 271 | + $block->getPreviewFallbackString()->willReturn($placeholder_string); |
| 272 | + |
| 273 | + $block_content = []; |
| 274 | + $block->build()->willReturn($block_content); |
| 275 | + $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); |
| 276 | + |
| 277 | + $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']); |
| 278 | + $event = new SectionComponentBuildRenderArrayEvent($component, [], TRUE); |
| 279 | + |
| 280 | + $subscriber = new BlockComponentRenderArray($this->account->reveal()); |
| 281 | + |
| 282 | + $expected_build = [ |
| 283 | + '#theme' => 'block', |
| 284 | + '#weight' => 0, |
| 285 | + '#configuration' => [], |
| 286 | + '#plugin_id' => 'block_plugin_id', |
| 287 | + '#base_plugin_id' => 'block_plugin_id', |
| 288 | + '#derivative_plugin_id' => NULL, |
| 289 | + 'content' => $block_content, |
| 290 | + ]; |
| 291 | + $expected_build['content']['#markup'] = $placeholder_string; |
| 292 | + |
| 293 | + $expected_cache = $expected_build + [ |
| 294 | + '#cache' => [ |
| 295 | + 'contexts' => [], |
| 296 | + 'tags' => ['test'], |
| 297 | + 'max-age' => 0, |
| 298 | + ], |
| 299 | + ]; |
| 300 | + |
| 301 | + $subscriber->onBuildRender($event); |
| 302 | + $result = $event->getBuild(); |
| 303 | + $this->assertEquals($expected_build, $result); |
| 304 | + $event->getCacheableMetadata()->applyTo($result); |
| 305 | + $this->assertEquals($expected_cache, $result); |
| 306 | + } |
| 307 | + |
| 308 | + /** |
| 309 | + * @covers ::onBuildRender |
| 310 | + */ |
| 311 | + public function testOnBuildRenderEmptyBuild() { |
| 312 | + $block = $this->prophesize(BlockPluginInterface::class); |
| 313 | + $access_result = AccessResult::allowed(); |
| 314 | + $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled(); |
| 315 | + $block->getCacheContexts()->willReturn([]); |
| 316 | + $block->getCacheTags()->willReturn(['test']); |
| 317 | + $block->getCacheMaxAge()->willReturn(Cache::PERMANENT); |
| 318 | + $block->getConfiguration()->willReturn([]); |
| 319 | + $block->getPluginId()->willReturn('block_plugin_id'); |
| 320 | + $block->getBaseId()->willReturn('block_plugin_id'); |
| 321 | + $block->getDerivativeId()->willReturn(NULL); |
| 322 | + |
| 323 | + $block->build()->willReturn([]); |
| 324 | + $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal()); |
| 325 | + |
| 326 | + $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']); |
| 327 | + $event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE); |
| 328 | + |
| 329 | + $subscriber = new BlockComponentRenderArray($this->account->reveal()); |
| 330 | + |
| 331 | + $expected_build = []; |
| 332 | + |
| 333 | + $expected_cache = $expected_build + [ |
| 334 | + '#cache' => [ |
| 335 | + 'contexts' => [], |
| 336 | + 'tags' => ['test'], |
| 337 | + 'max-age' => -1, |
| 338 | + ], |
| 339 | + ]; |
| 340 | + |
| 341 | + $subscriber->onBuildRender($event); |
| 342 | + $result = $event->getBuild(); |
| 343 | + $this->assertEquals($expected_build, $result); |
| 344 | + $event->getCacheableMetadata()->applyTo($result); |
| 345 | + $this->assertEquals($expected_cache, $result); |
| 346 | + } |
| 347 | + |
255 | 348 | /** |
256 | 349 | * @covers ::onBuildRender |
257 | 350 | */ |
|
0 commit comments