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