3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Include parent class definition
17 * Test contact custom search functions
22 class CRM_Contact_SelectorTest
extends CiviUnitTestCase
{
25 * Test the query from the selector class is consistent with the dataset expectation.
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.
31 * @dataProvider querySets
34 public function testSelectorQuery($dataSet) {
35 $params = CRM_Contact_BAO_Query
::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
36 $isDeleted = in_array(['deleted_contacts', '=', 1, 0, 0], $params);
37 foreach ($dataSet['settings'] as $setting) {
38 $this->callAPISuccess('Setting', 'create', [$setting['name'] => $setting['value']]);
40 $selector = new CRM_Contact_Selector(
42 $dataSet['form_values'],
44 $dataSet['return_properties'],
46 $dataSet['includeContactIds'],
47 $dataSet['searchDescendentGroups'],
50 $queryObject = $selector->getQueryObject();
51 // Make sure there is no fail on alphabet query.
52 $selector->alphabetQuery()->fetchAll();
53 $sql = $queryObject->query(FALSE, FALSE, FALSE, $isDeleted);
54 $this->wrangleDefaultClauses($dataSet['expected_query']);
55 foreach ($dataSet['expected_query'] as $index => $queryString) {
56 $this->assertLike($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
58 // Ensure that search builder return individual contact as per criteria
59 if ($dataSet['context'] === 'builder') {
60 $contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']);
61 if ('Search builder behaviour for Activity' === $dataSet['description']) {
62 $this->callAPISuccess('Activity', 'create', [
63 'activity_type_id' => 'Meeting',
65 'source_contact_id' => $contactID,
67 $rows = CRM_Core_DAO
::executeQuery(implode(' ', $sql))->fetchAll();
68 $this->assertCount(1, $rows);
69 $this->assertEquals($contactID, $rows[0]['source_contact_id']);
72 $this->callAPISuccess('Address', 'create', [
73 'contact_id' => $contactID,
74 'location_type_id' => 'Home',
78 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 50, '');
79 $this->assertEquals(1, count($rows));
81 CRM_Core_DAO
::reenableFullGroupByMode();
82 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 50, '');
84 $sortChar = $selector->alphabetQuery()->fetchAll();
85 // sort name is stored in '<last_name>, <first_name>' format, as per which the first character would be B of Bond
86 $this->assertEquals('B', $sortChar[0]['sort_name']);
87 $this->assertEquals($contactID, key($rows));
89 CRM_Core_DAO
::reenableFullGroupByMode();
90 $selector->getQueryObject()->getCachedContacts([$contactID], FALSE);
96 * Test advanced search results by uf_group_id.
98 public function testSearchByProfile() {
99 //Create search profile for contacts.
100 $ufGroup = $this->callAPISuccess('uf_group', 'create', [
101 'group_type' => 'Contact',
102 'name' => 'test_search_profile',
103 'title' => 'Test Search Profile',
104 'api.uf_field.create' => [
106 'field_name' => 'email',
107 'visibility' => 'Public Pages and Listings',
108 'field_type' => 'Contact',
114 $contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']);
115 //Put the email on hold.
116 $email = $this->callAPISuccess('Email', 'get', [
118 'contact_id' => $contactID,
120 $this->callAPISuccess('Email', 'create', [
121 'id' => $email['id'],
126 'description' => 'Normal default behaviour',
127 'class' => 'CRM_Contact_Selector',
129 'form_values' => ['email' => 'mickey@mouseville.com', 'uf_group_id' => $ufGroup['id']],
131 'return_properties' => NULL,
132 'context' => 'advanced',
133 'action' => CRM_Core_Action
::ADVANCED
,
134 'includeContactIds' => NULL,
135 'searchDescendentGroups' => FALSE,
137 $params = CRM_Contact_BAO_Query
::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
138 // create CRM_Contact_Selector instance and set desired query params
139 $selector = new CRM_Contact_Selector(
141 $dataSet['form_values'],
143 $dataSet['return_properties'],
145 $dataSet['includeContactIds'],
146 $dataSet['searchDescendentGroups'],
149 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 50, '');
150 $this->assertEquals(1, count($rows));
151 $this->assertEquals($contactID, key($rows));
153 //Check if email column contains (On Hold) string.
154 foreach ($rows[$contactID] as $key => $value) {
155 if (strpos($key, 'email') !== FALSE) {
156 $this->assertContains("(On Hold)", (string) $value);
162 * Test the civicrm_prevnext_cache entry if it correctly stores the search query result
164 public function testPrevNextCache() {
165 $contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']);
167 'description' => 'Normal default behaviour',
168 'class' => 'CRM_Contact_Selector',
170 'form_values' => ['email' => 'mickey@mouseville.com'],
172 'return_properties' => NULL,
173 'context' => 'advanced',
174 'action' => CRM_Core_Action
::ADVANCED
,
175 'includeContactIds' => NULL,
176 'searchDescendentGroups' => FALSE,
177 'expected_query' => [
180 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
183 $params = CRM_Contact_BAO_Query
::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
185 // create CRM_Contact_Selector instance and set desired query params
186 $selector = new CRM_Contact_Selector(
188 $dataSet['form_values'],
190 $dataSet['return_properties'],
192 $dataSet['includeContactIds'],
193 $dataSet['searchDescendentGroups'],
197 $key = substr(sha1(rand()), 0, 7);
198 $selector->setKey($key);
200 // fetch row and check the result
201 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 1, NULL);
202 $this->assertEquals(1, count($rows));
203 $this->assertEquals($contactID, key($rows));
205 // build cache key and use to it to fetch prev-next cache record
206 $cacheKey = 'civicrm search ' . $key;
207 $contacts = CRM_Utils_SQL_Select
::from('civicrm_prevnext_cache')
208 ->select(['entity_id1', 'cachekey'])
209 ->where("cachekey = @key")
210 ->param('key', $cacheKey)
213 $this->assertEquals(1, count($contacts));
214 // check the prevNext record matches
216 'entity_id1' => $contactID,
217 'cachekey' => $cacheKey,
219 $this->checkArrayEquals($contacts[0], $expectedEntry);
223 * Data sets for testing.
225 public function querySets() {
229 'description' => 'Empty group test',
230 'class' => 'CRM_Contact_Selector',
232 'form_values' => [['contact_type', '=', 'Individual', 1, 0], ['group', 'IS NULL', '', 1, 0]],
234 'return_properties' => NULL,
235 'context' => 'builder',
236 'action' => CRM_Core_Action
::NONE
,
237 'includeContactIds' => NULL,
238 'searchDescendentGroups' => FALSE,
239 'expected_query' => [],
244 'description' => 'Normal default behaviour',
245 'class' => 'CRM_Contact_Selector',
247 'form_values' => ['email' => 'mickey@mouseville.com'],
249 'return_properties' => NULL,
250 'context' => 'advanced',
251 'action' => CRM_Core_Action
::ADVANCED
,
252 'includeContactIds' => NULL,
253 'searchDescendentGroups' => FALSE,
254 'expected_query' => [
257 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
263 'description' => 'Normal default + user added wildcard',
264 'class' => 'CRM_Contact_Selector',
266 'form_values' => ['email' => '%mickey@mouseville.com', 'sort_name' => 'Mouse'],
268 'return_properties' => NULL,
269 'context' => 'advanced',
270 'action' => CRM_Core_Action
::ADVANCED
,
271 'includeContactIds' => NULL,
272 'searchDescendentGroups' => FALSE,
273 'expected_query' => [
276 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)",
282 'description' => 'Site set to not pre-pend wildcard',
283 'class' => 'CRM_Contact_Selector',
284 'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
285 'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'],
287 'return_properties' => NULL,
288 'context' => 'advanced',
289 'action' => CRM_Core_Action
::ADVANCED
,
290 'includeContactIds' => NULL,
291 'searchDescendentGroups' => FALSE,
292 'expected_query' => [
295 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)",
301 'description' => 'Site set to not pre-pend wildcard and check that trash value is respected',
302 'class' => 'CRM_Contact_Selector',
303 'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
304 'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse', 'deleted_contacts' => 1],
306 'return_properties' => NULL,
307 'context' => 'advanced',
308 'action' => CRM_Core_Action
::ADVANCED
,
309 'includeContactIds' => NULL,
310 'searchDescendentGroups' => FALSE,
311 'expected_query' => [
314 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)",
318 'description' => 'Use of quotes for exact string',
319 'use_case_comments' => 'This is something that was in the code but seemingly not working. No UI info on it though!',
320 'class' => 'CRM_Contact_Selector',
321 'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
322 'form_values' => ['email' => '"mickey@mouseville.com"', 'sort_name' => 'Mouse'],
324 'return_properties' => NULL,
325 'context' => 'advanced',
326 'action' => CRM_Core_Action
::ADVANCED
,
327 'includeContactIds' => NULL,
328 'searchDescendentGroups' => FALSE,
329 'expected_query' => [
332 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)",
338 'description' => 'Normal search builder behaviour',
339 'class' => 'CRM_Contact_Selector',
341 'form_values' => ['contact_type' => 'Individual', 'country' => ['IS NOT NULL' => 1]],
343 'return_properties' => [
345 'contact_sub_type' => 1,
348 'context' => 'builder',
349 'action' => CRM_Core_Action
::NONE
,
350 'includeContactIds' => NULL,
351 'searchDescendentGroups' => FALSE,
352 'expected_query' => [
353 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',
354 1 => ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )',
355 2 => 'WHERE ( contact_a.contact_type IN ("Individual") AND civicrm_address.country_id IS NOT NULL ) AND (contact_a.is_deleted = 0)',
361 'description' => 'Search builder behaviour for Activity',
362 'class' => 'CRM_Contact_Selector',
364 'form_values' => ['source_contact_id' => ['IS NOT NULL' => 1]],
366 'return_properties' => [
367 'source_contact_id' => 1,
369 'context' => 'builder',
370 'action' => CRM_Core_Action
::NONE
,
371 'includeContactIds' => NULL,
372 'searchDescendentGroups' => FALSE,
373 'expected_query' => [
374 0 => 'SELECT contact_a.id as contact_id, source_contact.id as source_contact_id',
375 2 => 'WHERE ( source_contact.id IS NOT NULL ) AND (contact_a.is_deleted = 0)',
381 'description' => 'Test display relationships',
382 'class' => 'CRM_Contact_Selector',
384 'form_values' => ['display_relationship_type' => '1_b_a'],
385 'return_properties' => NULL,
387 'context' => 'advanced',
388 'action' => CRM_Core_Action
::NONE
,
389 'includeContactIds' => NULL,
390 'searchDescendentGroups' => FALSE,
391 'expected_query' => [
392 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`',
393 2 => 'WHERE displayRelType.relationship_type_id = 1
394 AND displayRelType.is_active = 1
395 AND (contact_a.is_deleted = 0)',
403 * Test the contact ID query does not fail on country search.
405 public function testContactIDQuery() {
416 $searchOBJ = new CRM_Contact_Selector(NULL);
417 $searchOBJ->contactIDQuery($params, '1_u');
421 * Test the Search Builder using Non ASCII location type for email filter
423 public function testSelectorQueryOnNonASCIIlocationType() {
424 $contactID = $this->individualCreate();
425 $locationType = $this->locationTypeCreate([
426 'name' => 'Non ASCII Location Type',
427 'display_name' => 'Дом Location type',
428 'vcard_name' => 'Non ASCII Location Type',
431 $this->callAPISuccess('Email', 'create', [
432 'contact_id' => $contactID,
433 'location_type_id' => $locationType->id
,
434 'email' => 'test@test.com',
437 $selector = new CRM_Contact_Selector(
438 'CRM_Contact_Selector',
439 ['email' => ['IS NOT NULL' => 1]],
442 0 => 'email-' . $locationType->id
,
451 'contact_sub_type' => 1,
454 'Non ASCII Location Type' => [
455 'location_type' => $locationType->id
,
460 CRM_Core_Action
::NONE
,
466 $sql = $selector->getQueryObject()->query();
469 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`",
470 // @TODO these FROM clause doesn't matches due to extra spaces or special character
471 2 => "WHERE ( ( `Non_ASCII_Location_Type-email`.email IS NOT NULL ) ) AND (contact_a.is_deleted = 0)",
473 foreach ($expectedQuery as $index => $queryString) {
474 $this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
477 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 1, NULL);
478 $this->assertEquals(1, count($rows));
479 $this->assertEquals($contactID, key($rows));
480 $this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']);
484 * Test the value use in where clause if it's case sensitive or not against each MySQL operators
486 public function testWhereClauseByOperator() {
487 $contactID = $this->individualCreate(['first_name' => 'Adam']);
493 'RLIKE' => '^A[a-z]{3}$',
494 'IN' => ['IN' => ['Adam']],
496 $filtersByWhereClause = [
498 'IS NOT NULL' => '( contact_a.first_name IS NOT NULL )',
499 // case sensitive check
500 '=' => "( contact_a.first_name = 'Adam' )",
501 // case insensitive check
502 'LIKE' => "( contact_a.first_name LIKE '%Ad%' )",
503 // case sensitive check
504 'RLIKE' => "( contact_a.first_name RLIKE BINARY '^A[a-z]{3}$' )",
505 // case sensitive check
506 'IN' => '( contact_a.first_name IN ("Adam") )',
508 foreach ($filters as $op => $filter) {
509 $selector = new CRM_Contact_Selector(
510 'CRM_Contact_Selector',
511 ['first_name' => [$op => $filter]],
522 CRM_Core_Action
::NONE
,
528 $sql = $selector->getQueryObject()->query();
529 $this->assertEquals(TRUE, strpos($sql[2], $filtersByWhereClause[$op]));
531 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 1, NULL);
532 $this->assertEquals(1, count($rows));
533 $this->assertEquals($contactID, key($rows));
538 * Test if custom table is added in from clause when
539 * search results are ordered by a custom field.
541 public function testSelectorQueryOrderByCustomField() {
542 //Search for any params.
553 //Create a test custom group and field.
554 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
555 'title' => "test custom group",
556 'extends' => "Individual",
558 $cgTableName = $customGroup['values'][$customGroup['id']]['table_name'];
559 $customField = $this->callAPISuccess('CustomField', 'create', [
560 'custom_group_id' => $customGroup['id'],
561 'label' => "test field",
562 'html_type' => "Text",
564 $customFieldId = $customField['id'];
566 //Sort by the custom field created above.
569 'name' => 'test field',
570 'sort' => "custom_{$customFieldId}",
573 $sort = new CRM_Utils_Sort($sortParams, '1_d');
575 //Form a query to order by a custom field.
576 $query = new CRM_Contact_BAO_Query($params,
577 CRM_Contact_BAO_Query
::NO_RETURN_PROPERTIES
,
578 NULL, FALSE, FALSE, 1,
579 FALSE, TRUE, TRUE, NULL,
582 $query->searchQuery(0, 0, $sort,
586 //Check if custom table is included in $query->_tables.
587 $this->assertTrue(in_array($cgTableName, array_keys($query->_tables
)));
588 //Assert if from clause joins the custom table.
589 $this->assertTrue(strpos($query->_fromClause
, $cgTableName) !== FALSE);
590 $this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]);
591 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]);
595 * Check where clause of a date custom field when 'IS NOT EMPTY' operator is used
597 public function testCustomDateField() {
598 $contactID = $this->individualCreate();
599 //Create a test custom group and field.
600 $customGroup = $this->callAPISuccess('CustomGroup', 'create', [
601 'title' => "test custom group",
602 'extends' => "Individual",
604 $customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup['id'], 'return' => 'table_name']);
605 $customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name'];
607 $createdField = $this->callAPISuccess('customField', 'create', [
608 'data_type' => 'Date',
609 'html_type' => 'Select Date',
610 'date_format' => 'd M yy',
612 'label' => 'test field',
613 'custom_group_id' => $customGroup['id'],
615 $customFieldColumnName = $createdField['values'][$createdField['id']]['column_name'];
617 $this->callAPISuccess('Contact', 'create', [
619 'custom_' . $createdField['id'] => date('YmdHis'),
622 $selector = new CRM_Contact_Selector(
623 'CRM_Contact_Selector',
624 ['custom_' . $createdField['id'] => ['IS NOT EMPTY' => 1]],
627 0 => 'custom_' . $createdField['id'],
635 CRM_Core_Action
::NONE
,
641 $whereClause = $selector->getQueryObject()->query()[2];
642 $expectedClause = sprintf("( %s.%s IS NOT NULL )", $customGroupTableName, $customFieldColumnName);
643 // test the presence of expected date clause
644 $this->assertEquals(TRUE, strpos($whereClause, $expectedClause));
646 $rows = $selector->getRows(CRM_Core_Action
::VIEW
, 0, 1, NULL);
647 $this->assertEquals(1, count($rows));
651 * Get the default select string since this is generally consistent.
653 public function getDefaultSelectString() {
654 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`,'
655 . ' 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`,'
656 . ' 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`,'
657 . ' 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`,'
658 . ' contact_a.preferred_communication_method as `preferred_communication_method`, contact_a.preferred_language as `preferred_language`,'
659 . ' 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`,'
660 . ' 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`,'
661 . ' 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`,'
662 . ' contact_a.deceased_date as `deceased_date`, contact_a.household_name as `household_name`,'
663 . ' 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`,'
664 . ' IF ( contact_a.contact_type = \'Individual\', contact_a.organization_name, NULL ) as current_employer, civicrm_address.id as address_id,'
665 . ' civicrm_address.street_address as `street_address`, civicrm_address.supplemental_address_1 as `supplemental_address_1`, '
666 . '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`, '
667 . '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`, '
668 . '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, '
669 . '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, '
670 . 'civicrm_im.provider_id as provider_id, civicrm_im.name as `im`, civicrm_worldregion.id as worldregion_id, civicrm_worldregion.name as `world_region`';
674 * Get the default from string since this is generally consistent.
676 public function getDefaultFromString() {
677 return ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )'
678 . ' LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) '
679 . ' LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)'
680 . ' LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)'
681 . ' LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) '
682 . 'LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ';
686 * Strangle strings into a more matchable format.
688 * @param string $string
692 public function strWrangle($string) {
693 return trim(str_replace(' ', ' ', $string));
697 * Swap out default parts of the query for the actual string.
699 * Note that it seems to make more sense to resolve this earlier & pass it in from a clean code point of
700 * view, but the output on fail includes long sql statements that are of low relevance then.
702 * @param array $expectedQuery
704 public function wrangleDefaultClauses(&$expectedQuery) {
705 if (CRM_Utils_Array
::value(0, $expectedQuery) == 'default') {
706 $expectedQuery[0] = $this->getDefaultSelectString();
708 if (CRM_Utils_Array
::value(1, $expectedQuery) == 'default') {
709 $expectedQuery[1] = $this->getDefaultFromString();