X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FKernel%2FViews%2FDateTimeHandlerTestBase.php;fp=web%2Fcore%2Fmodules%2Fdatetime%2Ftests%2Fsrc%2FKernel%2FViews%2FDateTimeHandlerTestBase.php;h=590355164178c9bfb2523450c455e0b585f887b4;hp=20a3542319f8919da8ffe173aa8f856d86bf0c9d;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php b/web/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php index 20a354231..590355164 100644 --- a/web/core/modules/datetime/tests/src/Kernel/Views/DateTimeHandlerTestBase.php +++ b/web/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; @@ -28,6 +30,13 @@ abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase { */ protected static $field_name = 'field_date'; + /** + * Type of the field. + * + * @var string + */ + protected static $field_type = 'datetime'; + /** * Nodes to test. * @@ -48,13 +57,13 @@ abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase { // Add a date field to page nodes. $node_type = NodeType::create([ 'type' => 'page', - 'name' => 'page' + 'name' => 'page', ]); $node_type->save(); $fieldStorage = FieldStorageConfig::create([ 'field_name' => static::$field_name, 'entity_type' => 'node', - 'type' => 'datetime', + 'type' => static::$field_type, 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME], ]); $fieldStorage->save(); @@ -91,4 +100,42 @@ abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase { ->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), + ]; + } + }