function StringFormatterTest::testLinkToContentForEntitiesWithNoCanonicalPath

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

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

File

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

Class

StringFormatterTest
Tests the creation of text fields.

Namespace

Drupal\Tests\field\Kernel\KernelString

Code

public function testLinkToContentForEntitiesWithNoCanonicalPath() : void {
  $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);
}

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