Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / views / tests / src / Functional / Handler / FilterDateTest.php
1 <?php
2
3 namespace Drupal\Tests\views\Functional\Handler;
4
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;
11
12 /**
13  * Tests the core Drupal\views\Plugin\views\filter\Date handler.
14  *
15  * @group views
16  */
17 class FilterDateTest extends ViewTestBase {
18   use SchemaCheckTestTrait;
19
20   /**
21    * Views used by this test.
22    *
23    * @var array
24    */
25   public static $testViews = ['test_filter_date_between'];
26
27   /**
28    * Modules to enable.
29    *
30    * @var array
31    */
32   public static $modules = ['node', 'views_ui', 'datetime'];
33
34   protected function setUp($import_test_views = TRUE) {
35     parent::setUp($import_test_views);
36
37     // Add a date field so we can test datetime handling.
38     NodeType::create([
39       'type' => 'page',
40       'name' => 'Page',
41     ])->save();
42
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',
47       'type' => 'datetime',
48       'entity_type' => 'node',
49     ]);
50     $field_storage->save();
51
52     $field = FieldConfig::create([
53       'field_name' => 'field_date',
54       'entity_type' => 'node',
55       'bundle' => 'page',
56     ]);
57     $field->save();
58
59     // Add some basic test nodes.
60     $this->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]);
65
66     $this->map = [
67       'nid' => 'nid',
68     ];
69   }
70
71   /**
72    * Runs other test methods.
73    */
74   public function testDateFilter() {
75     $this->_testOffset();
76     $this->_testBetween();
77     $this->_testUiValidation();
78     $this->_testFilterDateUI();
79     $this->_testFilterDatetimeUI();
80   }
81
82   /**
83    * Test the general offset functionality.
84    */
85   protected function _testOffset() {
86     $view = Views::getView('test_filter_date_between');
87
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');
94     $expected_result = [
95       ['nid' => $this->nodes[3]->id()],
96     ];
97     $this->assertIdenticalResultset($view, $expected_result, $this->map);
98     $view->destroy();
99
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');
107     $expected_result = [
108       ['nid' => $this->nodes[3]->id()],
109     ];
110     $this->assertIdenticalResultset($view, $expected_result, $this->map);
111   }
112
113   /**
114    * Tests the filter operator between/not between.
115    */
116   protected function _testBetween() {
117     $view = Views::getView('test_filter_date_between');
118
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');
125     $expected_result = [
126       ['nid' => $this->nodes[1]->id()],
127     ];
128     $this->assertIdenticalResultset($view, $expected_result, $this->map);
129     $view->destroy();
130
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');
136     $expected_result = [
137       ['nid' => $this->nodes[0]->id()],
138       ['nid' => $this->nodes[1]->id()],
139     ];
140     $this->assertIdenticalResultset($view, $expected_result, $this->map);
141     $view->destroy();
142
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');
148
149     $view->executeDisplay('default');
150     $expected_result = [
151       ['nid' => $this->nodes[2]->id()],
152       ['nid' => $this->nodes[3]->id()],
153     ];
154     $this->assertIdenticalResultset($view, $expected_result, $this->map);
155     $view->destroy();
156
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');
162     $expected_result = [
163       ['nid' => $this->nodes[2]->id()],
164       ['nid' => $this->nodes[3]->id()],
165     ];
166     $this->assertIdenticalResultset($view, $expected_result, $this->map);
167   }
168
169   /**
170    * Make sure the validation callbacks works.
171    */
172   protected function _testUiValidation() {
173
174     $this->drupalLogin($this->drupalCreateUser(['administer views', 'administer site configuration']));
175
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');
178
179     $edit = [];
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.');
184   }
185
186   /**
187    * Test date filter UI.
188    */
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'));
194
195     $edit = [];
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');
209
210     $this->drupalPostForm(NULL, $edit, t('Apply'));
211
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);
218       }
219     }
220
221     $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
222     $this->assertConfigSchemaByName('views.view.test_filter_date_between');
223
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'));
229
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());
246
247     // Change the filter to a single filter to test the schema when the operator
248     // is not exposed.
249     $this->drupalPostForm('admin/structure/views/nojs/handler/test_filter_date_between/default/filter/created', [], t('Single filter'));
250     $edit = [];
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');
257
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());
268   }
269
270   /**
271    * Test datetime grouped filter UI.
272    */
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'));
276
277     $this->drupalPostForm(NULL, [], t('Expose filter'));
278     $this->drupalPostForm(NULL, [], t('Grouped filters'));
279
280     $edit = [];
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');
294
295     $this->drupalPostForm(NULL, $edit, t('Apply'));
296
297     $this->drupalPostForm('admin/structure/views/view/test_filter_date_between', [], t('Save'));
298     $this->assertConfigSchemaByName('views.view.test_filter_date_between');
299   }
300
301   /**
302    * Tests that the exposed date filter is displayed without errors.
303    */
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'));
308     $edit = [
309       'path' => 'exposed-date-filter',
310     ];
311     $this->drupalPostForm('admin/structure/views/nojs/display/test_filter_date_between/page_1/path', $edit, t('Apply'));
312
313     $this->drupalPostForm(NULL, [], t('Save'));
314
315     $this->drupalGet('exposed-date-filter');
316     $this->assertField('created');
317   }
318
319 }