summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlex Pott2017-02-21 16:50:21 (GMT)
committerAlex Pott2017-02-21 16:50:21 (GMT)
commit2736deb8d11dc0c62ffc02ca4e2532bfa3e8bbdc (patch)
treec32bcc43becaea11b54f64a6bc83f24d6ee3fd98
parent9f2371f26e66cefb60fa7574ea707420469b51bc (diff)
Issue #2846643 by mpdonadio, bkosborne, jhedstrom, alexpott: DateFormatter should not attempt to load a format from storage if the format is "custom"
-rw-r--r--core/lib/Drupal/Core/Datetime/DateFormatter.php16
-rw-r--r--core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php20
2 files changed, 29 insertions, 7 deletions
diff --git a/core/lib/Drupal/Core/Datetime/DateFormatter.php b/core/lib/Drupal/Core/Datetime/DateFormatter.php
index eb107e8..421b03b 100644
--- a/core/lib/Drupal/Core/Datetime/DateFormatter.php
+++ b/core/lib/Drupal/Core/Datetime/DateFormatter.php
@@ -122,11 +122,15 @@ class DateFormatter implements DateFormatterInterface {
$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 @@ class DateFormatter implements DateFormatterInterface {
* @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 5fbb4df..036b385 100644
--- a/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Datetime/FormatDateTest.php
@@ -51,6 +51,8 @@ class FormatDateTest extends KernelTestBase {
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 @@ class FormatDateTest extends KernelTestBase {
$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 @@ class FormatDateTest extends KernelTestBase {
// 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 @@ class FormatDateTest extends KernelTestBase {
$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("<script>alert('2007');</script>", $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('<em>2007</em>', $formatter->format($timestamp, 'custom', '\<\e\m\>Y\<\/\e\m\>'), 'Em tags are not removed from dates.');