diff --git a/core/lib/Drupal/Core/Datetime/Element/Datelist.php b/core/lib/Drupal/Core/Datetime/Element/Datelist.php index d3bc361..7f78fa8 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datelist.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datelist.php @@ -6,7 +6,6 @@ use Drupal\Core\Datetime\DateHelper; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Form\FormStateInterface; -use DateTimeZone; /** * Provides a datelist element. @@ -77,7 +76,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form if (!empty($element['#default_value'])) { $date = $element['#default_value']; if ($date instanceof DrupalDateTime && !$date->hasErrors()) { - $date->setTimezone(new DateTimeZone($element['#date_timezone'])); + $date->setTimezone(new \DateTimeZone($element['#date_timezone'])); static::incrementRound($date, $increment); foreach ($parts as $part) { switch ($part) { diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index 639dfd5..d6599ce 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -6,7 +6,6 @@ use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Datetime\Entity\DateFormat; -use DateTimeZone; /** * Provides a datetime element. @@ -97,7 +96,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form else { $date = $element['#default_value']; if ($date instanceof DrupalDateTime && !$date->hasErrors()) { - $date->setTimezone(new DateTimeZone($element['#date_timezone'])); + $date->setTimezone(new \DateTimeZone($element['#date_timezone'])); $input = [ 'date' => $date->format($element['#date_date_format']), 'time' => $date->format($element['#date_time_format']), diff --git a/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php b/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php index bdf2334..68cda5d 100644 --- a/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php +++ b/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php @@ -2,14 +2,13 @@ namespace Drupal\KernelTests\Core\Datetime\Element; -use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; +use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Datetime\Entity\DateFormat; -use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormBuilderInterface; use Drupal\Core\Form\FormInterface; use Drupal\Core\Form\FormState; -use DateTimeZone; -use Drupal\Core\Datetime\DrupalDateTime; +use Drupal\Core\Form\FormStateInterface; +use Drupal\KernelTests\Core\Entity\EntityKernelTestBase; /** * Tests the timezone handling of datetime and datelist element types. @@ -20,6 +19,7 @@ * * @group Form */ + class TimezoneTest extends EntityKernelTestBase implements FormInterface { /** @@ -128,7 +128,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { ]; $dateWithwithTimeZoneA = clone $this->date; - $dateWithwithTimeZoneA->setTimezone(new DateTimeZone($this->timezones['zone A'])); + $dateWithwithTimeZoneA->setTimezone(new \DateTimeZone($this->timezones['zone A'])); $form['test6'] = [ '#title' => 'Default date present with unusual timezone, #date_timezone same', '#type' => $this->elementType, @@ -180,7 +180,7 @@ protected function setUp() { $this->timeFormat = DateFormat::load('html_time')->getPattern(); $date = clone $this->date; foreach ($this->timezones as $label => $timezone) { - $date->setTimezone(new DateTimeZone($timezone)); + $date->setTimezone(new \DateTimeZone($timezone)); $this->formattedDates['date'][$label] = $date->format($this->dateFormat); $this->formattedDates['time'][$label] = $date->format($this->timeFormat); $this->formattedDates['day'][$label] = $date->format('j'); @@ -279,7 +279,7 @@ protected function assertTimesUnderstoodCorrectly($elementType, array $inputs) { $form_builder->submitForm($this, $form_state); // Examine the output of each test element. - $utc = new DateTimeZone('UTC'); + $utc = new \DateTimeZone('UTC'); $expectedDateUTC = clone $this->date; $expectedDateUTC->setTimezone($utc)->format('Y-m-d H:i:s'); $wrongDates = []; @@ -315,9 +315,9 @@ protected function assertTimesUnderstoodCorrectly($elementType, array $inputs) { } $message = "On all elements the time should be understood correctly as " . $expectedDateUTC . ": \n" . print_r($wrongDates, TRUE); - $this->assertTrue(($rightDates === $this->testConditions), $message); + $this->assertEquals($rightDates, $this->testConditions, $message); $message = "On all elements the correct timezone should be set on the value object: (expected, actual) \n" . print_r($wrongTimezones, TRUE); - $this->assertTrue((count($wrongTimezones) === 0), $message); + $this->assertCount(0, $wrongTimezones, $message); } /** @@ -347,7 +347,7 @@ public function assertDateTimezonePropertyProcessed($elementType) { } } $this->assertEquals($this->timezones['user'], drupal_get_user_timezone(), "Subsequent tests assume specific value for drupal_get_user_timezone()."); - $message = "The correct timezone should be set on the processed datetime elements: (expected, actual) \n" . print_r($wrongTimezones, TRUE); + $message = "The correct timezone should be set on the processed " . $this->elementType . " elements: (expected, actual) \n" . print_r($wrongTimezones, TRUE); $this->assertTrue((count($wrongTimezones) === 0), $message); } }