Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / field / tests / src / Kernel / String / StringFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\String;
4
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;
12
13 /**
14  * Tests the creation of text fields.
15  *
16  * @group field
17  */
18 class StringFormatterTest extends KernelTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['field', 'text', 'entity_test', 'system', 'filter', 'user'];
26
27   /**
28    * @var string
29    */
30   protected $entityType;
31
32   /**
33    * @var string
34    */
35   protected $bundle;
36
37   /**
38    * @var string
39    */
40   protected $fieldName;
41
42   /**
43    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
44    */
45   protected $display;
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp() {
51     parent::setUp();
52
53     // Configure the theme system.
54     $this->installConfig(['system', 'field']);
55     \Drupal::service('router.builder')->rebuild();
56     $this->installEntitySchema('entity_test_rev');
57
58     $this->entityType = 'entity_test_rev';
59     $this->bundle = $this->entityType;
60     $this->fieldName = mb_strtolower($this->randomMachineName());
61
62     $field_storage = FieldStorageConfig::create([
63       'field_name' => $this->fieldName,
64       'entity_type' => $this->entityType,
65       'type' => 'string',
66     ]);
67     $field_storage->save();
68
69     $instance = FieldConfig::create([
70       'field_storage' => $field_storage,
71       'bundle' => $this->bundle,
72       'label' => $this->randomMachineName(),
73     ]);
74     $instance->save();
75
76     $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
77       ->setComponent($this->fieldName, [
78         'type' => 'string',
79         'settings' => [],
80       ]);
81     $this->display->save();
82   }
83
84   /**
85    * Renders fields of a given entity with a given display.
86    *
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.
91    *
92    * @return string
93    *   The rendered entity fields.
94    */
95   protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
96     $content = $display->build($entity);
97     $content = $this->render($content);
98     return $content;
99   }
100
101   /**
102    * Tests string formatter output.
103    */
104   public function testStringFormatter() {
105     $value = $this->randomString();
106     $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
107     $value .= "\n\n" . $this->randomString();
108
109     $entity = EntityTestRev::create([]);
110     $entity->{$this->fieldName}->value = $value;
111
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)));
116
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.');
120
121     $value = $this->randomMachineName();
122     $entity->{$this->fieldName}->value = $value;
123     $entity->save();
124
125     // Set the formatter to link to the entity.
126     $this->display->setComponent($this->fieldName, [
127       'type' => 'string',
128       'settings' => [
129         'link_to_entity' => TRUE,
130       ],
131     ]);
132     $this->display->save();
133
134     $this->renderEntityFields($entity, $this->display);
135     $this->assertLink($value, 0);
136     $this->assertLinkByHref($entity->url());
137
138     // $entity->url('revision') falls back to the canonical URL if this is no
139     // revision.
140     $this->assertLinkByHref($entity->url('revision'));
141
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;
147     $entity->save();
148     $entity_new_revision = \Drupal::entityManager()->getStorage('entity_test_rev')->loadRevision($old_revision_id);
149
150     $this->renderEntityFields($entity, $this->display);
151     $this->assertLink($value2, 0);
152     $this->assertLinkByHref($entity->url('revision'));
153
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');
157   }
158
159 }