diff --git a/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php index 72845d5..0510fa4 100644 --- a/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -156,15 +156,16 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N * @param int $interval * The length of the interval in seconds. * @param int $granularity - * How many different units to display in the string. + * (optional) How many different units to display in the string (2 by + * default). * @param string $langcode - * (Optional) language code to translate to a language other than - * what is used to display the page. + * (optional) Language code to translate to a language other than what is + * used to display the page. Defaults to NULL. * * @return string * A translated string representation of the interval. */ - public function formatInterval($interval, $granularity, $langcode) { + public function formatInterval($interval, $granularity = 2, $langcode = NULL) { $output = ''; foreach ($this->units as $key => $value) { $key = explode('|', $key); diff --git a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php index 9be0fc9..32d2103 100644 --- a/core/tests/Drupal/Tests/Core/Datetime/DateTest.php +++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php @@ -59,16 +59,12 @@ public static function getInfo() { } protected function setUp() { - $this->entityManager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') - ->disableOriginalConstructor() - ->getMock(); + $this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->languageManager = $this->getMockBuilder('Drupal\Core\Language\LanguageManager') ->disableOriginalConstructor() ->getMock(); $this->stringTranslation = $this->getMock('Drupal\Core\StringTranslation\TranslationInterface'); - - $this->date = new Date($this->entityManager, $this->languageManager, $this->stringTranslation); } @@ -79,7 +75,7 @@ protected function setUp() { * * @see \Drupal\Core\Datetime\Date::formatInterval() */ - public function testFormatInterval($interval, $granularity, $expected, $langcode = 'en') { + public function testFormatInterval($interval, $granularity, $expected, $langcode = NULL) { // Mocks a simple formatPlural implementation. $this->stringTranslation->expects($this->any()) ->method('formatPlural') @@ -88,7 +84,13 @@ public function testFormatInterval($interval, $granularity, $expected, $langcode return $count == 1 ? $one : str_replace('@count', $count, $multiple); })); - $result = $this->date->formatInterval($interval, $granularity, $langcode); + // Check if the granularity is specified. + if ($granularity) { + $result = $this->date->formatInterval($interval, $granularity, $langcode); + } + else { + $result = $this->date->formatInterval($interval); + } $this->assertEquals($expected, $result); } @@ -121,6 +123,8 @@ public function providerTestFormatInterval() { array(2592000 * 2 + 604800 * 3 + 86400 * 4, 3, '2 months 3 weeks 4 days'), // Check for the langcode. array(61, 1, '1 min', 'xxx-lolspeak'), + // Check with an unspecified granularity. + array(61, NULL, '1 min 1 sec'), ); return $data;