Commit | Line | Data |
---|---|---|
2fe91f9d | 1 | <?php |
0eea664b | 2 | |
2fe91f9d | 3 | /** |
4 | * Include dataProvider for tests | |
acb109b7 | 5 | * @group headless |
2fe91f9d | 6 | */ |
7 | class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase { | |
8 | ||
9 | /** | |
10 | * Restore database to empty state. | |
11 | * | |
12 | * Note that rollback won't remove custom tables. | |
13 | * | |
14 | * @throws \Exception | |
15 | */ | |
16 | public function tearDown() { | |
17 | $tablesToTruncate = array( | |
18 | 'civicrm_contact', | |
19 | ); | |
20 | $this->quickCleanup($tablesToTruncate, TRUE); | |
21 | parent::tearDown(); | |
22 | } | |
23 | ||
24 | /** | |
25 | * Test filtering by relative custom data dates. | |
26 | */ | |
27 | public function testSearchCustomDataDateRelative() { | |
28 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); | |
29 | $dateCustomField = $this->customFieldCreate(array( | |
30 | 'custom_group_id' => $ids['custom_group_id'], | |
31 | 'label' => 'date field', | |
32 | 'data_type' => 'Date', | |
33 | 'html_type' => 'Select Date', | |
34 | 'default_value' => NULL, | |
35 | )); | |
36 | $dateCustomFieldName = 'custom_' . $dateCustomField['id']; | |
37 | $formValues = array( | |
38 | $dateCustomFieldName . '_relative' => 'this.year', | |
39 | $dateCustomFieldName . '_from' => '', | |
40 | $dateCustomFieldName . '_to' => '', | |
41 | ); | |
42 | // Assigning the relevant form value to be within a custom key is normally done in | |
43 | // build field params. It would be better if it were all done in convertFormValues | |
44 | // but for now we just imitate it. | |
45 | $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); | |
46 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
47 | $queryObj->Query(); | |
48 | $this->assertEquals( | |
49 | 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "' . date('Y') . '0101000000" AND "' . date('Y') . '1231235959"', | |
50 | $queryObj->_where[0][0] | |
51 | ); | |
52 | $this->assertEquals($queryObj->_qill[0][0], "date field BETWEEN 'January 1st, " . date('Y') . " 12:00 AM AND December 31st, " . date('Y') . " 11:59 PM'"); | |
acd6c6ab | 53 | $this->assertEquals([ |
54 | 'id' => $dateCustomField['id'], | |
55 | 'label' => 'date field', | |
1f61a7b1 | 56 | 'extends' => 'Contact', |
acd6c6ab | 57 | 'data_type' => 'Date', |
58 | 'html_type' => 'Select Date', | |
59 | 'is_search_range' => '0', | |
60 | 'column_name' => 'date_field_' . $dateCustomField['id'], | |
61 | 'table_name' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'], | |
62 | 'option_group_id' => NULL, | |
1f61a7b1 | 63 | 'groupTitle' => 'testSearchCustomDataDateRelative', |
64 | 'default_value' => NULL, | |
65 | 'text_length' => NULL, | |
66 | 'options_per_line' => NULL, | |
67 | 'custom_group_id' => '1', | |
68 | 'extends_entity_column_value' => NULL, | |
69 | 'extends_entity_column_id' => NULL, | |
70 | 'is_view' => '0', | |
71 | 'is_multiple' => '0', | |
72 | 'date_format' => 'mm/dd/yy', | |
73 | 'time_format' => NULL, | |
74 | 'is_required' => '0', | |
75 | 'extends_table' => 'civicrm_contact', | |
76 | 'search_table' => 'contact_a', | |
cef2e96c | 77 | 'headerPattern' => '//', |
78 | 'title' => 'date field', | |
79 | 'custom_field_id' => $dateCustomField['id'], | |
80 | 'name' => 'custom_' . $dateCustomField['id'], | |
81 | 'type' => 4, | |
82 | 'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'], | |
acd6c6ab | 83 | ], $queryObj->getFields()[$dateCustomField['id']]); |
84 | ||
2fe91f9d | 85 | } |
86 | ||
87 | /** | |
88 | * Test filtering by relative custom data dates. | |
89 | */ | |
90 | public function testSearchCustomDataDateFromTo() { | |
91 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); | |
92 | $dateCustomField = $this->customFieldCreate(array( | |
93 | 'custom_group_id' => $ids['custom_group_id'], | |
94 | 'label' => 'date field', | |
95 | 'data_type' => 'Date', | |
96 | 'html_type' => 'Select Date', | |
97 | 'default_value' => NULL, | |
98 | )); | |
99 | $dateCustomFieldName = 'custom_' . $dateCustomField['id']; | |
100 | // Assigning the relevant form value to be within a custom key is normally done in | |
101 | // build field params. It would be better if it were all done in convertFormValues | |
102 | // but for now we just imitate it. | |
103 | $formValues = array( | |
104 | $dateCustomFieldName . '_from' => '2014-06-06', | |
105 | $dateCustomFieldName . '_to' => '2015-06-06', | |
106 | ); | |
107 | ||
108 | $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); | |
109 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
110 | $queryObj->Query(); | |
111 | $this->assertEquals( | |
112 | 'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"', | |
113 | $queryObj->_where[0][0] | |
114 | ); | |
115 | $this->assertEquals($queryObj->_qill[0][0], "date field BETWEEN 'June 6th, 2014 12:00 AM AND June 6th, 2015 11:59 PM'"); | |
c65ec456 JP |
116 | |
117 | //CRM-17236 - Test custom date is correctly displayed without time. | |
118 | $formattedValue = CRM_Core_BAO_CustomField::displayValue(date('Ymdhms'), $dateCustomField['id']); | |
119 | $this->assertEquals(date('m/d/Y'), $formattedValue); | |
2fe91f9d | 120 | } |
121 | ||
122 | /** | |
123 | * Test filtering by relative custom data. | |
124 | */ | |
125 | public function testSearchCustomDataFromTo() { | |
126 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); | |
127 | $datas = array( | |
128 | 'Int' => 2, | |
129 | 'Float' => 12.123, | |
130 | 'Money' => 91.21, | |
131 | ); | |
132 | foreach ($datas as $type => $data) { | |
133 | $customField = $this->customFieldCreate( | |
134 | array( | |
135 | 'custom_group_id' => $ids['custom_group_id'], | |
136 | 'label' => "$type field", | |
137 | 'data_type' => $type, | |
138 | 'html_type' => 'Text', | |
139 | 'default_value' => NULL, | |
140 | ) | |
141 | ); | |
142 | $customFieldName = 'custom_' . $customField['id']; | |
143 | // Assigning the relevant form value to be within a custom key is normally done in | |
144 | // build field params. It would be better if it were all done in convertFormValues | |
145 | // but for now we just imitate it. | |
146 | $from = $data - 1; | |
147 | $to = $data; | |
148 | $formValues = array( | |
149 | $customFieldName . '_from' => $from, | |
150 | $customFieldName . '_to' => $to, | |
151 | ); | |
152 | ||
153 | $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); | |
154 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
155 | $queryObj->Query(); | |
156 | $this->assertEquals( | |
157 | "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"", | |
158 | $queryObj->_where[0][0] | |
159 | ); | |
160 | $this->assertEquals($queryObj->_qill[0][0], "$type field BETWEEN $from, $to"); | |
161 | } | |
162 | } | |
163 | ||
164 | /** | |
165 | * Test filtering by relative custom data. | |
166 | */ | |
167 | public function testSearchCustomDataFromAndTo() { | |
168 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); | |
169 | $datas = array( | |
170 | 'Date' => '2015-06-06', | |
171 | 'Int' => 2, | |
172 | 'Float' => 12.123, | |
173 | 'Money' => 91.21, | |
174 | ); | |
175 | foreach ($datas as $type => $data) { | |
176 | $isDate = ($type === 'Date'); | |
177 | $customField = $this->customFieldCreate( | |
178 | array( | |
179 | 'custom_group_id' => $ids['custom_group_id'], | |
180 | 'label' => "$type field", | |
181 | 'data_type' => $type, | |
182 | 'html_type' => ($isDate) ? 'Select Date' : 'Text', | |
183 | 'default_value' => NULL, | |
184 | ) | |
185 | ); | |
186 | $customFieldName = 'custom_' . $customField['id']; | |
187 | ||
188 | $expectedValue = ($isDate) ? '"20150606235959"' : (($type == 'Money') ? $data : "\"$data\""); | |
9a4f958b | 189 | $expectedQillValue = ($isDate) ? "'June 6th, 2015 11:59 PM'" : $data; |
2fe91f9d | 190 | |
191 | // Assigning the relevant form value to be within a custom key is normally done in | |
192 | // build field params. It would be better if it were all done in convertFormValues | |
193 | // but for now we just imitate it. | |
194 | ||
195 | //Scenrio 2 : TO date filter | |
196 | $formValues = array( | |
197 | $customFieldName . '_to' => $data, | |
198 | ); | |
199 | ||
200 | $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); | |
201 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
202 | $queryObj->Query(); | |
203 | $wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164); | |
204 | ||
205 | $this->assertEquals( | |
206 | "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} <= $expectedValue", | |
207 | $queryObj->_where[0][0] | |
208 | ); | |
209 | $this->assertEquals($queryObj->_qill[0][0], | |
210 | "$type field " . $wierdStringThatMeansGreaterEquals . " $expectedQillValue" | |
211 | ); | |
212 | ||
213 | //Scenrio 2 : FROM date filter | |
214 | $formValues = array( | |
215 | $customFieldName . '_from' => $data, | |
216 | ); | |
217 | ||
218 | $params = array($customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)); | |
219 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
220 | $queryObj->Query(); | |
221 | $wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165); | |
222 | ||
223 | $expectedValue = ($isDate) ? '"20150606000000"' : $expectedValue; | |
224 | $expectedQillValue = ($isDate) ? "'June 6th, 2015 12:00 AM'" : $expectedQillValue; | |
225 | $this->assertEquals( | |
226 | "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} >= $expectedValue", | |
227 | $queryObj->_where[0][0] | |
228 | ); | |
229 | $this->assertEquals($queryObj->_qill[0][0], | |
230 | "$type field " . $wierdStringThatMeansLessThanEquals . " $expectedQillValue" | |
231 | ); | |
232 | } | |
233 | } | |
234 | ||
235 | /** | |
236 | * Test filtering by relative custom data dates. | |
237 | */ | |
238 | public function testSearchCustomDataDateEquals() { | |
239 | $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest'); | |
240 | $dateCustomField = $this->customFieldCreate(array( | |
241 | 'custom_group_id' => $ids['custom_group_id'], | |
242 | 'label' => 'date field', | |
243 | 'data_type' => 'Date', | |
244 | 'html_type' => 'Select Date', | |
245 | 'default_value' => NULL, | |
246 | )); | |
247 | $dateCustomFieldName = 'custom_' . $dateCustomField['id']; | |
248 | $this->individualCreate(array($dateCustomFieldName => "2015-01-01")); | |
249 | // Assigning the relevant form value to be within a custom key is normally done in | |
250 | // build field params. It would be better if it were all done in convertFormValues | |
251 | // but for now we just imitate it. | |
252 | $formValues = array($dateCustomFieldName => '2015-06-06'); | |
253 | $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues); | |
254 | $queryObj = new CRM_Core_BAO_CustomQuery($params); | |
255 | $queryObj->Query(); | |
256 | ||
257 | $this->assertEquals( | |
258 | "civicrm_value_testsearchcus_1.date_field_2 = '2015-06-06'", | |
259 | $queryObj->_where[0][0] | |
260 | ); | |
261 | $this->assertEquals($queryObj->_qill[0][0], "date field = 'June 6th, 2015'"); | |
262 | } | |
263 | ||
264 | } |