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'];
30 protected $entityType;
43 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
50 protected function setUp() {
53 // Configure the theme system.
54 $this->installConfig(['system', 'field']);
55 \Drupal::service('router.builder')->rebuild();
56 $this->installEntitySchema('entity_test_rev');
58 $this->entityType = 'entity_test_rev';
59 $this->bundle = $this->entityType;
60 $this->fieldName = mb_strtolower($this->randomMachineName());
62 $field_storage = FieldStorageConfig::create([
63 'field_name' => $this->fieldName,
64 'entity_type' => $this->entityType,
67 $field_storage->save();
69 $instance = FieldConfig::create([
70 'field_storage' => $field_storage,
71 'bundle' => $this->bundle,
72 'label' => $this->randomMachineName(),
76 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
77 ->setComponent($this->fieldName, [
81 $this->display->save();
85 * Renders fields of a given entity with a given display.
87 * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
88 * The entity object with attached fields to render.
89 * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
90 * The display to render the fields in.
93 * The rendered entity fields.
95 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
96 $content = $display->build($entity);
97 $content = $this->render($content);
102 * Tests string formatter output.
104 public function testStringFormatter() {
105 $value = $this->randomString();
106 $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
107 $value .= "\n\n" . $this->randomString();
109 $entity = EntityTestRev::create([]);
110 $entity->{$this->fieldName}->value = $value;
112 // Verify that all HTML is escaped and newlines are retained.
113 $this->renderEntityFields($entity, $this->display);
114 $this->assertNoRaw($value);
115 $this->assertRaw(nl2br(Html::escape($value)));
117 // Verify the cache tags.
118 $build = $entity->{$this->fieldName}->view();
119 $this->assertTrue(!isset($build[0]['#cache']), 'The string formatter has no cache tags.');
121 $value = $this->randomMachineName();
122 $entity->{$this->fieldName}->value = $value;
125 // Set the formatter to link to the entity.
126 $this->display->setComponent($this->fieldName, [
129 'link_to_entity' => TRUE,
132 $this->display->save();
134 $this->renderEntityFields($entity, $this->display);
135 $this->assertLink($value, 0);
136 $this->assertLinkByHref($entity->url());
138 // $entity->url('revision') falls back to the canonical URL if this is no
140 $this->assertLinkByHref($entity->url('revision'));
142 // Make the entity a new revision.
143 $old_revision_id = $entity->getRevisionId();
144 $entity->setNewRevision(TRUE);
145 $value2 = $this->randomMachineName();
146 $entity->{$this->fieldName}->value = $value2;
148 $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id);
150 $this->renderEntityFields($entity, $this->display);
151 $this->assertLink($value2, 0);
152 $this->assertLinkByHref($entity->url('revision'));
154 $this->renderEntityFields($entity_new_revision, $this->display);
155 $this->assertLink($value, 0);
156 $this->assertLinkByHref('/entity_test_rev/' . $entity_new_revision->id() . '/revision/' . $entity_new_revision->getRevisionId() . '/view');