diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php index eb107e8e3931526d5045303dfb39c20477eb6fb3..421b03b51eb31c09e9eadbe8aee1d27f332879d8 100644 --- a/core/lib/Drupal/Core/Datetime/DateFormatter.php +++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php @@ -122,11 +122,15 @@ public function format($timestamp, $type = 'medium', $format = '', $timezone = N $date = DrupalDateTime::createFromTimestamp($timestamp, $this->timezones[$timezone], $create_settings); // If we have a non-custom date format use the provided date format pattern. - if ($date_format = $this->dateFormat($type, $langcode)) { - $format = $date_format->getPattern(); + if ($type !== 'custom') { + if ($date_format = $this->dateFormat($type, $langcode)) { + $format = $date_format->getPattern(); + } } - // Fall back to medium if a format was not found. + // Fall back to the 'medium' date format type if the format string is + // empty, either from not finding a requested date format or being given an + // empty custom format string. if (empty($format)) { $format = $this->dateFormat('fallback', $langcode)->getPattern(); } @@ -316,9 +320,9 @@ public function formatDiff($from, $to, $options = array()) { * @param string $langcode * The langcode of the language to use. * - * @return string|null - * The pattern for the date format in the given language for non-custom - * formats, NULL otherwise. + * @return \Drupal\Core\Datetime\DateFormatInterface|null + * The configuration entity for the date format in the given language for + * non-custom formats, NULL otherwise. */ protected function dateFormat($format, $langcode) { if (!isset($this->dateFormats[$format][$langcode])) { diff --git a/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php b/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php index 5fbb4dff088f0ae0a7ca8e13171e7812eb1f7094..036b385ae43abd3534d5c97d5a77528842db71e6 100644 --- a/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php +++ b/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php @@ -51,6 +51,8 @@ protected function setUp() { public function testFormatDate() { /** @var \Drupal\Core\Datetime\DateFormatterInterface $formatter */ $formatter = $this->container->get('date.formatter'); + /** @var \Drupal\Core\Language\LanguageManagerInterface $language_manager */ + $language_manager = $this->container->get('language_manager'); $timestamp = strtotime('2007-03-26T00:00:00+00:00'); $this->assertSame('Sunday, 25-Mar-07 17:00:00 PDT', $formatter->format($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Test all parameters.'); @@ -59,6 +61,14 @@ public function testFormatDate() { $this->assertSame('\\domingo, 25-Mar-07 17:00:00 PDT', $formatter->format($timestamp, 'custom', '\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'Test format containing backslash character.'); $this->assertSame('\\l, 25-Mar-07 17:00:00 PDT', $formatter->format($timestamp, 'custom', '\\\\\\l, d-M-y H:i:s T', 'America/Los_Angeles', self::LANGCODE), 'Test format containing backslash followed by escaped format string.'); $this->assertSame('Monday, 26-Mar-07 01:00:00 BST', $formatter->format($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London', 'en'), 'Test a different time zone.'); + $this->assertSame('Thu, 01/01/1970 - 00:00', $formatter->format(0, 'custom', '', 'UTC', 'en'), 'Test custom format with empty string.'); + + // Make sure we didn't change the configuration override language. + $this->assertSame('en', $language_manager->getConfigOverrideLanguage()->getId(), 'Configuration override language not disturbed,'); + + // Test bad format string will use the fallback format. + $this->assertSame($formatter->format($timestamp, 'fallback'), $formatter->format($timestamp, 'bad_format_string'), 'Test fallback format.'); + $this->assertSame('en', $language_manager->getConfigOverrideLanguage()->getId(), 'Configuration override language not disturbed,'); // Change the default language and timezone. $this->config('system.site')->set('default_langcode', static::LANGCODE)->save(); @@ -66,7 +76,8 @@ public function testFormatDate() { // Reset the language manager so new negotiations attempts will fall back on // on the new language. - $this->container->get('language_manager')->reset(); + $language_manager->reset(); + $this->assertSame('en', $language_manager->getConfigOverrideLanguage()->getId(), 'Configuration override language not disturbed,'); $this->assertSame('Sunday, 25-Mar-07 17:00:00 PDT', $formatter->format($timestamp, 'custom', 'l, d-M-y H:i:s T', 'America/Los_Angeles', 'en'), 'Test a different language.'); $this->assertSame('Monday, 26-Mar-07 01:00:00 BST', $formatter->format($timestamp, 'custom', 'l, d-M-y H:i:s T', 'Europe/London'), 'Test a different time zone.'); @@ -84,6 +95,13 @@ public function testFormatDate() { $this->assertSame('2007-03', $formatter->format($timestamp, 'html_month'), 'Test html_month date format.'); $this->assertSame('2007', $formatter->format($timestamp, 'html_year'), 'Test html_year date format.'); + // Make sure we didn't change the configuration override language. + $this->assertSame('en', $language_manager->getConfigOverrideLanguage()->getId(), 'Configuration override language not disturbed,'); + + // Test bad format string will use the fallback format. + $this->assertSame($formatter->format($timestamp, 'fallback'), $formatter->format($timestamp, 'bad_format_string'), 'Test fallback format.'); + $this->assertSame('en', $language_manager->getConfigOverrideLanguage()->getId(), 'Configuration override language not disturbed,'); + // HTML is not escaped by the date formatter, it must be escaped later. $this->assertSame("", $formatter->format($timestamp, 'custom', '\<\s\c\r\i\p\t\>\a\l\e\r\t\(\'Y\'\)\;\<\/\s\c\r\i\p\t\>'), 'Script tags not removed from dates.'); $this->assertSame('2007', $formatter->format($timestamp, 'custom', '\<\e\m\>Y\<\/\e\m\>'), 'Em tags are not removed from dates.');