Skip to content
Snippets Groups Projects
Commit 565849d6 authored by catch's avatar catch
Browse files

Issue #3162699 by tim.plunkett, smustgrave, nicxvan, sam152, lauriii: Improve...

Issue #3162699 by tim.plunkett, smustgrave, nicxvan, sam152, lauriii: Improve debugability of block plugins returning NULL in Layout Builder
parent f6a83bdf
No related branches found
No related tags found
4 merge requests!5423Draft: Resolve #3329907 "Test2",!3478Issue #3337882: Deleted menus are not removed from content type config,!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!579Issue #2230909: Simple decimals fail to pass validation
Pipeline #447447 passed with warnings
Pipeline: drupal

#447451

    <?php
    declare(strict_types=1);
    namespace Drupal\block_test\Plugin\Block;
    use Drupal\Core\Block\Attribute\Block;
    use Drupal\Core\Block\BlockBase;
    use Drupal\Core\StringTranslation\TranslatableMarkup;
    /**
    * Provides a block that returns an empty array.
    */
    #[Block(
    id: "test_empty",
    admin_label: new TranslatableMarkup("Test Empty block"),
    )]
    class TestEmptyBlock extends BlockBase {
    /**
    * {@inheritdoc}
    */
    public function build() {
    return [];
    }
    }
    ......@@ -69,6 +69,27 @@ protected function setUp(): void {
    $this->renderer = $this->container->get('renderer');
    }
    /**
    * Tests rendering a block plugin that returns an empty array.
    */
    public function testEmptyRender(): void {
    \Drupal::keyValue('block_test')->set('content', '');
    $entity = $this->controller->create([
    'id' => 'test_block1',
    'theme' => 'stark',
    'plugin' => 'test_empty',
    ]);
    $entity->save();
    // Test the rendering of a block.
    $entity = Block::load('test_block1');
    $builder = \Drupal::entityTypeManager()->getViewBuilder('block');
    $output = $builder->view($entity, 'block');
    $expected_output = '';
    $this->assertSame($expected_output, (string) $this->renderer->renderRoot($output));
    }
    /**
    * Tests the rendering of blocks.
    */
    ......
    ......@@ -104,6 +104,10 @@ public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    }
    $content = $block->build();
    // @todo Remove when https://www.drupal.org/node/3164389 is resolved.
    if (!is_array($content)) {
    throw new \UnexpectedValueException(sprintf('The block "%s" did not return an array', get_class($block)));
    }
    // We don't output the block render data if there are no render elements
    // found, but we want to capture the cache metadata from the block
    ......
    ......@@ -499,6 +499,29 @@ public function testOnBuildRenderEmptyBuildWithCacheTags(): void {
    $this->assertEqualsCanonicalizing($expected_cache, $result);
    }
    /**
    * @covers ::onBuildRender
    */
    public function testOnBuildRenderNullBuild(): void {
    $block = $this->prophesize(BlockPluginInterface::class);
    $access_result = AccessResult::allowed();
    $block->access($this->account->reveal(), TRUE)->willReturn($access_result)->shouldBeCalled();
    $block->getCacheContexts()->willReturn([]);
    $block->getCacheTags()->willReturn(['test']);
    $block->getCacheMaxAge()->willReturn(Cache::PERMANENT);
    $block->build()->willReturn(NULL);
    $this->expectException(\UnexpectedValueException::class);
    $this->expectExceptionMessage(sprintf('The block "%s" did not return an array', get_class($block->reveal())));
    $this->blockManager->createInstance('some_block_id', ['id' => 'some_block_id'])->willReturn($block->reveal());
    $component = new SectionComponent('some-uuid', 'some-region', ['id' => 'some_block_id']);
    $event = new SectionComponentBuildRenderArrayEvent($component, [], FALSE);
    $subscriber = new BlockComponentRenderArray($this->account->reveal());
    $subscriber->onBuildRender($event);
    }
    /**
    * @covers ::onBuildRender
    */
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment