f7386cc2c79438e571f3f968fdfa2f1692e4d22e
[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\datetime\Plugin\Field\FieldType\DateTimeItem;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\node\Entity\Node;
8 use Drupal\views\Views;
9
10 /**
11  * Tests date-only fields.
12  *
13  * @group datetime
14  */
15 class FilterDateTest extends DateTimeHandlerTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $testViews = ['test_filter_datetime'];
21
22   /**
23    * An array of timezone extremes to test.
24    *
25    * @var string[]
26    */
27   protected static $timezones = [
28     // UTC-12, no DST.
29     'Pacific/Kwajalein',
30     // UTC-11, no DST.
31     'Pacific/Midway',
32     // UTC-7, no DST.
33     'America/Phoenix',
34     // UTC.
35     'UTC',
36     // UTC+5:30, no DST.
37     'Asia/Kolkata',
38     // UTC+12, no DST.
39     'Pacific/Funafuti',
40     // UTC+13, no DST.
41     'Pacific/Tongatapu',
42   ];
43
44   /**
45    * {@inheritdoc}
46    *
47    * Create nodes with relative dates of yesterday, today, and tomorrow.
48    */
49   protected function setUp($import_test_views = TRUE) {
50     parent::setUp($import_test_views);
51
52     // Change field storage to date-only.
53     $storage = FieldStorageConfig::load('node.' . static::$field_name);
54     $storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
55     $storage->save();
56
57     // Retrieve tomorrow, today and yesterday dates just to create the nodes.
58     $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
59     $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
60
61     // Clean the nodes on setUp.
62     $this->nodes = [];
63     foreach ($dates as $date) {
64       $node = Node::create([
65         'title' => $this->randomMachineName(8),
66         'type' => 'page',
67         'field_date' => [
68           'value' => $date,
69         ],
70       ]);
71       $node->save();
72       $this->nodes[] = $node;
73     }
74   }
75
76   /**
77    * Test offsets with date-only fields.
78    */
79   public function testDateOffsets() {
80     $view = Views::getView('test_filter_datetime');
81     $field = static::$field_name . '_value';
82
83     foreach (static::$timezones as $timezone) {
84
85       $this->setSiteTimezone($timezone);
86       $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
87       $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
88       $this->updateNodesDateFieldsValues($dates);
89
90       // Test simple operations.
91       $view->initHandlers();
92
93       // A greater than or equal to 'now', should return the 'today' and the
94       // 'tomorrow' node.
95       $view->filter[$field]->operator = '>=';
96       $view->filter[$field]->value['type'] = 'offset';
97       $view->filter[$field]->value['value'] = 'now';
98       $view->setDisplay('default');
99       $this->executeView($view);
100       $expected_result = [
101         ['nid' => $this->nodes[0]->id()],
102         ['nid' => $this->nodes[1]->id()],
103       ];
104       $this->assertIdenticalResultset($view, $expected_result, $this->map);
105       $view->destroy();
106
107       // Only dates in the past.
108       $view->initHandlers();
109       $view->filter[$field]->operator = '<';
110       $view->filter[$field]->value['type'] = 'offset';
111       $view->filter[$field]->value['value'] = 'now';
112       $view->setDisplay('default');
113       $this->executeView($view);
114       $expected_result = [
115         ['nid' => $this->nodes[2]->id()],
116       ];
117       $this->assertIdenticalResultset($view, $expected_result, $this->map);
118       $view->destroy();
119
120       // Test offset for between operator. Only 'tomorrow' node should appear.
121       $view->initHandlers();
122       $view->filter[$field]->operator = 'between';
123       $view->filter[$field]->value['type'] = 'offset';
124       $view->filter[$field]->value['max'] = '+2 days';
125       $view->filter[$field]->value['min'] = '+1 day';
126       $view->setDisplay('default');
127       $this->executeView($view);
128       $expected_result = [
129         ['nid' => $this->nodes[0]->id()],
130       ];
131       $this->assertIdenticalResultset($view, $expected_result, $this->map);
132       $view->destroy();
133     }
134   }
135
136   /**
137    * Test date filter with date-only fields.
138    */
139   public function testDateIs() {
140     $view = Views::getView('test_filter_datetime');
141     $field = static::$field_name . '_value';
142
143     foreach (static::$timezones as $timezone) {
144
145       $this->setSiteTimezone($timezone);
146       $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
147       $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
148       $this->updateNodesDateFieldsValues($dates);
149
150       // Test simple operations.
151       $view->initHandlers();
152
153       // Filtering with nodes date-only values (format: Y-m-d) to test UTC
154       // conversion does NOT change the day.
155       $view->filter[$field]->operator = '=';
156       $view->filter[$field]->value['type'] = 'date';
157       $view->filter[$field]->value['value'] = $this->nodes[2]->field_date->first()->getValue()['value'];
158       $view->setDisplay('default');
159       $this->executeView($view);
160       $expected_result = [
161         ['nid' => $this->nodes[2]->id()],
162       ];
163       $this->assertIdenticalResultset($view, $expected_result, $this->map);
164       $view->destroy();
165
166       // Test offset for between operator. Only 'today' and 'tomorrow' nodes
167       // should appear.
168       $view->initHandlers();
169       $view->filter[$field]->operator = 'between';
170       $view->filter[$field]->value['type'] = 'date';
171       $view->filter[$field]->value['max'] = $this->nodes[0]->field_date->first()->getValue()['value'];
172       $view->filter[$field]->value['min'] = $this->nodes[1]->field_date->first()->getValue()['value'];
173       $view->setDisplay('default');
174       $this->executeView($view);
175       $expected_result = [
176         ['nid' => $this->nodes[0]->id()],
177         ['nid' => $this->nodes[1]->id()],
178       ];
179       $this->assertIdenticalResultset($view, $expected_result, $this->map);
180       $view->destroy();
181     }
182   }
183
184   /**
185    * Updates tests nodes date fields values.
186    *
187    * @param array $dates
188    *   An array of DATETIME_DATE_STORAGE_FORMAT date values.
189    */
190   protected function updateNodesDateFieldsValues(array $dates) {
191     foreach ($dates as $index => $date) {
192       $this->nodes[$index]->{static::$field_name}->value = $date;
193       $this->nodes[$index]->save();
194     }
195   }
196
197 }