function DefaultsSectionStorageTest::testExtractEntityFromRoute

Same name in other branches
  1. 8.9.x core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\DefaultsSectionStorageTest::testExtractEntityFromRoute()
  2. 10 core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\DefaultsSectionStorageTest::testExtractEntityFromRoute()
  3. 11.x core/modules/layout_builder/tests/src/Unit/DefaultsSectionStorageTest.php \Drupal\Tests\layout_builder\Unit\DefaultsSectionStorageTest::testExtractEntityFromRoute()

@covers ::extractEntityFromRoute

@dataProvider providerTestExtractEntityFromRoute

Parameters

bool $success: Whether a successful result is expected.

string|null $expected_entity_id: The expected entity 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/DefaultsSectionStorageTest.php, line 123

Class

DefaultsSectionStorageTest
@coversDefaultClass \Drupal\layout_builder\Plugin\SectionStorage\DefaultsSectionStorage

Namespace

Drupal\Tests\layout_builder\Unit

Code

public function testExtractEntityFromRoute($success, $expected_entity_id, $value, array $defaults) {
    if ($expected_entity_id) {
        $entity_storage = $this->prophesize(EntityStorageInterface::class);
        $entity_storage->load($expected_entity_id)
            ->willReturn('the_return_value');
        $this->entityTypeManager
            ->getDefinition('entity_view_display')
            ->willReturn(new EntityType([
            'id' => 'entity_view_display',
        ]));
        $this->entityTypeManager
            ->getStorage('entity_view_display')
            ->willReturn($entity_storage->reveal());
    }
    else {
        $this->entityTypeManager
            ->getDefinition('entity_view_display')
            ->shouldNotBeCalled();
        $this->entityTypeManager
            ->getStorage('entity_view_display')
            ->shouldNotBeCalled();
    }
    $method = new \ReflectionMethod($this->plugin, 'extractEntityFromRoute');
    $method->setAccessible(TRUE);
    $result = $method->invoke($this->plugin, $value, $defaults);
    if ($success) {
        $this->assertEquals('the_return_value', $result);
    }
    else {
        $this->assertNull($result);
    }
}

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