function OverridesSectionStorageTest::testExtractEntityFromRoute

Same name in other branches
  1. 9 core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\OverridesSectionStorageTest::testExtractEntityFromRoute()
  2. 10 core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\OverridesSectionStorageTest::testExtractEntityFromRoute()
  3. 11.x 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);
    }
}

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