You are here

public function EntityUsageLayoutBuilderTest::testLayoutBuilderInlineBlockUsage in Entity Usage 8.2

Same name in this branch
  1. 8.2 tests/src/Functional/EntityUsageLayoutBuilderTest.php \Drupal\Tests\entity_usage\Functional\EntityUsageLayoutBuilderTest::testLayoutBuilderInlineBlockUsage()
  2. 8.2 tests/src/Kernel/EntityUsageLayoutBuilderTest.php \Drupal\Tests\entity_usage\Kernel\EntityUsageLayoutBuilderTest::testLayoutBuilderInlineBlockUsage()
Same name and namespace in other branches
  1. 8.4 tests/src/Functional/EntityUsageLayoutBuilderTest.php \Drupal\Tests\entity_usage\Functional\EntityUsageLayoutBuilderTest::testLayoutBuilderInlineBlockUsage()

Test entities referenced by block content in LB are shown on usage page.

E.g, if entityHost (with LB) -> Block Content -> entityInner, when navigating to entityInner, the source relationship is shown as ultimately coming from entityHost (via Block Content).

File

tests/src/Functional/EntityUsageLayoutBuilderTest.php, line 82

Class

EntityUsageLayoutBuilderTest
Tests layout builder usage through Inline Blocks displays in UI.

Namespace

Drupal\Tests\entity_usage\Functional

Code

public function testLayoutBuilderInlineBlockUsage() {
  $innerEntity = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $innerEntity
    ->save();
  $type = BlockContentType::create([
    'id' => 'foo',
    'label' => 'Foo',
  ]);
  $type
    ->save();
  $fieldStorage = FieldStorageConfig::create([
    'field_name' => 'myref',
    'entity_type' => 'block_content',
    'type' => 'entity_reference',
    'settings' => [
      'target_type' => 'entity_test',
    ],
  ]);
  $fieldStorage
    ->save();
  $field = FieldConfig::create([
    'field_storage' => $fieldStorage,
    'bundle' => $type
      ->id(),
  ]);
  $field
    ->save();
  $block = BlockContent::create([
    'type' => $type
      ->id(),
    'reusable' => 0,
    'myref' => $innerEntity,
  ]);
  $block
    ->save();
  $sectionData = [
    new Section('layout_onecol', [], [
      'first-uuid' => new SectionComponent('first-uuid', 'content', [
        'id' => 'inline_block:' . $type
          ->id(),
        'block_revision_id' => $block
          ->getRevisionId(),
      ]),
    ]),
  ];
  $entityHost = EntityTest::create([
    'name' => $this
      ->randomMachineName(),
    OverridesSectionStorage::FIELD_NAME => $sectionData,
  ]);
  $entityHost
    ->save();
  $this
    ->drupalLogin($this
    ->drupalCreateUser([
    'access entity usage statistics',
    'view test entity',
  ]));
  $this
    ->drupalGet(Url::fromRoute('entity.entity_test.entity_usage', [
    'entity_test' => $innerEntity
      ->id(),
  ]));
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $row1 = $this
    ->assertSession()
    ->elementExists('css', 'table tbody tr:nth-child(1)');
  $link = $this
    ->assertSession()
    ->elementExists('css', 'td:nth-child(1) a', $row1);
  $this
    ->assertEquals($entityHost
    ->label(), $link
    ->getText());
  $this
    ->assertEquals($link
    ->getAttribute('href'), $entityHost
    ->toUrl()
    ->toString());
}