diff -u b/core/core.services.yml b/core/core.services.yml --- b/core/core.services.yml +++ b/core/core.services.yml @@ -537,7 +537,7 @@ arguments: ['@module_handler'] date: class: Drupal\Core\Datetime\Date - arguments: ['@entity.manager', '@language_manager', '@string_translation'] + arguments: ['@entity.manager', '@language_manager'] feed.bridge.reader: class: Drupal\Component\Bridge\ZfExtensionManagerSfContainer calls: @@ -555,7 +555,7 @@ arguments: ['@module_handler'] date: class: Drupal\Core\Datetime\Date - arguments: ['@entity.manager', '@language_manager'] + arguments: ['@entity.manager', '@language_manager', '@string_translation'] feed.bridge.reader: class: Drupal\Component\Bridge\ZfExtensionManagerSfContainer calls: diff -u b/core/includes/common.inc b/core/includes/common.inc --- b/core/includes/common.inc +++ b/core/includes/common.inc @@ -919,8 +919,7 @@ * @see t() * @see format_string() * - * @deprecated as of Drupal 8.0. Use the string_translation service formatPlural - * method. + * @deprecated as of Drupal 8.0. Use \Drupal::translation()->formatPlural() */ function format_plural($count, $singular, $plural, array $args = array(), array $options = array()) { return \Drupal::translation()->formatPlural($count, $singular, $plural, $args, $options); @@ -1002,7 +1001,7 @@ * @return * A translated string representation of the interval. * - * @deprecated as of Drupal 8.0. Use the date service formatInterval method. + * @deprecated as of Drupal 8.0. Use \Drupal::service('date')->formatInterval(). */ function format_interval($interval, $granularity = 2, $langcode = NULL) { return \Drupal::service('date')->formatInterval($interval, $granularity, $langcode); diff -u b/core/lib/Drupal/Core/Datetime/Date.php b/core/lib/Drupal/Core/Datetime/Date.php --- b/core/lib/Drupal/Core/Datetime/Date.php +++ b/core/lib/Drupal/Core/Datetime/Date.php @@ -46,8 +46,9 @@ /** * Contains the different date interval units. * - * This array is keyed by strings representating the unit (like 1 year|@count - * years) and with the amount of values of the unit in seconds. + * This array is keyed by strings representing the unit (e.g. + * '1 year|@count years') and with the amount of values of the unit in + * seconds. * * @var array */ @@ -58,7 +59,7 @@ '1 day|@count days' => 86400, '1 hour|@count hours' => 3600, '1 min|@count min' => 60, - '1 sec|@count sec' => 1 + '1 sec|@count sec' => 1, ); /** @@ -168,7 +169,7 @@ foreach ($this->units as $key => $value) { $key = explode('|', $key); if ($interval >= $value) { - $output .= ($output ? ' ' : '') . format_plural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode)); + $output .= ($output ? ' ' : '') . $this->stringTranslation->formatPlural(floor($interval / $value), $key[0], $key[1], array(), array('langcode' => $langcode)); $interval %= $value; $granularity--; } diff -u b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php --- b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationInterface.php @@ -79,5 +79,5 @@ * @see format_string() */ - function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()); + public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()); } diff -u b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php --- b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php +++ b/core/lib/Drupal/Core/StringTranslation/TranslationManager.php @@ -129,7 +129,7 @@ /** * {@inheritdoc} */ - function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { + public function formatPlural($count, $singular, $plural, array $args = array(), array $options = array()) { $args['@count'] = $count; // Join both forms to search a translation. $tranlatable_string = implode(LOCALE_PLURAL_DELIMITER, array($singular, $plural)); only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Core/Datetime/DateTest.php @@ -0,0 +1,143 @@ + 'Date service test.', + 'description' => 'Tests the date service.', + 'group' => 'System' + ); + } + + protected function setUp() { + $this->entityManager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + $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); + } + + /** + * Tests the formatPlugin method. + * + * @dataProvider providerTestFormatInterval + * + * @see \Drupal\Core\Datetime\Date::formatInterval() + */ + public function testFormatInterval($interval, $granularity, $expected, $langcode = 'en') { + // Mocks a simple formatPlural implementation. + $this->stringTranslation->expects($this->any()) + ->method('formatPlural') + ->with($this->anything(), $this->anything(), $this->anything(), array(), array('langcode' => $langcode)) + ->will($this->returnCallback(function($count, $one, $multiple) { + return $count == 1 ? $one : str_replace('@count', $count, $multiple); + })); + + $result = $this->date->formatInterval($interval, $granularity, $langcode); + + $this->assertEquals($expected, $result); + } + + /** + * Provides some test data for the format interval test. + */ + public function providerTestFormatInterval() { + $data = array( + // Checks for basic seconds. + array(1, 1, '1 sec'), + array(1, 2, '1 sec'), + array(2, 1, '2 sec'), + array(2, 2, '2 sec'), + // Checks for minutes with seconds. + array(61, 1, '1 min'), + array(61, 2, '1 min 1 sec'), + array(62, 2, '1 min 2 sec'), + array(121, 1, '2 min'), + array(121, 2, '2 min 1 sec'), + // Check for hours with minutes and seconds. + array(3601, 1, '1 hour'), + array(3601, 2, '1 hour 1 sec'), + // Check for higher units. + array(86401, 1, '1 day'), + array(604800, 1, '1 week'), + array(2592000 * 2, 1, '2 months'), + array(31536000 * 2, 1, '2 years'), + // Check for a complicated one with months weeks and days. + 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'), + ); + + return $data; + } + + /** + * Tests the formatInterval method for 0 second. + */ + public function testFormatIntervalZeroSecond() { + $this->stringTranslation->expects($this->once()) + ->method('translate') + ->with('0 sec', array(), array('langcode' => 'xxx-lolspeak')) + ->will($this->returnValue('0 sec')); + + $result = $this->date->formatInterval(0, 1, 'xxx-lolspeak'); + + $this->assertEquals('0 sec', $result); + } + +}