function BlockComponentRenderArrayTest::testOnBuildRender

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php \Drupal\Tests\layout_builder\Unit\BlockComponentRenderArrayTest::testOnBuildRender()
  2. 8.9.x core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php \Drupal\Tests\layout_builder\Unit\BlockComponentRenderArrayTest::testOnBuildRender()
  3. 11.x core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php \Drupal\Tests\layout_builder\Unit\BlockComponentRenderArrayTest::testOnBuildRender()

@covers ::onBuildRender

@dataProvider providerBlockTypes

File

core/modules/layout_builder/tests/src/Unit/BlockComponentRenderArrayTest.php, line 77

Class

BlockComponentRenderArrayTest
@coversDefaultClass \Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray @group layout_builder

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testOnBuildRender($refinable_dependent_access) : void {
    $contexts = [];
    if ($refinable_dependent_access) {
        $block = $this->prophesize(TestBlockPluginWithRefinableDependentAccessInterface::class)
            ->willImplement(PreviewFallbackInterface::class);
        $layout_entity = $this->prophesize(EntityInterface::class);
        $layout_entity = $layout_entity->reveal();
        $context = $this->prophesize(ContextInterface::class);
        $context->getContextValue()
            ->willReturn($layout_entity);
        $contexts['layout_builder.entity'] = $context->reveal();
        $block->setAccessDependency($layout_entity)
            ->shouldBeCalled();
    }
    else {
        $block = $this->prophesize(BlockPluginInterface::class)
            ->willImplement(PreviewFallbackInterface::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->getConfiguration()
        ->willReturn([]);
    $block->getPluginId()
        ->willReturn('block_plugin_id');
    $block->getBaseId()
        ->willReturn('block_plugin_id');
    $block->getDerivativeId()
        ->willReturn(NULL);
    $placeholder_label = 'Placeholder Label';
    $block->getPreviewFallbackString()
        ->willReturn($placeholder_label);
    $block_content = [
        '#markup' => 'The block content.',
        '#cache' => [
            'tags' => [
                'build-tag',
            ],
        ],
    ];
    $block->build()
        ->willReturn($block_content);
    $this->blockManager
        ->createInstance('some_block_id', [
        'id' => 'some_block_id',
    ])
        ->willReturn($block->reveal());
    $component = new SectionComponent('some-uuid', 'some-region', [
        'id' => 'some_block_id',
    ]);
    $in_preview = FALSE;
    $event = new SectionComponentBuildRenderArrayEvent($component, $contexts, $in_preview);
    $subscriber = new BlockComponentRenderArray($this->account
        ->reveal());
    $expected_build = [
        '#theme' => 'block',
        '#weight' => 0,
        '#configuration' => [],
        '#plugin_id' => 'block_plugin_id',
        '#base_plugin_id' => 'block_plugin_id',
        '#derivative_plugin_id' => NULL,
        'content' => $block_content,
        '#in_preview' => FALSE,
    ];
    $expected_build_with_expected_cache = $expected_build + [
        '#cache' => [
            'contexts' => [],
            'tags' => [
                'build-tag',
                'test',
            ],
            'max-age' => -1,
        ],
    ];
    $subscriber->onBuildRender($event);
    $result = $event->getBuild();
    $this->assertEquals($expected_build, $result);
    $event->getCacheableMetadata()
        ->applyTo($result);
    $this->assertEqualsCanonicalizing($expected_build_with_expected_cache['#cache'], $result['#cache']);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.