3 namespace Drupal\Tests\datetime\Kernel\Views;
5 use Drupal\Component\Datetime\DateTimePlus;
6 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;
7 use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
11 use Drupal\views\Tests\ViewTestData;
12 use Drupal\field\Entity\FieldStorageConfig;
15 * Base class for testing datetime handlers.
17 abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase {
22 public static $modules = ['datetime_test', 'node', 'datetime', 'field'];
27 * Note, this is used in the default test view.
31 protected static $field_name = 'field_date';
38 protected static $field_type = 'datetime';
43 * @var \Drupal\node\NodeInterface[]
45 protected $nodes = [];
50 protected function setUp($import_test_views = TRUE) {
51 parent::setUp($import_test_views);
53 $this->installSchema('node', 'node_access');
54 $this->installEntitySchema('node');
55 $this->installEntitySchema('user');
57 // Add a date field to page nodes.
58 $node_type = NodeType::create([
63 $fieldStorage = FieldStorageConfig::create([
64 'field_name' => static::$field_name,
65 'entity_type' => 'node',
66 'type' => static::$field_type,
67 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],
69 $fieldStorage->save();
70 $field = FieldConfig::create([
71 'field_storage' => $fieldStorage,
77 // Views needs to be aware of the new field.
78 $this->container->get('views.views_data')->clear();
86 ViewTestData::createTestViews(get_class($this), ['datetime_test']);
90 * Sets the site timezone to a given timezone.
92 * @param string $timezone
93 * The timezone identifier to set.
95 protected function setSiteTimezone($timezone) {
96 // Set an explicit site timezone, and disallow per-user timezones.
97 $this->config('system.date')
98 ->set('timezone.user.configurable', 0)
99 ->set('timezone.default', $timezone)
104 * Returns UTC timestamp of user's TZ 'now'.
106 * The date field stores date_only values without conversion, considering them
107 * already as UTC. This method returns the UTC equivalent of user's 'now' as a
108 * unix timestamp, so they match using Y-m-d format.
113 protected function getUTCEquivalentOfUserNowAsTimestamp() {
114 $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone()));
115 $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
117 return $utc_equivalent->getTimestamp();
121 * Returns an array formatted date_only values relative to timestamp.
123 * @param int $timestamp
124 * Unix Timestamp used as 'today'.
127 * An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In
128 * order tomorrow, today and yesterday.
130 protected function getRelativeDateValuesFromTimestamp($timestamp) {
133 \Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
135 \Drupal::service('date.formatter')->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
137 \Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),