3 namespace Drupal\Tests\field\Kernel\String;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\entity_test\Entity\EntityTestRev;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\KernelTests\KernelTestBase;
14 * Tests the creation of text fields.
18 class StringFormatterTest extends KernelTestBase {
25 public static $modules = ['field', 'text', 'entity_test', 'system', 'filter', 'user'];
28 * The entity type manager.
30 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
32 protected $entityTypeManager;
37 protected $entityType;
50 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
57 protected function setUp() {
60 // Configure the theme system.
61 $this->installConfig(['system', 'field']);
62 \Drupal::service('router.builder')->rebuild();
63 $this->installEntitySchema('entity_test_rev');
65 $this->entityType = 'entity_test_rev';
66 $this->bundle = $this->entityType;
67 $this->fieldName = mb_strtolower($this->randomMachineName());
69 $field_storage = FieldStorageConfig::create([
70 'field_name' => $this->fieldName,
71 'entity_type' => $this->entityType,
74 $field_storage->save();
76 $instance = FieldConfig::create([
77 'field_storage' => $field_storage,
78 'bundle' => $this->bundle,
79 'label' => $this->randomMachineName(),
83 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
84 ->setComponent($this->fieldName, [
88 $this->display->save();
90 $this->entityTypeManager = \Drupal::entityTypeManager();
94 * Renders fields of a given entity with a given display.
96 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
97 * The entity object with attached fields to render.
98 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
99 * The display to render the fields in.
102 * The rendered entity fields.
104 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
105 $content = $display->build($entity);
106 $content = $this->render($content);
111 * Tests string formatter output.
113 public function testStringFormatter() {
114 $value = $this->randomString();
115 $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
116 $value .= "\n\n" . $this->randomString();
118 $entity = EntityTestRev::create([]);
119 $entity->{$this->fieldName}->value = $value;
121 // Verify that all HTML is escaped and newlines are retained.
122 $this->renderEntityFields($entity, $this->display);
123 $this->assertNoRaw($value);
124 $this->assertRaw(nl2br(Html::escape($value)));
126 // Verify the cache tags.
127 $build = $entity->{$this->fieldName}->view();
128 $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
130 $value = $this->randomMachineName();
131 $entity->{$this->fieldName}->value = $value;
134 // Set the formatter to link to the entity.
135 $this->display->setComponent($this->fieldName, [
138 'link_to_entity' => TRUE,
141 $this->display->save();
143 $this->renderEntityFields($entity, $this->display);
144 $this->assertLink($value, 0);
145 $this->assertLinkByHref($entity->url());
147 // $entity->url('revision') falls back to the canonical URL if this is no
149 $this->assertLinkByHref($entity->url('revision'));
151 // Make the entity a new revision.
152 $old_revision_id = $entity->getRevisionId();
153 $entity->setNewRevision(TRUE);
154 $value2 = $this->randomMachineName();
155 $entity->{$this->fieldName}->value = $value2;
157 $entity_new_revision = $this->entityTypeManager->getStorage('entity_test_rev')->loadRevision($old_revision_id);
159 $this->renderEntityFields($entity, $this->display);
160 $this->assertLink($value2, 0);
161 $this->assertLinkByHref($entity->url('revision'));
163 $this->renderEntityFields($entity_new_revision, $this->display);
164 $this->assertLink($value, 0);
165 $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');
167 // Check that linking to a revisionable entity works if the entity type does
168 // not specify a 'revision' link template.
169 $entity_type = clone $this->entityTypeManager->getDefinition('entity_test_rev');
170 $link_templates = $entity_type->getLinkTemplates();
171 unset($link_templates['revision']);
172 $entity_type->set('links', $link_templates);
173 \Drupal::state()->set('entity_test_rev.entity_type', $entity_type);
174 $this->entityTypeManager->clearCachedDefinitions();
176 $this->renderEntityFields($entity_new_revision, $this->display);
177 $this->assertLink($value, 0);
178 $this->assertLinkByHref($entity->url('canonical'));