0abbe0b1bc797a150b92839e0bdfe5aec816bd9c
[civicrm-core.git] / tests / phpunit / CRM / Contact / SelectorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Include parent class definition
14 */
15
16 /**
17 * Test contact custom search functions
18 *
19 * @package CiviCRM
20 * @group headless
21 */
22 class CRM_Contact_SelectorTest extends CiviUnitTestCase {
23
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
32 * @throws \Exception
33 */
34 public function testSelectorQuery($dataSet) {
35 $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
36 foreach ($dataSet['settings'] as $setting) {
37 $this->callAPISuccess('Setting', 'create', [$setting['name'] => $setting['value']]);
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();
50 // Make sure there is no fail on alphabet query.
51 $selector->alphabetQuery()->fetchAll();
52 $sql = $queryObject->query();
53 $this->wrangleDefaultClauses($dataSet['expected_query']);
54 foreach ($dataSet['expected_query'] as $index => $queryString) {
55 $this->assertLike($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
56 }
57 // Ensure that search builder return individual contact as per criteria
58 if ($dataSet['context'] == 'builder') {
59 $contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']);
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));
79
80 CRM_Core_DAO::reenableFullGroupByMode();
81 $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
82
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));
87
88 CRM_Core_DAO::reenableFullGroupByMode();
89 $selector->getQueryObject()->getCachedContacts([$contactID], FALSE);
90 }
91 }
92 }
93
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']);
99 $dataSet = [
100 'description' => 'Normal default behaviour',
101 'class' => 'CRM_Contact_Selector',
102 'settings' => [],
103 'form_values' => ['email' => 'mickey@mouseville.com'],
104 'params' => [],
105 'return_properties' => NULL,
106 'context' => 'advanced',
107 'action' => CRM_Core_Action::ADVANCED,
108 'includeContactIds' => NULL,
109 'searchDescendentGroups' => FALSE,
110 'expected_query' => [
111 0 => 'default',
112 1 => 'default',
113 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
114 ],
115 ];
116 $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
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
134 $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
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')
141 ->select(['entity_id1', 'cachekey'])
142 ->where("cachekey = @key")
143 ->param('key', $cacheKey)
144 ->execute()
145 ->fetchAll();
146 $this->assertEquals(1, count($contacts));
147 // check the prevNext record matches
148 $expectedEntry = [
149 'entity_id1' => $contactID,
150 'cachekey' => $cacheKey,
151 ];
152 $this->checkArrayEquals($contacts[0], $expectedEntry);
153 }
154
155 /**
156 * Data sets for testing.
157 */
158 public function querySets() {
159 return [
160 [
161 [
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,
172 'expected_query' => [],
173 ],
174 ],
175 [
176 [
177 'description' => 'Normal default behaviour',
178 'class' => 'CRM_Contact_Selector',
179 'settings' => [],
180 'form_values' => ['email' => 'mickey@mouseville.com'],
181 'params' => [],
182 'return_properties' => NULL,
183 'context' => 'advanced',
184 'action' => CRM_Core_Action::ADVANCED,
185 'includeContactIds' => NULL,
186 'searchDescendentGroups' => FALSE,
187 'expected_query' => [
188 0 => 'default',
189 1 => 'default',
190 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
191 ],
192 ],
193 ],
194 [
195 [
196 'description' => 'Normal default + user added wildcard',
197 'class' => 'CRM_Contact_Selector',
198 'settings' => [],
199 'form_values' => ['email' => '%mickey@mouseville.com', 'sort_name' => 'Mouse'],
200 'params' => [],
201 'return_properties' => NULL,
202 'context' => 'advanced',
203 'action' => CRM_Core_Action::ADVANCED,
204 'includeContactIds' => NULL,
205 'searchDescendentGroups' => FALSE,
206 'expected_query' => [
207 0 => 'default',
208 1 => 'default',
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)",
210 ],
211 ],
212 ],
213 [
214 [
215 'description' => 'Site set to not pre-pend wildcard',
216 'class' => 'CRM_Contact_Selector',
217 'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
218 'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'],
219 'params' => [],
220 'return_properties' => NULL,
221 'context' => 'advanced',
222 'action' => CRM_Core_Action::ADVANCED,
223 'includeContactIds' => NULL,
224 'searchDescendentGroups' => FALSE,
225 'expected_query' => [
226 0 => 'default',
227 1 => 'default',
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)",
229 ],
230 ],
231 ],
232 [
233 [
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',
237 'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
238 'form_values' => ['email' => '"mickey@mouseville.com"', 'sort_name' => 'Mouse'],
239 'params' => [],
240 'return_properties' => NULL,
241 'context' => 'advanced',
242 'action' => CRM_Core_Action::ADVANCED,
243 'includeContactIds' => NULL,
244 'searchDescendentGroups' => FALSE,
245 'expected_query' => [
246 0 => 'default',
247 1 => 'default',
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)",
249 ],
250 ],
251 ],
252 [
253 [
254 'description' => 'Normal search builder behaviour',
255 'class' => 'CRM_Contact_Selector',
256 'settings' => [],
257 'form_values' => ['contact_type' => 'Individual', 'country' => ['IS NOT NULL' => 1]],
258 'params' => [],
259 'return_properties' => [
260 'contact_type' => 1,
261 'contact_sub_type' => 1,
262 'sort_name' => 1,
263 ],
264 'context' => 'builder',
265 'action' => CRM_Core_Action::NONE,
266 'includeContactIds' => NULL,
267 'searchDescendentGroups' => FALSE,
268 'expected_query' => [
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)',
272 ],
273 ],
274 ],
275 [
276 [
277 'description' => 'Search builder behaviour for Activity',
278 'class' => 'CRM_Contact_Selector',
279 'settings' => [],
280 'form_values' => ['source_contact_id' => ['IS NOT NULL' => 1]],
281 'params' => [],
282 'return_properties' => [
283 'source_contact_id' => 1,
284 ],
285 'context' => 'builder',
286 'action' => CRM_Core_Action::NONE,
287 'includeContactIds' => NULL,
288 'searchDescendentGroups' => FALSE,
289 'expected_query' => [
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)',
292 ],
293 ],
294 ],
295 [
296 [
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,
307 'expected_query' => [
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)',
312 ],
313 ],
314 ],
315 ];
316 }
317
318 /**
319 * Test the contact ID query does not fail on country search.
320 */
321 public function testContactIDQuery() {
322 $params = [
323 [
324 0 => 'country-1',
325 1 => '=',
326 2 => '1228',
327 3 => 1,
328 4 => 0,
329 ],
330 ];
331
332 $searchOBJ = new CRM_Contact_Selector(NULL);
333 $searchOBJ->contactIDQuery($params, '1_u');
334 }
335
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]],
356 [
357 [
358 0 => 'email-' . $locationType->id,
359 1 => 'IS NOT NULL',
360 2 => NULL,
361 3 => 1,
362 4 => 0,
363 ],
364 ],
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
387 2 => "WHERE ( ( `Non_ASCII_Location_Type-email`.email IS NOT NULL ) ) AND (contact_a.is_deleted = 0)",
388 ];
389 foreach ($expectedQuery as $index => $queryString) {
390 $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
391 }
392
393 $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
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
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 = [
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") )',
423 ];
424 foreach ($filters as $op => $filter) {
425 $selector = new CRM_Contact_Selector(
426 'CRM_Contact_Selector',
427 ['first_name' => [$op => $filter]],
428 [
429 [
430 0 => 'first_name',
431 1 => $op,
432 2 => $filter,
433 3 => 1,
434 4 => 0,
435 ],
436 ],
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
447 $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
448 $this->assertEquals(1, count($rows));
449 $this->assertEquals($contactID, key($rows));
450 }
451 }
452
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.
459 $params = [
460 [
461 0 => 'country-1',
462 1 => '=',
463 2 => '1228',
464 3 => 1,
465 4 => 0,
466 ],
467 ];
468
469 //Create a test custom group and field.
470 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
471 'title' => "test custom group",
472 'extends' => "Individual",
473 ]);
474 $cgTableName = $customGroup['values'][$customGroup['id']]['table_name'];
475 $customField = $this->callAPISuccess('CustomField', 'create', [
476 'custom_group_id' => $customGroup['id'],
477 'label' => "test field",
478 'html_type' => "Text",
479 ]);
480 $customFieldId = $customField['id'];
481
482 //Sort by the custom field created above.
483 $sortParams = [
484 1 => [
485 'name' => 'test field',
486 'sort' => "custom_{$customFieldId}",
487 ],
488 ];
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);
506 $this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]);
507 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]);
508 }
509
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.
516 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
517 'title' => "test custom group",
518 'extends' => "Individual",
519 ]);
520 $customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup['id'], 'return' => 'table_name']);
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]],
541 [
542 [
543 0 => 'custom_' . $createdField['id'],
544 1 => 'IS NOT NULL',
545 2 => 1,
546 3 => 1,
547 4 => 0,
548 ],
549 ],
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
562 $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
563 $this->assertEquals(1, count($rows));
564 }
565
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`,'
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`';
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 )'
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 ';
599 }
600
601 /**
602 * Strangle strings into a more matchable format.
603 *
604 * @param string $string
605 *
606 * @return string
607 */
608 public function strWrangle($string) {
609 return trim(str_replace(' ', ' ', $string));
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) {
621 if (CRM_Utils_Array::value(0, $expectedQuery) == 'default') {
622 $expectedQuery[0] = $this->getDefaultSelectString();
623 }
624 if (CRM_Utils_Array::value(1, $expectedQuery) == 'default') {
625 $expectedQuery[1] = $this->getDefaultFromString();
626 }
627 }
628
629 }