public function TimestampFormatterTest::testTimestampAgoFormatter in Drupal 9
Same name and namespace in other branches
- 8 core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampFormatterTest::testTimestampAgoFormatter()
- 10 core/modules/field/tests/src/Kernel/Timestamp/TimestampFormatterTest.php \Drupal\Tests\field\Kernel\Timestamp\TimestampFormatterTest::testTimestampAgoFormatter()
Tests TimestampAgoFormatter.
File
- core/
modules/ field/ tests/ src/ Kernel/ Timestamp/ TimestampFormatterTest.php, line 146
Class
- TimestampFormatterTest
- Tests the timestamp formatters.
Namespace
Drupal\Tests\field\Kernel\TimestampCode
public function testTimestampAgoFormatter() {
$data = [];
foreach ([
1,
2,
3,
4,
5,
6,
] as $granularity) {
$data[] = [
'future_format' => '@interval hence',
'past_format' => '@interval ago',
'granularity' => $granularity,
];
}
foreach ($data as $settings) {
$future_format = $settings['future_format'];
$past_format = $settings['past_format'];
$granularity = $settings['granularity'];
$request_time = \Drupal::requestStack()
->getCurrentRequest()->server
->get('REQUEST_TIME');
// Test a timestamp in the past
$value = $request_time - 87654321;
$expected = new FormattableMarkup($past_format, [
'@interval' => \Drupal::service('date.formatter')
->formatTimeDiffSince($value, [
'granularity' => $granularity,
]),
]);
$component = $this->display
->getComponent($this->fieldName);
$component['type'] = 'timestamp_ago';
$component['settings'] = $settings;
$this->display
->setComponent($this->fieldName, $component);
$entity = EntityTest::create([]);
$entity->{$this->fieldName}->value = $value;
$this
->renderEntityFields($entity, $this->display);
$this
->assertRaw($expected);
// Test a timestamp in the future
$value = $request_time + 87654321;
$expected = new FormattableMarkup($future_format, [
'@interval' => \Drupal::service('date.formatter')
->formatTimeDiffUntil($value, [
'granularity' => $granularity,
]),
]);
$component = $this->display
->getComponent($this->fieldName);
$component['type'] = 'timestamp_ago';
$component['settings'] = $settings;
$this->display
->setComponent($this->fieldName, $component);
$entity = EntityTest::create([]);
$entity->{$this->fieldName}->value = $value;
$this
->renderEntityFields($entity, $this->display);
$this
->assertRaw($expected);
}
}