Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Timestamp / TimestampFormatterTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Timestamp;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
7 use Drupal\Core\Entity\FieldableEntityInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11 use Drupal\KernelTests\KernelTestBase;
12
13 /**
14  * Tests the timestamp formatters.
15  *
16  * @group field
17  */
18 class TimestampFormatterTest extends KernelTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['system', 'field', 'text', 'entity_test', 'user'];
24
25   /**
26    * @var string
27    */
28   protected $entityType;
29
30   /**
31    * @var string
32    */
33   protected $bundle;
34
35   /**
36    * @var string
37    */
38   protected $fieldName;
39
40   /**
41    * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
42    */
43   protected $display;
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     parent::setUp();
50
51     $this->installConfig(['system']);
52     $this->installConfig(['field']);
53     $this->installEntitySchema('entity_test');
54
55     $this->entityType = 'entity_test';
56     $this->bundle = $this->entityType;
57     $this->fieldName = mb_strtolower($this->randomMachineName());
58
59     $field_storage = FieldStorageConfig::create([
60       'field_name' => $this->fieldName,
61       'entity_type' => $this->entityType,
62       'type' => 'timestamp',
63     ]);
64     $field_storage->save();
65
66     $instance = FieldConfig::create([
67       'field_storage' => $field_storage,
68       'bundle' => $this->bundle,
69       'label' => $this->randomMachineName(),
70     ]);
71     $instance->save();
72
73     $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
74       ->setComponent($this->fieldName, [
75         'type' => 'boolean',
76         'settings' => [],
77       ]);
78     $this->display->save();
79   }
80
81   /**
82    * Renders fields of a given entity with a given display.
83    *
84    * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
85    *   The entity object with attached fields to render.
86    * @param \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display
87    *   The display to render the fields in.
88    *
89    * @return string
90    *   The rendered entity fields.
91    */
92   protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
93     $content = $display->build($entity);
94     $content = $this->render($content);
95     return $content;
96   }
97
98   /**
99    * Tests TimestampFormatter.
100    */
101   public function testTimestampFormatter() {
102     $data = [];
103
104     // Test standard formats.
105     $date_formats = array_keys(\Drupal::entityManager()->getStorage('date_format')->loadMultiple());
106
107     foreach ($date_formats as $date_format) {
108       $data[] = ['date_format' => $date_format, 'custom_date_format' => '', 'timezone' => ''];
109     }
110
111     $data[] = ['date_format' => 'custom', 'custom_date_format' => 'r', 'timezone' => ''];
112     $data[] = ['date_format' => 'custom', 'custom_date_format' => 'e', 'timezone' => 'Asia/Tokyo'];
113
114     foreach ($data as $settings) {
115       list($date_format, $custom_date_format, $timezone) = array_values($settings);
116       if (empty($timezone)) {
117         $timezone = NULL;
118       }
119
120       $value = REQUEST_TIME - 87654321;
121       $expected = \Drupal::service('date.formatter')->format($value, $date_format, $custom_date_format, $timezone);
122
123       $component = $this->display->getComponent($this->fieldName);
124       $component['type'] = 'timestamp';
125       $component['settings'] = $settings;
126       $this->display->setComponent($this->fieldName, $component);
127
128       $entity = EntityTest::create([]);
129       $entity->{$this->fieldName}->value = $value;
130
131       $this->renderEntityFields($entity, $this->display);
132       $this->assertRaw($expected);
133     }
134   }
135
136   /**
137    * Tests TimestampAgoFormatter.
138    */
139   public function testTimestampAgoFormatter() {
140     $data = [];
141
142     foreach ([1, 2, 3, 4, 5, 6] as $granularity) {
143       $data[] = [
144         'future_format' => '@interval hence',
145         'past_format' => '@interval ago',
146         'granularity' => $granularity,
147       ];
148     }
149
150     foreach ($data as $settings) {
151       $future_format = $settings['future_format'];
152       $past_format = $settings['past_format'];
153       $granularity = $settings['granularity'];
154       $request_time = \Drupal::requestStack()->getCurrentRequest()->server->get('REQUEST_TIME');
155
156       // Test a timestamp in the past
157       $value = $request_time - 87654321;
158       $expected = new FormattableMarkup($past_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffSince($value, ['granularity' => $granularity])]);
159
160       $component = $this->display->getComponent($this->fieldName);
161       $component['type'] = 'timestamp_ago';
162       $component['settings'] = $settings;
163       $this->display->setComponent($this->fieldName, $component);
164
165       $entity = EntityTest::create([]);
166       $entity->{$this->fieldName}->value = $value;
167
168       $this->renderEntityFields($entity, $this->display);
169       $this->assertRaw($expected);
170
171       // Test a timestamp in the future
172       $value = $request_time + 87654321;
173       $expected = new FormattableMarkup($future_format, ['@interval' => \Drupal::service('date.formatter')->formatTimeDiffUntil($value, ['granularity' => $granularity])]);
174
175       $component = $this->display->getComponent($this->fieldName);
176       $component['type'] = 'timestamp_ago';
177       $component['settings'] = $settings;
178       $this->display->setComponent($this->fieldName, $component);
179
180       $entity = EntityTest::create([]);
181       $entity->{$this->fieldName}->value = $value;
182
183       $this->renderEntityFields($entity, $this->display);
184       $this->assertRaw($expected);
185     }
186   }
187
188 }