f4a6342956a7cf2fe0a45ca6363f5e5cdd3f8aaa
[yaffs-website] / web / core / modules / datetime / tests / src / Kernel / Views / FilterDateTest.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\field\Entity\FieldStorageConfig;
8 use Drupal\node\Entity\Node;
9 use Drupal\views\Views;
10
11 /**
12  * Tests date-only fields.
13  *
14  * @group datetime
15  */
16 class FilterDateTest extends DateTimeHandlerTestBase {
17
18   /**
19    * {@inheritdoc}
20    */
21   public static $testViews = ['test_filter_datetime'];
22
23   /**
24    * An array of timezone extremes to test.
25    *
26    * @var string[]
27    */
28   protected static $timezones = [
29     // UTC-12, no DST.
30     'Pacific/Kwajalein',
31     // UTC-11, no DST.
32     'Pacific/Midway',
33     // UTC-7, no DST.
34     'America/Phoenix',
35     // UTC.
36     'UTC',
37     // UTC+5:30, no DST.
38     'Asia/Kolkata',
39     // UTC+12, no DST.
40     'Pacific/Funafuti',
41     // UTC+13, no DST.
42     'Pacific/Tongatapu',
43   ];
44
45   /**
46    * {@inheritdoc}
47    *
48    * Create nodes with relative dates of yesterday, today, and tomorrow.
49    */
50   protected function setUp($import_test_views = TRUE) {
51     parent::setUp($import_test_views);
52
53     // Change field storage to date-only.
54     $storage = FieldStorageConfig::load('node.' . static::$field_name);
55     $storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
56     $storage->save();
57
58     // Retrieve tomorrow, today and yesterday dates just to create the nodes.
59     $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
60     $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
61
62     // Clean the nodes on setUp.
63     $this->nodes = [];
64     foreach ($dates as $date) {
65       $node = Node::create([
66         'title' => $this->randomMachineName(8),
67         'type' => 'page',
68         'field_date' => [
69           'value' => $date,
70         ],
71       ]);
72       $node->save();
73       $this->nodes[] = $node;
74     }
75   }
76
77   /**
78    * Test offsets with date-only fields.
79    */
80   public function testDateOffsets() {
81     $view = Views::getView('test_filter_datetime');
82     $field = static::$field_name . '_value';
83
84     foreach (static::$timezones as $timezone) {
85
86       $this->setSiteTimezone($timezone);
87       $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
88       $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
89       $this->updateNodesDateFieldsValues($dates);
90
91       // Test simple operations.
92       $view->initHandlers();
93
94       // A greater than or equal to 'now', should return the 'today' and the
95       // 'tomorrow' node.
96       $view->filter[$field]->operator = '>=';
97       $view->filter[$field]->value['type'] = 'offset';
98       $view->filter[$field]->value['value'] = 'now';
99       $view->setDisplay('default');
100       $this->executeView($view);
101       $expected_result = [
102         ['nid' => $this->nodes[0]->id()],
103         ['nid' => $this->nodes[1]->id()],
104       ];
105       $this->assertIdenticalResultset($view, $expected_result, $this->map);
106       $view->destroy();
107
108       // Only dates in the past.
109       $view->initHandlers();
110       $view->filter[$field]->operator = '<';
111       $view->filter[$field]->value['type'] = 'offset';
112       $view->filter[$field]->value['value'] = 'now';
113       $view->setDisplay('default');
114       $this->executeView($view);
115       $expected_result = [
116         ['nid' => $this->nodes[2]->id()],
117       ];
118       $this->assertIdenticalResultset($view, $expected_result, $this->map);
119       $view->destroy();
120
121       // Test offset for between operator. Only 'tomorrow' node should appear.
122       $view->initHandlers();
123       $view->filter[$field]->operator = 'between';
124       $view->filter[$field]->value['type'] = 'offset';
125       $view->filter[$field]->value['max'] = '+2 days';
126       $view->filter[$field]->value['min'] = '+1 day';
127       $view->setDisplay('default');
128       $this->executeView($view);
129       $expected_result = [
130         ['nid' => $this->nodes[0]->id()],
131       ];
132       $this->assertIdenticalResultset($view, $expected_result, $this->map);
133       $view->destroy();
134     }
135   }
136
137   /**
138    * Test date filter with date-only fields.
139    */
140   public function testDateIs() {
141     $view = Views::getView('test_filter_datetime');
142     $field = static::$field_name . '_value';
143
144     foreach (static::$timezones as $timezone) {
145
146       $this->setSiteTimezone($timezone);
147       $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
148       $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
149       $this->updateNodesDateFieldsValues($dates);
150
151       // Test simple operations.
152       $view->initHandlers();
153
154       // Filtering with nodes date-only values (format: Y-m-d) to test UTC
155       // conversion does NOT change the day.
156       $view->filter[$field]->operator = '=';
157       $view->filter[$field]->value['type'] = 'date';
158       $view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value'];
159       $view->setDisplay('default');
160       $this->executeView($view);
161       $expected_result = [
162         ['nid' => $this->nodes[2]->id()],
163       ];
164       $this->assertIdenticalResultset($view, $expected_result, $this->map);
165       $view->destroy();
166
167       // Test offset for between operator. Only 'today' and 'tomorrow' nodes
168       // should appear.
169       $view->initHandlers();
170       $view->filter[$field]->operator = 'between';
171       $view->filter[$field]->value['type'] = 'date';
172       $view->filter[$field]->value['max'] = $this->nodes[0]->field_date->first()->getValue()['value'];
173       $view->filter[$field]->value['min'] = $this->nodes[1]->field_date->first()->getValue()['value'];
174       $view->setDisplay('default');
175       $this->executeView($view);
176       $expected_result = [
177         ['nid' => $this->nodes[0]->id()],
178         ['nid' => $this->nodes[1]->id()],
179       ];
180       $this->assertIdenticalResultset($view, $expected_result, $this->map);
181       $view->destroy();
182     }
183   }
184
185   /**
186    * Returns UTC timestamp of user's TZ 'now'.
187    *
188    * The date field stores date_only values without conversion, considering them
189    * already as UTC. This method returns the UTC equivalent of user's 'now' as a
190    * unix timestamp, so they match using Y-m-d format.
191    *
192    * @return int
193    *   Unix timestamp.
194    */
195   protected function getUTCEquivalentOfUserNowAsTimestamp() {
196     $user_now = new DateTimePlus('now', new \DateTimeZone(drupal_get_user_timezone()));
197     $utc_equivalent = new DateTimePlus($user_now->format('Y-m-d H:i:s'), new \DateTimeZone(DATETIME_STORAGE_TIMEZONE));
198
199     return $utc_equivalent->getTimestamp();
200   }
201
202   /**
203    * Returns an array formatted date_only values.
204    *
205    * @param int $timestamp
206    *   Unix Timestamp equivalent to user's "now".
207    *
208    * @return array
209    *   An array of DATETIME_DATE_STORAGE_FORMAT date values. In order tomorrow,
210    *   today and yesterday.
211    */
212   protected function getRelativeDateValuesFromTimestamp($timestamp) {
213     return [
214       // Tomorrow.
215       \Drupal::service('date.formatter')->format($timestamp + 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
216       // Today.
217       \Drupal::service('date.formatter')->format($timestamp, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
218       // Yesterday.
219       \Drupal::service('date.formatter')->format($timestamp - 86400, 'custom', DATETIME_DATE_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE),
220     ];
221   }
222
223   /**
224    * Updates tests nodes date fields values.
225    *
226    * @param array $dates
227    *   An array of DATETIME_DATE_STORAGE_FORMAT date values.
228    */
229   protected function updateNodesDateFieldsValues(array $dates) {
230     foreach ($dates as $index => $date) {
231       $this->nodes[$index]->{static::$field_name}->value = $date;
232       $this->nodes[$index]->save();
233     }
234   }
235
236 }