diff --git a/facets.services.yml b/facets.services.yml index 7432eed..03b0025 100644 --- a/facets.services.yml +++ b/facets.services.yml @@ -24,3 +24,5 @@ services: - '@entity_type.manager' facets.utility.date_handler: class: Drupal\facets\utility\FacetsDateHandler + arguments: + - '@date.formatter' diff --git a/src/Utility/FacetsDateHandler.php b/src/Utility/FacetsDateHandler.php index de98ed1..ae9a703 100644 --- a/src/Utility/FacetsDateHandler.php +++ b/src/Utility/FacetsDateHandler.php @@ -7,6 +7,8 @@ namespace Drupal\facets\utility; +use Drupal\Core\Datetime\DateFormatter; + /** * Dates Handler service. */ @@ -63,6 +65,23 @@ class FacetsDateHandler { const FACETS_REGEX_DATE_RANGE = '/^\[(/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/) TO (/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$/)\]$/'; /** + * The date formatting service. + * + * @var \Drupal\Core\Datetime\DateFormatter + */ + protected $dateFormatter; + + /** + * FacetsDateHandler constructor. + * + * @param \Drupal\Core\Datetime\DateFormatter $date_formatter + * The date formatting service. + */ + public function __construct(DateFormatter $date_formatter) { + $this->dateFormatter = $date_formatter; + } + + /** * Converts dates from Unix timestamps into ISO 8601 format. * * @param int $timestamp @@ -245,25 +264,25 @@ class FacetsDateHandler { * @return string * A gap-appropriate display date used in the facet link. */ - public function formatTimestamp($timestamp, $gap = FACETS_DATE_YEAR) { + public function formatTimestamp($timestamp, $gap = self::FACETS_DATE_YEAR) { switch ($gap) { case static::FACETS_DATE_MONTH: - return format_date($timestamp, 'custom', 'F Y', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'F Y', 'UTC'); case static::FACETS_DATE_DAY: - return format_date($timestamp, 'custom', 'F j, Y', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'F j, Y', 'UTC'); case static::FACETS_DATE_HOUR: - return format_date($timestamp, 'custom', 'g A', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'g A', 'UTC'); case static::FACETS_DATE_MINUTE: - return format_date($timestamp, 'custom', 'g:i A', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'g:i A', 'UTC'); case static::FACETS_DATE_SECOND: - return format_date($timestamp, 'custom', 'g:i:s A', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'g:i:s A', 'UTC'); default: - return format_date($timestamp, 'custom', 'Y', 'UTC'); + return $this->dateFormatter->format($timestamp, 'custom', 'Y', 'UTC'); } } diff --git a/tests/src/Unit/Utility/FacetsDateHandlerTest.php b/tests/src/Unit/Utility/FacetsDateHandlerTest.php index 0ac9739..f60edb8 100644 --- a/tests/src/Unit/Utility/FacetsDateHandlerTest.php +++ b/tests/src/Unit/Utility/FacetsDateHandlerTest.php @@ -7,6 +7,9 @@ namespace Drupal\Tests\facets\Unit\utility; +use Drupal\Core\Datetime\DateFormatter; +use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Language\Language; use Drupal\facets\utility\FacetsDateHandler; use Drupal\Tests\UnitTestCase; @@ -18,22 +21,64 @@ use Drupal\Tests\UnitTestCase; class FacetsDateHandlerTest extends UnitTestCase { /** - * Timestamp used by tests: Thu, 26 Nov 1987 20:43:04 GMT + * Timestamp used by tests: Thu, 26 Nov 1987 20:43:04 GMT. */ const TIMESTAMP = 564957784; /** - * ISO date used by tests: Thu, 26 Nov 1987 20:43:04 GMT + * ISO date used by tests: Thu, 26 Nov 1987 20:43:04 GMT. */ const ISO_DATE = '1987-11-26T20:43:04Z'; /** + * The system under test. + * + * @var \Drupal\facets\utility\FacetsDateHandler + */ + protected $handler; + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + + $entity_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface'); + + $em = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); + $em->expects($this->any()) + ->method('getStorage') + ->with('date_format') + ->willReturn($entity_storage); + + $language = new Language(['id' => 'en']); + + $lm = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); + $lm->method('getCurrentLanguage') + ->willReturn($language); + $st = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); + $rs = $this->getMock('Symfony\Component\HttpFoundation\RequestStack'); + $cf = $this->getConfigFactoryStub(); + + $config_factory = $this->getConfigFactoryStub([ + 'system.date' => ['country' => ['default' => 'GB']], + ]); + $container = new ContainerBuilder(); + $container->set('config.factory', $config_factory); + \Drupal::setContainer($container); + + $date_formatter = new DateFormatter($em, $lm, $st, $cf, $rs); + + $this->handler = new FacetsDateHandler($date_formatter); + } + + /** * Tests the isoDate method. * * @dataProvider provideIsoDates */ public function testIsoDate($iso_date, $gap) { - $fd = new FacetsDateHandler(); + $fd = $this->handler; $this->assertEquals($iso_date, $fd->isoDate(static::TIMESTAMP, $gap)); } @@ -41,7 +86,7 @@ class FacetsDateHandlerTest extends UnitTestCase { * Tests for ::getNextDateGap. */ public function testGetNextDateGap() { - $fd = new FacetsDateHandler(); + $fd = $this->handler; $gap = $fd->getNextDateGap($fd::FACETS_DATE_SECOND); $this->assertEquals($fd::FACETS_DATE_SECOND, $gap); @@ -66,33 +111,33 @@ class FacetsDateHandlerTest extends UnitTestCase { } /** - * Tests for ::getTimestampGap + * Tests for ::getTimestampGap. */ public function testGetTimestampGap() { - $fd = new FacetsDateHandler(); + $fd = $this->handler; // The best search gap between two dates must be a year. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 31536000); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 31536000); $this->assertEquals($fd::FACETS_DATE_YEAR, $date_gap); // The best search gap between two dates must be a month. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400 * 60); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400 * 60); $this->assertEquals($fd::FACETS_DATE_MONTH, $date_gap); // The best search gap between two dates must be a day. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 86400); $this->assertEquals($fd::FACETS_DATE_DAY, $date_gap); // The best search gap between two dates must be an hour. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 3600); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 3600); $this->assertEquals($fd::FACETS_DATE_HOUR, $date_gap); // The best search gap between two dates must be a minute. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 60); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 60); $this->assertEquals($fd::FACETS_DATE_MINUTE, $date_gap); // The best search gap between two dates must be a second. - $date_gap = $fd->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 59); + $date_gap = $this->handler->getTimestampGap(static::TIMESTAMP, static::TIMESTAMP + 59); $this->assertEquals($fd::FACETS_DATE_SECOND, $date_gap); } @@ -100,7 +145,7 @@ class FacetsDateHandlerTest extends UnitTestCase { * Tests for ::getDateGap method. */ public function testGetDateGap() { - $fd = new FacetsDateHandler(); + $fd = $this->handler; // Cannot convert to timestamp. $this->assertFalse($fd->getDateGap(static::TIMESTAMP, static::TIMESTAMP)); @@ -133,16 +178,14 @@ class FacetsDateHandlerTest extends UnitTestCase { * @dataProvider provideNextDateIncrementData */ public function testNextDateIncrement($incremented_iso_date, $gap) { - $fd = new FacetsDateHandler(); - - $this->assertEquals($incremented_iso_date, $fd->getNextDateIncrement(static::ISO_DATE, $gap)); + $this->assertEquals($incremented_iso_date, $this->handler->getNextDateIncrement(static::ISO_DATE, $gap)); } /** * Tests for ::gapCompare method. */ public function testGapCompare() { - $fd = new FacetsDateHandler(); + $fd = $this->handler; // Timestamps are equals. $this->assertEquals(0, $fd->gapCompare(static::TIMESTAMP, static::TIMESTAMP)); @@ -150,17 +193,28 @@ class FacetsDateHandlerTest extends UnitTestCase { // Timestamps are equals. $this->assertEquals(0, $fd->gapCompare($fd::FACETS_DATE_YEAR, $fd::FACETS_DATE_YEAR)); - // gap1 is less than gap2 + // gap1 is less than gap2. $this->assertEquals(-1, $fd->gapCompare($fd::FACETS_DATE_MONTH, $fd::FACETS_DATE_YEAR)); - // gap1 is less than gap2 + // gap1 is less than gap2. $this->assertEquals(1, $fd->gapCompare($fd::FACETS_DATE_MONTH, $fd::FACETS_DATE_DAY)); } /** + * Tests for ::formatTimestamp method. + */ + public function testFormatTimestamp() { + $fd = $this->handler; + + $year = $fd->formatTimestamp(static::TIMESTAMP); + $this->assertEquals(1987, $year); + } + + /** * Returns a data provider for the ::testIsoDate(). * * @return array + * Arrays with data for the test data. */ public function provideIsoDates() { return [ @@ -178,6 +232,7 @@ class FacetsDateHandlerTest extends UnitTestCase { * Returns a data provider for the ::testNextDateIncrement(). * * @return array + * Arrays with data for the test data. */ public function provideNextDateIncrementData() { return [ @@ -191,4 +246,3 @@ class FacetsDateHandlerTest extends UnitTestCase { } } -