3 namespace Drupal\Tests\field\Kernel\Timestamp;
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;
14 * Tests the timestamp formatters.
18 class TimestampFormatterTest extends KernelTestBase {
23 public static $modules = ['system', 'field', 'text', 'entity_test', 'user'];
28 protected $entityType;
41 * @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface
48 protected function setUp() {
51 $this->installConfig(['system']);
52 $this->installConfig(['field']);
53 $this->installEntitySchema('entity_test');
55 $this->entityType = 'entity_test';
56 $this->bundle = $this->entityType;
57 $this->fieldName = mb_strtolower($this->randomMachineName());
59 $field_storage = FieldStorageConfig::create([
60 'field_name' => $this->fieldName,
61 'entity_type' => $this->entityType,
62 'type' => 'timestamp',
64 $field_storage->save();
66 $instance = FieldConfig::create([
67 'field_storage' => $field_storage,
68 'bundle' => $this->bundle,
69 'label' => $this->randomMachineName(),
73 $this->display = entity_get_display($this->entityType, $this->bundle, 'default')
74 ->setComponent($this->fieldName, [
78 $this->display->save();
82 * Renders fields of a given entity with a given display.
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.
90 * The rendered entity fields.
92 protected function renderEntityFields(FieldableEntityInterface $entity, EntityViewDisplayInterface $display) {
93 $content = $display->build($entity);
94 $content = $this->render($content);
99 * Tests TimestampFormatter.
101 public function testTimestampFormatter() {
104 // Test standard formats.
105 $date_formats = array_keys(\Drupal::entityManager()->getStorage('date_format')->loadMultiple());
107 foreach ($date_formats as $date_format) {
108 $data[] = ['date_format' => $date_format, 'custom_date_format' => '', 'timezone' => ''];
111 $data[] = ['date_format' => 'custom', 'custom_date_format' => 'r', 'timezone' => ''];
112 $data[] = ['date_format' => 'custom', 'custom_date_format' => 'e', 'timezone' => 'Asia/Tokyo'];
114 foreach ($data as $settings) {
115 list($date_format, $custom_date_format, $timezone) = array_values($settings);
116 if (empty($timezone)) {
120 $value = REQUEST_TIME - 87654321;
121 $expected = \Drupal::service('date.formatter')->format($value, $date_format, $custom_date_format, $timezone);
123 $component = $this->display->getComponent($this->fieldName);
124 $component['type'] = 'timestamp';
125 $component['settings'] = $settings;
126 $this->display->setComponent($this->fieldName, $component);
128 $entity = EntityTest::create([]);
129 $entity->{$this->fieldName}->value = $value;
131 $this->renderEntityFields($entity, $this->display);
132 $this->assertRaw($expected);
137 * Tests TimestampAgoFormatter.
139 public function testTimestampAgoFormatter() {
142 foreach ([1, 2, 3, 4, 5, 6] as $granularity) {
144 'future_format' => '@interval hence',
145 'past_format' => '@interval ago',
146 'granularity' => $granularity,
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');
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])]);
160 $component = $this->display->getComponent($this->fieldName);
161 $component['type'] = 'timestamp_ago';
162 $component['settings'] = $settings;
163 $this->display->setComponent($this->fieldName, $component);
165 $entity = EntityTest::create([]);
166 $entity->{$this->fieldName}->value = $value;
168 $this->renderEntityFields($entity, $this->display);
169 $this->assertRaw($expected);
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])]);
175 $component = $this->display->getComponent($this->fieldName);
176 $component['type'] = 'timestamp_ago';
177 $component['settings'] = $settings;
178 $this->display->setComponent($this->fieldName, $component);
180 $entity = EntityTest::create([]);
181 $entity->{$this->fieldName}->value = $value;
183 $this->renderEntityFields($entity, $this->display);
184 $this->assertRaw($expected);