You are here

public function EntityReferenceFormatterTest::testLabelFormatter in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
  2. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()

Tests the label formatter.

File

core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php, line 343

Class

EntityReferenceFormatterTest
Tests the formatters functionality.

Namespace

Drupal\Tests\field\Kernel\EntityReference

Code

public function testLabelFormatter() {
  $this
    ->installEntitySchema('entity_test_label');

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $formatter = 'entity_reference_label';

  // The 'link' settings is TRUE by default.
  $build = $this
    ->buildRenderArray([
    $this->referencedEntity,
    $this->unsavedReferencedEntity,
  ], $formatter);
  $expected_field_cacheability = [
    'contexts' => [],
    'tags' => [],
    'max-age' => Cache::PERMANENT,
  ];
  $this
    ->assertEquals($expected_field_cacheability, $build['#cache'], 'The field render array contains the entity access cacheability metadata');
  $expected_item_1 = [
    '#type' => 'link',
    '#title' => $this->referencedEntity
      ->label(),
    '#url' => $this->referencedEntity
      ->toUrl(),
    '#options' => $this->referencedEntity
      ->toUrl()
      ->getOptions(),
    '#cache' => [
      'contexts' => [
        'user.permissions',
      ],
      'tags' => $this->referencedEntity
        ->getCacheTags(),
    ],
  ];
  $this
    ->assertEquals($renderer
    ->renderRoot($expected_item_1), $renderer
    ->renderRoot($build[0]), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
  $this
    ->assertEquals(CacheableMetadata::createFromRenderArray($expected_item_1), CacheableMetadata::createFromRenderArray($build[0]));

  // The second referenced entity is "autocreated", therefore not saved and
  // lacking any URL info.
  $expected_item_2 = [
    '#plain_text' => $this->unsavedReferencedEntity
      ->label(),
    '#cache' => [
      'contexts' => [
        'user.permissions',
      ],
      'tags' => $this->unsavedReferencedEntity
        ->getCacheTags(),
      'max-age' => Cache::PERMANENT,
    ],
  ];
  $this
    ->assertEquals($expected_item_2, $build[1], sprintf('The render array returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));

  // Test with the 'link' setting set to FALSE.
  $build = $this
    ->buildRenderArray([
    $this->referencedEntity,
    $this->unsavedReferencedEntity,
  ], $formatter, [
    'link' => FALSE,
  ]);
  $this
    ->assertEquals($this->referencedEntity
    ->label(), $build[0]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
  $this
    ->assertEquals($this->unsavedReferencedEntity
    ->label(), $build[1]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an item with a unsaved entity.', $formatter));

  // Test an entity type that doesn't have any link templates, which means
  // \Drupal\Core\Entity\EntityInterface::urlInfo() will throw an exception
  // and the label formatter will output only the label instead of a link.
  $field_storage_config = FieldStorageConfig::loadByName($this->entityType, $this->fieldName);
  $field_storage_config
    ->setSetting('target_type', 'entity_test_label');
  $field_storage_config
    ->save();
  $referenced_entity_with_no_link_template = EntityTestLabel::create([
    'name' => $this
      ->randomMachineName(),
  ]);
  $referenced_entity_with_no_link_template
    ->save();
  $build = $this
    ->buildRenderArray([
    $referenced_entity_with_no_link_template,
  ], $formatter, [
    'link' => TRUE,
  ]);
  $this
    ->assertEquals($referenced_entity_with_no_link_template
    ->label(), $build[0]['#plain_text'], sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}