function BlockComponentRenderArrayTest::testOnBuildRenderInPreviewEmptyBuild

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

@covers ::onBuildRender

File

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

Class

BlockComponentRenderArrayTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21layout_builder%21src%21EventSubscriber%21BlockComponentRenderArray.php/class/BlockComponentRenderArray/9" title="Builds render arrays and handles access for all block components." class="local">\Drupal\layout_builder\EventSubscriber\BlockComponentRenderArray</a> @group layout_builder

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testOnBuildRenderInPreviewEmptyBuild() {
    $block = $this->prophesize(BlockPluginInterface::class)
        ->willImplement(PreviewFallbackInterface::class);
    $block->access($this->account
        ->reveal(), TRUE)
        ->shouldNotBeCalled();
    $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);
    $block->getPluginDefinition()
        ->willReturn([
        'admin_label' => 'adminlabel',
    ]);
    $placeholder_string = 'The placeholder string';
    $block->getPreviewFallbackString()
        ->willReturn($placeholder_string);
    $block_content = [];
    $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',
    ]);
    $event = new SectionComponentBuildRenderArrayEvent($component, [], TRUE);
    $subscriber = new BlockComponentRenderArray($this->account
        ->reveal());
    $translation = $this->prophesize(TranslationInterface::class);
    $translation->translateString(Argument::type(TranslatableMarkup::class))
        ->willReturn($placeholder_string);
    $subscriber->setStringTranslation($translation->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,
        '#attributes' => [
            'data-layout-content-preview-placeholder-label' => $placeholder_string,
        ],
        '#in_preview' => TRUE,
    ];
    $expected_build['content']['#markup'] = $placeholder_string;
    $expected_cache = $expected_build + [
        '#cache' => [
            'contexts' => [],
            'tags' => [
                'test',
            ],
            'max-age' => 0,
        ],
        '#in_preview' => TRUE,
    ];
    $subscriber->onBuildRender($event);
    $result = $event->getBuild();
    $this->assertEquals($expected_build, $result);
    $event->getCacheableMetadata()
        ->applyTo($result);
    $this->assertEquals($expected_cache, $result);
}

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