diff --git a/core/core.services.yml b/core/core.services.yml index 9ca753e6a4..8b77e0ce27 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -1527,6 +1527,7 @@ services: arguments: ['@private_key', '@cache.bootstrap', '@cache.static'] current_user: class: Drupal\Core\Session\AccountProxy + arguments: ['@event_dispatcher'] session_configuration: class: Drupal\Core\Session\SessionConfiguration arguments: ['%session.storage.options%'] diff --git a/core/core.services.yml b/core/core.services.yml.orig similarity index 100% copy from core/core.services.yml copy to core/core.services.yml.orig diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc index afd267adcf..0e40f3c82b 100644 --- a/core/includes/bootstrap.inc +++ b/core/includes/bootstrap.inc @@ -530,20 +530,15 @@ function drupal_get_messages($type = NULL, $clear_queue = TRUE) { * * @return string * The name of the current user's timezone or the name of the default timezone. + * + * @deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. + * Use date_default_timezone_get() instead. + * + * @see https://www.drupal.org/node/3009387 */ function drupal_get_user_timezone() { - $user = \Drupal::currentUser(); - $config = \Drupal::config('system.date'); - - if ($user && $config->get('timezone.user.configurable') && $user->isAuthenticated() && $user->getTimezone()) { - return $user->getTimezone(); - } - else { - // Ignore PHP strict notice if time zone has not yet been set in the php.ini - // configuration. - $config_data_default_timezone = $config->get('timezone.default'); - return !empty($config_data_default_timezone) ? $config_data_default_timezone : @date_default_timezone_get(); - } + @trigger_error('drupal_get_user_timezone() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Use date_default_timezone_get() instead. https://www.drupal.org/node/3009387.', E_USER_DEPRECATED); + return date_default_timezone_get(); } /** diff --git a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php index f921b45b46..111d1741b3 100644 --- a/core/lib/Drupal/Core/Datetime/DrupalDateTime.php +++ b/core/lib/Drupal/Core/Datetime/DrupalDateTime.php @@ -71,7 +71,7 @@ public function __construct($time = 'now', $timezone = NULL, $settings = []) { protected function prepareTimezone($timezone) { if (empty($timezone)) { // Fallback to user or system default timezone. - $timezone = drupal_get_user_timezone(); + $timezone = date_default_timezone_get(); } return parent::prepareTimezone($timezone); } diff --git a/core/lib/Drupal/Core/Datetime/Element/Datelist.php b/core/lib/Drupal/Core/Datetime/Element/Datelist.php index d81f39a3eb..b37bf11c63 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datelist.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datelist.php @@ -33,7 +33,7 @@ public function getInfo() { '#date_year_range' => '1900:2050', '#date_increment' => 1, '#date_date_callbacks' => [], - '#date_timezone' => drupal_get_user_timezone(), + '#date_timezone' => date_default_timezone_get(), ]; } @@ -149,7 +149,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form * minute. * - #date_timezone: The local timezone to use when displaying or * interpreting dates. Defaults to the value returned by - * drupal_get_user_timezone(). + * date_default_timezone_get(). * * Example usage: * @code diff --git a/core/lib/Drupal/Core/Datetime/Element/Datetime.php b/core/lib/Drupal/Core/Datetime/Element/Datetime.php index 4b6afaadf3..830e54233e 100644 --- a/core/lib/Drupal/Core/Datetime/Element/Datetime.php +++ b/core/lib/Drupal/Core/Datetime/Element/Datetime.php @@ -61,7 +61,7 @@ public function getInfo() { '#date_time_callbacks' => [], '#date_year_range' => '1900:2050', '#date_increment' => 1, - '#date_timezone' => drupal_get_user_timezone(), + '#date_timezone' => date_default_timezone_get(), ]; } @@ -190,7 +190,7 @@ public static function valueCallback(&$element, $input, FormStateInterface $form * second. * - #date_timezone: The local timezone to use when displaying or * interpreting dates. Defaults to the value returned by - * drupal_get_user_timezone(). + * date_default_timezone_get(). * * Example usage: * @code diff --git a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php index 5377fbf240..d8d0da74e5 100644 --- a/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/AuthenticationSubscriber.php @@ -2,8 +2,8 @@ namespace Drupal\Core\EventSubscriber; -use Drupal\Core\Authentication\AuthenticationProviderFilterInterface; use Drupal\Core\Authentication\AuthenticationProviderChallengeInterface; +use Drupal\Core\Authentication\AuthenticationProviderFilterInterface; use Drupal\Core\Authentication\AuthenticationProviderInterface; use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface; @@ -80,8 +80,6 @@ public function onKernelRequestAuthenticate(GetResponseEvent $event) { return; } } - // No account has been set explicitly, initialize the timezone here. - date_default_timezone_set(drupal_get_user_timezone()); } } diff --git a/core/lib/Drupal/Core/Session/AccountProxy.php b/core/lib/Drupal/Core/Session/AccountProxy.php index cedf93edec..ff4c23bc63 100644 --- a/core/lib/Drupal/Core/Session/AccountProxy.php +++ b/core/lib/Drupal/Core/Session/AccountProxy.php @@ -2,6 +2,9 @@ namespace Drupal\Core\Session; +use Drupal\Core\DependencyInjection\DependencySerializationTrait; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + /** * A proxied implementation of AccountInterface. * @@ -15,6 +18,8 @@ */ class AccountProxy implements AccountProxyInterface { + use DependencySerializationTrait; + /** * The instantiated account. * @@ -38,6 +43,23 @@ class AccountProxy implements AccountProxyInterface { */ protected $initialAccountId; + /** + * Event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + + /** + * AccountProxy constructor. + * + * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher + * Event dispatcher. + */ + public function __construct(EventDispatcherInterface $eventDispatcher) { + $this->eventDispatcher = $eventDispatcher; + } + /** * {@inheritdoc} */ @@ -49,7 +71,7 @@ public function setAccount(AccountInterface $account) { } $this->account = $account; $this->id = $account->id(); - date_default_timezone_set(drupal_get_user_timezone()); + $this->eventDispatcher->dispatch(AccountSetEvent::SET_USER, new AccountSetEvent($account)); } /** diff --git a/core/lib/Drupal/Core/Session/AccountSetEvent.php b/core/lib/Drupal/Core/Session/AccountSetEvent.php new file mode 100644 index 0000000000..091c578f6d --- /dev/null +++ b/core/lib/Drupal/Core/Session/AccountSetEvent.php @@ -0,0 +1,53 @@ +account = $account; + } + + /** + * Gets the account. + * + * @return \Drupal\Core\Session\AccountInterface + * The account. + */ + public function getAccount() { + return $this->account; + } + +} diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php index e3b9daf2ee..4ade45b7a5 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeCustomFormatter.php @@ -55,7 +55,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ protected function formatDate($date) { $format = $this->getSetting('date_format'); - $timezone = $this->getSetting('timezone_override'); + $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName(); return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $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 a6680f6b4d..5db2e24836 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimeFormatterBase.php @@ -160,8 +160,6 @@ public function viewElements(FieldItemListInterface $items, $langcode) { * zone applied to it. This method will apply the time zone for the current * user, based on system and user settings. * - * @see drupal_get_user_timezone() - * * @param \Drupal\Core\Datetime\DrupalDateTime $date * A DrupalDateTime object. */ @@ -171,7 +169,7 @@ protected function setTimeZone(DrupalDateTime $date) { $timezone = DateTimeItemInterface::STORAGE_TIMEZONE; } else { - $timezone = drupal_get_user_timezone(); + $timezone = date_default_timezone_get(); } $date->setTimeZone(timezone_open($timezone)); } diff --git a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php index 8ddfdda2ae..b8b25bc61e 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php +++ b/core/modules/datetime/src/Plugin/Field/FieldFormatter/DateTimePlainFormatter.php @@ -42,7 +42,7 @@ public function viewElements(FieldItemListInterface $items, $langcode) { */ protected function formatDate($date) { $format = $this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE ? DateTimeItemInterface::DATE_STORAGE_FORMAT : DateTimeItemInterface::DATETIME_STORAGE_FORMAT; - $timezone = $this->getSetting('timezone_override'); + $timezone = $this->getSetting('timezone_override') ?: $date->getTimezone()->getName(); return $this->dateFormatter->format($date->getTimestamp(), 'custom', $format, $timezone != '' ? $timezone : NULL); } diff --git a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php index 2af64d7bb9..203f15d0bd 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php @@ -95,7 +95,7 @@ public static function processDefaultValue($default_value, FieldableEntityInterf if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) { // A default date only value should be in the format used for date // storage but in the user's local timezone. - $date = new DrupalDateTime($default_value[0]['default_date'], drupal_get_user_timezone()); + $date = new DrupalDateTime($default_value[0]['default_date'], date_default_timezone_get()); $format = DateTimeItemInterface::DATE_STORAGE_FORMAT; } else { diff --git a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php index d723482824..41e9853a55 100644 --- a/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeWidgetBase.php @@ -22,7 +22,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#type' => 'datetime', '#default_value' => NULL, '#date_increment' => 1, - '#date_timezone' => drupal_get_user_timezone(), + '#date_timezone' => date_default_timezone_get(), '#required' => $element['#required'], ]; diff --git a/core/modules/datetime/src/Plugin/views/filter/Date.php b/core/modules/datetime/src/Plugin/views/filter/Date.php index 41c4726f0e..f73a675fef 100644 --- a/core/modules/datetime/src/Plugin/views/filter/Date.php +++ b/core/modules/datetime/src/Plugin/views/filter/Date.php @@ -151,7 +151,7 @@ protected function opSimple($field) { protected function getTimezone() { return $this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT ? DateTimeItemInterface::STORAGE_TIMEZONE - : drupal_get_user_timezone(); + : date_default_timezone_get(); } /** @@ -173,7 +173,7 @@ protected function getOffset($time, $timezone) { // the user's offset from UTC for use in the query. $origin_offset = 0; if ($this->dateFormat === DateTimeItemInterface::DATE_STORAGE_FORMAT && $this->value['type'] === 'offset') { - $origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(drupal_get_user_timezone()), new \DateTime($time, new \DateTimeZone($timezone))); + $origin_offset = $origin_offset + timezone_offset_get(new \DateTimeZone(date_default_timezone_get()), new \DateTime($time, new \DateTimeZone($timezone))); } return $origin_offset; diff --git a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php index 792e75bef0..c2a776f921 100644 --- a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php +++ b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php @@ -248,7 +248,7 @@ public function testDatetimeField() { $date = new DrupalDateTime($value, 'UTC'); // Update the timezone to the system default. - $date->setTimezone(timezone_open(drupal_get_user_timezone())); + $date->setTimezone(timezone_open(date_default_timezone_get())); // Submit a valid date and ensure it is accepted. $date_format = DateFormat::load('html_date')->getPattern(); @@ -695,7 +695,7 @@ public function testDefaultValue() { // Create a new node to check that datetime field default value is today. $new_node = Node::create(['type' => 'date_content']); - $expected_date = new DrupalDateTime('now', drupal_get_user_timezone()); + $expected_date = new DrupalDateTime('now', date_default_timezone_get()); $this->assertEqual($new_node->get($field_name) ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT)); @@ -734,7 +734,7 @@ public function testDefaultValue() { // Create a new node to check that datetime field default value is +90 // days. $new_node = Node::create(['type' => 'date_content']); - $expected_date = new DrupalDateTime('+90 days', drupal_get_user_timezone()); + $expected_date = new DrupalDateTime('+90 days', date_default_timezone_get()); $this->assertEqual($new_node->get($field_name) ->offsetGet(0)->value, $expected_date->format(DateTimeItemInterface::DATE_STORAGE_FORMAT)); diff --git a/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php b/core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php.orig similarity index 100% copy from core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php copy to core/modules/datetime/tests/src/Functional/DateTimeFieldTest.php.orig diff --git a/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php b/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php index 5903551641..82c54def04 100644 --- a/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php +++ b/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php @@ -111,7 +111,7 @@ protected function setSiteTimezone($timezone) { * Unix timestamp. */ protected function getUTCEquivalentOfUserNowAsTimestamp() { - $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone())); + $user_now = new DateTimePlus('now', new \DateTimeZone(date_default_timezone_get())); $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)); return $utc_equivalent->getTimestamp(); diff --git a/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php index d98804e011..b0f3ea7be7 100644 --- a/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php @@ -62,7 +62,7 @@ public function massageFormValues(array $values, array $form, FormStateInterface } $storage_timezone = new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE); - $user_timezone = new \DateTimeZone(drupal_get_user_timezone()); + $user_timezone = new \DateTimeZone(date_default_timezone_get()); foreach ($values as &$item) { if (!empty($item['value']) && $item['value'] instanceof DrupalDateTime) { diff --git a/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php b/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php index 9cecd605f0..b95d179b71 100644 --- a/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeFieldTest.php @@ -299,8 +299,8 @@ public function testDatetimeRangeField() { $end_date = new DrupalDateTime($end_value, 'UTC'); // Update the timezone to the system default. - $start_date->setTimezone(timezone_open(drupal_get_user_timezone())); - $end_date->setTimezone(timezone_open(drupal_get_user_timezone())); + $start_date->setTimezone(timezone_open(date_default_timezone_get())); + $end_date->setTimezone(timezone_open(date_default_timezone_get())); // Submit a valid date and ensure it is accepted. $date_format = DateFormat::load('html_date')->getPattern(); @@ -381,7 +381,7 @@ public function testDatetimeRangeField() { $this->drupalGet('entity_test/add'); $value = '2012-12-31 00:00:00'; $start_date = new DrupalDateTime($value, 'UTC'); - $start_date->setTimezone(timezone_open(drupal_get_user_timezone())); + $start_date->setTimezone(timezone_open(date_default_timezone_get())); $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); @@ -468,9 +468,9 @@ public function testAlldayRangeField() { // Build up dates in the proper timezone. $value = '2012-12-31 00:00:00'; - $start_date = new DrupalDateTime($value, timezone_open(drupal_get_user_timezone())); + $start_date = new DrupalDateTime($value, timezone_open(date_default_timezone_get())); $end_value = '2013-06-06 23:59:59'; - $end_date = new DrupalDateTime($end_value, timezone_open(drupal_get_user_timezone())); + $end_date = new DrupalDateTime($end_value, timezone_open(date_default_timezone_get())); // Submit a valid date and ensure it is accepted. $date_format = DateFormat::load('html_date')->getPattern(); @@ -549,9 +549,9 @@ public function testAlldayRangeField() { $this->drupalGet('entity_test/add'); $value = '2012-12-31 00:00:00'; - $start_date = new DrupalDateTime($value, timezone_open(drupal_get_user_timezone())); + $start_date = new DrupalDateTime($value, timezone_open(date_default_timezone_get())); $end_value = '2012-12-31 23:59:59'; - $end_date = new DrupalDateTime($end_value, timezone_open(drupal_get_user_timezone())); + $end_date = new DrupalDateTime($end_value, timezone_open(date_default_timezone_get())); $date_format = DateFormat::load('html_date')->getPattern(); $time_format = DateFormat::load('html_time')->getPattern(); diff --git a/core/modules/system/src/TimeZoneResolver.php b/core/modules/system/src/TimeZoneResolver.php new file mode 100644 index 0000000000..8540b796f3 --- /dev/null +++ b/core/modules/system/src/TimeZoneResolver.php @@ -0,0 +1,100 @@ +configFactory = $config_factory; + $this->currentUser = $current_user; + } + + /** + * Sets the default time zone. + */ + public function setDefaultTimeZone() { + if ($time_zone = $this->getTimeZone()) { + date_default_timezone_set($time_zone); + } + } + + /** + * Updates the default time zone when time zone config changes. + * + * @param \Drupal\Core\Config\ConfigCrudEvent $event + * The config crud event. + */ + public function onConfigSave(ConfigCrudEvent $event) { + $saved_config = $event->getConfig(); + if ($saved_config->getName() == 'system.date' && ($event->isChanged('timezone.default') || $event->isChanged('timezone.user.configurable'))) { + $this->setDefaultTimeZone(); + } + } + + /** + * {@inheritdoc} + */ + public static function getSubscribedEvents() { + $events[ConfigEvents::SAVE][] = ['onConfigSave', 0]; + // The priority for this must run directly after the authentication + // subscriber. + $events[KernelEvents::REQUEST][] = ['setDefaultTimeZone', 299]; + $events[AccountSetEvent::SET_USER][] = ['setDefaultTimeZone']; + return $events; + } + + /** + * Gets the time zone based on site and user configuration. + * + * @return string|null + * The time zone, or NULL if nothing is set. + */ + protected function getTimeZone() { + $config = $this->configFactory->get('system.date'); + if ($config->get('timezone.user.configurable') && $this->currentUser->isAuthenticated() && $this->currentUser->getTimezone()) { + return $this->currentUser->getTimeZone(); + } + elseif ($default_timezone = $config->get('timezone.default')) { + return $default_timezone; + } + return NULL; + } + +} diff --git a/core/modules/system/system.services.yml b/core/modules/system/system.services.yml index ccfabe88b2..047a725c06 100644 --- a/core/modules/system/system.services.yml +++ b/core/modules/system/system.services.yml @@ -43,3 +43,8 @@ services: arguments: ['@theme_handler', '@cache_tags.invalidator'] tags: - { name: event_subscriber } + system.timezone_resolver: + class: Drupal\system\TimeZoneResolver + arguments: ['@current_user', '@config.factory'] + tags: + - { name: event_subscriber } diff --git a/core/modules/system/tests/src/Kernel/TimeZoneDeprecationTest.php b/core/modules/system/tests/src/Kernel/TimeZoneDeprecationTest.php new file mode 100644 index 0000000000..95a1ae8c15 --- /dev/null +++ b/core/modules/system/tests/src/Kernel/TimeZoneDeprecationTest.php @@ -0,0 +1,30 @@ +assertEquals('Australia/Sydney', drupal_get_user_timezone()); + } + +} diff --git a/core/modules/system/tests/src/Kernel/TimezoneResolverTest.php b/core/modules/system/tests/src/Kernel/TimezoneResolverTest.php new file mode 100644 index 0000000000..7c7f889ed4 --- /dev/null +++ b/core/modules/system/tests/src/Kernel/TimezoneResolverTest.php @@ -0,0 +1,62 @@ +installEntitySchema('user'); + $this->installSchema('system', ['sequences']); + $this->installConfig(['system']); + + // Check the default test timezone. + $this->assertEquals('Australia/Sydney', date_default_timezone_get()); + + // Test the configured system timezone. + $configFactory = $this->container->get('config.factory'); + $timeZoneConfig = $configFactory->getEditable('system.date'); + $timeZoneConfig->set('timezone.default', 'Australia/Adelaide'); + $timeZoneConfig->save(); + + $eventDispatcher = $this->container->get('event_dispatcher'); + $kernel = $this->container->get('kernel'); + + $eventDispatcher->dispatch(KernelEvents::REQUEST, new GetResponseEvent($kernel, Request::create('http://www.example.com'), HttpKernelInterface::MASTER_REQUEST)); + + $this->assertEquals('Australia/Adelaide', date_default_timezone_get()); + + $user = $this->createUser([]); + $user->set('timezone', 'Australia/Lord_Howe'); + $user->save(); + + $this->setCurrentUser($user); + + $this->assertEquals('Australia/Lord_Howe', date_default_timezone_get()); + + } + +} diff --git a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php index 83e0bcfa24..b86a78433d 100644 --- a/core/modules/views/src/Plugin/views/query/QueryPluginBase.php +++ b/core/modules/views/src/Plugin/views/query/QueryPluginBase.php @@ -221,13 +221,13 @@ public function getDateField($field, $string_date = FALSE, $calculate_offset = T } /** - * Set the database to the current user timezone, + * Set the database to the current user timezone. * * @return string - * The current timezone as returned by drupal_get_user_timezone(). + * The current timezone as returned by date_default_timezone_get(). */ public function setupTimezone() { - return drupal_get_user_timezone(); + return date_default_timezone_get(); } /** diff --git a/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php b/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php index 8faf8c6c91..1d26c25159 100644 --- a/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php +++ b/core/tests/Drupal/FunctionalTests/Datetime/TimestampTest.php @@ -104,7 +104,7 @@ public function testWidget() { $date = new DrupalDateTime($value, 'UTC'); // Update the timezone to the system default. - $date->setTimezone(timezone_open(drupal_get_user_timezone())); + $date->setTimezone(timezone_open(date_default_timezone_get())); // Display creation form. $this->drupalGet('entity_test/add'); diff --git a/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php b/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php index 074d1b1a98..8a030b3fd1 100644 --- a/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php +++ b/core/tests/Drupal/KernelTests/Core/Datetime/Element/TimezoneTest.php @@ -191,9 +191,8 @@ protected function setUp() { } // Validate the timezone setup. - $this->assertEquals($this->timezones['user'], drupal_get_user_timezone(), 'Subsequent tests assume specific value for drupal_get_user_timezone().'); - $this->assertEquals(drupal_get_user_timezone(), date_default_timezone_get(), "Subsequent tests may assume PHP's time is set to Drupal user's time zone."); - $this->assertEquals(drupal_get_user_timezone(), $this->date->getTimezone()->getName(), 'Subsequent tests assume DrupalDateTime objects default to Drupal user time zone if none specified'); + $this->assertEquals($this->timezones['user'], date_default_timezone_get(), 'Subsequent tests assume specific value for date_default_timezone_get().'); + $this->assertEquals(date_default_timezone_get(), $this->date->getTimezone()->getName(), 'Subsequent tests assume DrupalDateTime objects default to Drupal user time zone if none specified'); } /** @@ -295,7 +294,7 @@ protected function assertTimesUnderstoodCorrectly($elementType, array $inputs) { // Check that $this->date has not anywhere been accidentally changed // from its default timezone, invalidating the test logic. - $this->assertEquals(drupal_get_user_timezone(), $this->date->getTimezone()->getName(), "Test date still set to user timezone."); + $this->assertEquals(date_default_timezone_get(), $this->date->getTimezone()->getName(), "Test date still set to user timezone."); // Build a list of cases where the result is not as expected. // Check the time has been understood correctly. @@ -348,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().'); + $this->assertEquals($this->timezones['user'], date_default_timezone_get(), 'Subsequent tests assume specific value for date_default_timezone_get().'); $message = "The correct timezone should be set on the processed {$this->elementType} elements: (expected, actual) \n" . print_r($wrongTimezones, TRUE); $this->assertCount(0, $wrongTimezones, $message); } diff --git a/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php index 3e6d501723..1ec9c35bfa 100644 --- a/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php +++ b/core/tests/Drupal/Tests/Core/Session/AccountProxyTest.php @@ -3,8 +3,9 @@ namespace Drupal\Tests\Core\Session; use Drupal\Core\Session\AccountInterface; -use Drupal\Tests\UnitTestCase; use Drupal\Core\Session\AccountProxy; +use Drupal\Tests\UnitTestCase; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * @coversDefaultClass \Drupal\Core\Session\AccountProxy @@ -17,7 +18,8 @@ class AccountProxyTest extends UnitTestCase { * @covers ::setInitialAccountId */ public function testId() { - $account_proxy = new AccountProxy(); + $dispatcher = $this->prophesize(EventDispatcherInterface::class); + $account_proxy = new AccountProxy($dispatcher->reveal()); $this->assertSame(0, $account_proxy->id()); $account_proxy->setInitialAccountId(1); $this->assertFalse(\Drupal::hasContainer()); @@ -36,20 +38,11 @@ public function testId() { */ public function testSetInitialAccountIdException() { $this->setExpectedException(\LogicException::class); - $account_proxy = new AccountProxy(); + $dispatcher = $this->prophesize(EventDispatcherInterface::class); + $account_proxy = new AccountProxy($dispatcher->reveal()); $current_user = $this->prophesize(AccountInterface::class); $account_proxy->setAccount($current_user->reveal()); $account_proxy->setInitialAccountId(1); } } - -namespace Drupal\Core\Session; - -if (!function_exists('drupal_get_user_timezone')) { - - function drupal_get_user_timezone() { - return date_default_timezone_get(); - } - -}