View source
<?php
namespace Drupal\Tests\entity_usage\Kernel;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block_content\Entity\BlockContentType;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
class EntityUsageLayoutBuilderTest extends KernelTestBase {
protected static $modules = [
'entity_track',
'entity_usage',
'entity_test',
'block_content',
'block',
'text',
'user',
'layout_builder',
'layout_discovery',
'field',
'system',
];
protected function setUp() {
parent::setUp();
$this
->installSchema('entity_usage', [
'entity_usage',
]);
$this
->installSchema('system', [
'key_value_expire',
]);
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('block_content');
LayoutBuilderEntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
])
->enableLayoutBuilder()
->setOverridable()
->save();
}
public function testLayoutBuilderInlineBlockUsage() {
$type = BlockContentType::create([
'id' => 'foo',
'label' => 'Foo',
]);
$type
->save();
$block = BlockContent::create([
'type' => $type
->id(),
]);
$block
->save();
$sectionData = [
new Section('layout_onecol', [], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'inline_block:' . $type
->id(),
'block_revision_id' => $block
->getRevisionId(),
]),
'second-uuid' => new SectionComponent('second-uuid', 'content', [
'id' => 'foo_block:plugin_not_found',
]),
]),
];
$entity = EntityTest::create([
OverridesSectionStorage::FIELD_NAME => $sectionData,
]);
$entity
->save();
$entityUsage = \Drupal::service('entity_usage.usage');
$usage = $entityUsage
->listSources($block);
$expected = [
$entity
->getEntityTypeId() => [
$entity
->id() => [
[
'source_langcode' => 'en',
'source_vid' => '0',
'method' => 'layout_builder',
'field_name' => 'layout_builder__layout',
'count' => '1',
],
],
],
];
$this
->assertEquals($expected, $usage);
}
}