590355164178c9bfb2523450c455e0b585f887b4
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / Views / DateTimeHandlerTestBase.php
1 <?php
2
3 namespace Drupal\Tests\datetime\Kernel\Views;
4
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;
13
14 /**
15  * Base class for testing datetime handlers.
16  */
17 abstract class DateTimeHandlerTestBase extends ViewsKernelTestBase {
18
19   /**
20    * {@inheritdoc}
21    */
22   public static $modules = ['datetime_test', 'node', 'datetime', 'field'];
23
24   /**
25    * Name of the field.
26    *
27    * Note, this is used in the default test view.
28    *
29    * @var string
30    */
31   protected static $field_name = 'field_date';
32
33   /**
34    * Type of the field.
35    *
36    * @var string
37    */
38   protected static $field_type = 'datetime';
39
40   /**
41    * Nodes to test.
42    *
43    * @var \Drupal\node\NodeInterface[]
44    */
45   protected $nodes = [];
46
47   /**
48    * {@inheritdoc}
49    */
50   protected function setUp($import_test_views = TRUE) {
51     parent::setUp($import_test_views);
52
53     $this->installSchema('node', 'node_access');
54     $this->installEntitySchema('node');
55     $this->installEntitySchema('user');
56
57     // Add a date field to page nodes.
58     $node_type = NodeType::create([
59       'type' => 'page',
60       'name' => 'page',
61     ]);
62     $node_type->save();
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],
68     ]);
69     $fieldStorage->save();
70     $field = FieldConfig::create([
71       'field_storage' => $fieldStorage,
72       'bundle' => 'page',
73       'required' => TRUE,
74     ]);
75     $field->save();
76
77     // Views needs to be aware of the new field.
78     $this->container->get('views.views_data')->clear();
79
80     // Set column map.
81     $this->map = [
82       'nid' => 'nid',
83     ];
84
85     // Load test views.
86     ViewTestData::createTestViews(get_class($this), ['datetime_test']);
87   }
88
89   /**
90    * Sets the site timezone to a given timezone.
91    *
92    * @param string $timezone
93    *   The timezone identifier to set.
94    */
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)
100       ->save();
101   }
102
103   /**
104    * Returns UTC timestamp of user's TZ 'now'.
105    *
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.
109    *
110    * @return int
111    *   Unix timestamp.
112    */
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));
116
117     return $utc_equivalent->getTimestamp();
118   }
119
120   /**
121    * Returns an array formatted date_only values relative to timestamp.
122    *
123    * @param int $timestamp
124    *   Unix Timestamp used as 'today'.
125    *
126    * @return array
127    *   An array of DateTimeItemInterface::DATE_STORAGE_FORMAT date values. In
128    *   order tomorrow, today and yesterday.
129    */
130   protected function getRelativeDateValuesFromTimestamp($timestamp) {
131     return [
132       // Tomorrow.
133       \Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
134       // Today.
135       \Drupal::service('date.formatter')->format($timestamp, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
136       // Yesterday.
137       \Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DateTimeItemInterface::DATE_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE),
138     ];
139   }
140
141 }