You are here

public function LayoutEntityHelperTraitTest::testGetSectionStorageForEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/layout_builder/tests/src/Kernel/LayoutEntityHelperTraitTest.php \Drupal\Tests\layout_builder\Kernel\LayoutEntityHelperTraitTest::testGetSectionStorageForEntity()

@covers ::getSectionStorageForEntity

@dataProvider providerTestGetSectionStorageForEntity

File

core/modules/layout_builder/tests/src/Kernel/LayoutEntityHelperTraitTest.php, line 81

Class

LayoutEntityHelperTraitTest
@coversDefaultClass \Drupal\layout_builder\LayoutEntityHelperTrait

Namespace

Drupal\Tests\layout_builder\Kernel

Code

public function testGetSectionStorageForEntity($entity_type_id, $values, $expected_context_keys) {
  $section_storage_manager = $this
    ->prophesize(SectionStorageManagerInterface::class);
  $section_storage_manager
    ->load('')
    ->willReturn(NULL);
  $section_storage_manager
    ->findByContext(Argument::cetera())
    ->will(function ($arguments) {
    return $arguments[0];
  });
  $this->container
    ->set('plugin.manager.layout_builder.section_storage', $section_storage_manager
    ->reveal());
  $entity = $this->container
    ->get('entity_type.manager')
    ->getStorage($entity_type_id)
    ->create($values);
  $entity
    ->save();
  $class = new TestLayoutEntityHelperTrait();
  $result = $class
    ->getSectionStorageForEntity($entity);
  $this
    ->assertEquals($expected_context_keys, array_keys($result));
  if ($entity instanceof EntityViewDisplayInterface) {
    $this
      ->assertEquals(EntityContext::fromEntity($entity), $result['display']);
  }
  elseif ($entity instanceof FieldableEntityInterface) {
    $this
      ->assertEquals(EntityContext::fromEntity($entity), $result['entity']);
    $this
      ->assertInstanceOf(Context::class, $result['view_mode']);
    $this
      ->assertEquals('full', $result['view_mode']
      ->getContextData()
      ->getValue());
    $expected_display = EntityViewDisplay::collectRenderDisplay($entity, 'full');
    $this
      ->assertInstanceOf(EntityContext::class, $result['display']);

    /** @var \Drupal\Core\Plugin\Context\EntityContext $display_entity_context */
    $display_entity_context = $result['display'];

    /** @var \Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay $display_entity */
    $display_entity = $display_entity_context
      ->getContextData()
      ->getValue();
    $this
      ->assertInstanceOf(LayoutBuilderEntityViewDisplay::class, $display_entity);
    $this
      ->assertEquals('full', $display_entity
      ->getMode());
    $this
      ->assertEquals($expected_display
      ->getEntityTypeId(), $display_entity
      ->getEntityTypeId());
    $this
      ->assertEquals($expected_display
      ->getComponents(), $display_entity
      ->getComponents());
    $this
      ->assertEquals($expected_display
      ->getThirdPartySettings('layout_builder'), $display_entity
      ->getThirdPartySettings('layout_builder'));
  }
  else {
    throw new \UnexpectedValueException("Unexpected entity type.");
  }
}