function OverridesSectionStorageTest::testExtractEntityFromRoute

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()
  2. 8.9.x core/modules/layout_builder/tests/src/Unit/OverridesSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\OverridesSectionStorageTest::testExtractEntityFromRoute()
  3. 10 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 91

Class

OverridesSectionStorageTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21layout_builder%21src%21Plugin%21SectionStorage%21OverridesSectionStorage.php/class/OverridesSectionStorage/11.x" title="Defines the &#039;overrides&#039; section storage type." class="local">\Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage</a>

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testExtractEntityFromRoute($success, $expected_entity_type_id, $value, array $defaults) : void {
    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');
    $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.