You are here

public function OverridesSectionStorageTest::testExtractEntityFromRoute in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\OverridesSectionStorageTest::testExtractEntityFromRoute()

@covers ::extractEntityFromRoute

@dataProvider providerTestExtractEntityFromRoute

Parameters

bool $success: Whether a successful result is expected.

string|null $expected_entity_type_id: The expected entity type ID.

string $value: The value to pass to ::extractEntityFromRoute().

array $defaults: The defaults to pass to ::extractEntityFromRoute().

File

core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php, line 198

Class

OverridesSectionStorageTest
@coversDefaultClass \Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testExtractEntityFromRoute($success, $expected_entity_type_id, $value, array $defaults) {
  if ($expected_entity_type_id) {
    $entity_without_layout = $this
      ->prophesize(FieldableEntityInterface::class);
    $entity_without_layout
      ->hasField(OverridesSectionStorage::FIELD_NAME)
      ->willReturn(FALSE);
    $this->entityRepository
      ->getActive($expected_entity_type_id, 'entity_without_layout')
      ->willReturn($entity_without_layout
      ->reveal());
    $entity_with_layout = $this
      ->prophesize(FieldableEntityInterface::class);
    $entity_with_layout
      ->hasField(OverridesSectionStorage::FIELD_NAME)
      ->willReturn(TRUE);
    $this->entityRepository
      ->getActive($expected_entity_type_id, 'entity_with_layout')
      ->willReturn($entity_with_layout
      ->reveal());
    $entity_type = new EntityType([
      'id' => $expected_entity_type_id,
    ]);
    $this->entityTypeManager
      ->getDefinition($expected_entity_type_id)
      ->willReturn($entity_type);
  }
  else {
    $this->entityRepository
      ->getActive(Argument::any())
      ->shouldNotBeCalled();
  }
  $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute');
  $method
    ->setAccessible(TRUE);
  $result = $method
    ->invoke($this->plugin, $value, $defaults);
  if ($success) {
    $this
      ->assertInstanceOf(FieldableEntityInterface::class, $result);
  }
  else {
    $this
      ->assertNull($result);
  }
}