3 namespace Drupal\Tests\field\Kernel\Timestamp;
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;
15 * Tests the timestamp formatters.
19 class TimestampFormatterTest extends KernelTestBase {
24 public static $modules = ['system', 'field', 'text', 'entity_test', 'user'];
29 protected $entityType;
42 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
49 protected function setUp() {
52 $this->installConfig(['system']);
53 $this->installConfig(['field']);
54 $this->installEntitySchema('entity_test');
56 $this->entityType = 'entity_test';
57 $this->bundle = $this->entityType;
58 $this->fieldName = Unicode::strtolower($this->randomMachineName());
60 $field_storage = FieldStorageConfig::create([
61 'field_name' => $this->fieldName,
62 'entity_type' => $this->entityType,
63 'type' => 'timestamp',
65 $field_storage->save();
67 $instance = FieldConfig::create([
68 'field_storage' => $field_storage,
69 'bundle' => $this->bundle,
70 'label' => $this->randomMachineName(),
74 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
75 ->setComponent($this->fieldName, [
79 $this->display->save();
83 * Renders fields of a given entity with a given display.
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.
91 * The rendered entity fields.
93 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
94 $content = $display->build($entity);
95 $content = $this->render($content);
100 * Tests TimestampFormatter.
102 public function testTimestampFormatter() {
105 // Test standard formats.
106 $date_formats = array_keys(\Drupal::entityManager()->getStorage('date_format')->loadMultiple());
108 foreach ($date_formats as $date_format) {
109 $data[] = ['date_format' => $date_format, 'custom_date_format' => '', 'timezone' => ''];
112 $data[] = ['date_format' => 'custom', 'custom_date_format' => 'r', 'timezone' => ''];
113 $data[] = ['date_format' => 'custom', 'custom_date_format' => 'e', 'timezone' => 'Asia/Tokyo'];
115 foreach ($data as $settings) {
116 list($date_format, $custom_date_format, $timezone) = array_values($settings);
117 if (empty($timezone)) {
121 $value = REQUEST_TIME - 87654321;
122 $expected = \Drupal::service('date.formatter')->format($value, $date_format, $custom_date_format, $timezone);
124 $component = $this->display->getComponent($this->fieldName);
125 $component['type'] = 'timestamp';
126 $component['settings'] = $settings;
127 $this->display->setComponent($this->fieldName, $component);
129 $entity = EntityTest::create([]);
130 $entity->{$this->fieldName}->value = $value;
132 $this->renderEntityFields($entity, $this->display);
133 $this->assertRaw($expected);
138 * Tests TimestampAgoFormatter.
140 public function testTimestampAgoFormatter() {
143 foreach ([1, 2, 3, 4, 5, 6] as $granularity) {
145 'future_format' => '@interval hence',
146 'past_format' => '@interval ago',
147 'granularity' => $granularity,
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');
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])]);
161 $component = $this->display->getComponent($this->fieldName);
162 $component['type'] = 'timestamp_ago';
163 $component['settings'] = $settings;
164 $this->display->setComponent($this->fieldName, $component);
166 $entity = EntityTest::create([]);
167 $entity->{$this->fieldName}->value = $value;
169 $this->renderEntityFields($entity, $this->display);
170 $this->assertRaw($expected);
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])]);
176 $component = $this->display->getComponent($this->fieldName);
177 $component['type'] = 'timestamp_ago';
178 $component['settings'] = $settings;
179 $this->display->setComponent($this->fieldName, $component);
181 $entity = EntityTest::create([]);
182 $entity->{$this->fieldName}->value = $value;
184 $this->renderEntityFields($entity, $this->display);
185 $this->assertRaw($expected);