3 namespace Drupal\Tests\datetime\Kernel\Views;
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;
11 * Tests date-only fields.
15 class FilterDateTest extends DateTimeHandlerTestBase {
20 public static $testViews = ['test_filter_datetime'];
23 * An array of timezone extremes to test.
27 protected static $timezones = [
47 * Create nodes with relative dates of yesterday, today, and tomorrow.
49 protected function setUp($import_test_views = TRUE) {
50 parent::setUp($import_test_views);
52 // Change field storage to date-only.
53 $storage = FieldStorageConfig::load('node.' . static::$field_name);
54 $storage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);
57 // Retrieve tomorrow, today and yesterday dates just to create the nodes.
58 $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
59 $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
61 // Clean the nodes on setUp.
63 foreach ($dates as $date) {
64 $node = Node::create([
65 'title' => $this->randomMachineName(8),
72 $this->nodes[] = $node;
77 * Test offsets with date-only fields.
79 public function testDateOffsets() {
80 $view = Views::getView('test_filter_datetime');
81 $field = static::$field_name . '_value';
83 foreach (static::$timezones as $timezone) {
85 $this->setSiteTimezone($timezone);
86 $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
87 $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
88 $this->updateNodesDateFieldsValues($dates);
90 // Test simple operations.
91 $view->initHandlers();
93 // A greater than or equal to 'now', should return the 'today' and the
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);
101 ['nid' => $this->nodes[0]->id()],
102 ['nid' => $this->nodes[1]->id()],
104 $this->assertIdenticalResultset($view, $expected_result, $this->map);
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);
115 ['nid' => $this->nodes[2]->id()],
117 $this->assertIdenticalResultset($view, $expected_result, $this->map);
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);
129 ['nid' => $this->nodes[0]->id()],
131 $this->assertIdenticalResultset($view, $expected_result, $this->map);
137 * Test date filter with date-only fields.
139 public function testDateIs() {
140 $view = Views::getView('test_filter_datetime');
141 $field = static::$field_name . '_value';
143 foreach (static::$timezones as $timezone) {
145 $this->setSiteTimezone($timezone);
146 $timestamp = $this->getUTCEquivalentOfUserNowAsTimestamp();
147 $dates = $this->getRelativeDateValuesFromTimestamp($timestamp);
148 $this->updateNodesDateFieldsValues($dates);
150 // Test simple operations.
151 $view->initHandlers();
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);
161 ['nid' => $this->nodes[2]->id()],
163 $this->assertIdenticalResultset($view, $expected_result, $this->map);
166 // Test offset for between operator. Only 'today' and 'tomorrow' nodes
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);
176 ['nid' => $this->nodes[0]->id()],
177 ['nid' => $this->nodes[1]->id()],
179 $this->assertIdenticalResultset($view, $expected_result, $this->map);
185 * Updates tests nodes date fields values.
187 * @param array $dates
188 * An array of DATETIME_DATE_STORAGE_FORMAT date values.
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();