Pull merge.
[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    * The entity type manager.
29    *
30    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
31    */
32   protected $entityTypeManager;
33
34   /**
35    * @var string
36    */
37   protected $entityType;
38
39   /**
40    * @var string
41    */
42   protected $bundle;
43
44   /**
45    * @var string
46    */
47   protected $fieldName;
48
49   /**
50    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
51    */
52   protected $display;
53
54   /**
55    * {@inheritdoc}
56    */
57   protected function setUp() {
58     parent::setUp();
59
60     // Configure the theme system.
61     $this->installConfig(['system', 'field']);
62     \Drupal::service('router.builder')->rebuild();
63     $this->installEntitySchema('entity_test_rev');
64
65     $this->entityType = 'entity_test_rev';
66     $this->bundle = $this->entityType;
67     $this->fieldName = mb_strtolower($this->randomMachineName());
68
69     $field_storage = FieldStorageConfig::create([
70       'field_name' => $this->fieldName,
71       'entity_type' => $this->entityType,
72       'type' => 'string',
73     ]);
74     $field_storage->save();
75
76     $instance = FieldConfig::create([
77       'field_storage' => $field_storage,
78       'bundle' => $this->bundle,
79       'label' => $this->randomMachineName(),
80     ]);
81     $instance->save();
82
83     $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
84       ->setComponent($this->fieldName, [
85         'type' => 'string',
86         'settings' => [],
87       ]);
88     $this->display->save();
89
90     $this->entityTypeManager = \Drupal::entityTypeManager();
91   }
92
93   /**
94    * Renders fields of a given entity with a given display.
95    *
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.
100    *
101    * @return string
102    *   The rendered entity fields.
103    */
104   protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
105     $content = $display->build($entity);
106     $content = $this->render($content);
107     return $content;
108   }
109
110   /**
111    * Tests string formatter output.
112    */
113   public function testStringFormatter() {
114     $value = $this->randomString();
115     $value .= "\n\n<strong>" . $this->randomString() . '</strong>';
116     $value .= "\n\n" . $this->randomString();
117
118     $entity = EntityTestRev::create([]);
119     $entity->{$this->fieldName}->value = $value;
120
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)));
125
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.');
129
130     $value = $this->randomMachineName();
131     $entity->{$this->fieldName}->value = $value;
132     $entity->save();
133
134     // Set the formatter to link to the entity.
135     $this->display->setComponent($this->fieldName, [
136       'type' => 'string',
137       'settings' => [
138         'link_to_entity' => TRUE,
139       ],
140     ]);
141     $this->display->save();
142
143     $this->renderEntityFields($entity, $this->display);
144     $this->assertLink($value, 0);
145     $this->assertLinkByHref($entity->url());
146
147     // $entity->url('revision') falls back to the canonical URL if this is no
148     // revision.
149     $this->assertLinkByHref($entity->url('revision'));
150
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;
156     $entity->save();
157     $entity_new_revision = $this->entityTypeManager->getStorage('entity_test_rev')->loadRevision($old_revision_id);
158
159     $this->renderEntityFields($entity, $this->display);
160     $this->assertLink($value2, 0);
161     $this->assertLinkByHref($entity->url('revision'));
162
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');
166
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();
175
176     $this->renderEntityFields($entity_new_revision, $this->display);
177     $this->assertLink($value, 0);
178     $this->assertLinkByHref($entity->url('canonical'));
179   }
180
181 }