Skip to content
Snippets Groups Projects
Commit abe69518 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2780549 by andrewbelcher, alexpott, mpdonadio:...

Issue #2780549 by andrewbelcher, alexpott, mpdonadio: FormattedDateDiff::$maxAge never applied to caches
parent f24af6a0
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -51,9 +51,9 @@ public function getString() { ...@@ -51,9 +51,9 @@ public function getString() {
} }
/** /**
* @return int * {@inheritdoc}
*/ */
public function getMaxAge() { public function getCacheMaxAge() {
return $this->maxAge; return $this->maxAge;
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Datetime; namespace Drupal\Tests\Core\Datetime;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Datetime\DateFormatter; use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Datetime\FormattedDateDiff; use Drupal\Core\Datetime\FormattedDateDiff;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
...@@ -392,6 +393,36 @@ public function providerTestFormatDiff() { ...@@ -392,6 +393,36 @@ public function providerTestFormatDiff() {
return $data; return $data;
} }
/**
* Tests FormattedDateDiff.
*
* @covers \Drupal\Core\Datetime\FormattedDateDiff::toRenderable
* @covers \Drupal\Core\Datetime\FormattedDateDiff::getString
* @covers \Drupal\Core\Datetime\FormattedDateDiff::getCacheMaxAge
*/
public function testFormattedDateDiff() {
$string = '10 minutes';
$max_age = 60;
$object = new FormattedDateDiff($string, $max_age);
// Test conversion to a render array.
$expected = [
'#markup' => $string,
'#cache' => [
'max-age' => $max_age,
],
];
$this->assertArrayEquals($expected, $object->toRenderable());
// Test retrieving the formatted time difference string.
$this->assertEquals($string, $object->getString());
// Test applying cacheability data to an existing build.
$build = [];
CacheableMetadata::createFromObject($object)->applyTo($build);
$this->assertEquals($max_age, $build['#cache']['max-age']);
}
/** /**
* Creates a UNIX timestamp given a date and time string in the format * Creates a UNIX timestamp given a date and time string in the format
* year-month-day hour:minute:seconds (e.g. 2013-12-11 10:09:08). * year-month-day hour:minute:seconds (e.g. 2013-12-11 10:09:08).
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment