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