function PageContextTest::testBuildEntityLabel

Tests the build of an entity label within the page context plugin.

Attributes

#[DataProvider('entityLabelProvider')]

Parameters

mixed $label_value: The return value of the entity label method. Can be a string, render array, stringable object, or an invalid value.

string|array|null $expected: The expected parsed label value for the page context. If the label is invalid, the value should be NULL.

File

core/modules/navigation/tests/src/Unit/PageContextTest.php, line 71

Class

PageContextTest
Tests the PageContext Top Bar item build output.

Namespace

Drupal\Tests\navigation\Unit

Code

public function testBuildEntityLabel(mixed $label_value, string|array|null $expected) : void {
  // Route returns an entity with different label return types.
  $entity = $this->prophesize(ContentEntityInterface::class);
  $entity->label()
    ->willReturn($label_value);
  $route_helper = $this->prophesize(EntityRouteHelper::class);
  $route_helper->getContentEntityFromRoute()
    ->willReturn($entity->reveal());
  $entity_repository = $this->prophesize(EntityRepositoryInterface::class)
    ->reveal();
  $moderation_information = $this->prophesize(ModerationInformationInterface::class)
    ->reveal();
  $plugin = new PageContext([], 'page_context', [], $route_helper->reveal(), $entity_repository, $moderation_information);
  $build = $plugin->build();
  $expected_base = [
    '#cache' => [
      'contexts' => [
        'route',
      ],
    ],
  ];
  if ($expected === NULL) {
    // Invalid label → no components added.
    $this->assertSame($expected_base, $build);
    return;
  }
  // Title component should be present as the first item.
  $this->assertArrayHasKey(0, $build);
  $this->assertSame('component', $build[0]['#type']);
  $this->assertSame('navigation:title', $build[0]['#component']);
  $this->assertSame('database', $build[0]['#props']['icon']);
  $this->assertSame($expected, $build[0]['#slots']['content']);
}

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