diff --git a/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php b/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php index 0340ba4..de236c9 100644 --- a/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php +++ b/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php @@ -2,7 +2,9 @@ namespace Drupal\Tests\datetime\Kernel\Views; +use Drupal\Component\Datetime\DateTimePlus; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\field\Entity\FieldConfig; use Drupal\node\Entity\NodeType; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; @@ -98,4 +100,42 @@ protected function setSiteTimezone($timezone) { ->save(); } + /** + * Returns UTC timestamp of user's TZ 'now'. + * + * The date field stores date_only values without conversion, considering them + * already as UTC. This method returns the UTC equivalent of user's 'now' as a + * unix timestamp, so they match using Y-m-d format. + * + * @return int + * Unix timestamp. + */ + protected function getUTCEquivalentOfUserNowAsTimestamp() { + $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone())); + $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE)); + + return $utc_equivalent->getTimestamp(); + } + + /** + * Returns an array formatted date_only values relative to timestamp. + * + * @param int $timestamp + * Unix Timestamp used as 'today'. + * + * @return array + * An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In + * order tomorrow, today and yesterday. + */ + protected function getRelativeDateValuesFromTimestamp($timestamp) { + return [ + // Tomorrow. + \Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE), + // Today. + \Drupal::service('date.formatter')->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE), + // Yesterday. + \Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE), + ]; + } + } diff --git a/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php b/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php index f4a6342..f7386cc 100644 --- a/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php +++ b/core/modules/datetime/tests/src/Kernel/Views/FilterDateTest.php @@ -2,7 +2,6 @@ namespace Drupal\Tests\datetime\Kernel\Views; -use Drupal\Component\Datetime\DateTimePlus; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; use Drupal\field\Entity\FieldStorageConfig; use Drupal\node\Entity\Node; @@ -183,44 +182,6 @@ public function testDateIs() { } /** - * Returns UTC timestamp of user's TZ 'now'. - * - * The date field stores date_only values without conversion, considering them - * already as UTC. This method returns the UTC equivalent of user's 'now' as a - * unix timestamp, so they match using Y-m-d format. - * - * @return int - * Unix timestamp. - */ - protected function getUTCEquivalentOfUserNowAsTimestamp() { - $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone())); - $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DATETIME_STORAGE_TIMEZONE)); - - return $utc_equivalent->getTimestamp(); - } - - /** - * Returns an array formatted date_only values. - * - * @param int $timestamp - * Unix Timestamp equivalent to user's "now". - * - * @return array - * An array of DATETIME_DATE_STORAGE_FORMAT date values. In order tomorrow, - * today and yesterday. - */ - protected function getRelativeDateValuesFromTimestamp($timestamp) { - return [ - // Tomorrow. - \Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - // Today. - \Drupal::service('date.formatter')->format($timestamp, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - // Yesterday. - \Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - ]; - } - - /** * Updates tests nodes date fields values. * * @param array $dates diff --git a/core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php b/core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php index f32ef8a..270159c 100644 --- a/core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php +++ b/core/modules/datetime_range/tests/src/Kernel/Views/FilterDateTest.php @@ -48,23 +48,15 @@ protected function setUp($import_test_views = TRUE) { parent::setUp($import_test_views); // Set to 'today'. - $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone())); - $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DATETIME_STORAGE_TIMEZONE)); - static::$date = $utc_equivalent->getTimestamp(); + static::$date = $this->getUTCEquivalentOfUserNowAsTimestamp(); // Change field storage to date-only. $storage = FieldStorageConfig::load('node.' . static::$field_name); $storage->setSetting('datetime_type', DateRangeItem::DATETIME_TYPE_DATE); $storage->save(); - $dates = [ - // Tomorrow. - \Drupal::service('date.formatter')->format(static::$date + 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - // Today. - \Drupal::service('date.formatter')->format(static::$date, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - // Yesterday. - \Drupal::service('date.formatter')->format(static::$date - 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE), - ]; + // Retrieve tomorrow, today and yesterday dates. + $dates = $this->getRelativeDateValuesFromTimestamp(static::$date); // Node 0: Yesterday - Today. $node = Node::create([