function EntityReferenceFormatterTest::testLabelFormatter

Same name and namespace in other branches
  1. 9 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
  2. 10 core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php \Drupal\Tests\field\Kernel\EntityReference\EntityReferenceFormatterTest::testLabelFormatter()
  3. 11.x 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 346

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->assertEqual($build['#cache'], $expected_field_cacheability, '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->assertEqual($renderer->renderRoot($build[0]), $renderer->renderRoot($expected_item_1), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
    $this->assertEqual(CacheableMetadata::createFromRenderArray($build[0]), CacheableMetadata::createFromRenderArray($expected_item_1));
    // 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->assertEqual($build[1], $expected_item_2, 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->assertEqual($build[0]['#plain_text'], $this->referencedEntity
        ->label(), sprintf('The markup returned by the %s formatter is correct for an item with a saved entity.', $formatter));
    $this->assertEqual($build[1]['#plain_text'], $this->unsavedReferencedEntity
        ->label(), 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->assertEqual($build[0]['#plain_text'], $referenced_entity_with_no_link_template->label(), sprintf('The markup returned by the %s formatter is correct for an entity type with no valid link template.', $formatter));
}

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