1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Definition of ViewsHandlerFilterDateTest.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Tests the core views_handler_filter_date handler.
|
10
|
*/
|
11
|
class ViewsHandlerFilterDateTest extends ViewsSqlTest {
|
12
|
public static function getInfo() {
|
13
|
return array(
|
14
|
'name' => 'Filter: Date',
|
15
|
'description' => 'Test the core views_handler_filter_date handler.',
|
16
|
'group' => 'Views Handlers',
|
17
|
);
|
18
|
}
|
19
|
|
20
|
/**
|
21
|
* {@inheritdoc}
|
22
|
*/
|
23
|
public function setUp(array $modules = array()) {
|
24
|
parent::setUp($modules);
|
25
|
|
26
|
// Add some basic test nodes.
|
27
|
$this->nodes = array();
|
28
|
$this->nodes[] = $this->drupalCreateNode(array('created' => 100000));
|
29
|
$this->nodes[] = $this->drupalCreateNode(array('created' => 200000));
|
30
|
$this->nodes[] = $this->drupalCreateNode(array('created' => 300000));
|
31
|
$this->nodes[] = $this->drupalCreateNode(array('created' => time() + 86400));
|
32
|
|
33
|
$this->map = array(
|
34
|
'nid' => 'nid',
|
35
|
);
|
36
|
$this->enableViewsUi();
|
37
|
}
|
38
|
|
39
|
/**
|
40
|
* Test the general offset functionality.
|
41
|
*/
|
42
|
function testOffset() {
|
43
|
$view = $this->views_test_offset();
|
44
|
// Test offset for simple operator.
|
45
|
$view->set_display('default');
|
46
|
$view->init_handlers();
|
47
|
$view->filter['created']->operator = '>';
|
48
|
$view->filter['created']->value['type'] = 'offset';
|
49
|
$view->filter['created']->value['value'] = '+1 hour';
|
50
|
$view->execute_display('default');
|
51
|
$expected_result = array(
|
52
|
array('nid' => $this->nodes[3]->nid),
|
53
|
);
|
54
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
55
|
$view->destroy();
|
56
|
|
57
|
// Test "first day of" type of relative dates for simple operator.
|
58
|
$view->set_display('default');
|
59
|
$view->init_handlers();
|
60
|
$view->filter['created']->operator = '<';
|
61
|
$view->filter['created']->value['type'] = 'offset';
|
62
|
$view->filter['created']->value['value'] = 'last day of January 1970';
|
63
|
$view->execute_display('default');
|
64
|
$expected_result = array(
|
65
|
array('nid' => $this->nodes[0]->nid),
|
66
|
array('nid' => $this->nodes[1]->nid),
|
67
|
array('nid' => $this->nodes[2]->nid),
|
68
|
);
|
69
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
70
|
$view->destroy();
|
71
|
|
72
|
// Test offset for between operator.
|
73
|
$view->set_display('default');
|
74
|
$view->init_handlers();
|
75
|
$view->filter['created']->operator = 'between';
|
76
|
$view->filter['created']->value['type'] = 'offset';
|
77
|
$view->filter['created']->value['max'] = '+2 days';
|
78
|
$view->filter['created']->value['min'] = '+1 hour';
|
79
|
$view->execute_display('default');
|
80
|
$expected_result = array(
|
81
|
array('nid' => $this->nodes[3]->nid),
|
82
|
);
|
83
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
84
|
$view->destroy();
|
85
|
|
86
|
// Test "first day of" type of relative dates for between operator.
|
87
|
$view->set_display('default');
|
88
|
$view->init_handlers();
|
89
|
$view->filter['created']->operator = 'between';
|
90
|
$view->filter['created']->value['type'] = 'offset';
|
91
|
$view->filter['created']->value['max'] = 'last day of January 1970';
|
92
|
$view->filter['created']->value['min'] = 'first day of January 1970';
|
93
|
$view->execute_display('default');
|
94
|
$expected_result = array(
|
95
|
array('nid' => $this->nodes[0]->nid),
|
96
|
array('nid' => $this->nodes[1]->nid),
|
97
|
array('nid' => $this->nodes[2]->nid),
|
98
|
);
|
99
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
100
|
$view->destroy();
|
101
|
}
|
102
|
|
103
|
|
104
|
/**
|
105
|
* Tests the filter operator between/not between.
|
106
|
*/
|
107
|
function testBetween() {
|
108
|
// Test between with min and max.
|
109
|
$view = $this->views_test_between();
|
110
|
$view->set_display('default');
|
111
|
$view->init_handlers();
|
112
|
$view->filter['created']->operator = 'between';
|
113
|
$view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
|
114
|
$view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
|
115
|
$view->execute_display('default');
|
116
|
$expected_result = array(
|
117
|
array('nid' => $this->nodes[1]->nid),
|
118
|
);
|
119
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
120
|
$view->destroy();
|
121
|
|
122
|
// Test between with just max.
|
123
|
$view = $this->views_test_between();
|
124
|
$view->set_display('default');
|
125
|
$view->init_handlers();
|
126
|
$view->filter['created']->operator = 'between';
|
127
|
$view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
|
128
|
$view->execute_display('default');
|
129
|
$expected_result = array(
|
130
|
array('nid' => $this->nodes[0]->nid),
|
131
|
array('nid' => $this->nodes[1]->nid),
|
132
|
);
|
133
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
134
|
$view->destroy();
|
135
|
|
136
|
// Test not between with min and max.
|
137
|
$view = $this->views_test_between();
|
138
|
$view->set_display('default');
|
139
|
$view->init_handlers();
|
140
|
$view->filter['created']->operator = 'not between';
|
141
|
$view->filter['created']->value['min'] = format_date(150000, 'custom', 'Y-m-d H:s');
|
142
|
$view->filter['created']->value['max'] = format_date(250000, 'custom', 'Y-m-d H:s');
|
143
|
$view->execute_display('default');
|
144
|
$expected_result = array(
|
145
|
array('nid' => $this->nodes[0]->nid),
|
146
|
array('nid' => $this->nodes[2]->nid),
|
147
|
array('nid' => $this->nodes[3]->nid),
|
148
|
);
|
149
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
150
|
$view->destroy();
|
151
|
|
152
|
// Test not between with just max.
|
153
|
$view = $this->views_test_between();
|
154
|
$view->set_display('default');
|
155
|
$view->init_handlers();
|
156
|
$view->filter['created']->operator = 'not between';
|
157
|
$view->filter['created']->value['max'] = format_date(150000, 'custom', 'Y-m-d H:s');
|
158
|
$view->execute_display('default');
|
159
|
$expected_result = array(
|
160
|
array('nid' => $this->nodes[1]->nid),
|
161
|
array('nid' => $this->nodes[2]->nid),
|
162
|
array('nid' => $this->nodes[3]->nid),
|
163
|
);
|
164
|
$this->assertIdenticalResultset($view, $expected_result, $this->map);
|
165
|
$view->destroy();
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Make sure the validation callbacks works.
|
170
|
*/
|
171
|
function testUiValidation() {
|
172
|
$view = $this->views_test_between();
|
173
|
$view->save();
|
174
|
|
175
|
$admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
|
176
|
$this->drupalLogin($admin_user);
|
177
|
menu_rebuild();
|
178
|
$this->drupalGet('admin/structure/views/view/test_filter_date_between/edit');
|
179
|
$this->drupalGet('admin/structure/views/nojs/config-item/test_filter_date_between/default/filter/created');
|
180
|
|
181
|
$edit = array();
|
182
|
// Generate a definitive wrong value, which should be checked by validation.
|
183
|
$edit['options[value][value]'] = $this->randomString() . '-------';
|
184
|
$this->drupalPost(NULL, $edit, t('Apply'));
|
185
|
$this->assertText(t('Invalid date format.'), 'Make sure that validation is runned and the invalidate date format is identified.');
|
186
|
}
|
187
|
|
188
|
function views_test_between() {
|
189
|
$view = new view();
|
190
|
$view->name = 'test_filter_date_between';
|
191
|
$view->description = '';
|
192
|
$view->tag = '';
|
193
|
$view->base_table = 'node';
|
194
|
$view->human_name = '';
|
195
|
$view->core = 0;
|
196
|
$view->api_version = '3.0';
|
197
|
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
|
198
|
|
199
|
/* Display: Master */
|
200
|
$handler = $view->new_display('default', 'Master', 'default');
|
201
|
$handler->display->display_options['access']['type'] = 'none';
|
202
|
$handler->display->display_options['cache']['type'] = 'none';
|
203
|
$handler->display->display_options['query']['type'] = 'views_query';
|
204
|
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
|
205
|
$handler->display->display_options['exposed_form']['type'] = 'basic';
|
206
|
$handler->display->display_options['pager']['type'] = 'full';
|
207
|
$handler->display->display_options['style_plugin'] = 'default';
|
208
|
$handler->display->display_options['row_plugin'] = 'fields';
|
209
|
/* Field: Content: Nid */
|
210
|
$handler->display->display_options['fields']['nid']['id'] = 'nid';
|
211
|
$handler->display->display_options['fields']['nid']['table'] = 'node';
|
212
|
$handler->display->display_options['fields']['nid']['field'] = 'nid';
|
213
|
/* Filter criterion: Content: Post date */
|
214
|
$handler->display->display_options['filters']['created']['id'] = 'created';
|
215
|
$handler->display->display_options['filters']['created']['table'] = 'node';
|
216
|
$handler->display->display_options['filters']['created']['field'] = 'created';
|
217
|
|
218
|
return $view;
|
219
|
}
|
220
|
|
221
|
function views_test_offset() {
|
222
|
$view = $this->views_test_between();
|
223
|
return $view;
|
224
|
}
|
225
|
|
226
|
}
|