Skip to content
Snippets Groups Projects
Commit 2736deb8 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2846643 by mpdonadio, bkosborne, jhedstrom, alexpott: DateFormatter...

Issue #2846643 by mpdonadio, bkosborne, jhedstrom, alexpott: DateFormatter should not attempt to load a format from storage if the format is "custom"
parent 9f2371f2
No related branches found
No related tags found
No related merge requests found
......@@ -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])) {
......
......@@ -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("<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.');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment