forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelectorTest.php
More file actions
856 lines (811 loc) · 37.8 KB
/
Copy pathSelectorTest.php
File metadata and controls
856 lines (811 loc) · 37.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Include parent class definition
*/
/**
* Test contact custom search functions
*
* @package CiviCRM
* @group headless
*/
class CRM_Contact_SelectorTest extends CiviUnitTestCase {
public function tearDown(): void {
$this->quickCleanup(['civicrm_tag', 'civicrm_entity_tag']);
parent::tearDown();
}
/**
* Test the query from the selector class is consistent with the dataset
* expectation.
*
* @param array $dataSet
* The data set to be tested. Note that when adding new datasets often only
* form_values and expected where clause will need changing.
*
* @dataProvider querySets
* @throws \Civi\Core\Exception\DBQueryException
*/
public function testSelectorQuery(array $dataSet): void {
$tag = $this->callAPISuccess('Tag', 'create', [
'name' => 'Test Tag Name',
'parent_id' => 1,
]);
if (!empty($dataSet['limitedPermissions'])) {
CRM_Core_Config::singleton()->userPermissionClass->permissions = [
'access CiviCRM',
'access deleted contacts',
];
}
$params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
$isDeleted = in_array(['deleted_contacts', '=', 1, 0, 0], $params, TRUE);
foreach ($dataSet['settings'] as $setting) {
$this->callAPISuccess('Setting', 'create', [$setting['name'] => $setting['value']]);
}
$selector = new CRM_Contact_Selector(
$dataSet['class'],
$dataSet['form_values'],
$params,
$dataSet['return_properties'],
$dataSet['action'],
$dataSet['includeContactIds'],
$dataSet['searchDescendentGroups'],
$dataSet['context']
);
$queryObject = $selector->getQueryObject();
// Make sure there is no fail on alphabet query.
$selector->alphabetQuery()->fetchAll();
$sql = $queryObject->query(FALSE, FALSE, FALSE, $isDeleted);
foreach ($dataSet['expected_query'] as $index => $queryString) {
$this->assertLike($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
}
if (!empty($dataSet['where_contains'])) {
$this->assertStringContainsString($this->strWrangle(str_replace('@tagid', $tag['id'], $dataSet['where_contains'])), $this->strWrangle($sql[2]));
}
// Ensure that search builder return individual contact as per criteria
if ($dataSet['context'] === 'builder') {
$contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']);
if ('Search builder behaviour for Activity' === $dataSet['description']) {
$this->callAPISuccess('Activity', 'create', [
'activity_type_id' => 'Meeting',
'subject' => 'Test',
'source_contact_id' => $contactID,
]);
$rows = CRM_Core_DAO::executeQuery(implode(' ', $sql))->fetchAll();
$this->assertCount(1, $rows);
$this->assertEquals($contactID, $rows[0]['source_contact_id']);
}
else {
$this->callAPISuccess('EntityTag', 'create', [
'entity_id' => $contactID,
'tag_id' => $tag['id'],
'entity_table' => 'civicrm_contact',
]);
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'is_primary' => 1,
'country_id' => 'IN',
]);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
$this->assertCount(1, $rows);
CRM_Core_DAO::reenableFullGroupByMode();
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
$sortChar = $selector->alphabetQuery()->fetchAll();
// sort name is stored in '<last_name>, <first_name>' format, as per which the first character would be B of Bond
$this->assertEquals('B', $sortChar[0]['sort_name']);
$this->assertEquals($contactID, key($rows));
CRM_Core_DAO::reenableFullGroupByMode();
$selector->getQueryObject()->getCachedContacts([$contactID], FALSE);
}
}
if (!empty($dataSet['limitedPermissions'])) {
$this->cleanUpAfterACLs();
}
}
/**
* Test advanced search results by uf_group_id.
*/
public function testSearchByProfile(): void {
//Create search profile for contacts.
$this->setTestEntityID('UFGroup', $this->callAPISuccess('UFGroup', 'create', [
'group_type' => 'Contact',
'name' => 'test_search_profile',
'title' => 'Test Search Profile',
'api.uf_field.create' => [
[
'field_name' => 'email',
'visibility' => 'Public Pages and Listings',
'field_type' => 'Contact',
'label' => 'Email',
'in_selector' => 1,
],
],
])['id'], 'test_profile');
$contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']);
//Put the email on hold.
$email = $this->callAPISuccess('Email', 'get', [
'sequential' => 1,
'contact_id' => $contactID,
]);
$this->callAPISuccess('Email', 'create', [
'id' => $email['id'],
'on_hold' => 1,
]);
$dataSet = [
'description' => 'Normal default behaviour',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['email' => 'mickey@mouseville.com', 'uf_group_id' => $this->ids['UFGroup']['test_profile']],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
];
$params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
// create CRM_Contact_Selector instance and set desired query params
$selector = new CRM_Contact_Selector(
$dataSet['class'],
$dataSet['form_values'],
$params,
$dataSet['return_properties'],
$dataSet['action'],
$dataSet['includeContactIds'],
$dataSet['searchDescendentGroups'],
$dataSet['context']
);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, '');
$this->assertCount(1, $rows);
$this->assertEquals($contactID, key($rows));
//Check if email column contains (On Hold) string.
foreach ($rows[$contactID] as $key => $value) {
if (str_contains($key, 'email')) {
$this->assertStringContainsString('(On Hold)', (string) $value);
}
}
}
/**
* Test the civicrm_prevnext_cache entry if it correctly stores the search query result
*/
public function testPrevNextCache(): void {
$contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']);
$dataSet = [
'description' => 'Normal default behaviour',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['email' => 'mickey@mouseville.com'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => 'default',
1 => 'default',
2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)",
],
];
$params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, []);
// create CRM_Contact_Selector instance and set desired query params
$selector = new CRM_Contact_Selector(
$dataSet['class'],
$dataSet['form_values'],
$params,
$dataSet['return_properties'],
$dataSet['action'],
$dataSet['includeContactIds'],
$dataSet['searchDescendentGroups'],
$dataSet['context']
);
// set cache key
$selector->setKey('abc');
// fetch row and check the result
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertCount(1, $rows);
$this->assertEquals($contactID, key($rows));
// build cache key and use to it to fetch prev-next cache record
$cacheKey = 'civicrm search abc';
$contacts = CRM_Utils_SQL_Select::from('civicrm_prevnext_cache')
->select(['entity_id1', 'cachekey'])
->where('cachekey = @key')
->param('key', $cacheKey)
->execute()
->fetchAll();
$this->assertCount(1, $contacts);
// check the prevNext record matches
$expectedEntry = [
'entity_id1' => $contactID,
'cachekey' => $cacheKey,
];
$this->checkArrayEquals($contacts[0], $expectedEntry);
}
/**
* Test that the prevnext cache handles duplicates and limit/offset sorting correctly.
*/
public function testPrevNextCacheDuplicatesWithSort(): void {
Civi::settings()->set('searchPrimaryDetailsOnly', 0);
$contactIDs = [];
// Create 30 contacts named Natalie
for ($i = 0; $i < 30; $i++) {
$contactID = $this->individualCreate(['first_name' => 'Natalie', 'last_name' => 'Test' . $i]);
$contactIDs[] = $contactID;
// Add multiple Home addresses
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'street_address' => "Home St A $i",
'city' => "City A",
'is_primary' => 1,
]);
$this->callAPISuccess('Address', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'street_address' => "Home St B $i",
'city' => "City B",
'is_primary' => 0,
]);
// Add multiple Home emails
$this->callAPISuccess('Email', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'email' => "natalie.home.a.$i@example.com",
'is_primary' => 1,
]);
$this->callAPISuccess('Email', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'email' => "natalie.home.b.$i@example.com",
'is_primary' => 0,
]);
// Add multiple Home phones
$this->callAPISuccess('Phone', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'phone' => "111-111-$i",
'is_primary' => 1,
]);
$this->callAPISuccess('Phone', 'create', [
'contact_id' => $contactID,
'location_type_id' => 'Home',
'phone' => "222-222-$i",
'is_primary' => 0,
]);
}
$formValues = [
'first_name' => 'Natalie',
'email' => 'natalie',
'phone_numeric' => '111',
];
$params = CRM_Contact_BAO_Query::convertFormValues($formValues, 0, FALSE, NULL, []);
$selector = new CRM_Contact_Selector(
'CRM_Contact_Selector',
$formValues,
$params,
NULL,
CRM_Core_Action::ADVANCED,
NULL,
FALSE,
'advanced'
);
$selector->setKey('natalie_test');
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 50, 'city');
$this->assertCount(30, $rows, 'Should return all 30 contacts even when they have duplicates in the prevnext cache due to left joins.');
}
/**
* Data sets for testing.
*/
public static function querySets(): array {
return [
'Empty group test' => [
[
'description' => 'Empty group test',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => [['contact_type', '=', 'Individual', 1, 0], ['group', 'IS NULL', '', 1, 0]],
'params' => [],
'return_properties' => NULL,
'context' => 'builder',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [],
],
],
'Tag Equals Test' => [
[
'description' => 'Tag Equals Test',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => [['contact_type', '=', 'Individual', 1, 0], ['tag', '=', '1', 1, 0]],
'params' => [],
'return_properties' => NULL,
'context' => 'builder',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [],
'where_contains' => 'tag_id IN ( @tagid )',
],
],
'Normal default behaviour' => [
[
'description' => 'Normal default behaviour',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['email' => 'mickey@mouseville.com'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => self::getDefaultFromString(),
2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND ( 1 ) AND (contact_a.is_deleted = 0)",
],
],
],
'Normal default + user added wildcard' => [
[
'description' => 'Normal default + user added wildcard',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['email' => '%mickey@mouseville.com', 'sort_name' => 'Mouse'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => self::getDefaultFromString(),
2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE '%Mouse%' ) OR ( civicrm_email.email LIKE '%Mouse%' ) ) ) ) AND ( 1 ) AND (contact_a.is_deleted = 0)",
],
],
],
'Site set to not pre-pend wildcard' => [
[
'description' => 'Site set to not pre-pend wildcard',
'class' => 'CRM_Contact_Selector',
'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => self::getDefaultFromString(),
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND ( 1 ) AND (contact_a.is_deleted = 0)",
],
],
],
'Site set to not pre-pend wildcard and check that trash value is respected' => [
[
'description' => 'Site set to not pre-pend wildcard and check that trash value is respected',
'class' => 'CRM_Contact_Selector',
'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse', 'deleted_contacts' => 1],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => self::getDefaultFromString(),
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND ( 1 ) AND (contact_a.is_deleted)",
],
],
],
'Ensure that the Join to the acl contact cache is correct' => [
[
'description' => 'Ensure that the Join to the acl contact cache is correct and that if we are searching in deleted contacts appropriate where clause is added',
'class' => 'CRM_Contact_Selector',
'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse', 'deleted_contacts' => 1],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'limitedPermissions' => TRUE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => 'FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id INNER JOIN civicrm_acl_contact_cache aclContactCache ON contact_a.id = aclContactCache.contact_id',
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND aclContactCache.user_id = 0 AND aclContactCache.domain_id = 1 AND (contact_a.is_deleted)",
],
],
],
'Ensure that the Join to the acl contact cache is correct, no deleted contacts' => [
[
'description' => 'Ensure that the Join to the acl contact cache is correct and that if we are not searching in the trash trashed contacts are not returned',
'class' => 'CRM_Contact_Selector',
'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
'form_values' => ['email' => 'mickey@mouseville.com', 'sort_name' => 'Mouse'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'limitedPermissions' => TRUE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => 'FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1) LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1) LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id INNER JOIN civicrm_acl_contact_cache aclContactCache ON contact_a.id = aclContactCache.contact_id',
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND aclContactCache.user_id = 0 AND aclContactCache.domain_id = 1 AND (contact_a.is_deleted = 0)",
],
],
],
'Use of quotes for exact string' => [
[
'description' => 'Use of quotes for exact string',
'use_case_comments' => 'This is something that was in the code but seemingly not working. No UI info on it though!',
'class' => 'CRM_Contact_Selector',
'settings' => [['name' => 'includeWildCardInName', 'value' => FALSE]],
'form_values' => ['email' => '"mickey@mouseville.com"', 'sort_name' => 'Mouse'],
'params' => [],
'return_properties' => NULL,
'context' => 'advanced',
'action' => CRM_Core_Action::ADVANCED,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => self::getDefaultSelectString(),
1 => self::getDefaultFromString(),
2 => "WHERE ( civicrm_email.email = 'mickey@mouseville.com' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND ( 1 ) AND (contact_a.is_deleted = 0)",
],
],
],
'Normal search builder behaviour' => [
[
'description' => 'Normal search builder behaviour',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['contact_type' => 'Individual', 'country' => ['IS NOT NULL' => 1]],
'params' => [],
'return_properties' => [
'contact_type' => 1,
'contact_sub_type' => 1,
'sort_name' => 1,
],
'context' => 'builder',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
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',
1 => ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )',
2 => 'WHERE ( contact_a.contact_type IN ("Individual") AND civicrm_address.country_id IS NOT NULL ) AND ( 1 ) AND (contact_a.is_deleted = 0)',
],
],
],
'Search builder behaviour for Activity' => [
[
'description' => 'Search builder behaviour for Activity',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['source_contact_id' => ['IS NOT NULL' => 1]],
'params' => [],
'return_properties' => [
'source_contact_id' => 1,
],
'context' => 'builder',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
0 => 'SELECT contact_a.id as contact_id, source_contact.id as source_contact_id',
],
'where_contains' => 'WHERE ( source_contact.id IS NOT NULL ) AND ( 1 ) AND (contact_a.is_deleted = 0)',
],
],
'Test display relationships' => [
[
'description' => 'Test display relationships',
'class' => 'CRM_Contact_Selector',
'settings' => [],
'form_values' => ['display_relationship_type' => '1_b_a'],
'return_properties' => NULL,
'params' => [],
'context' => 'advanced',
'action' => CRM_Core_Action::NONE,
'includeContactIds' => NULL,
'searchDescendentGroups' => FALSE,
'expected_query' => [
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.external_identifier as `external_identifier`, IF ( contact_a.contact_type = \'Individual\', NULL, contact_a.organization_name ) as organization_name, contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_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.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.source as `contact_source`, 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`, 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`',
2 => 'WHERE displayRelType.relationship_type_id = 1
AND displayRelType.is_active = 1
AND ( 1 ) AND (contact_a.is_deleted = 0)',
],
],
],
];
}
/**
* Test the contact ID query does not fail on country search.
*/
public function testContactIDQuery(): void {
$params = [
[
0 => 'country-1',
1 => '=',
2 => '1228',
3 => 1,
4 => 0,
],
];
$searchOBJ = new CRM_Contact_Selector(NULL);
$searchOBJ->contactIDQuery($params, '1_u');
}
/**
* Test the Search Builder using Non ASCII location type for email filter
*/
public function testSelectorQueryOnNonASCILocationType(): void {
$contactID = $this->individualCreate();
$locationTypeID = $this->locationTypeCreate([
'name' => 'Non ASCII Location Type',
'display_name' => 'Дом Location type',
'vcard_name' => 'Non ASCII Location Type',
'is_active' => 1,
]);
$this->callAPISuccess('Email', 'create', [
'contact_id' => $contactID,
'location_type_id' => $locationTypeID,
'email' => 'test@test.com',
]);
$selector = new CRM_Contact_Selector(
'CRM_Contact_Selector',
['email' => ['IS NOT NULL' => 1]],
[
[
0 => 'email-' . $locationTypeID,
1 => 'IS NOT NULL',
2 => NULL,
3 => 1,
4 => 0,
],
],
[
'contact_type' => 1,
'contact_sub_type' => 1,
'sort_name' => 1,
'location' => [
'Non ASCII Location Type' => [
'location_type' => $locationTypeID,
'email' => 1,
],
],
],
CRM_Core_Action::NONE,
NULL,
FALSE,
'builder'
);
$sql = $selector->getQueryObject()->query();
$expectedQuery = [
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-address`.id as `Non_ASCII_Location_Type-address_id`, `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`',
// @TODO these FROM clause doesn't matches due to extra spaces or special character
2 => 'WHERE ( ( `Non_ASCII_Location_Type-email`.email IS NOT NULL ) ) AND ( 1 ) AND (contact_a.is_deleted = 0)',
];
foreach ($expectedQuery as $index => $queryString) {
$this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
}
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertCount(1, $rows);
$this->assertEquals($contactID, key($rows));
$this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']);
}
/**
* Test the value use in where clause if it's case sensitive or not against each MySQL operators
*/
public function testWhereClauseByOperator(): void {
$contactID = $this->individualCreate(['first_name' => 'Adam']);
$filters = [
'IS NOT NULL' => 1,
'=' => 'Adam',
'LIKE' => '%Ad%',
'RLIKE' => '^A[a-z]{3}$',
'IN' => ['IN' => ['Adam']],
];
$filtersByWhereClause = [
// doesn't matter
'IS NOT NULL' => '( contact_a.first_name IS NOT NULL )',
// case sensitive check
'=' => "( contact_a.first_name = 'Adam' )",
// case insensitive check
'LIKE' => "( contact_a.first_name LIKE '%Ad%' )",
// case sensitive check
'RLIKE' => "( CAST(contact_a.first_name AS BINARY) RLIKE BINARY '^A[a-z]{3}$' )",
// case sensitive check
'IN' => '( contact_a.first_name IN ("Adam") )',
];
foreach ($filters as $op => $filter) {
$selector = new CRM_Contact_Selector(
'CRM_Contact_Selector',
['first_name' => [$op => $filter]],
[
[
0 => 'first_name',
1 => $op,
2 => $filter,
3 => 1,
4 => 0,
],
],
[],
CRM_Core_Action::NONE,
NULL,
FALSE,
'builder'
);
$sql = $selector->getQueryObject()->query();
$this->assertEquals(TRUE, strpos($sql[2], $filtersByWhereClause[$op]));
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertCount(1, $rows);
$this->assertEquals($contactID, key($rows));
}
}
/**
* Test if custom table is added in from clause when
* search results are ordered by a custom field.
*
* @throws \CRM_Core_Exception
*/
public function testSelectorQueryOrderByCustomField(): void {
//Search for any params.
$params = [
[
0 => 'country-1',
1 => '=',
2 => '1228',
3 => 1,
4 => 0,
],
];
//Create a test custom group and field.
$customGroup = $this->callAPISuccess('CustomGroup', 'create', [
'title' => 'test custom group',
'extends' => 'Individual',
]);
$cgTableName = $customGroup['values'][$customGroup['id']]['table_name'];
$customField = $this->callAPISuccess('CustomField', 'create', [
'custom_group_id' => $customGroup['id'],
'label' => 'test field',
'html_type' => 'Text',
]);
$customFieldId = $customField['id'];
//Sort by the custom field created above.
$sortParams = [
1 => [
'name' => 'test field',
'sort' => "custom_$customFieldId",
],
];
$sort = new CRM_Utils_Sort($sortParams, '1_d');
//Form a query to order by a custom field.
$query = new CRM_Contact_BAO_Query($params,
CRM_Contact_BAO_Query::NO_RETURN_PROPERTIES,
NULL, FALSE, FALSE, 1,
FALSE, TRUE, TRUE, NULL,
'AND'
);
$query->searchQuery(0, 0, $sort);
//Check if custom table is included in $query->_tables.
$this->assertArrayHasKey($cgTableName, $query->_tables);
//Assert if from clause joins the custom table.
$this->assertNotFalse(strpos($query->_fromClause, $cgTableName));
$this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]);
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]);
}
/**
* Check where clause of a date custom field when 'IS NOT EMPTY' operator is used
*/
public function testCustomDateField(): void {
$contactID = $this->individualCreate();
//Create a test custom group and field.
$customGroup = $this->callAPISuccess('CustomGroup', 'create', [
'title' => 'test custom group',
'extends' => 'Individual',
]);
$customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name'];
$createdField = $this->callAPISuccess('customField', 'create', [
'data_type' => 'Date',
'html_type' => 'Select Date',
'date_format' => 'd M yy',
'time_format' => 1,
'label' => 'test field',
'custom_group_id' => $customGroup['id'],
]);
$customFieldColumnName = $createdField['values'][$createdField['id']]['column_name'];
$this->callAPISuccess('Contact', 'create', [
'id' => $contactID,
'custom_' . $createdField['id'] => date('YmdHis'),
]);
$selector = new CRM_Contact_Selector(
'CRM_Contact_Selector',
['custom_' . $createdField['id'] => ['IS NOT EMPTY' => 1]],
[
[
0 => 'custom_' . $createdField['id'],
1 => 'IS NOT NULL',
2 => 1,
3 => 1,
4 => 0,
],
],
[],
CRM_Core_Action::NONE,
NULL,
FALSE,
'builder'
);
$whereClause = $selector->getQueryObject()->query()[2];
$expectedClause = sprintf('( %s.%s IS NOT NULL )', $customGroupTableName, $customFieldColumnName);
// test the presence of expected date clause
$this->assertEquals(TRUE, strpos($whereClause, $expectedClause));
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertCount(1, $rows);
}
/**
* Get the default select string since this is generally consistent.
*/
public static function getDefaultSelectString(): string {
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`,'
. ' contact_a.display_name as `display_name`, contact_a.external_identifier as `external_identifier`,'
. ' IF ( contact_a.contact_type = \'Individual\', NULL, contact_a.organization_name ) as organization_name,'
. ' contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_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.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.source as `contact_source`,'
. ' 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`,'
. ' 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`';
}
/**
* Get the default from string since this is generally consistent.
*/
public static function getDefaultFromString(): string {
return ' FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )'
. ' LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id ) '
. ' LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)'
. ' LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)'
. ' LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1) '
. 'LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id ';
}
/**
* Strangle strings into a more matchable format.
*
* @param string $string
*
* @return string
*/
public function strWrangle(string $string): string {
return trim(str_replace(' ', ' ', $string));
}
}