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