diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php index 3fac252..03c92ae 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeDefaultFormatter.php @@ -86,7 +86,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ protected function formatDate($date) { $format_type = $this->getSetting('format_type'); - $timezone = $this->getSetting('timezone_override'); + $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName(); return $this->dateFormatter->format($date->getTimestamp(), $format_type, '', $timezone != '' ? $timezone : NULL); } diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php index 5d5a40c..be9df38 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -141,7 +141,7 @@ public function settingsSummary() { * A DrupalDateTime object. */ protected function setTimeZone(DrupalDateTime $date) { - if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) { + if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { // A date without time has no timezone conversion. $timezone = DATETIME_STORAGE_TIMEZONE; } diff --git a/core/modules/datetime/src/Tests/DateTimeFieldTest.php b/core/modules/datetime/src/Tests/DateTimeFieldTest.php index d8787a3..4fde203 100644 --- a/core/modules/datetime/src/Tests/DateTimeFieldTest.php +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php @@ -54,18 +54,25 @@ class DateTimeFieldTest extends WebTestBase { protected $field; /** + * An array of timezone extremes to test. + * + * @var string[] + */ + protected static $timezones = [ + // UTC-11. + 'Pacific/Midway', + // UTC. + 'UTC', + // UTC+14. + 'Pacific/Auckland', + ]; + + /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - // Set an explicit site timezone, and disallow per-user timezones. - $this->config('system.date') - ->set('timezone.user.configurable', 0) - // A timezone with an offset greater than UTC+12 is used. - ->set('timezone.default', 'Pacific/Auckland') - ->save(); - $web_user = $this->drupalCreateUser(array( 'access content', 'view test entity', @@ -118,6 +125,12 @@ protected function setUp() { function testDateField() { $field_name = $this->fieldStorage->getName(); + // Loop through defined timezones to test that date-only fields work at the + // extremes. + foreach (static::$timezones as $timezone) { + + $this->setSiteTimezone($timezone); + // Display creation form. $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value][date]", '', 'Date element found.'); @@ -157,6 +170,12 @@ function testDateField() { $entity = EntityTest::load($id); $this->assertEqual('2012-12-31', $entity->{$field_name}->value); + // Reset display options since these get changed below. + $this->displayOptions = array( + 'type' => 'datetime_default', + 'label' => 'hidden', + 'settings' => array('format_type' => 'medium') + $this->defaultSettings, + ); // Verify that the date is output according to the formatter settings. $options = array( 'format_type' => array('short', 'medium', 'long'), @@ -172,9 +191,10 @@ function testDateField() { $this->renderTestEntity($id); switch ($setting) { case 'format_type': - // Verify that a date is displayed. - $expected = format_date($date->getTimestamp(), $new_value); - $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', 'UTC'); + // Verify that a date is displayed. Since this is a date-only + // field, it is expected to display the time as 00:00:00. + $expected = format_date($date->getTimestamp(), $new_value, '', DATETIME_STORAGE_TIMEZONE); + $expected_iso = format_date($date->getTimestamp(), 'custom', 'Y-m-d\TH:i:s\Z', DATETIME_STORAGE_TIMEZONE); $this->renderTestEntity($id); $this->assertFieldByXPath('//time[@datetime="' . $expected_iso . '"]', $expected, SafeMarkup::format('Formatted date field using %value format displayed as %expected with %expected_iso attribute.', array('%value' => $new_value, '%expected' => $expected, '%expected_iso' => $expected_iso))); break; @@ -250,6 +270,7 @@ function testDateField() { $this->renderTestEntity($id); $this->assertText($expected, SafeMarkup::format('Formatted date field using datetime_time_ago format displayed as %expected.', array('%expected' => $expected))); } + } /** * Tests date and time field. @@ -884,4 +905,19 @@ protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { $this->verbose($output); } + /** + * Sets the site timezone to a given timezone. + * + * @param string $timezone + * The timezone identifier to set. + */ + protected function setSiteTimezone($timezone) { + // Set an explicit site timezone, and disallow per-user timezones. + $this->config('system.date') + ->set('timezone.user.configurable', 0) + // A timezone with an offset greater than UTC+12 is used. + ->set('timezone.default', $timezone) + ->save(); + } + }