3 namespace Drupal\Tests\field\Kernel\String;
5 use Drupal\Component\Utility\Html;
6 use Drupal\Component\Utility\Unicode;
7 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
8 use Drupal\Core\Entity\FieldableEntityInterface;
9 use Drupal\entity_test\Entity\EntityTestRev;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\KernelTests\KernelTestBase;
15 * Tests the creation of text fields.
19 class StringFormatterTest extends KernelTestBase {
26 public static $modules = ['field', 'text', 'entity_test', 'system', 'filter', 'user'];
31 protected $entityType;
44 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
51 protected function setUp() {
54 // Configure the theme system.
55 $this->installConfig(['system', 'field']);
56 \Drupal::service('router.builder')->rebuild();
57 $this->installEntitySchema('entity_test_rev');
59 $this->entityType = 'entity_test_rev';
60 $this->bundle = $this->entityType;
61 $this->fieldName = Unicode::strtolower($this->randomMachineName());
63 $field_storage = FieldStorageConfig::create([
64 'field_name' => $this->fieldName,
65 'entity_type' => $this->entityType,
68 $field_storage->save();
70 $instance = FieldConfig::create([
71 'field_storage' => $field_storage,
72 'bundle' => $this->bundle,
73 'label' => $this->randomMachineName(),
77 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
78 ->setComponent($this->fieldName, [
82 $this->display->save();
86 * Renders fields of a given entity with a given display.
88 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
89 * The entity object with attached fields to render.
90 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
91 * The display to render the fields in.
94 * The rendered entity fields.
96 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
97 $content = $display->build($entity);
98 $content = $this->render($content);
103 * Tests string formatter output.
105 public function testStringFormatter() {
106 $value = $this->randomString();
107 $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
108 $value .= "\n\n" . $this->randomString();
110 $entity = EntityTestRev::create([]);
111 $entity->{$this->fieldName}->value = $value;
113 // Verify that all HTML is escaped and newlines are retained.
114 $this->renderEntityFields($entity, $this->display);
115 $this->assertNoRaw($value);
116 $this->assertRaw(nl2br(Html::escape($value)));
118 // Verify the cache tags.
119 $build = $entity->{$this->fieldName}->view();
120 $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
122 $value = $this->randomMachineName();
123 $entity->{$this->fieldName}->value = $value;
126 // Set the formatter to link to the entity.
127 $this->display->setComponent($this->fieldName, [
130 'link_to_entity' => TRUE,
133 $this->display->save();
135 $this->renderEntityFields($entity, $this->display);
136 $this->assertLink($value, 0);
137 $this->assertLinkByHref($entity->url());
139 // $entity->url('revision') falls back to the canonical URL if this is no
141 $this->assertLinkByHref($entity->url('revision'));
143 // Make the entity a new revision.
144 $old_revision_id = $entity->getRevisionId();
145 $entity->setNewRevision(TRUE);
146 $value2 = $this->randomMachineName();
147 $entity->{$this->fieldName}->value = $value2;
149 $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id);
151 $this->renderEntityFields($entity, $this->display);
152 $this->assertLink($value2, 0);
153 $this->assertLinkByHref($entity->url('revision'));
155 $this->renderEntityFields($entity_new_revision, $this->display);
156 $this->assertLink($value, 0);
157 $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');