summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2016-08-15 17:20:00 (GMT)
committerAlex Pott2016-08-15 17:20:00 (GMT)
commit1bd3a04d6cac357453722fe4413a80f5520fd722 (patch)
tree5cff87b58e3103750c9b8a53d24979139bdb1551
parentaf1abd851c2fa7c436f6a75eb8d2028ccf37d9da (diff)
Issue #2780549 by andrewbelcher, alexpott, mpdonadio: FormattedDateDiff::$maxAge never applied to caches
-rw-r--r--core/lib/Drupal/Core/Datetime/FormattedDateDiff.php15
-rw-r--r--core/tests/Drupal/Tests/Core/Datetime/DateTest.php33
2 files changed, 47 insertions, 1 deletions
diff --git a/core/lib/Drupal/Core/Datetime/FormattedDateDiff.php b/core/lib/Drupal/Core/Datetime/FormattedDateDiff.php
index 07bb836..266022b 100644
--- a/core/lib/Drupal/Core/Datetime/FormattedDateDiff.php
+++ b/core/lib/Drupal/Core/Datetime/FormattedDateDiff.php
@@ -51,10 +51,23 @@ class FormattedDateDiff implements RenderableInterface, CacheableDependencyInter
}
/**
+ * {@inheritdoc}
+ */
+ public function getCacheMaxAge() {
+ return $this->maxAge;
+ }
+
+ /**
+ * The maximum age for which this object may be cached.
+ *
* @return int
+ * The maximum time in seconds that this object may be cached.
+ *
+ * @deprecated in Drupal 8.1.9 and will be removed before Drupal 9.0.0. Use
+ * \Drupal\Core\Datetime\FormattedDateDiff::getCacheMaxAge() instead.
*/
public function getMaxAge() {
- return $this->maxAge;
+ return $this->getCacheMaxAge();
}
/**
diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php
index 238b5c8..edf6d71 100644
--- a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php
+++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php
@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Datetime;
+use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Datetime\DateFormatter;
use Drupal\Core\Datetime\FormattedDateDiff;
use Drupal\Core\DependencyInjection\ContainerBuilder;
@@ -393,6 +394,38 @@ class DateTest extends UnitTestCase {
}
/**
+ * 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']);
+ // Test the BC layer.
+ $this->assertSame($object->getCacheMaxAge(), $object->getMaxAge());
+ }
+
+ /**
* 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).
*