3 namespace Drupal\Tests\views\Functional\Handler;
5 use Drupal\config\Tests\SchemaCheckTestTrait;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\Tests\views\Functional\ViewTestBase;
10 use Drupal\views\Views;
13 * Tests the core Drupal\views\Plugin\views\filter\Date handler.
17 class FilterDateTest extends ViewTestBase {
18 use SchemaCheckTestTrait;
21 * Views used by this test.
25 public static $testViews = ['test_filter_date_between'];
32 public static $modules = ['node', 'views_ui', 'datetime'];
34 protected function setUp($import_test_views = TRUE) {
35 parent::setUp($import_test_views);
37 // Add a date field so we can test datetime handling.
43 // Setup a field storage and field, but also change the views data for the
44 // entity_test entity type.
45 $field_storage = FieldStorageConfig::create([
46 'field_name' => 'field_date',
48 'entity_type' => 'node',
50 $field_storage->save();
52 $field = FieldConfig::create([
53 'field_name' => 'field_date',
54 'entity_type' => 'node',
59 // Add some basic test nodes.
61 $this->nodes[] = $this->drupalCreateNode(['created' => 100000, 'field_date' => 10000]);
62 $this->nodes[] = $this->drupalCreateNode(['created' => 200000, 'field_date' => 20000]);
63 $this->nodes[] = $this->drupalCreateNode(['created' => 300000, 'field_date' => 30000]);
64 $this->nodes[] = $this->drupalCreateNode(['created' => time() + 86400, 'field_date' => time() + 86400]);
72 * Runs other test methods.
74 public function testDateFilter() {
76 $this->_testBetween();
77 $this->_testUiValidation();
78 $this->_testFilterDateUI();
79 $this->_testFilterDatetimeUI();
83 * Test the general offset functionality.
85 protected function _testOffset() {
86 $view = Views::getView('test_filter_date_between');
88 // Test offset for simple operator.
89 $view->initHandlers();
90 $view->filter['created']->operator = '>';
91 $view->filter['created']->value['type'] = 'offset';
92 $view->filter['created']->value['value'] = '+1 hour';
93 $view->executeDisplay('default');
95 ['nid' => $this->nodes[3]->id()],
97 $this->assertIdenticalResultset($view, $expected_result, $this->map);
100 // Test offset for between operator.
101 $view->initHandlers();
102 $view->filter['created']->operator = 'between';
103 $view->filter['created']->value['type'] = 'offset';
104 $view->filter['created']->value['max'] = '+2 days';
105 $view->filter['created']->value['min'] = '+1 hour';
106 $view->executeDisplay('default');
108 ['nid' => $this->nodes[3]->id()],
110 $this->assertIdenticalResultset($view, $expected_result, $this->map);
114 * Tests the filter operator between/not between.
116 protected function _testBetween() {
117 $view = Views::getView('test_filter_date_between');
119 // Test between with min and max.
120 $view->initHandlers();
121 $view->filter['created']->operator = 'between';
122 $view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:i:s');
123 $view->filter['created']->value['max'] = format_date(200000, 'custom', 'Y-m-d H:i:s');
124 $view->executeDisplay('default');
126 ['nid' => $this->nodes[1]->id()],
128 $this->assertIdenticalResultset($view, $expected_result, $this->map);
131 // Test between with just max.
132 $view->initHandlers();
133 $view->filter['created']->operator = 'between';
134 $view->filter['created']->value['max'] = format_date(200000, 'custom', 'Y-m-d H:i:s');
135 $view->executeDisplay('default');
137 ['nid' => $this->nodes[0]->id()],
138 ['nid' => $this->nodes[1]->id()],
140 $this->assertIdenticalResultset($view, $expected_result, $this->map);
143 // Test not between with min and max.
144 $view->initHandlers();
145 $view->filter['created']->operator = 'not between';
146 $view->filter['created']->value['min'] = format_date(100000, 'custom', 'Y-m-d H:i:s');
147 $view->filter['created']->value['max'] = format_date(200000, 'custom', 'Y-m-d H:i:s');
149 $view->executeDisplay('default');
151 ['nid' => $this->nodes[2]->id()],
152 ['nid' => $this->nodes[3]->id()],
154 $this->assertIdenticalResultset($view, $expected_result, $this->map);
157 // Test not between with just max.
158 $view->initHandlers();
159 $view->filter['created']->operator = 'not between';
160 $view->filter['created']->value['max'] = format_date(200000, 'custom', 'Y-m-d H:i:s');
161 $view->executeDisplay('default');
163 ['nid' => $this->nodes[2]->id()],
164 ['nid' => $this->nodes[3]->id()],
166 $this->assertIdenticalResultset($view, $expected_result, $this->map);
170 * Make sure the validation callbacks works.
172 protected function _testUiValidation() {
174 $this->drupalLogin($this->drupalCreateUser(['administer views', 'administer site configuration']));
176 $this->drupalGet('admin/structure/views/view/test_filter_date_between/edit');
177 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
180 // Generate a definitive wrong value, which should be checked by validation.
181 $edit['options[value][value]'] = $this->randomString() . '-------';
182 $this->drupalPostForm(NULL, $edit, t('Apply'));
183 $this->assertText(t('Invalid date format.'), 'Make sure that validation is run and the invalidate date format is identified.');
187 * Test date filter UI.
189 protected function _testFilterDateUI() {
190 $this->drupalLogin($this->drupalCreateUser(['administer views']));
191 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
192 $this->drupalPostForm(NULL, [], t('Expose filter'));
193 $this->drupalPostForm(NULL, [], t('Grouped filters'));
196 $edit['options[group_info][group_items][1][title]'] = 'simple-offset';
197 $edit['options[group_info][group_items][1][operator]'] = '>';
198 $edit['options[group_info][group_items][1][value][type]'] = 'offset';
199 $edit['options[group_info][group_items][1][value][value]'] = '+1 hour';
200 $edit['options[group_info][group_items][2][title]'] = 'between-offset';
201 $edit['options[group_info][group_items][2][operator]'] = 'between';
202 $edit['options[group_info][group_items][2][value][type]'] = 'offset';
203 $edit['options[group_info][group_items][2][value][min]'] = '+1 hour';
204 $edit['options[group_info][group_items][2][value][max]'] = '+2 days';
205 $edit['options[group_info][group_items][3][title]'] = 'between-date';
206 $edit['options[group_info][group_items][3][operator]'] = 'between';
207 $edit['options[group_info][group_items][3][value][min]'] = format_date(150000, 'custom', 'Y-m-d H:i:s');
208 $edit['options[group_info][group_items][3][value][max]'] = format_date(250000, 'custom', 'Y-m-d H:i:s');
210 $this->drupalPostForm(NULL, $edit, t('Apply'));
212 $this->drupalGet('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created');
213 foreach ($edit as $name => $value) {
214 $this->assertFieldByName($name, $value);
215 if (strpos($name, '[value][type]')) {
216 $radio = $this->cssSelect('input[name="' . $name . '"][checked="checked"][type="radio"]');
217 $this->assertEqual($radio[0]->getAttribute('value'), $value);
221 $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
222 $this->assertConfigSchemaByName('views.view.test_filter_date_between');
224 // Test that the exposed filter works as expected.
225 $path = 'test_filter_date_between-path';
226 $this->drupalPostForm('admin/structure/views/view/test_filter_date_between/edit', [], 'Add Page');
227 $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_date_between/page_1/path', ['path' => $path], 'Apply');
228 $this->drupalPostForm(NULL, [], t('Save'));
230 $this->drupalGet($path);
231 $this->drupalPostForm(NULL, [], 'Apply');
232 $results = $this->cssSelect('.view-content .field-content');
233 $this->assertEqual(count($results), 4);
234 $this->drupalPostForm(NULL, ['created' => '1'], 'Apply');
235 $results = $this->cssSelect('.view-content .field-content');
236 $this->assertEqual(count($results), 1);
237 $this->assertEqual($results[0]->getText(), $this->nodes[3]->id());
238 $this->drupalPostForm(NULL, ['created' => '2'], 'Apply');
239 $results = $this->cssSelect('.view-content .field-content');
240 $this->assertEqual(count($results), 1);
241 $this->assertEqual($results[0]->getText(), $this->nodes[3]->id());
242 $this->drupalPostForm(NULL, ['created' => '3'], 'Apply');
243 $results = $this->cssSelect('.view-content .field-content');
244 $this->assertEqual(count($results), 1);
245 $this->assertEqual($results[0]->getText(), $this->nodes[1]->id());
247 // Change the filter to a single filter to test the schema when the operator
249 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created', [], t('Single filter'));
251 $edit['options[operator]'] = '>';
252 $edit['options[value][type]'] = 'date';
253 $edit['options[value][value]'] = format_date(350000, 'custom', 'Y-m-d H:i:s');
254 $this->drupalPostForm(NULL, $edit, t('Apply'));
255 $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
256 $this->assertConfigSchemaByName('views.view.test_filter_date_between');
258 // Test that the filter works as expected.
259 $this->drupalGet($path);
260 $results = $this->cssSelect('.view-content .field-content');
261 $this->assertEqual(count($results), 1);
262 $this->assertEqual($results[0]->getText(), $this->nodes[3]->id());
263 $this->drupalPostForm(NULL, ['created' => format_date(250000, 'custom', 'Y-m-d H:i:s')], 'Apply');
264 $results = $this->cssSelect('.view-content .field-content');
265 $this->assertEqual(count($results), 2);
266 $this->assertEqual($results[0]->getText(), $this->nodes[2]->id());
267 $this->assertEqual($results[1]->getText(), $this->nodes[3]->id());
271 * Test datetime grouped filter UI.
273 protected function _testFilterDatetimeUI() {
274 $this->drupalLogin($this->drupalCreateUser(['administer views']));
275 $this->drupalPostForm('admin/structure/views/nojs/add-handler/test_filter_date_between/default/filter', ['name[node__field_date.field_date_value]' => 'node__field_date.field_date_value'], t('Add and configure filter criteria'));
277 $this->drupalPostForm(NULL, [], t('Expose filter'));
278 $this->drupalPostForm(NULL, [], t('Grouped filters'));
281 $edit['options[group_info][group_items][1][title]'] = 'simple-offset';
282 $edit['options[group_info][group_items][1][operator]'] = '>';
283 $edit['options[group_info][group_items][1][value][type]'] = 'offset';
284 $edit['options[group_info][group_items][1][value][value]'] = '+1 hour';
285 $edit['options[group_info][group_items][2][title]'] = 'between-offset';
286 $edit['options[group_info][group_items][2][operator]'] = 'between';
287 $edit['options[group_info][group_items][2][value][type]'] = 'offset';
288 $edit['options[group_info][group_items][2][value][min]'] = '+1 hour';
289 $edit['options[group_info][group_items][2][value][max]'] = '+2 days';
290 $edit['options[group_info][group_items][3][title]'] = 'between-date';
291 $edit['options[group_info][group_items][3][operator]'] = 'between';
292 $edit['options[group_info][group_items][3][value][min]'] = format_date(150000, 'custom', 'Y-m-d H:i:s');
293 $edit['options[group_info][group_items][3][value][max]'] = format_date(250000, 'custom', 'Y-m-d H:i:s');
295 $this->drupalPostForm(NULL, $edit, t('Apply'));
297 $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
298 $this->assertConfigSchemaByName('views.view.test_filter_date_between');
302 * Tests that the exposed date filter is displayed without errors.
304 public function testExposedFilter() {
305 $this->drupalLogin($this->drupalCreateUser(['administer views']));
306 $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created', [], t('Expose filter'));
307 $this->drupalPostForm('admin/structure/views/view/test_filter_date_between/edit', [], t('Add Page'));
309 'path' => 'exposed-date-filter',
311 $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_date_between/page_1/path', $edit, t('Apply'));
313 $this->drupalPostForm(NULL, [], t('Save'));
315 $this->drupalGet('exposed-date-filter');
316 $this->assertField('created');