function StringFormatterTest::testLinkToContentForEntitiesWithMissingLinkTemplate

Same name and namespace in other branches
  1. 11.x core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php \Drupal\Tests\field\Kernel\KernelString\StringFormatterTest::testLinkToContentForEntitiesWithMissingLinkTemplate()

Test "link_to_entity" feature on fields which are added to config entity.

Attributes

#[TestWith([ 'canonical', ])] #[TestWith([ 'edit-form', ])]

File

core/modules/field/tests/src/Kernel/KernelString/StringFormatterTest.php, line 205

Class

StringFormatterTest
Tests the creation of text fields.

Namespace

Drupal\Tests\field\Kernel\KernelString

Code

public function testLinkToContentForEntitiesWithMissingLinkTemplate(string $link_rel) : void {
  $this->enableModules([
    'entity_test',
  ]);
  $this->installEntitySchema('entity_test_label');
  $field_name = 'test_field_name';
  $entity_type = $bundle = 'entity_test_label';
  $field_storage = FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'string',
  ]);
  $field_storage->save();
  $instance = FieldConfig::create([
    'field_storage' => $field_storage,
    'bundle' => $entity_type,
    'label' => $this->randomMachineName(),
  ]);
  $instance->save();
  $display = \Drupal::service('entity_display.repository')->getViewDisplay($entity_type, $bundle)
    ->setComponent($field_name, [
    'type' => 'string',
    'settings' => [
      'link_to_entity' => TRUE,
      'link_rel' => $link_rel,
    ],
    'region' => 'content',
  ]);
  $display->save();
  $value = $this->randomMachineName();
  $entity = EntityTestLabel::create([
    'name' => 'test',
  ]);
  // To prevent this test from falsely passing, ensure that there is, in fact,
  // no link template.
  $this->assertFalse($entity->getEntityType()
    ->hasLinkTemplate($link_rel));
  $entity->{$field_name}->value = $value;
  $entity->save();
  $this->renderEntityFields($entity, $display);
  $this->assertRaw($value);
  // No links should appear in the main content, since this entity type has no
  // link templates.
  $crawler = new Crawler($this->getRawContent());
  $this->assertCount(0, $crawler->filter('main a'));
}

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