You are here

public function StringFormatterTest::testLinkToContentForEntitiesWithNoCanonicalPath in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/field/tests/src/Kernel/String/StringFormatterTest.php \Drupal\Tests\field\Kernel\String\StringFormatterTest::testLinkToContentForEntitiesWithNoCanonicalPath()

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

File

core/modules/field/tests/src/Kernel/String/StringFormatterTest.php, line 193

Class

StringFormatterTest
Tests the creation of text fields.

Namespace

Drupal\Tests\field\Kernel\String

Code

public function testLinkToContentForEntitiesWithNoCanonicalPath() {
  $this
    ->enableModules([
    'entity_test',
  ]);
  $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,
    ],
    'region' => 'content',
  ]);
  $display
    ->save();
  $value = $this
    ->randomMachineName();
  $entity = EntityTestLabel::create([
    'name' => 'test',
  ]);
  $entity->{$field_name}->value = $value;
  $entity
    ->save();
  $this
    ->renderEntityFields($entity, $display);
  $this
    ->assertRaw($value);
}