1000d640789f4c6b548e58dbf7a2c629065bf949
[yaffs-website] / web / core / modules / field / tests / src / Kernel / String / RawStringFormatterTest.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\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\field\Entity\FieldStorageConfig;
12 use Drupal\KernelTests\KernelTestBase;
13
14 /**
15  * Tests the raw string formatter
16  *
17  * @group field
18  */
19 class RawStringFormatterTest 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');
58
59     $this->entityType = 'entity_test';
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_long',
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 = EntityTest::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
123 }