1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests date filter handler in Views.
|
6
|
*/
|
7
|
|
8
|
class DateViewsFilterTestCase extends DateFieldTestBase {
|
9
|
|
10
|
/**
|
11
|
* {@inheritdoc}
|
12
|
*/
|
13
|
public static function getInfo() {
|
14
|
return array(
|
15
|
'name' => 'Date Views - Filter Test',
|
16
|
'description' => 'Tests date filter handler in Views, including Date Popup fields.',
|
17
|
'group' => 'Date Views',
|
18
|
);
|
19
|
}
|
20
|
|
21
|
/**
|
22
|
* {@inheritdoc}
|
23
|
*/
|
24
|
public function setUp(array $modules = array()) {
|
25
|
$modules[] = 'views_ui';
|
26
|
$modules[] = 'date_views';
|
27
|
$modules[] = 'date_popup';
|
28
|
parent::setUp($modules);
|
29
|
|
30
|
// Initialize values.
|
31
|
$this->initialize();
|
32
|
|
33
|
// Reset/rebuild all data structures after enabling the modules.
|
34
|
$this->resetAll();
|
35
|
|
36
|
// Create a date field.
|
37
|
$this->createDateField();
|
38
|
|
39
|
// Create sample nodes.
|
40
|
$this->createDateContent();
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Initialize test values.
|
45
|
*/
|
46
|
public function initialize() {
|
47
|
$this->config_url = 'admin/structure/views/nojs/config-item/test_date_filters/default/filter';
|
48
|
$this->extra_url = 'admin/structure/views/nojs/config-item-extra/test_date_filters/default/filter';
|
49
|
|
50
|
// This is used in views configuration and on the view page.
|
51
|
variable_set('date_format_short', 'Y-m-d H:i');
|
52
|
|
53
|
if ($this->privileged_user) {
|
54
|
// Add required permissions.
|
55
|
$roles = $this->privileged_user->roles;
|
56
|
unset($roles[2]);
|
57
|
$rid = key($roles);
|
58
|
$permissions = array('administer views'/*, 'administer site configuration'*/);
|
59
|
user_role_grant_permissions($rid, $permissions);
|
60
|
}
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Adds a date field to a node bundle.
|
65
|
*/
|
66
|
public function createDateField($values = array()) {
|
67
|
// Create a date field.
|
68
|
$values = array(
|
69
|
'label' => 'Sample date',
|
70
|
'widget_type' => 'date_select',
|
71
|
'field_name' => 'sample_date',
|
72
|
'field_type' => 'datetime',
|
73
|
'input_format' => 'm/d/Y - H:i:s',
|
74
|
'granularity' => array('year', 'month', 'day'),
|
75
|
);
|
76
|
parent::createDateField($values);
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* Saves content with a date field.
|
81
|
*/
|
82
|
public function createDateContent() {
|
83
|
$years = range(date('Y') - 3, date('Y') + 3);
|
84
|
foreach ($years as $year) {
|
85
|
for($month = 1; $month < 13; $month++) {
|
86
|
$node = array(
|
87
|
'type' => 'story',
|
88
|
'title' => "Story from $year-$month-01",
|
89
|
'uid' => '1',
|
90
|
);
|
91
|
$node['sample_date']['und'][0]['value'] = "$year-$month-01";
|
92
|
node_save((object) $node);
|
93
|
}
|
94
|
}
|
95
|
}
|
96
|
|
97
|
/**
|
98
|
* Creates a view with a date filter for each date widget type.
|
99
|
*/
|
100
|
public function createDateFilterView() {
|
101
|
if ($view = views_get_view('test_date_filters')) {
|
102
|
// Delete view, then recreate it in a known state.
|
103
|
views_delete_view($view);
|
104
|
}
|
105
|
|
106
|
// Create the view.
|
107
|
$view = new view();
|
108
|
$view->name = 'test_date_filters';
|
109
|
$view->description = '';
|
110
|
$view->tag = 'default';
|
111
|
$view->base_table = 'node';
|
112
|
$view->human_name = 'Test date filters';
|
113
|
$view->core = 7;
|
114
|
$view->api_version = '3.0';
|
115
|
$view->disabled = FALSE;
|
116
|
|
117
|
/* Display: Master */
|
118
|
$handler = $view->new_display('default', 'Master', 'default');
|
119
|
$handler->display->display_options['title'] = 'Date filters';
|
120
|
$handler->display->display_options['use_more_always'] = FALSE;
|
121
|
$handler->display->display_options['access']['type'] = 'none';
|
122
|
$handler->display->display_options['cache']['type'] = 'none';
|
123
|
$handler->display->display_options['query']['type'] = 'views_query';
|
124
|
$handler->display->display_options['exposed_form']['type'] = 'basic';
|
125
|
$handler->display->display_options['exposed_form']['options']['reset_button'] = TRUE;
|
126
|
$handler->display->display_options['pager']['type'] = 'full';
|
127
|
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
|
128
|
$handler->display->display_options['style_plugin'] = 'table';
|
129
|
$handler->display->display_options['style_options']['columns'] = array(
|
130
|
'nid' => 'nid',
|
131
|
'title' => 'title',
|
132
|
'sample_date' => 'sample_date',
|
133
|
);
|
134
|
$handler->display->display_options['style_options']['default'] = '-1';
|
135
|
$handler->display->display_options['style_options']['info'] = array(
|
136
|
'nid' => array(
|
137
|
'sortable' => 0,
|
138
|
'default_sort_order' => 'asc',
|
139
|
'align' => '',
|
140
|
'separator' => '',
|
141
|
'empty_column' => 0,
|
142
|
),
|
143
|
'title' => array(
|
144
|
'sortable' => 0,
|
145
|
'default_sort_order' => 'asc',
|
146
|
'align' => '',
|
147
|
'separator' => '',
|
148
|
'empty_column' => 0,
|
149
|
),
|
150
|
'sample_date' => array(
|
151
|
'sortable' => 1,
|
152
|
'default_sort_order' => 'asc',
|
153
|
'align' => '',
|
154
|
'separator' => '',
|
155
|
'empty_column' => 0,
|
156
|
),
|
157
|
);
|
158
|
/* Field: Content: Nid */
|
159
|
$handler->display->display_options['fields']['nid']['id'] = 'nid';
|
160
|
$handler->display->display_options['fields']['nid']['table'] = 'node';
|
161
|
$handler->display->display_options['fields']['nid']['field'] = 'nid';
|
162
|
/* Field: Content: Title */
|
163
|
$handler->display->display_options['fields']['title']['id'] = 'title';
|
164
|
$handler->display->display_options['fields']['title']['table'] = 'node';
|
165
|
$handler->display->display_options['fields']['title']['field'] = 'title';
|
166
|
$handler->display->display_options['fields']['title']['label'] = '';
|
167
|
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
|
168
|
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
|
169
|
/* Field: Content: Sample date */
|
170
|
$handler->display->display_options['fields']['sample_date']['id'] = 'sample_date';
|
171
|
$handler->display->display_options['fields']['sample_date']['table'] = 'field_data_sample_date';
|
172
|
$handler->display->display_options['fields']['sample_date']['field'] = 'sample_date';
|
173
|
$handler->display->display_options['fields']['sample_date']['settings'] = array(
|
174
|
'format_type' => 'short',
|
175
|
'custom_date_format' => '',
|
176
|
'fromto' => 'both',
|
177
|
'multiple_number' => '',
|
178
|
'multiple_from' => '',
|
179
|
'multiple_to' => '',
|
180
|
'show_remaining_days' => 0,
|
181
|
'show_repeat_rule' => 'show',
|
182
|
);
|
183
|
/* Filter criterion: Content: Type */
|
184
|
$handler->display->display_options['filters']['type']['id'] = 'type';
|
185
|
$handler->display->display_options['filters']['type']['table'] = 'node';
|
186
|
$handler->display->display_options['filters']['type']['field'] = 'type';
|
187
|
$handler->display->display_options['filters']['type']['value'] = array(
|
188
|
'story' => 'story',
|
189
|
);
|
190
|
$handler->display->display_options['filters']['type']['group'] = 1;
|
191
|
/* Filter criterion: Content: Sample date (sample_date) */
|
192
|
$handler->display->display_options['filters']['sample_date_select']['id'] = 'sample_date_select';
|
193
|
$handler->display->display_options['filters']['sample_date_select']['table'] = 'field_data_sample_date';
|
194
|
$handler->display->display_options['filters']['sample_date_select']['field'] = 'sample_date_value';
|
195
|
$handler->display->display_options['filters']['sample_date_select']['group'] = 1;
|
196
|
$handler->display->display_options['filters']['sample_date_select']['exposed'] = TRUE;
|
197
|
$handler->display->display_options['filters']['sample_date_select']['expose']['operator_id'] = 'sample_date_select_op';
|
198
|
$handler->display->display_options['filters']['sample_date_select']['expose']['label'] = 'Select filter';
|
199
|
$handler->display->display_options['filters']['sample_date_select']['expose']['operator'] = 'sample_date_select_op';
|
200
|
$handler->display->display_options['filters']['sample_date_select']['expose']['identifier'] = 'sample_date_select';
|
201
|
$handler->display->display_options['filters']['sample_date_select']['expose']['remember_roles'] = array(
|
202
|
2 => 0,
|
203
|
1 => 0,
|
204
|
);
|
205
|
$handler->display->display_options['filters']['sample_date_select']['granularity'] = 'month';
|
206
|
$handler->display->display_options['filters']['sample_date_select']['year_range'] = '-2:+2';
|
207
|
/* Filter criterion: Content: Sample date (sample_date) */
|
208
|
$handler->display->display_options['filters']['sample_date_text']['id'] = 'sample_date_text';
|
209
|
$handler->display->display_options['filters']['sample_date_text']['table'] = 'field_data_sample_date';
|
210
|
$handler->display->display_options['filters']['sample_date_text']['field'] = 'sample_date_value';
|
211
|
$handler->display->display_options['filters']['sample_date_text']['group'] = 1;
|
212
|
$handler->display->display_options['filters']['sample_date_text']['exposed'] = TRUE;
|
213
|
$handler->display->display_options['filters']['sample_date_text']['expose']['operator_id'] = 'sample_date_text_op';
|
214
|
$handler->display->display_options['filters']['sample_date_text']['expose']['label'] = 'Text filter';
|
215
|
$handler->display->display_options['filters']['sample_date_text']['expose']['operator'] = 'sample_date_text_op';
|
216
|
$handler->display->display_options['filters']['sample_date_text']['expose']['identifier'] = 'sample_date_text';
|
217
|
$handler->display->display_options['filters']['sample_date_text']['expose']['remember_roles'] = array(
|
218
|
2 => 0,
|
219
|
1 => 0,
|
220
|
);
|
221
|
$handler->display->display_options['filters']['sample_date_text']['granularity'] = 'month';
|
222
|
$handler->display->display_options['filters']['sample_date_text']['form_type'] = 'date_text';
|
223
|
$handler->display->display_options['filters']['sample_date_text']['year_range'] = '-2:+2';
|
224
|
/* Filter criterion: Content: Sample date (sample_date) */
|
225
|
$handler->display->display_options['filters']['sample_date_popup']['id'] = 'sample_date_popup';
|
226
|
$handler->display->display_options['filters']['sample_date_popup']['table'] = 'field_data_sample_date';
|
227
|
$handler->display->display_options['filters']['sample_date_popup']['field'] = 'sample_date_value';
|
228
|
$handler->display->display_options['filters']['sample_date_popup']['group'] = 1;
|
229
|
$handler->display->display_options['filters']['sample_date_popup']['exposed'] = TRUE;
|
230
|
$handler->display->display_options['filters']['sample_date_popup']['expose']['operator_id'] = 'sample_date_popup_op';
|
231
|
$handler->display->display_options['filters']['sample_date_popup']['expose']['label'] = 'Popup filter';
|
232
|
$handler->display->display_options['filters']['sample_date_popup']['expose']['operator'] = 'sample_date_popup_op';
|
233
|
$handler->display->display_options['filters']['sample_date_popup']['expose']['identifier'] = 'sample_date_popup';
|
234
|
$handler->display->display_options['filters']['sample_date_popup']['expose']['remember_roles'] = array(
|
235
|
2 => 0,
|
236
|
1 => 0,
|
237
|
);
|
238
|
$handler->display->display_options['filters']['sample_date_popup']['granularity'] = 'month';
|
239
|
$handler->display->display_options['filters']['sample_date_popup']['form_type'] = 'date_popup';
|
240
|
$handler->display->display_options['filters']['sample_date_popup']['year_range'] = '-2:+2';
|
241
|
|
242
|
/* Display: Page */
|
243
|
$handler = $view->new_display('page', 'Page', 'page_samples');
|
244
|
$handler->display->display_options['path'] = 'test-date-filter';
|
245
|
$handler->display->display_options['menu']['type'] = 'normal';
|
246
|
$handler->display->display_options['menu']['title'] = 'Date filters';
|
247
|
$handler->display->display_options['menu']['weight'] = '0';
|
248
|
$handler->display->display_options['menu']['context'] = 0;
|
249
|
$handler->display->display_options['menu']['context_only_inline'] = 0;
|
250
|
|
251
|
$view->save();
|
252
|
}
|
253
|
|
254
|
/**
|
255
|
* Test date filters.
|
256
|
*/
|
257
|
public function testDateFilters() {
|
258
|
if (!$this->loggedInUser) {
|
259
|
return;
|
260
|
}
|
261
|
|
262
|
try {
|
263
|
$this->checkSelectFilter();
|
264
|
$this->checkTextFilter('sample_date_text');
|
265
|
$this->checkTextFilter('sample_date_popup');
|
266
|
|
267
|
// @todo Seems to be a bug in popup code that reuses the last filter value
|
268
|
// so that query condition is invalid.
|
269
|
// (DATE_FORMAT(field_data_sample_date.sample_date_value, '%Y') = '2018-06')
|
270
|
$this->validateFilters();
|
271
|
}
|
272
|
catch(Exception $e) {
|
273
|
}
|
274
|
|
275
|
if ($this->loggedInUser) {
|
276
|
$this->drupalLogout();
|
277
|
}
|
278
|
}
|
279
|
|
280
|
/**
|
281
|
* Test date filter using select widget.
|
282
|
*/
|
283
|
public function checkSelectFilter() {
|
284
|
// Restore the view.
|
285
|
$this->createDateFilterView();
|
286
|
|
287
|
$year = (string) (date('Y') - 1);
|
288
|
$month = '06';
|
289
|
$filter = 'sample_date_select';
|
290
|
|
291
|
// Set the filter value.
|
292
|
$edit = array(
|
293
|
'options[value][value_group][value][year]' => $year,
|
294
|
'options[value][value_group][value][month]' => substr($month, -1),
|
295
|
);
|
296
|
$this->setFilter($filter, $edit);
|
297
|
|
298
|
// Inspect the table.
|
299
|
$this->checkTable(1, $year);
|
300
|
|
301
|
// Inspect the filter value.
|
302
|
$this->checkSelect($filter, 'year', $year);
|
303
|
$this->checkSelect($filter, 'month', substr($month, -1));
|
304
|
|
305
|
// Change granularity.
|
306
|
$this->setGranularity($filter, 'year');
|
307
|
|
308
|
// Inspect the table.
|
309
|
$this->checkTable(10, $year);
|
310
|
|
311
|
// Inspect the filter value.
|
312
|
$this->checkSelect($filter, 'year', $year);
|
313
|
}
|
314
|
|
315
|
/**
|
316
|
* Test date filter using text or popup widget.
|
317
|
*
|
318
|
* @param string $filter
|
319
|
* Filter name.
|
320
|
*/
|
321
|
public function checkTextFilter($filter) {
|
322
|
// Restore the view.
|
323
|
$this->createDateFilterView();
|
324
|
|
325
|
$year = (string) (date('Y') - 1);
|
326
|
$month = '06';
|
327
|
|
328
|
// Set the filter value.
|
329
|
$edit = array(
|
330
|
'options[value][value_group][value][date]' => "$year-$month",
|
331
|
);
|
332
|
$this->setFilter($filter, $edit);
|
333
|
|
334
|
// Inspect the table.
|
335
|
$this->checkTable(1, $year);
|
336
|
|
337
|
// Inspect the filter value.
|
338
|
$this->checkTextField($filter, "$year-$month");
|
339
|
|
340
|
// Change granularity.
|
341
|
$this->setGranularity($filter, 'year');
|
342
|
|
343
|
// Set the filter value.
|
344
|
// This is not necessary with the text widget but is with the popup widget.
|
345
|
// The popup element retains the first value and the views query uses it.
|
346
|
$edit = array(
|
347
|
'options[value][value_group][value][date]' => "$year",
|
348
|
);
|
349
|
$this->setFilter($filter, $edit);
|
350
|
|
351
|
// Inspect the table.
|
352
|
$this->checkTable(10, $year);
|
353
|
|
354
|
// Inspect the filter value.
|
355
|
$this->checkTextField($filter, $year);
|
356
|
}
|
357
|
|
358
|
/**
|
359
|
* Test date filter validation using all widgets.
|
360
|
*/
|
361
|
public function validateFilters() {
|
362
|
// Restore the view.
|
363
|
$this->createDateFilterView();
|
364
|
|
365
|
$year = (string) (date('Y') - 1);
|
366
|
$month = '06';
|
367
|
|
368
|
// Set the filter values.
|
369
|
$edit = array(
|
370
|
'sample_date_select[value][year]' => $year,
|
371
|
'sample_date_text[value][date]' => $year,
|
372
|
'sample_date_popup[value][date]' => $year,
|
373
|
);
|
374
|
|
375
|
// View the page.
|
376
|
// The views exposed form uses a GET method not POST.
|
377
|
$options = array('query' => $edit);
|
378
|
$this->drupalGet('test-date-filter', $options);
|
379
|
|
380
|
$messages = array(
|
381
|
'The value input for field Select filter is invalid',
|
382
|
'The month is missing',
|
383
|
'The value input for field Text filter is invalid',
|
384
|
"The value $year does not match the expected format",
|
385
|
'The value input for field Popup filter is invalid',
|
386
|
"The value $year does not match the expected format",
|
387
|
);
|
388
|
foreach ($messages as $message) {
|
389
|
$this->assertText($message, "Found error message: {$message}");
|
390
|
}
|
391
|
}
|
392
|
|
393
|
/**
|
394
|
* Set value of a date filter.
|
395
|
*
|
396
|
* @param string $filter
|
397
|
* Filter name.
|
398
|
* @param string $edit
|
399
|
* The associative array of form input values.
|
400
|
*/
|
401
|
public function setFilter($filter, $edit) {
|
402
|
// Load view configuration dialog.
|
403
|
$this->drupalGet($this->config_url . '/' . $filter);
|
404
|
$this->drupalPost(NULL, $edit, t('Apply'));
|
405
|
|
406
|
// Save the view.
|
407
|
$edit = array();
|
408
|
$this->drupalPost(NULL, $edit, t('Save'));
|
409
|
}
|
410
|
|
411
|
/**
|
412
|
* Set granularity of a date filter.
|
413
|
*
|
414
|
* @param string $filter
|
415
|
* Filter name.
|
416
|
* @param string $granularity
|
417
|
* The granularity value.
|
418
|
*/
|
419
|
public function setGranularity($filter, $granularity) {
|
420
|
// Load view configuration dialog.
|
421
|
$this->drupalGet($this->extra_url . '/' . $filter);
|
422
|
$edit = array(
|
423
|
'options[granularity]' => $granularity,
|
424
|
);
|
425
|
$this->drupalPost(NULL, $edit, t('Apply'));
|
426
|
|
427
|
// Save the view.
|
428
|
$edit = array();
|
429
|
$this->drupalPost(NULL, $edit, t('Save'));
|
430
|
}
|
431
|
|
432
|
/**
|
433
|
* Inspect the select field[s] of a date filter.
|
434
|
*
|
435
|
* @param string $filter
|
436
|
* Filter name.
|
437
|
* @param string $part
|
438
|
* Date part.
|
439
|
* @param string $expected
|
440
|
* The expected field value.
|
441
|
*/
|
442
|
public function checkSelect($filter, $part, $expected) {
|
443
|
// Inspect the filter value.
|
444
|
$xpath = '//select[@name="' . $filter . '[value][' . $part . ']"]';
|
445
|
$elements = $this->xpath($xpath);
|
446
|
if (is_array($elements)) {
|
447
|
$select = $elements[0];
|
448
|
$value = $this->getSelectedItem($select);
|
449
|
$this->assertEqual($value, $expected, "Filter contains $expected");
|
450
|
}
|
451
|
else {
|
452
|
$this->assertTrue(FALSE, "Filter does NOT contain $expected");
|
453
|
}
|
454
|
}
|
455
|
|
456
|
/**
|
457
|
* Inspect the text field of a date filter.
|
458
|
*
|
459
|
* @param string $filter
|
460
|
* Filter name.
|
461
|
* @param string $expected
|
462
|
* The expected field value.
|
463
|
*/
|
464
|
public function checkTextField($filter, $expected) {
|
465
|
// Inspect the filter value.
|
466
|
$xpath = '//input[@name="' . $filter . '[value][date]"]';
|
467
|
$elements = $this->xpath($xpath);
|
468
|
if (is_array($elements)) {
|
469
|
$value = (array) $elements[0];
|
470
|
$value = $value['@attributes']['value'];
|
471
|
$this->assertEqual($value, $expected, "Filter contains $expected");
|
472
|
}
|
473
|
else {
|
474
|
$this->assertTrue(FALSE, "Filter does NOT contain $expected");
|
475
|
}
|
476
|
}
|
477
|
|
478
|
/**
|
479
|
* Inspect contents of view output table.
|
480
|
*
|
481
|
* @param int $count
|
482
|
* Expected number of rows in table.
|
483
|
* @param string $year
|
484
|
* Expected year value in cells of table.
|
485
|
*/
|
486
|
public function checkTable($count, $year) {
|
487
|
// View the page.
|
488
|
$this->drupalGet('test-date-filter');
|
489
|
|
490
|
// Count rows in table.
|
491
|
$xpath = '//table[contains(@class, "views-table")]//tbody//tr';
|
492
|
$elements = $this->xpath($xpath);
|
493
|
if (is_array($elements)) {
|
494
|
$this->assertEqual(count($elements), $count, "Table contains $count rows for $year");
|
495
|
}
|
496
|
else {
|
497
|
$this->assertTrue(FALSE, "Table is empty for $year");
|
498
|
}
|
499
|
|
500
|
// Check values in table.
|
501
|
// views-field views-field-sample-date
|
502
|
$xpath = '//td[contains(@class, "views-field-sample-date")]//span';
|
503
|
$elements = $this->xpath($xpath);
|
504
|
if (is_array($elements)) {
|
505
|
foreach ($elements as $key => $element) {
|
506
|
$row = $key + 1;
|
507
|
$this->assertTrue(strpos((string) $element, $year) !== FALSE, "Year in row $row is $year");
|
508
|
}
|
509
|
}
|
510
|
else {
|
511
|
$this->assertTrue(FALSE, "Table rows are empty for $year");
|
512
|
}
|
513
|
}
|
514
|
|
515
|
}
|