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