Merge pull request #15937 from seamuslee001/dev_core_1405
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / QueryTest.php
1 <?php
2
3 /**
4 * Include dataProvider for tests
5 *
6 * @group headless
7 */
8 class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
9
10 use CRMTraits_Financial_FinancialACLTrait;
11 use CRMTraits_Financial_PriceSetTrait;
12
13 /**
14 * @return CRM_Contact_BAO_QueryTestDataProvider
15 */
16 public function dataProvider() {
17 return new CRM_Contact_BAO_QueryTestDataProvider();
18 }
19
20 public function setUp() {
21 parent::setUp();
22 }
23
24 /**
25 * Clean up after test.
26 *
27 * @throws \Exception
28 */
29 public function tearDown() {
30 $this->quickCleanUpFinancialEntities();
31 $tablesToTruncate = [
32 'civicrm_group_contact',
33 'civicrm_group',
34 'civicrm_saved_search',
35 'civicrm_entity_tag',
36 'civicrm_tag',
37 'civicrm_contact',
38 'civicrm_address',
39 ];
40 $this->quickCleanup($tablesToTruncate);
41 parent::tearDown();
42 }
43
44 /**
45 * Test CRM_Contact_BAO_Query::searchQuery().
46 *
47 * @dataProvider dataProvider
48 *
49 * @param $fv
50 * @param $count
51 * @param $ids
52 *
53 * @throws \CRM_Core_Exception
54 */
55 public function testSearch($fv, $count, $ids) {
56 $this->callAPISuccess('SavedSearch', 'create', ['form_values' => 'a:9:{s:5:"qfKey";s:32:"0123456789abcdef0123456789abcdef";s:13:"includeGroups";a:1:{i:0;s:1:"3";}s:13:"excludeGroups";a:0:{}s:11:"includeTags";a:0:{}s:11:"excludeTags";a:0:{}s:4:"task";s:2:"14";s:8:"radio_ts";s:6:"ts_all";s:14:"customSearchID";s:1:"4";s:17:"customSearchClass";s:36:"CRM_Contact_Form_Search_Custom_Group";}']);
57 $this->callAPISuccess('SavedSearch', 'create', ['form_values' => 'a:9:{s:5:"qfKey";s:32:"0123456789abcdef0123456789abcdef";s:13:"includeGroups";a:1:{i:0;s:1:"3";}s:13:"excludeGroups";a:0:{}s:11:"includeTags";a:0:{}s:11:"excludeTags";a:0:{}s:4:"task";s:2:"14";s:8:"radio_ts";s:6:"ts_all";s:14:"customSearchID";s:1:"4";s:17:"customSearchClass";s:36:"CRM_Contact_Form_Search_Custom_Group";}']);
58
59 $tag7 = $this->ids['Tag'][7] = $this->tagCreate(['name' => 'Test Tag 7', 'description' => 'Test Tag 7'])['id'];
60 $tag9 = $this->ids['Tag'][9] = $this->tagCreate(['name' => 'Test Tag 9', 'description' => 'Test Tag 9'])['id'];
61 $this->tagCreate(['name' => 'Test Tag 10']);
62 $groups = [
63 3 => ['name' => 'Test Group 3'],
64 4 => ['name' => 'Test Smart Group 4', 'saved_search_id' => 1],
65 5 => ['name' => 'Test Group 5'],
66 6 => ['name' => 'Test Smart Group 6', 'saved_search_id' => 2],
67 ];
68
69 foreach ($groups as $id => $group) {
70 $this->ids['Group'][$id] = $this->groupCreate(array_merge($group, ['title' => $group['name']]));
71 }
72 $individuals = [
73 ['first_name' => 'Test', 'last_name' => 'Test Contact 9', 'gender_id' => 1, 'prefix_id' => 1, 'suffix_id' => 1],
74 ['first_name' => 'Test', 'last_name' => 'Test Contact 10', 'gender_id' => 2, 'prefix_id' => 2, 'suffix_id' => 2, 'api.entity_tag.create' => ['tag_id' => $tag9]],
75 ['first_name' => 'Test', 'last_name' => 'Test Contact 11', 'gender_id' => 3, 'prefix_id' => 3, 'suffix_id' => 3, 'api.entity_tag.create' => ['tag_id' => $tag7]],
76 [
77 'first_name' => 'Test',
78 'last_name' => 'Test Contact 12',
79 'gender_id' => 3,
80 'prefix_id' => 4,
81 'suffix_id' => 4,
82 'api.entity_tag.create' => ['tag_id' => $tag9],
83 'api.entity_tag.create.2' => ['tag_id' => $tag7],
84 ],
85 ['first_name' => 'Test', 'last_name' => 'Test Contact 13', 'gender_id' => 2, 'prefix_id' => 2, 'suffix_id' => 2],
86 ['first_name' => 'Test', 'last_name' => 'Test Contact 14', 'gender_id' => 3, 'prefix_id' => 4, 'suffix_id' => 4, 'api.entity_tag.create' => ['tag_id' => $tag9]],
87 ['first_name' => 'Test', 'last_name' => 'Test Contact 15', 'gender_id' => 3, 'prefix_id' => 4, 'suffix_id' => 5, 'api.entity_tag.create' => ['tag_id' => $tag7]],
88 [
89 'first_name' => 'Test',
90 'last_name' => 'Test Contact 16',
91 'gender_id' => 3,
92 'prefix_id' => 4,
93 'suffix_id' => 6,
94 'api.entity_tag.create' => ['tag_id' => $tag9],
95 'api.entity_tag.create.2' => ['tag_id' => $tag7],
96 ],
97 ['first_name' => 'Test', 'last_name' => 'Test Contact 17', 'gender_id' => 2, 'prefix_id' => 4, 'suffix_id' => 7],
98 ['first_name' => 'Test', 'last_name' => 'Test Contact 18', 'gender_id' => 2, 'prefix_id' => 4, 'suffix_id' => 4, 'api.entity_tag.create' => ['tag_id' => $tag9]],
99 ['first_name' => 'Test', 'last_name' => 'Test Contact 19', 'gender_id' => 2, 'prefix_id' => 4, 'suffix_id' => 6, 'api.entity_tag.create.2' => ['tag_id' => $tag7]],
100 [
101 'first_name' => 'Test',
102 'last_name' => 'Test Contact 20',
103 'gender_id' => 1,
104 'prefix_id' => 4,
105 'suffix_id' => 6,
106 'api.entity_tag.create' => ['tag_id' => $tag9],
107 'api.entity_tag.create.2' => ['tag_id' => $tag7],
108 ],
109 ['first_name' => 'Test', 'last_name' => 'Test Contact 21', 'gender_id' => 3, 'prefix_id' => 1, 'suffix_id' => 6],
110 ['first_name' => 'Test', 'last_name' => 'Test Contact 22', 'gender_id' => 1, 'prefix_id' => 1, 'suffix_id' => 1, 'api.entity_tag.create' => ['tag_id' => $tag9]],
111 ['first_name' => 'Test', 'last_name' => 'Test Contact 23', 'gender_id' => 3, 'prefix_id' => 1, 'suffix_id' => 1, 'api.entity_tag.create' => ['tag_id' => $tag7]],
112 [
113 'first_name' => 'Test',
114 'last_name' => 'Test Contact 24',
115 'gender_id' => 3,
116 'prefix_id' => 3,
117 'suffix_id' => 2,
118 'api.entity_tag.create' => ['tag_id' => $tag9],
119 'api.entity_tag.create.2' => ['tag_id' => $tag7],
120 ],
121 ];
122 foreach ($individuals as $individual) {
123 $this->ids['Contact'][$individual['last_name']] = $this->individualCreate($individual);
124 }
125 $groupContacts = [
126 [5 => 13],
127 [5 => 14],
128 [5 => 15],
129 [5 => 16],
130 [5 => 21],
131 [5 => 22],
132 [5 => 23],
133 [5 => 24],
134 [3 => 17],
135 [3 => 18],
136 [3 => 19],
137 [3 => 20],
138 [3 => 21],
139 [3 => 22],
140 [3 => 23],
141 [3 => 24],
142 ];
143 foreach ($groupContacts as $group) {
144 $groupID = $this->ids['Group'][key($group)];
145 $contactID = $this->ids['Contact']['Test Contact ' . reset($group)];
146 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID, 'status' => 'Added']);
147 }
148
149 // We have migrated from a hard-coded dataset to a dynamic one but are still working with the same
150 // dataprovider at this stage -> wrangle.
151 foreach ($fv as $key => $value) {
152 $entity = ucfirst($key);
153 if (!array_key_exists($entity, $this->ids)) {
154 continue;
155 }
156 if (is_numeric($value)) {
157 $fv[$key] = $this->ids[$entity][$value];
158 }
159 elseif (!empty($value[0])) {
160 foreach ($value as $index => $oldGroup) {
161 $fv[$key][$index] = $this->ids[$entity][$oldGroup];
162 }
163 }
164 else {
165 foreach (array_keys($value) as $index) {
166 unset($fv[$key][$index]);
167 $fv[$key][$this->ids[$entity][$index]] = 1;
168 }
169 }
170 }
171
172 $params = CRM_Contact_BAO_Query::convertFormValues($fv);
173 $obj = new CRM_Contact_BAO_Query($params);
174
175 // let's set useGroupBy=true since we are listing contacts here who might belong to
176 // more than one group / tag / notes etc.
177 $obj->_useGroupBy = TRUE;
178
179 $dao = $obj->searchQuery();
180
181 $contacts = [];
182 while ($dao->fetch()) {
183 $contacts[] = $dao->contact_id;
184 }
185
186 sort($contacts, SORT_NUMERIC);
187
188 $expectedIDs = [];
189 foreach ($ids as $id) {
190 $expectedIDs[] = $this->ids['Contact']['Test Contact ' . $id];
191 }
192 $this->assertEquals($expectedIDs, $contacts);
193 }
194
195 /**
196 * Check that we get a successful result querying for home address.
197 * CRM-14263 search builder failure with search profile & address in criteria
198 *
199 * @throws \CRM_Core_Exception
200 */
201 public function testSearchProfileHomeCityCRM14263() {
202 $contactID = $this->individualCreate();
203 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
204 $this->callAPISuccess('address', 'create', [
205 'contact_id' => $contactID,
206 'city' => 'Cool City',
207 'location_type_id' => 1,
208 ]);
209 $params = [
210 0 => [
211 0 => 'city-1',
212 1 => '=',
213 2 => 'Cool City',
214 3 => 1,
215 4 => 0,
216 ],
217 ];
218 $returnProperties = [
219 'contact_type' => 1,
220 'contact_sub_type' => 1,
221 'sort_name' => 1,
222 ];
223
224 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
225 try {
226 $resultDAO = $queryObj->searchQuery();
227 $this->assertTrue($resultDAO->fetch());
228 }
229 catch (PEAR_Exception $e) {
230 $err = $e->getCause();
231 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
232
233 }
234 }
235
236 /**
237 * Check that we get a successful result querying for home address.
238 * CRM-14263 search builder failure with search profile & address in criteria
239 *
240 * @throws \CRM_Core_Exception
241 */
242 public function testSearchProfileHomeCityNoResultsCRM14263() {
243 $contactID = $this->individualCreate();
244 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
245 $this->callAPISuccess('address', 'create', [
246 'contact_id' => $contactID,
247 'city' => 'Cool City',
248 'location_type_id' => 1,
249 ]);
250 $params = [
251 0 => [
252 0 => 'city-1',
253 1 => '=',
254 2 => 'Dumb City',
255 3 => 1,
256 4 => 0,
257 ],
258 ];
259 $returnProperties = [
260 'contact_type' => 1,
261 'contact_sub_type' => 1,
262 'sort_name' => 1,
263 ];
264
265 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
266 try {
267 $resultDAO = $queryObj->searchQuery();
268 $this->assertFalse($resultDAO->fetch());
269 }
270 catch (PEAR_Exception $e) {
271 $err = $e->getCause();
272 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
273
274 }
275 }
276
277 /**
278 * Test searchPrimaryDetailsOnly setting.
279 *
280 * @throws \CRM_Core_Exception
281 */
282 public function testSearchPrimaryLocTypes() {
283 $contactID = $this->individualCreate();
284 $params = [
285 'contact_id' => $contactID,
286 'email' => 'primary@example.com',
287 'is_primary' => 1,
288 ];
289 $this->callAPISuccess('email', 'create', $params);
290
291 unset($params['is_primary']);
292 $params['email'] = 'secondary@team.com';
293 $this->callAPISuccess('email', 'create', $params);
294
295 foreach ([0, 1] as $searchPrimary) {
296 Civi::settings()->set('searchPrimaryDetailsOnly', $searchPrimary);
297
298 $params = [
299 0 => [
300 0 => 'email',
301 1 => 'LIKE',
302 2 => 'sEcondary@example.com',
303 3 => 0,
304 4 => 1,
305 ],
306 ];
307 $returnProperties = [
308 'contact_type' => 1,
309 'contact_sub_type' => 1,
310 'sort_name' => 1,
311 ];
312
313 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
314 $resultDAO = $queryObj->searchQuery();
315
316 if ($searchPrimary) {
317 $this->assertEquals($resultDAO->N, 0);
318 }
319 else {
320 //Assert secondary email gets included in search results.
321 while ($resultDAO->fetch()) {
322 $this->assertEquals('secondary@example.com', $resultDAO->email);
323 }
324 }
325
326 // API should always return primary email.
327 $result = $this->callAPISuccess('Contact', 'get', ['contact_id' => $contactID]);
328 $this->assertEquals('primary@example.com', $result['values'][$contactID]['email']);
329 }
330 }
331
332 /**
333 * Test created to prove failure of search on state when location
334 * display name is different form location name (issue 607)
335 *
336 * @throws \CRM_Core_Exception
337 */
338 public function testSearchOtherLocationUpperLower() {
339
340 $params = [
341 0 => [
342 0 => 'state_province-4',
343 1 => 'IS NOT EMPTY',
344 2 => '',
345 3 => 1,
346 4 => 0,
347 ],
348 ];
349 $returnProperties = [
350 'contact_type' => 1,
351 'contact_sub_type' => 1,
352 'sort_name' => 1,
353 'location' => [
354 'other' => [
355 'location_type' => 4,
356 'state_province' => 1,
357 ],
358 ],
359 ];
360
361 // update with the api does not work because it updates both the name and the
362 // the display_name. Plain SQL however does the job
363 CRM_Core_DAO::executeQuery('update civicrm_location_type set name=%2 where id=%1',
364 [
365 1 => [4, 'Integer'],
366 2 => ['other', 'String'],
367 ]);
368
369 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
370
371 $resultDAO = $queryObj->searchQuery();
372 $resultDAO->fetch();
373 }
374
375 /**
376 * CRM-14263 search builder failure with search profile & address in criteria.
377 *
378 * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
379 * so few tests detecting any change seems good here :-)
380 *
381 * @dataProvider getSearchProfileData
382 *
383 * @param array $params
384 * @param string $selectClause
385 * @param string $whereClause
386 *
387 * @throws \CRM_Core_Exception
388 */
389 public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $whereClause) {
390 $contactID = $this->individualCreate();
391 CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
392 $this->callAPISuccess('address', 'create', [
393 'contact_id' => $contactID,
394 'city' => 'Cool CITY',
395 'street_address' => 'Long STREET',
396 'location_type_id' => 1,
397 ]);
398 $returnProperties = [
399 'contact_type' => 1,
400 'contact_sub_type' => 1,
401 'sort_name' => 1,
402 ];
403 $expectedSQL = "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, " . $selectClause . " FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( " . $whereClause . " ) ) AND (contact_a.is_deleted = 0) ORDER BY `contact_a`.`sort_name` ASC, `contact_a`.`id` ";
404 $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
405 try {
406 $this->assertLike($expectedSQL, $queryObj->getSearchSQL());
407 list($select, $from, $where, $having) = $queryObj->query();
408 $dao = CRM_Core_DAO::executeQuery("$select $from $where $having");
409 $dao->fetch();
410 $this->assertEquals('Anderson, Anthony', $dao->sort_name);
411 }
412 catch (PEAR_Exception $e) {
413 $err = $e->getCause();
414 $this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
415
416 }
417 }
418
419 /**
420 * Get data sets to test for search.
421 */
422 public function getSearchProfileData() {
423 return [
424 [
425 [['city', '=', 'Cool City', 1, 0]],
426 "civicrm_address.city as `city`",
427 "civicrm_address.city = 'Cool City'",
428 ],
429 [
430 // Note that in the query 'long street' is lower cased. We eventually want to change that & not mess with the vars - it turns out
431 // it doesn't work on some charsets. However, the the lcasing affects more vars & we are looking to stagger removal of lcasing 'in case'
432 // (although we have been removing without blowback since 2017)
433 [['street_address', '=', 'Long Street', 1, 0]],
434 "civicrm_address.street_address as `street_address`",
435 "civicrm_address.street_address LIKE '%Long Street%'",
436 ],
437 ];
438 }
439
440 /**
441 * Test set up to test calling the query object per GroupContactCache BAO usage.
442 *
443 * CRM-17254 ensure that if only the contact_id is required other fields should
444 * not be appended.
445 */
446 public function testGroupContactCacheAddSearch() {
447 $returnProperties = ['contact_id'];
448 $params = [['group', 'IN', [1], 0, 0]];
449
450 $query = new CRM_Contact_BAO_Query(
451 $params, $returnProperties,
452 NULL, TRUE, FALSE, 1,
453 TRUE,
454 TRUE, FALSE
455 );
456
457 list($select) = $query->query(FALSE);
458 $this->assertEquals('SELECT contact_a.id as contact_id', $select);
459 }
460
461 /**
462 * Test smart groups with non-numeric don't fail on range queries.
463 *
464 * CRM-14720
465 *
466 * @throws \CRM_Core_Exception
467 */
468 public function testNumericPostal() {
469 // Precaution as hitting some inconsistent set up running in isolation vs in the suite.
470 CRM_Core_DAO::executeQuery('UPDATE civicrm_address SET postal_code = NULL');
471
472 $this->individualCreate(['api.address.create' => ['postal_code' => 5, 'location_type_id' => 'Main']]);
473 $this->individualCreate(['api.address.create' => ['postal_code' => 'EH10 4RB-889', 'location_type_id' => 'Main']]);
474 $this->individualCreate(['api.address.create' => ['postal_code' => '4', 'location_type_id' => 'Main']]);
475 $this->individualCreate(['api.address.create' => ['postal_code' => '6', 'location_type_id' => 'Main']]);
476 $this->individualCreate(['api.address.create' => ['street_address' => 'just a street', 'location_type_id' => 'Main']]);
477 $this->individualCreate(['api.address.create' => ['postal_code' => '12345678444455555555555555555555555555555555551314151617181920', 'location_type_id' => 'Main']]);
478
479 $params = [['postal_code_low', '=', 5, 0, 0]];
480 CRM_Contact_BAO_Query::convertFormValues($params);
481
482 $query = new CRM_Contact_BAO_Query(
483 $params, ['contact_id'],
484 NULL, TRUE, FALSE, 1,
485 TRUE,
486 TRUE, FALSE
487 );
488
489 $sql = $query->query(FALSE);
490 $result = CRM_Core_DAO::executeQuery(implode(' ', $sql));
491 $this->assertEquals(2, $result->N);
492
493 // We save this as a smart group and then load it. With mysql warnings on & CRM-14720 this
494 // results in mysql warnings & hence fatal errors.
495 /// I was unable to get mysql warnings to activate in the context of the unit tests - but
496 // felt this code still provided a useful bit of coverage as it runs the various queries to load
497 // the group & could generate invalid sql if a bug were introduced.
498 $groupParams = ['title' => 'postal codes', 'formValues' => $params, 'is_active' => 1];
499 $group = CRM_Contact_BAO_Group::createSmartGroup($groupParams);
500 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
501 }
502
503 /**
504 * Test searches are case insensitive.
505 *
506 * @throws \CRM_Core_Exception
507 */
508 public function testCaseInsensitive() {
509 $orgID = $this->organizationCreate(['organization_name' => 'BOb']);
510 $params = [
511 'display_name' => 'Minnie Mouse',
512 'first_name' => 'Minnie',
513 'last_name' => 'Mouse',
514 'employer_id' => $orgID,
515 'contact_type' => 'Individual',
516 'nick_name' => 'Mins',
517 ];
518 $this->callAPISuccess('Contact', 'create', $params);
519 unset($params['contact_type']);
520 foreach ($params as $key => $value) {
521 if ($key === 'employer_id') {
522 $searchParams = [['current_employer', '=', 'bob', 0, 1]];
523 }
524 else {
525 $searchParams = [[$key, '=', strtolower($value), 0, 1]];
526 }
527
528 $result = CRM_Contact_BAO_Query::apiQuery($searchParams);
529 $this->assertCount(1, $result[0], 'search for ' . $key);
530 $contact = reset($result[0]);
531 $this->assertEquals('Minnie Mouse', $contact['display_name']);
532 $this->assertEquals('BOb', $contact['current_employer']);
533 }
534 }
535
536 /**
537 * Test smart groups with non-numeric don't fail on equal queries.
538 *
539 * CRM-14720
540 *
541 * @throws \CRM_Core_Exception
542 */
543 public function testNonNumericEqualsPostal() {
544 $this->individualCreate(['api.address.create' => ['postal_code' => 5, 'location_type_id' => 'Main']]);
545 $this->individualCreate(['api.address.create' => ['postal_code' => 'EH10 4RB-889', 'location_type_id' => 'Main']]);
546 $this->individualCreate(['api.address.create' => ['postal_code' => '4', 'location_type_id' => 'Main']]);
547 $this->individualCreate(['api.address.create' => ['postal_code' => '6', 'location_type_id' => 'Main']]);
548
549 $params = [['postal_code', '=', 'EH10 4RB-889', 0, 0]];
550 CRM_Contact_BAO_Query::convertFormValues($params);
551
552 $query = new CRM_Contact_BAO_Query(
553 $params, ['contact_id'],
554 NULL, TRUE, FALSE, 1,
555 TRUE,
556 TRUE, FALSE
557 );
558
559 $sql = $query->query(FALSE);
560 $this->assertEquals("WHERE ( civicrm_address.postal_code = 'EH10 4RB-889' ) AND (contact_a.is_deleted = 0)", $sql[2]);
561 $result = CRM_Core_DAO::executeQuery(implode(' ', $sql));
562 $this->assertEquals(1, $result->N);
563
564 }
565
566 /**
567 * Test relationship description.
568 *
569 * @throws \CRM_Core_Exception
570 */
571 public function testRelationshipDescription() {
572 $relType = $this->callAPISuccess('RelationshipType', 'create', [
573 'name_a_b' => 'blah',
574 'name_b_a' => 'other blah',
575 ]);
576 $contactID_a = $this->individualCreate([], 1);
577 $contactID_b = $this->individualCreate([], 2);
578 $contactID_c = $this->individualCreate([], 3);
579 $contactID_d = $this->individualCreate([], 4);
580 $desc = uniqid('rel', TRUE);
581 $this->callAPISuccess('Relationship', 'create', [
582 'contact_id_a' => $contactID_a,
583 'contact_id_b' => $contactID_b,
584 'relationship_type_id' => $relType['id'],
585 'is_active' => 1,
586 'description' => $desc,
587 ]);
588 $this->callAPISuccess('Relationship', 'create', [
589 'contact_id_a' => $contactID_c,
590 'contact_id_b' => $contactID_d,
591 'relationship_type_id' => $relType['id'],
592 'is_active' => 1,
593 'description' => 'nothing of interest',
594 ]);
595 $params = [
596 ['relation_description', '=', substr($desc, 3, 18), 0, 0],
597 ];
598
599 $query = new CRM_Contact_BAO_Query($params);
600 $dao = $query->searchQuery();
601 // This is a little weird but seems consistent with the behavior of the search form in general.
602 // Technically there are 2 contacts who share a relationship with the description searched for,
603 // so one might expect the search form to return both of them instead of just Contact A... but it doesn't.
604 $this->assertEquals('1', $dao->N, 'Search query returns exactly 1 result?');
605 $this->assertTrue($dao->fetch(), 'Search query returns success?');
606 $this->assertEquals($contactID_a, $dao->contact_id, 'Search query returns contact A?');
607 }
608
609 /**
610 * Test non-reciprocal relationship.
611 *
612 * @throws \CRM_Core_Exception
613 */
614 public function testNonReciprocalRelationshipTargetGroupIsCorrectResults() {
615 $contactID_a = $this->individualCreate();
616 $contactID_b = $this->individualCreate();
617 $this->callAPISuccess('Relationship', 'create', [
618 'contact_id_a' => $contactID_a,
619 'contact_id_b' => $contactID_b,
620 'relationship_type_id' => 1,
621 'is_active' => 1,
622 ]);
623 // Create a group and add contact A to it.
624 $groupID = $this->groupCreate();
625 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID_a, 'status' => 'Added']);
626
627 // Add another (sans-relationship) contact to the group,
628 $contactID_c = $this->individualCreate();
629 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID_c, 'status' => 'Added']);
630
631 $params = [
632 [
633 0 => 'relation_type_id',
634 1 => 'IN',
635 2 =>
636 [
637 0 => '1_b_a',
638 ],
639 3 => 0,
640 4 => 0,
641 ],
642 [
643 0 => 'relation_target_group',
644 1 => 'IN',
645 2 =>
646 [
647 0 => $groupID,
648 ],
649 3 => 0,
650 4 => 0,
651 ],
652 ];
653
654 $query = new CRM_Contact_BAO_Query($params);
655 $dao = $query->searchQuery();
656 $this->assertEquals('1', $dao->N, "Search query returns exactly 1 result?");
657 $this->assertTrue($dao->fetch(), "Search query returns success?");
658 $this->assertEquals($contactID_b, $dao->contact_id, "Search query returns parent of contact A?");
659 }
660
661 /**
662 * @throws \CRM_Core_Exception
663 */
664 public function testReciprocalRelationshipTargetGroupIsCorrectResults() {
665 $contactID_a = $this->individualCreate();
666 $contactID_b = $this->individualCreate();
667 $this->callAPISuccess('Relationship', 'create', [
668 'contact_id_a' => $contactID_a,
669 'contact_id_b' => $contactID_b,
670 'relationship_type_id' => 2,
671 'is_active' => 1,
672 ]);
673 // Create a group and add contact A to it.
674 $groupID = $this->groupCreate();
675 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID_a, 'status' => 'Added']);
676
677 // Add another (sans-relationship) contact to the group,
678 $contactID_c = $this->individualCreate();
679 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $contactID_c, 'status' => 'Added']);
680
681 $params = [
682 [
683 0 => 'relation_type_id',
684 1 => 'IN',
685 2 =>
686 [
687 0 => '2_a_b',
688 ],
689 3 => 0,
690 4 => 0,
691 ],
692 [
693 0 => 'relation_target_group',
694 1 => 'IN',
695 2 =>
696 [
697 0 => $groupID,
698 ],
699 3 => 0,
700 4 => 0,
701 ],
702 ];
703
704 $query = new CRM_Contact_BAO_Query($params);
705 $dao = $query->searchQuery();
706 $this->assertEquals('1', $dao->N, "Search query returns exactly 1 result?");
707 $this->assertTrue($dao->fetch(), "Search query returns success?");
708 $this->assertEquals($contactID_b, $dao->contact_id, "Search query returns spouse of contact A?");
709 }
710
711 public function testReciprocalRelationshipTargetGroupUsesTempTable() {
712 $groupID = $this->groupCreate();
713 $params = [
714 [
715 0 => 'relation_type_id',
716 1 => 'IN',
717 2 =>
718 [
719 0 => '2_a_b',
720 ],
721 3 => 0,
722 4 => 0,
723 ],
724 [
725 0 => 'relation_target_group',
726 1 => 'IN',
727 2 =>
728 [
729 0 => $groupID,
730 ],
731 3 => 0,
732 4 => 0,
733 ],
734 ];
735 $sql = CRM_Contact_BAO_Query::getQuery($params);
736 $this->assertContains('INNER JOIN civicrm_tmp_e', $sql, "Query appears to use temporary table of compiled relationships?", TRUE);
737 }
738
739 /**
740 * Test relationship permission clause.
741 *
742 * @throws \CRM_Core_Exception
743 */
744 public function testRelationshipPermissionClause() {
745 $params = [['relation_type_id', 'IN', ['1_b_a'], 0, 0], ['relation_permission', 'IN', [2], 0, 0]];
746 $sql = CRM_Contact_BAO_Query::getQuery($params);
747 $this->assertContains('(civicrm_relationship.is_permission_a_b IN (2))', $sql);
748 }
749
750 /**
751 * Test Relationship Clause
752 *
753 * @throws \CRM_Core_Exception
754 */
755 public function testRelationshipClause() {
756 $today = date('Ymd');
757 $from1 = " FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
758 $from2 = " FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
759 $where1 = "WHERE ( (
760 civicrm_relationship.is_active = 1 AND
761 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
762 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
763 ) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8) ) AND (contact_a.is_deleted = 0)";
764 $where2 = "WHERE ( (
765 civicrm_relationship.is_active = 1 AND
766 ( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
767 ( civicrm_relationship.start_date IS NULL OR civicrm_relationship.start_date <= {$today} )
768 ) AND (contact_b.is_deleted = 0) AND civicrm_relationship.relationship_type_id IN (8,10) ) AND (contact_a.is_deleted = 0)";
769 // Test Traditional single select format
770 $params1 = [['relation_type_id', '=', '8_a_b', 0, 0]];
771 $query1 = new CRM_Contact_BAO_Query(
772 $params1, ['contact_id'],
773 NULL, TRUE, FALSE, 1,
774 TRUE,
775 TRUE, FALSE
776 );
777 $sql1 = $query1->query(FALSE);
778 $this->assertLike($from1, $sql1[1]);
779 $this->assertLike($where1, $sql1[2]);
780 // Test single relationship type selected in multiple select.
781 $params2 = [['relation_type_id', 'IN', ['8_a_b'], 0, 0]];
782 $query2 = new CRM_Contact_BAO_Query(
783 $params2, ['contact_id'],
784 NULL, TRUE, FALSE, 1,
785 TRUE,
786 TRUE, FALSE
787 );
788 $sql2 = $query2->query(FALSE);
789 $this->assertLike($from1, $sql2[1]);
790 $this->assertLike($where1, $sql2[2]);
791 // Test multiple relationship types selected.
792 $params3 = [['relation_type_id', 'IN', ['8_a_b', '10_a_b'], 0, 0]];
793 $query3 = new CRM_Contact_BAO_Query(
794 $params3, ['contact_id'],
795 NULL, TRUE, FALSE, 1,
796 TRUE,
797 TRUE, FALSE
798 );
799 $sql3 = $query3->query(FALSE);
800 $this->assertLike($from1, $sql3[1]);
801 $this->assertLike($where2, $sql3[2]);
802 // Test Multiple Relationship type selected where one doesn't actually exist.
803 $params4 = [['relation_type_id', 'IN', ['8_a_b', '10_a_b', '14_a_b'], 0, 0]];
804 $query4 = new CRM_Contact_BAO_Query(
805 $params4, ['contact_id'],
806 NULL, TRUE, FALSE, 1,
807 TRUE,
808 TRUE, FALSE
809 );
810 $sql4 = $query4->query(FALSE);
811 $this->assertLike($from1, $sql4[1]);
812 $this->assertLike($where2, $sql4[2]);
813
814 // Test Multiple b to a Relationship type .
815 $params5 = [['relation_type_id', 'IN', ['8_b_a', '10_b_a', '14_b_a'], 0, 0]];
816 $query5 = new CRM_Contact_BAO_Query(
817 $params5, ['contact_id'],
818 NULL, TRUE, FALSE, 1,
819 TRUE,
820 TRUE, FALSE
821 );
822 $sql5 = $query5->query(FALSE);
823 $this->assertLike($from2, $sql5[1]);
824 $this->assertLike($where2, $sql5[2]);
825 }
826
827 /**
828 * Test we can narrow a group get by status.
829 *
830 * @throws \CRM_Core_Exception
831 */
832 public function testGetByGroupWithStatus() {
833 $groupID = $this->groupCreate();
834 $this->groupContactCreate($groupID, 3);
835 $groupContactID = $this->callAPISuccessGetSingle('GroupContact', ['group_id' => $groupID, 'options' => ['limit' => 1]])['id'];
836 $this->callAPISuccess('GroupContact', 'create', ['id' => $groupContactID, 'status' => 'Removed']);
837 $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0], ['group_contact_status', 'IN', ['Removed' => 1], 0, 0]]);
838 $resultDAO = $queryObj->searchQuery();
839 $this->assertEquals(1, $resultDAO->N);
840
841 $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0], ['group_contact_status', 'IN', ['Added' => 1], 0, 0]]);
842 $resultDAO = $queryObj->searchQuery();
843 $this->assertEquals(2, $resultDAO->N);
844
845 $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0]]);
846 $resultDAO = $queryObj->searchQuery();
847 $this->assertEquals(2, $resultDAO->N);
848 }
849
850 /**
851 * Test we can narrow a group get by status.
852 *
853 * @throws \Exception
854 */
855 public function testGetByGroupWithStatusSmartGroup() {
856 $groupID = $this->smartGroupCreate();
857 // This means they are actually all hard-added, which is fine for this purpose.
858 $this->groupContactCreate($groupID, 3);
859 $groupContactID = $this->callAPISuccessGetSingle('GroupContact', ['group_id' => $groupID, 'options' => ['limit' => 1]])['id'];
860 $this->callAPISuccess('GroupContact', 'create', ['id' => $groupContactID, 'status' => 'Removed']);
861
862 $queryObj = new CRM_Contact_BAO_Query([['group', '=', $groupID, 0, 0], ['group_contact_status', 'IN', ['Removed' => 1], 0, 0]]);
863 $resultDAO = $queryObj->searchQuery();
864 $this->assertEquals(1, $resultDAO->N);
865 }
866
867 /**
868 * Test the group contact clause does not contain an OR.
869 *
870 * The search should return 3 contacts - 2 households in the smart group of
871 * Contact Type = Household and one Individual hard-added to it. The
872 * Household that meets both criteria should be returned once.
873 *
874 * @throws \Exception
875 */
876 public function testGroupClause() {
877 $this->householdCreate();
878 $householdID = $this->householdCreate();
879 $individualID = $this->individualCreate();
880 $groupID = $this->smartGroupCreate();
881 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $individualID, 'status' => 'Added']);
882 $this->callAPISuccess('GroupContact', 'create', ['group_id' => $groupID, 'contact_id' => $householdID, 'status' => 'Added']);
883
884 // Refresh the cache for test purposes. It would be better to alter to alter the GroupContact add function to add contacts to the cache.
885 CRM_Contact_BAO_GroupContactCache::clearGroupContactCache($groupID);
886
887 $sql = CRM_Contact_BAO_Query::getQuery(
888 [['group', 'IN', [$groupID], 0, 0]],
889 ['contact_id']
890 );
891
892 $dao = CRM_Core_DAO::executeQuery($sql);
893 $this->assertEquals(3, $dao->N);
894 $this->assertFalse(strstr($sql, ' OR '));
895
896 $sql = CRM_Contact_BAO_Query::getQuery(
897 [['group', 'IN', [$groupID], 0, 0]],
898 ['contact_id' => 1, 'group' => 1]
899 );
900
901 $dao = CRM_Core_DAO::executeQuery($sql);
902 $this->assertEquals(3, $dao->N);
903 $this->assertFalse(strstr($sql, ' OR '), 'Query does not include or');
904 while ($dao->fetch()) {
905 $this->assertTrue(($dao->groups == $groupID || $dao->groups == ',' . $groupID), $dao->groups . ' includes ' . $groupID);
906 }
907 }
908
909 /**
910 * CRM-19562 ensure that only ids are used for contact_id searching.
911 *
912 * @throws \CRM_Core_Exception
913 */
914 public function testContactIDClause() {
915 $params = [
916 ['mark_x_2', '=', 1, 0, 0],
917 ['mark_x_foo@example.com', '=', 1, 0, 0],
918 ];
919 $returnProperties = [
920 'sort_name' => 1,
921 'email' => 1,
922 'do_not_email' => 1,
923 'is_deceased' => 1,
924 'on_hold' => 1,
925 'display_name' => 1,
926 'preferred_mail_format' => 1,
927 ];
928 $numberOfContacts = 2;
929
930 try {
931 CRM_Contact_BAO_Query::apiQuery($params, $returnProperties, NULL, NULL, 0, $numberOfContacts);
932 }
933 catch (Exception $e) {
934 $this->assertEquals(
935 'One of parameters (value: foo@example.com) is not of the type Positive',
936 $e->getMessage()
937 );
938 $this->assertTrue(TRUE);
939 return;
940 }
941 $this->fail('Test failed for some reason which is not good');
942 }
943
944 /**
945 * Test the sorting on the contact ID query works.
946 *
947 * Checking for lack of fatal.
948 *
949 * @param string $sortOrder
950 * Param reflecting how sort is passed in.
951 * - 1_d is column 1 descending.
952 *
953 * @dataProvider getSortOptions
954 */
955 public function testContactIDQuery($sortOrder) {
956 $selector = new CRM_Contact_Selector(NULL, ['radio_ts' => 'ts_all'], NULL, ['sort_name' => 1]);
957 $selector->contactIDQuery([], $sortOrder);
958 }
959
960 /**
961 * Test the sorting on the contact ID query works with a profile search.
962 *
963 * Checking for lack of fatal.
964 *
965 * @throws \CRM_Core_Exception
966 */
967 public function testContactIDQueryProfileSearchResults() {
968 $profile = $this->callAPISuccess('UFGroup', 'create', ['group_type' => 'Contact', 'name' => 'search', 'title' => 'search']);
969 $this->callAPISuccess('UFField', 'create', [
970 'uf_group_id' => $profile['id'],
971 'field_name' => 'postal_code',
972 'field_type' => 'Contact',
973 'in_selector' => TRUE,
974 'is_searchable' => TRUE,
975 'label' => 'postal code',
976 'visibility' => 'Public Pages and Listings',
977 ]);
978 $selector = new CRM_Contact_Selector(NULL, ['radio_ts' => 'ts_all', 'uf_group_id' => $profile['id']], NULL, ['sort_name' => 1]);
979 $selector->contactIDQuery([], '2_d');
980 }
981
982 /**
983 * Get search options to reflect how a UI search would look.
984 *
985 * @return array
986 */
987 public function getSortOptions() {
988 return [
989 ['1_d'],
990 ['2_d'],
991 ['3_d'],
992 ['4_d'],
993 ['5_d'],
994 ['6_d'],
995 ];
996 }
997
998 /**
999 * Test the summary query does not add an acl clause when acls not enabled..
1000 *
1001 * @throws \CRM_Core_Exception
1002 */
1003 public function testGetSummaryQueryWithFinancialACLDisabled() {
1004 $this->createContributionsForSummaryQueryTests();
1005
1006 // Test the function directly
1007 $where = $from = NULL;
1008 $queryObject = new CRM_Contact_BAO_Query();
1009 $queryObject->appendFinancialTypeWhereAndFromToQueryStrings($where,
1010 $from);
1011 $this->assertEquals(NULL, $where);
1012 $this->assertEquals(NULL, $from);
1013
1014 // Test the function in action
1015 $queryObject = new CRM_Contact_BAO_Query([['contribution_source', '=', 'SSF', '', '']]);
1016 $summary = $queryObject->summaryContribution();
1017 $this->assertEquals([
1018 'total' => [
1019 'avg' => '$ 233.33',
1020 'amount' => '$ 1,400.00',
1021 'count' => 6,
1022 ],
1023 'cancel' => [
1024 'count' => 2,
1025 'amount' => '$ 100.00',
1026 'avg' => '$ 50.00',
1027 ],
1028 ], $summary);
1029 }
1030
1031 /**
1032 * Test the summary query accurately adds financial acl filters.
1033 *
1034 * @throws \CRM_Core_Exception
1035 */
1036 public function testGetSummaryQueryWithFinancialACLEnabled() {
1037 $where = $from = NULL;
1038 $this->createContributionsForSummaryQueryTests();
1039 $this->enableFinancialACLs();
1040 $this->createLoggedInUserWithFinancialACL();
1041
1042 // Test the function directly
1043 $queryObject = new CRM_Contact_BAO_Query();
1044 $queryObject->appendFinancialTypeWhereAndFromToQueryStrings($where,
1045 $from);
1046 $donationTypeID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation');
1047 $this->assertEquals(
1048 " LEFT JOIN civicrm_line_item li
1049 ON civicrm_contribution.id = li.contribution_id AND
1050 li.entity_table = 'civicrm_contribution' AND li.financial_type_id NOT IN ({$donationTypeID}) ", $from);
1051
1052 // Test the function in action
1053 $queryObject = new CRM_Contact_BAO_Query([['contribution_source', '=', 'SSF', '', '']]);
1054 $summary = $queryObject->summaryContribution();
1055 $this->assertEquals([
1056 'total' => [
1057 'avg' => '$ 200.00',
1058 'amount' => '$ 400.00',
1059 'count' => 2,
1060 ],
1061 'cancel' => [
1062 'count' => 1,
1063 'amount' => '$ 50.00',
1064 'avg' => '$ 50.00',
1065 ],
1066 ], $summary);
1067 $this->disableFinancialACLs();
1068 }
1069
1070 /**
1071 * Create contributions to test summary calculations.
1072 *
1073 * financial type | cancel_date |total_amount| source | line_item_financial_types |number_line_items| line_amounts
1074 * Donation |NULL | 100.00 |SSF | Donation | 1 | 100.00
1075 * Member Dues |NULL | 100.00 |SSF | Member Dues | 1 | 100.00
1076 * Donation |NULL | 300.00 |SSF | Event Fee,Event Fee | 2 | 200.00,100.00
1077 * Donation |NULL | 300.00 |SSF | Event Fee,Donation | 2 | 200.00,100.00
1078 * Donation |NULL | 300.00 |SSF | Donation,Donation | 2 | 200.00,100.00
1079 * Donation |2019-02-13 00:00:00 | 50.00 |SSF | Donation | 1 | 50.00
1080 * Member Dues |2019-02-13 00:00:00 | 50.00 |SSF | Member Dues | 1 | 50.00
1081 *
1082 * @throws \CRM_Core_Exception
1083 */
1084 protected function createContributionsForSummaryQueryTests() {
1085 $contactID = $this->individualCreate();
1086 $this->contributionCreate(['contact_id' => $contactID]);
1087 $this->contributionCreate([
1088 'contact_id' => $contactID,
1089 'total_amount' => 100,
1090 'financial_type_id' => 'Member Dues',
1091 ]);
1092 $eventFeeType = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Event Fee');
1093 $this->createContributionWithTwoLineItemsAgainstPriceSet(['contact_id' => $contactID, 'source' => 'SSF']);
1094 $this->createContributionWithTwoLineItemsAgainstPriceSet(['contact_id' => $contactID, 'source' => 'SSF'], [
1095 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
1096 $eventFeeType,
1097 ]);
1098 $this->createContributionWithTwoLineItemsAgainstPriceSet(['contact_id' => $contactID, 'source' => 'SSF'], [
1099 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
1100 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
1101 ]);
1102 $this->createContributionWithTwoLineItemsAgainstPriceSet(['contact_id' => $contactID, 'source' => 'SSF', 'financial_type_id' => $eventFeeType], [
1103 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
1104 CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
1105 ]);
1106 $this->contributionCreate([
1107 'contact_id' => $contactID,
1108 'total_amount' => 50,
1109 'contribution_status_id' => 'Cancelled',
1110 'cancel_date' => 'yesterday',
1111 ]);
1112 $this->contributionCreate([
1113 'contact_id' => $contactID,
1114 'total_amount' => 50,
1115 'contribution_status_id' => 'Cancelled',
1116 'cancel_date' => 'yesterday',
1117 'financial_type_id' => 'Member Dues',
1118 ]);
1119 }
1120
1121 /**
1122 * Test the options are handled for the qill.
1123 */
1124 public function testQillOptions() {
1125 $qill = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_BAO_Activity', 'activity_type_id', 2, '=');
1126 $this->assertEquals(['=', 'Phone Call'], $qill);
1127
1128 $qill = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Activity_BAO_Activity', 'priority_id', 2, '=');
1129 $this->assertEquals(['=', 'Normal'], $qill);
1130 }
1131
1132 /**
1133 * Test tests that a value on 'any entity' with the right metadata will be handled.
1134 *
1135 * @throws \CRM_Core_Exception
1136 */
1137 public function testGenericWhereHandling() {
1138 $query = new CRM_Contact_BAO_Query([['suffix_id', '=', 2, 0]]);
1139 $this->assertEquals("contact_a.suffix_id = 2", $query->_where[0][0]);
1140 $this->assertEquals('Individual Suffix = Sr.', $query->_qill[0][0]);
1141 $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
1142
1143 $query = new CRM_Contact_BAO_Query([['prefix_id', '=', 2, 0]]);
1144 $this->assertEquals('contact_a.prefix_id = 2', $query->_where[0][0]);
1145 $this->assertEquals('Individual Prefix = Ms.', $query->_qill[0][0]);
1146 $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
1147
1148 $query = new CRM_Contact_BAO_Query([['gender_id', '=', 2, 0]]);
1149 $this->assertEquals('contact_a.gender_id = 2', $query->_where[0][0]);
1150 $this->assertEquals('Gender = Male', $query->_qill[0][0]);
1151 $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
1152
1153 $query = new CRM_Contact_BAO_Query([['communication_style_id', '=', 2, 0]]);
1154 $this->assertEquals('contact_a.communication_style_id = 2', $query->_where[0][0]);
1155 $this->assertEquals('Communication Style = Familiar', $query->_qill[0][0]);
1156
1157 $query = new CRM_Contact_BAO_Query([['communication_style_id', '=', 2, 0]]);
1158 $this->assertEquals('contact_a.communication_style_id = 2', $query->_where[0][0]);
1159 $this->assertEquals('Communication Style = Familiar', $query->_qill[0][0]);
1160
1161 $query = new CRM_Contact_BAO_Query([['contact_type', '=', 'Household', 0]]);
1162 $this->assertEquals("contact_a.contact_type = 'Household'", $query->_where[0][0]);
1163 $this->assertEquals('Contact Type = Household', $query->_qill[0][0]);
1164
1165 $query = new CRM_Contact_BAO_Query([['on_hold', '=', 0, 0]]);
1166 $this->assertEquals('civicrm_email.on_hold = 0', $query->_where[0][0]);
1167 $this->assertEquals('On Hold = 0', $query->_qill[0][0]);
1168
1169 $query = new CRM_Contact_BAO_Query([['on_hold', '=', 1, 0]]);
1170 $this->assertEquals('civicrm_email.on_hold = 1', $query->_where[0][0]);
1171 $this->assertEquals('On Hold = 1', $query->_qill[0][0]);
1172
1173 $query = new CRM_Contact_BAO_Query([['world_region', '=', 3, 0]]);
1174 $this->assertEquals('civicrm_worldregion.id = 3', $query->_where[0][0]);
1175 $this->assertEquals('World Region = Middle East and North Africa', $query->_qill[0][0]);
1176 }
1177
1178 }