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