Commit | Line | Data |
---|---|---|
836eb043 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
836eb043 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
836eb043 | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * Include parent class definition | |
30 | */ | |
836eb043 | 31 | |
32 | /** | |
33 | * Test contact custom search functions | |
34 | * | |
35 | * @package CiviCRM | |
acb109b7 | 36 | * @group headless |
836eb043 | 37 | */ |
1d17e4fc | 38 | class CRM_Contact_SelectorTest extends CiviUnitTestCase { |
836eb043 | 39 | |
40 | public function tearDown() { | |
41 | ||
42 | } | |
39b959db | 43 | |
836eb043 | 44 | /** |
45 | * Test the query from the selector class is consistent with the dataset expectation. | |
46 | * | |
47 | * @param array $dataSet | |
48 | * The data set to be tested. Note that when adding new datasets often only form_values and expected where | |
49 | * clause will need changing. | |
50 | * | |
51 | * @dataProvider querySets | |
52 | */ | |
53 | public function testSelectorQuery($dataSet) { | |
54 | $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, array()); | |
55 | foreach ($dataSet['settings'] as $setting) { | |
56 | $this->callAPISuccess('Setting', 'create', array($setting['name'] => $setting['value'])); | |
57 | } | |
58 | $selector = new CRM_Contact_Selector( | |
59 | $dataSet['class'], | |
60 | $dataSet['form_values'], | |
61 | $params, | |
62 | $dataSet['return_properties'], | |
63 | $dataSet['action'], | |
64 | $dataSet['includeContactIds'], | |
65 | $dataSet['searchDescendentGroups'], | |
66 | $dataSet['context'] | |
67 | ); | |
68 | $queryObject = $selector->getQueryObject(); | |
69 | $sql = $queryObject->query(); | |
70 | $this->wrangleDefaultClauses($dataSet['expected_query']); | |
71 | foreach ($dataSet['expected_query'] as $index => $queryString) { | |
72 | $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index])); | |
73 | } | |
7ccccc32 | 74 | // Ensure that search builder return individual contact as per criteria |
75 | if (!empty($dataSet['context'] == 'builder')) { | |
d567ed10 | 76 | $contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']); |
fd95406d | 77 | if ('Search builder behaviour for Activity' == $dataSet['description']) { |
78 | $this->callAPISuccess('Activity', 'create', [ | |
79 | 'activity_type_id' => 'Meeting', | |
80 | 'subject' => "Test", | |
81 | 'source_contact_id' => $contactID, | |
82 | ]); | |
83 | $rows = CRM_Core_DAO::executeQuery(implode(' ', $sql))->fetchAll(); | |
84 | $this->assertEquals(1, count($rows)); | |
85 | $this->assertEquals($contactID, $rows[0]['source_contact_id']); | |
86 | } | |
87 | else { | |
88 | $this->callAPISuccess('Address', 'create', [ | |
89 | 'contact_id' => $contactID, | |
90 | 'location_type_id' => "Home", | |
91 | 'is_primary' => 1, | |
92 | 'country_id' => "IN", | |
93 | ]); | |
94 | $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, ''); | |
95 | $this->assertEquals(1, count($rows)); | |
96 | $sortChar = $selector->alphabetQuery()->fetchAll(); | |
97 | // sort name is stored in '<last_name>, <first_name>' format, as per which the first character would be B of Bond | |
98 | $this->assertEquals('B', $sortChar[0]['sort_name']); | |
99 | $this->assertEquals($contactID, key($rows)); | |
100 | } | |
7ccccc32 | 101 | } |
836eb043 | 102 | } |
103 | ||
2eb3ff3e | 104 | /** |
105 | * Test the civicrm_prevnext_cache entry if it correctly stores the search query result | |
106 | */ | |
107 | public function testPrevNextCache() { | |
108 | $contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']); | |
109 | $dataSet = array( | |
110 | 'description' => 'Normal default behaviour', | |
111 | 'class' => 'CRM_Contact_Selector', | |
112 | 'settings' => array(), | |
113 | 'form_values' => array('email' => 'mickey@mouseville.com'), | |
114 | 'params' => array(), | |
115 | 'return_properties' => NULL, | |
116 | 'context' => 'advanced', | |
117 | 'action' => CRM_Core_Action::ADVANCED, | |
118 | 'includeContactIds' => NULL, | |
119 | 'searchDescendentGroups' => FALSE, | |
120 | 'expected_query' => array( | |
121 | 0 => 'default', | |
122 | 1 => 'default', | |
123 | 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)", | |
124 | ), | |
125 | ); | |
126 | $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, array()); | |
127 | ||
128 | // create CRM_Contact_Selector instance and set desired query params | |
129 | $selector = new CRM_Contact_Selector( | |
130 | $dataSet['class'], | |
131 | $dataSet['form_values'], | |
132 | $params, | |
133 | $dataSet['return_properties'], | |
134 | $dataSet['action'], | |
135 | $dataSet['includeContactIds'], | |
136 | $dataSet['searchDescendentGroups'], | |
137 | $dataSet['context'] | |
138 | ); | |
139 | // set cache key | |
140 | $key = substr(sha1(rand()), 0, 7); | |
141 | $selector->setKey($key); | |
142 | ||
143 | // fetch row and check the result | |
1d17e4fc | 144 | $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL); |
2eb3ff3e | 145 | $this->assertEquals(1, count($rows)); |
146 | $this->assertEquals($contactID, key($rows)); | |
147 | ||
148 | // build cache key and use to it to fetch prev-next cache record | |
149 | $cacheKey = 'civicrm search ' . $key; | |
150 | $contacts = CRM_Utils_SQL_Select::from('civicrm_prevnext_cache') | |
39b959db SL |
151 | ->select(['entity_id1', 'cacheKey']) |
152 | ->where("cacheKey = @key") | |
153 | ->param('key', $cacheKey) | |
154 | ->execute() | |
155 | ->fetchAll(); | |
2eb3ff3e | 156 | $this->assertEquals(1, count($contacts)); |
157 | // check the prevNext record matches | |
158 | $expectedEntry = [ | |
2eb3ff3e | 159 | 'entity_id1' => $contactID, |
160 | 'cacheKey' => $cacheKey, | |
161 | ]; | |
162 | $this->checkArrayEquals($contacts[0], $expectedEntry); | |
163 | } | |
164 | ||
836eb043 | 165 | /** |
166 | * Data sets for testing. | |
167 | */ | |
168 | public function querySets() { | |
169 | return array( | |
170 | array( | |
171 | array( | |
172 | 'description' => 'Normal default behaviour', | |
173 | 'class' => 'CRM_Contact_Selector', | |
174 | 'settings' => array(), | |
175 | 'form_values' => array('email' => 'mickey@mouseville.com'), | |
176 | 'params' => array(), | |
177 | 'return_properties' => NULL, | |
178 | 'context' => 'advanced', | |
179 | 'action' => CRM_Core_Action::ADVANCED, | |
180 | 'includeContactIds' => NULL, | |
181 | 'searchDescendentGroups' => FALSE, | |
182 | 'expected_query' => array( | |
183 | 0 => 'default', | |
184 | 1 => 'default', | |
185 | 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)", | |
186 | ), | |
187 | ), | |
188 | ), | |
189 | array( | |
190 | array( | |
191 | 'description' => 'Normal default + user added wildcard', | |
192 | 'class' => 'CRM_Contact_Selector', | |
193 | 'settings' => array(), | |
194 | 'form_values' => array('email' => '%mickey@mouseville.com', 'sort_name' => 'Mouse'), | |
195 | 'params' => array(), | |
196 | 'return_properties' => NULL, | |
197 | 'context' => 'advanced', | |
198 | 'action' => CRM_Core_Action::ADVANCED, | |
199 | 'includeContactIds' => NULL, | |
200 | 'searchDescendentGroups' => FALSE, | |
201 | 'expected_query' => array( | |
202 | 0 => 'default', | |
203 | 1 => 'default', | |
1d17e4fc | 204 | 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE '%Mouse%' ) OR ( civicrm_email.email LIKE '%Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)", |
836eb043 | 205 | ), |
206 | ), | |
207 | ), | |
208 | array( | |
209 | array( | |
210 | 'description' => 'Site set to not pre-pend wildcard', | |
211 | 'class' => 'CRM_Contact_Selector', | |
212 | 'settings' => array(array('name' => 'includeWildCardInName', 'value' => FALSE)), | |
213 | 'form_values' => array('email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'), | |
214 | 'params' => array(), | |
215 | 'return_properties' => NULL, | |
216 | 'context' => 'advanced', | |
217 | 'action' => CRM_Core_Action::ADVANCED, | |
218 | 'includeContactIds' => NULL, | |
219 | 'searchDescendentGroups' => FALSE, | |
220 | 'expected_query' => array( | |
221 | 0 => 'default', | |
222 | 1 => 'default', | |
1d17e4fc | 223 | 2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)", |
836eb043 | 224 | ), |
225 | ), | |
226 | ), | |
227 | array( | |
228 | array( | |
229 | 'description' => 'Use of quotes for exact string', | |
230 | 'use_case_comments' => 'This is something that was in the code but seemingly not working. No UI info on it though!', | |
231 | 'class' => 'CRM_Contact_Selector', | |
232 | 'settings' => array(array('name' => 'includeWildCardInName', 'value' => FALSE)), | |
233 | 'form_values' => array('email' => '"mickey@mouseville.com"', 'sort_name' => 'Mouse'), | |
234 | 'params' => array(), | |
235 | 'return_properties' => NULL, | |
236 | 'context' => 'advanced', | |
237 | 'action' => CRM_Core_Action::ADVANCED, | |
238 | 'includeContactIds' => NULL, | |
239 | 'searchDescendentGroups' => FALSE, | |
240 | 'expected_query' => array( | |
241 | 0 => 'default', | |
242 | 1 => 'default', | |
1d17e4fc | 243 | 2 => "WHERE ( civicrm_email.email = 'mickey@mouseville.com' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)", |
836eb043 | 244 | ), |
245 | ), | |
246 | ), | |
7ccccc32 | 247 | array( |
248 | array( | |
249 | 'description' => 'Normal search builder behaviour', | |
250 | 'class' => 'CRM_Contact_Selector', | |
251 | 'settings' => array(), | |
a220b357 | 252 | 'form_values' => array('contact_type' => 'Individual', 'country' => array('IS NOT NULL' => 1)), |
7ccccc32 | 253 | 'params' => array(), |
254 | 'return_properties' => array( | |
255 | 'contact_type' => 1, | |
256 | 'contact_sub_type' => 1, | |
257 | 'sort_name' => 1, | |
258 | ), | |
259 | 'context' => 'builder', | |
260 | 'action' => CRM_Core_Action::NONE, | |
261 | 'includeContactIds' => NULL, | |
262 | 'searchDescendentGroups' => FALSE, | |
263 | 'expected_query' => array( | |
a220b357 | 264 | 0 => 'SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, civicrm_address.country_id as country_id', |
265 | 1 => ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )', | |
266 | 2 => 'WHERE ( contact_a.contact_type IN ("Individual") AND civicrm_address.country_id IS NOT NULL ) AND (contact_a.is_deleted = 0)', | |
7ccccc32 | 267 | ), |
268 | ), | |
269 | ), | |
fd95406d | 270 | array( |
271 | array( | |
272 | 'description' => 'Search builder behaviour for Activity', | |
273 | 'class' => 'CRM_Contact_Selector', | |
274 | 'settings' => array(), | |
275 | 'form_values' => array('source_contact_id' => array('IS NOT NULL' => 1)), | |
276 | 'params' => array(), | |
277 | 'return_properties' => array( | |
278 | 'source_contact_id' => 1, | |
279 | ), | |
280 | 'context' => 'builder', | |
281 | 'action' => CRM_Core_Action::NONE, | |
282 | 'includeContactIds' => NULL, | |
283 | 'searchDescendentGroups' => FALSE, | |
284 | 'expected_query' => array( | |
285 | 0 => 'SELECT contact_a.id as contact_id, source_contact.id as source_contact_id', | |
286 | 2 => 'WHERE ( source_contact.id IS NOT NULL ) AND (contact_a.is_deleted = 0)', | |
287 | ), | |
288 | ), | |
289 | ), | |
836eb043 | 290 | ); |
291 | } | |
292 | ||
ae066a2b | 293 | /** |
294 | * Test the contact ID query does not fail on country search. | |
295 | */ | |
296 | public function testContactIDQuery() { | |
39b959db SL |
297 | $params = [ |
298 | [ | |
299 | 0 => 'country-1', | |
300 | 1 => '=', | |
301 | 2 => '1228', | |
302 | 3 => 1, | |
303 | 4 => 0, | |
304 | ], | |
305 | ]; | |
ae066a2b | 306 | |
307 | $searchOBJ = new CRM_Contact_Selector(NULL); | |
308 | $searchOBJ->contactIDQuery($params, '1_u'); | |
309 | } | |
310 | ||
f9ba4d01 | 311 | /** |
312 | * Test the Search Builder using Non ASCII location type for email filter | |
313 | */ | |
314 | public function testSelectorQueryOnNonASCIIlocationType() { | |
315 | $contactID = $this->individualCreate(); | |
316 | $locationType = $this->locationTypeCreate([ | |
317 | 'name' => 'Non ASCII Location Type', | |
318 | 'display_name' => 'Дом Location type', | |
319 | 'vcard_name' => 'Non ASCII Location Type', | |
320 | 'is_active' => 1, | |
321 | ]); | |
322 | $this->callAPISuccess('Email', 'create', [ | |
323 | 'contact_id' => $contactID, | |
324 | 'location_type_id' => $locationType->id, | |
325 | 'email' => 'test@test.com', | |
326 | ]); | |
327 | ||
328 | $selector = new CRM_Contact_Selector( | |
329 | 'CRM_Contact_Selector', | |
330 | ['email' => ['IS NOT NULL' => 1]], | |
39b959db SL |
331 | [ |
332 | [ | |
333 | 0 => 'email-' . $locationType->id, | |
334 | 1 => 'IS NOT NULL', | |
335 | 2 => NULL, | |
336 | 3 => 1, | |
337 | 4 => 0, | |
338 | ], | |
339 | ], | |
f9ba4d01 | 340 | [ |
341 | 'contact_type' => 1, | |
342 | 'contact_sub_type' => 1, | |
343 | 'sort_name' => 1, | |
344 | 'location' => [ | |
345 | 'Non ASCII Location Type' => [ | |
346 | 'location_type' => $locationType->id, | |
347 | 'email' => 1, | |
348 | ], | |
349 | ], | |
350 | ], | |
351 | CRM_Core_Action::NONE, | |
352 | NULL, | |
353 | FALSE, | |
354 | 'builder' | |
355 | ); | |
356 | ||
357 | $sql = $selector->getQueryObject()->query(); | |
358 | ||
359 | $expectedQuery = [ | |
360 | 0 => "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, `Non_ASCII_Location_Type-location_type`.id as `Non_ASCII_Location_Type-location_type_id`, `Non_ASCII_Location_Type-location_type`.name as `Non_ASCII_Location_Type-location_type`, `Non_ASCII_Location_Type-email`.id as `Non_ASCII_Location_Type-email_id`, `Non_ASCII_Location_Type-email`.email as `Non_ASCII_Location_Type-email`", | |
361 | // @TODO these FROM clause doesn't matches due to extra spaces or special character | |
1d17e4fc | 362 | 2 => "WHERE ( ( `Non_ASCII_Location_Type-email`.email IS NOT NULL ) ) AND (contact_a.is_deleted = 0)", |
f9ba4d01 | 363 | ]; |
364 | foreach ($expectedQuery as $index => $queryString) { | |
365 | $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index])); | |
366 | } | |
367 | ||
1d17e4fc | 368 | $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL); |
f9ba4d01 | 369 | $this->assertEquals(1, count($rows)); |
370 | $this->assertEquals($contactID, key($rows)); | |
371 | $this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']); | |
372 | } | |
373 | ||
a0475688 | 374 | /** |
375 | * Test the value use in where clause if it's case sensitive or not against each MySQL operators | |
376 | */ | |
377 | public function testWhereClauseByOperator() { | |
378 | $contactID = $this->individualCreate(['first_name' => 'Adam']); | |
379 | ||
380 | $filters = [ | |
381 | 'IS NOT NULL' => 1, | |
382 | '=' => 'Adam', | |
383 | 'LIKE' => '%Ad%', | |
384 | 'RLIKE' => '^A[a-z]{3}$', | |
385 | 'IN' => ['IN' => ['Adam']], | |
386 | ]; | |
387 | $filtersByWhereClause = [ | |
39b959db SL |
388 | // doesn't matter |
389 | 'IS NOT NULL' => '( contact_a.first_name IS NOT NULL )', | |
390 | // case sensitive check | |
391 | '=' => "( contact_a.first_name = 'Adam' )", | |
392 | // case insensitive check | |
393 | 'LIKE' => "( contact_a.first_name LIKE '%Ad%' )", | |
394 | // case sensitive check | |
395 | 'RLIKE' => "( contact_a.first_name RLIKE BINARY '^A[a-z]{3}$' )", | |
396 | // case sensitive check | |
397 | 'IN' => '( contact_a.first_name IN ("Adam") )', | |
a0475688 | 398 | ]; |
399 | foreach ($filters as $op => $filter) { | |
400 | $selector = new CRM_Contact_Selector( | |
401 | 'CRM_Contact_Selector', | |
402 | ['first_name' => [$op => $filter]], | |
39b959db SL |
403 | [ |
404 | [ | |
405 | 0 => 'first_name', | |
406 | 1 => $op, | |
407 | 2 => $filter, | |
408 | 3 => 1, | |
409 | 4 => 0, | |
410 | ], | |
411 | ], | |
a0475688 | 412 | [], |
413 | CRM_Core_Action::NONE, | |
414 | NULL, | |
415 | FALSE, | |
416 | 'builder' | |
417 | ); | |
418 | ||
419 | $sql = $selector->getQueryObject()->query(); | |
420 | $this->assertEquals(TRUE, strpos($sql[2], $filtersByWhereClause[$op])); | |
421 | ||
1d17e4fc | 422 | $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL); |
a0475688 | 423 | $this->assertEquals(1, count($rows)); |
424 | $this->assertEquals($contactID, key($rows)); | |
425 | } | |
426 | } | |
427 | ||
744050b0 JP |
428 | /** |
429 | * Test if custom table is added in from clause when | |
430 | * search results are ordered by a custom field. | |
431 | */ | |
432 | public function testSelectorQueryOrderByCustomField() { | |
433 | //Search for any params. | |
39b959db SL |
434 | $params = [ |
435 | [ | |
436 | 0 => 'country-1', | |
437 | 1 => '=', | |
438 | 2 => '1228', | |
439 | 3 => 1, | |
440 | 4 => 0, | |
441 | ], | |
442 | ]; | |
744050b0 JP |
443 | |
444 | //Create a test custom group and field. | |
445 | $customGroup = $this->callAPISuccess('CustomGroup', 'create', array( | |
446 | 'title' => "test custom group", | |
447 | 'extends' => "Individual", | |
448 | )); | |
449 | $cgTableName = $customGroup['values'][$customGroup['id']]['table_name']; | |
450 | $customField = $this->callAPISuccess('CustomField', 'create', array( | |
451 | 'custom_group_id' => $customGroup['id'], | |
452 | 'label' => "test field", | |
453 | 'html_type' => "Text", | |
454 | )); | |
455 | $customFieldId = $customField['id']; | |
456 | ||
457 | //Sort by the custom field created above. | |
458 | $sortParams = array( | |
459 | 1 => array( | |
460 | 'name' => 'test field', | |
461 | 'sort' => "custom_{$customFieldId}", | |
462 | ), | |
463 | ); | |
464 | $sort = new CRM_Utils_Sort($sortParams, '1_d'); | |
465 | ||
466 | //Form a query to order by a custom field. | |
467 | $query = new CRM_Contact_BAO_Query($params, | |
468 | CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES, | |
469 | NULL, FALSE, FALSE, 1, | |
470 | FALSE, TRUE, TRUE, NULL, | |
471 | 'AND' | |
472 | ); | |
473 | $query->searchQuery(0, 0, $sort, | |
474 | FALSE, FALSE, FALSE, | |
475 | FALSE, FALSE | |
476 | ); | |
477 | //Check if custom table is included in $query->_tables. | |
478 | $this->assertTrue(in_array($cgTableName, array_keys($query->_tables))); | |
479 | //Assert if from clause joins the custom table. | |
480 | $this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE); | |
1d17e4fc SL |
481 | $this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]); |
482 | $this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]); | |
744050b0 JP |
483 | } |
484 | ||
8ad22b15 | 485 | /** |
486 | * Check where clause of a date custom field when 'IS NOT EMPTY' operator is used | |
487 | */ | |
488 | public function testCustomDateField() { | |
489 | $contactID = $this->individualCreate(); | |
490 | //Create a test custom group and field. | |
491 | $customGroup = $this->callAPISuccess('CustomGroup', 'create', array( | |
492 | 'title' => "test custom group", | |
493 | 'extends' => "Individual", | |
494 | )); | |
1d17e4fc | 495 | $customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup['id'], 'return' => 'table_name']); |
8ad22b15 | 496 | $customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name']; |
497 | ||
498 | $createdField = $this->callAPISuccess('customField', 'create', [ | |
499 | 'data_type' => 'Date', | |
500 | 'html_type' => 'Select Date', | |
501 | 'date_format' => 'd M yy', | |
502 | 'time_format' => 1, | |
503 | 'label' => 'test field', | |
504 | 'custom_group_id' => $customGroup['id'], | |
505 | ]); | |
506 | $customFieldColumnName = $createdField['values'][$createdField['id']]['column_name']; | |
507 | ||
508 | $this->callAPISuccess('Contact', 'create', [ | |
509 | 'id' => $contactID, | |
510 | 'custom_' . $createdField['id'] => date('YmdHis'), | |
511 | ]); | |
512 | ||
513 | $selector = new CRM_Contact_Selector( | |
514 | 'CRM_Contact_Selector', | |
515 | ['custom_' . $createdField['id'] => ['IS NOT EMPTY' => 1]], | |
516 | [[ | |
517 | 0 => 'custom_' . $createdField['id'], | |
518 | 1 => 'IS NOT NULL', | |
519 | 2 => 1, | |
520 | 3 => 1, | |
521 | 4 => 0, | |
39b959db SL |
522 | ], |
523 | ], | |
8ad22b15 | 524 | [], |
525 | CRM_Core_Action::NONE, | |
526 | NULL, | |
527 | FALSE, | |
528 | 'builder' | |
529 | ); | |
530 | ||
531 | $whereClause = $selector->getQueryObject()->query()[2]; | |
532 | $expectedClause = sprintf("( %s.%s IS NOT NULL )", $customGroupTableName, $customFieldColumnName); | |
533 | // test the presence of expected date clause | |
534 | $this->assertEquals(TRUE, strpos($whereClause, $expectedClause)); | |
535 | ||
1d17e4fc | 536 | $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL); |
8ad22b15 | 537 | $this->assertEquals(1, count($rows)); |
538 | } | |
539 | ||
836eb043 | 540 | /** |
541 | * Get the default select string since this is generally consistent. | |
542 | */ | |
543 | public function getDefaultSelectString() { | |
544 | return 'SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`,' | |
545 | . ' contact_a.display_name as `display_name`, contact_a.do_not_email as `do_not_email`, contact_a.do_not_phone as `do_not_phone`, contact_a.do_not_mail as `do_not_mail`,' | |
546 | . ' contact_a.do_not_sms as `do_not_sms`, contact_a.do_not_trade as `do_not_trade`, contact_a.is_opt_out as `is_opt_out`, contact_a.legal_identifier as `legal_identifier`,' | |
547 | . ' contact_a.external_identifier as `external_identifier`, contact_a.nick_name as `nick_name`, contact_a.legal_name as `legal_name`, contact_a.image_URL as `image_URL`,' | |
548 | . ' contact_a.preferred_communication_method as `preferred_communication_method`, contact_a.preferred_language as `preferred_language`,' | |
549 | . ' contact_a.preferred_mail_format as `preferred_mail_format`, contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_name`,' | |
550 | . ' contact_a.prefix_id as `prefix_id`, contact_a.suffix_id as `suffix_id`, contact_a.formal_title as `formal_title`, contact_a.communication_style_id as `communication_style_id`,' | |
551 | . ' contact_a.job_title as `job_title`, contact_a.gender_id as `gender_id`, contact_a.birth_date as `birth_date`, contact_a.is_deceased as `is_deceased`,' | |
552 | . ' contact_a.deceased_date as `deceased_date`, contact_a.household_name as `household_name`,' | |
553 | . ' IF ( contact_a.contact_type = \'Individual\', NULL, contact_a.organization_name ) as organization_name, contact_a.sic_code as `sic_code`, contact_a.is_deleted as `contact_is_deleted`,' | |
554 | . ' IF ( contact_a.contact_type = \'Individual\', contact_a.organization_name, NULL ) as current_employer, civicrm_address.id as address_id,' | |
555 | . ' civicrm_address.street_address as `street_address`, civicrm_address.supplemental_address_1 as `supplemental_address_1`, ' | |
207f62c6 | 556 | . 'civicrm_address.supplemental_address_2 as `supplemental_address_2`, civicrm_address.supplemental_address_3 as `supplemental_address_3`, civicrm_address.city as `city`, civicrm_address.postal_code_suffix as `postal_code_suffix`, ' |
836eb043 | 557 | . 'civicrm_address.postal_code as `postal_code`, civicrm_address.geo_code_1 as `geo_code_1`, civicrm_address.geo_code_2 as `geo_code_2`, ' |
558 | . 'civicrm_address.state_province_id as state_province_id, civicrm_address.country_id as country_id, civicrm_phone.id as phone_id, civicrm_phone.phone_type_id as phone_type_id, ' | |
559 | . 'civicrm_phone.phone as `phone`, civicrm_email.id as email_id, civicrm_email.email as `email`, civicrm_email.on_hold as `on_hold`, civicrm_im.id as im_id, ' | |
560 | . 'civicrm_im.provider_id as provider_id, civicrm_im.name as `im`, civicrm_worldregion.id as worldregion_id, civicrm_worldregion.name as `world_region`'; | |
561 | } | |
562 | ||
563 | /** | |
564 | * Get the default from string since this is generally consistent. | |
565 | */ | |
566 | public function getDefaultFromString() { | |
567 | return ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )' | |
7f594311 | 568 | . ' LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) ' |
836eb043 | 569 | . ' LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)' |
570 | . ' LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)' | |
571 | . ' LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) ' | |
7f594311 | 572 | . 'LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id '; |
836eb043 | 573 | } |
574 | ||
575 | /** | |
576 | * Strangle strings into a more matchable format. | |
577 | * | |
578 | * @param string $string | |
579 | * @return string | |
580 | */ | |
581 | public function strWrangle($string) { | |
582 | return str_replace(' ', ' ', $string); | |
583 | } | |
584 | ||
585 | /** | |
586 | * Swap out default parts of the query for the actual string. | |
587 | * | |
588 | * Note that it seems to make more sense to resolve this earlier & pass it in from a clean code point of | |
589 | * view, but the output on fail includes long sql statements that are of low relevance then. | |
590 | * | |
591 | * @param array $expectedQuery | |
592 | */ | |
593 | public function wrangleDefaultClauses(&$expectedQuery) { | |
fd95406d | 594 | if (CRM_Utils_Array::value(0, $expectedQuery) == 'default') { |
836eb043 | 595 | $expectedQuery[0] = $this->getDefaultSelectString(); |
596 | } | |
fd95406d | 597 | if (CRM_Utils_Array::value(1, $expectedQuery) == 'default') { |
836eb043 | 598 | $expectedQuery[1] = $this->getDefaultFromString(); |
599 | } | |
600 | } | |
601 | ||
602 | } |