-
-
Notifications
You must be signed in to change notification settings - Fork 827
/
Copy pathContactTest.php
4855 lines (4482 loc) · 175 KB
/
ContactTest.php
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
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* File for the TestContact class.
*
* (PHP 5)
*
* @author Walt Haas <walt@dharmatech.org> (801) 534-1262
* @copyright Copyright CiviCRM LLC (C) 2009
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
* GNU Affero General Public License version 3
* @version $Id: ContactTest.php 31254 2010-12-15 10:09:29Z eileen $
* @package CiviCRM
*
* This file is part of CiviCRM
*
* CiviCRM is free software; you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* CiviCRM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
use Civi\Api4\Contact;
/**
* Test APIv3 civicrm_contact* functions
*
* @package CiviCRM_APIv3
* @subpackage API_Contact
* @group headless
*/
class api_v3_ContactTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;
protected $_entity;
protected $_params;
protected $_contactID;
protected $_financialTypeId = 1;
/**
* Entity to be extended.
*
* @var string
*/
protected $entity = 'Contact';
/**
* Test setup for every test.
*
* Connect to the database, truncate the tables that will be used
* and redirect stdin to a temporary file
*/
public function setUp(): void {
// Connect to the database.
parent::setUp();
$this->_entity = 'contact';
$this->_params = [
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
];
}
/**
* Restore the DB for the next test.
*
* @throws \Exception
*/
public function tearDown(): void {
$this->_apiversion = 3;
$this->callAPISuccess('Setting', 'create', ['includeOrderByClause' => TRUE]);
// truncate a few tables
$tablesToTruncate = [
'civicrm_email',
'civicrm_website',
'civicrm_relationship',
'civicrm_uf_match',
'civicrm_file',
'civicrm_entity_file',
'civicrm_phone',
'civicrm_address',
'civicrm_acl_contact_cache',
'civicrm_group',
'civicrm_group_contact',
'civicrm_group_contact_cache',
'civicrm_saved_search',
'civicrm_prevnext_cache',
];
$this->quickCleanUpFinancialEntities();
$this->deleteNonDefaultRelationshipTypes();
$this->restoreMembershipTypes();
$this->quickCleanup($tablesToTruncate, TRUE);
parent::tearDown();
}
/**
* Test civicrm_contact_create.
*
* Verify that attempt to create individual contact with only
* first and last names succeeds
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*
*/
public function testAddCreateIndividual(int $version): void {
$this->_apiversion = $version;
$oldCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
$params = [
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
];
$contact = $this->callAPISuccess('contact', 'create', $params);
$this->assertIsNumeric($contact['id']);
$this->assertTrue($contact['id'] > 0);
$newCount = CRM_Core_DAO::singleValueQuery('select count(*) from civicrm_contact');
$this->assertEquals($oldCount + 1, $newCount);
$this->assertDBState('CRM_Contact_DAO_Contact',
$contact['id'],
$params
);
}
/**
* Test that it is possible to prevent cache clearing via option.
*
* Cache clearing is bypassed if 'options' => array('do_not_reset_cache' => 1 is used.
*
* @throws \CRM_Core_Exception
*/
public function testCreateIndividualNoCacheClear(): void {
$contact = $this->callAPISuccess('contact', 'create', $this->_params);
$smartGroupParams = ['form_values' => ['contact_type' => ['IN' => ['Household']]]];
$savedSearch = CRM_Contact_BAO_SavedSearch::create($smartGroupParams);
$groupID = $this->groupCreate(['saved_search_id' => $savedSearch->id]);
$this->putGroupContactCacheInClearableState($groupID, $contact);
$this->callAPISuccess('contact', 'create', ['id' => $contact['id']]);
$this->assertEquals(0, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_group_contact_cache'));
// Rinse & repeat, but with the option.
$this->putGroupContactCacheInClearableState($groupID, $contact);
CRM_Core_Config::setPermitCacheFlushMode(FALSE);
$this->callAPISuccess('contact', 'create', ['id' => $contact['id']]);
$this->assertEquals(1, CRM_Core_DAO::singleValueQuery('SELECT count(*) FROM civicrm_group_contact_cache'));
CRM_Core_Config::setPermitCacheFlushMode(TRUE);
}
/**
* Test for international string acceptance (CRM-10210).
* Requires the database to be in utf8.
*
* @dataProvider getInternationalStrings
*
* @param string $string
* String to be tested.
*
* Bool to see if we should check charset.
*
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*/
public function testInternationalStrings(string $string): void {
$this->callAPISuccess('Contact', 'create', array_merge(
$this->_params,
['first_name' => $string]
));
$result = $this->callAPISuccessGetSingle('Contact', ['first_name' => $string, 'return' => 'first_name']);
$this->assertEquals($string, $result['first_name']);
$this->callAPISuccess('Contact', 'create', [
'organization_name' => $string,
'contact_type' => 'Organization',
]);
$this->validateContactField('organization_name', $string, NULL, ['organization_name', '=', $string]);
}
/**
* Get international string data for testing against api calls.
*/
public function getInternationalStrings(): array {
$invocations = [];
$invocations[] = ['Scarabée'];
$invocations[] = ['Iñtërnâtiônàlizætiøn'];
$invocations[] = ['これは日本語のテキストです。読めますか'];
$invocations[] = ['देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।'];
return $invocations;
}
/**
* Test civicrm_contact_create.
*
* Verify that preferred language can be set.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testAddCreateIndividualWithPreferredLanguage(int $version): void {
$this->_apiversion = $version;
$params = [
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
'preferred_language' => 'es_ES',
];
$contact = $this->callAPISuccess('Contact', 'create', $params);
$this->getAndCheck($params, $contact['id'], 'Contact');
}
/**
* Test civicrm_contact_create with sub-types.
*
* Verify that sub-types are created successfully and not deleted by
* subsequent updates.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testIndividualSubType(int $version): void {
$this->_apiversion = $version;
$params = [
'first_name' => 'test abc',
'contact_type' => 'Individual',
'last_name' => 'test xyz',
'contact_sub_type' => ['Student', 'Staff'],
];
$contact = $this->callAPISuccess('contact', 'create', $params);
$cid = $contact['id'];
$params = [
'id' => $cid,
'middle_name' => 'foo',
];
$this->callAPISuccess('contact', 'create', $params);
$contact = $this->callAPISuccess('contact', 'get', ['id' => $cid]);
$this->assertEquals(['Student', 'Staff'], $contact['values'][$cid]['contact_sub_type']);
$this->callAPISuccess('Contact', 'create', [
'id' => $cid,
'contact_sub_type' => [],
]);
$contact = $this->callAPISuccess('contact', 'get', ['id' => $cid]);
$this->assertEmpty($contact['values'][$cid]['contact_sub_type']);
}
/**
* Verify that we can retrieve contacts of different sub types
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testGetMultipleContactSubTypes(int $version): void {
$this->_apiversion = $version;
// This test presumes that there are no parents or students in the dataset
// create a student
$student = $this->callAPISuccess('contact', 'create', [
'email' => 'student@example.com',
'contact_type' => 'Individual',
'contact_sub_type' => 'Student',
]);
// create a parent
$parent = $this->callAPISuccess('contact', 'create', [
'email' => 'parent@example.com',
'contact_type' => 'Individual',
'contact_sub_type' => 'Parent',
]);
// create a parent
$this->callAPISuccess('contact', 'create', [
'email' => 'parent@example.com',
'contact_type' => 'Individual',
]);
// get all students and parents
$result = $this->callAPISuccess('Contact', 'get', ['return' => 'id', 'contact_sub_type' => ['IN' => ['Parent', 'Student']]])['values'];
// check that we retrieved the student and the parent.
// On MySQL 8 this can have different order in the array as there is no specific order set.
$this->assertArrayHasKey($student['id'], $result);
$this->assertArrayHasKey($parent['id'], $result);
$this->assertCount(2, $result);
}
/**
* Verify that attempt to create contact with empty params fails.
*/
public function testCreateEmptyContact(): void {
$this->callAPIFailure('contact', 'create', []);
}
/**
* Verify that attempt to create contact with bad contact type fails.
*/
public function testCreateBadTypeContact(): void {
$params = [
'email' => 'man1@yahoo.com',
'contact_type' => 'Does not Exist',
];
$this->callAPIFailure('contact', 'create', $params, "'Does not Exist' is not a valid option for field contact_type");
}
/**
* Verify that attempt to create individual contact without required fields fails.
*/
public function testCreateBadRequiredFieldsIndividual(): void {
$params = [
'middle_name' => 'This field is not required',
'contact_type' => 'Individual',
];
$this->callAPIFailure('contact', 'create', $params);
}
/**
* Verify that attempt to create household contact without required fields fails.
*/
public function testCreateBadRequiredFieldsHousehold(): void {
$params = [
'middle_name' => 'This field is not required',
'contact_type' => 'Household',
];
$this->callAPIFailure('contact', 'create', $params);
}
/**
* Test required field check.
*
* Verify that attempt to create organization contact without required fields fails.
*/
public function testCreateBadRequiredFieldsOrganization(): void {
$params = [
'middle_name' => 'This field is not required',
'contact_type' => 'Organization',
];
$this->callAPIFailure('contact', 'create', $params);
}
/**
* Verify that attempt to create individual contact with only an email succeeds.
*
* @throws \CRM_Core_Exception
*/
public function testCreateEmailIndividual(): void {
$primaryEmail = 'man3@yahoo.com';
$notPrimaryEmail = 'man4@yahoo.com';
$params = [
'email' => $primaryEmail,
'contact_type' => 'Individual',
'location_type_id' => 1,
];
$contact1 = $this->callAPISuccess('contact', 'create', $params);
$this->assertEquals(3, $contact1['id']);
$email1 = $this->callAPISuccess('email', 'get', ['contact_id' => $contact1['id']]);
$this->assertEquals(1, $email1['count']);
$this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
$this->callAPISuccess('email', 'create', ['contact_id' => $contact1['id'], 'is_primary' => 0, 'email' => $notPrimaryEmail]);
// Case 1: Check with criteria primary 'email' => array('IS NOT NULL' => 1)
$this->callAPISuccess('contact', 'get', ['email' => ['IS NOT NULL' => 1]]);
$this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
// Case 2: Check with criteria primary 'email' => array('<>' => '')
$this->callAPISuccess('contact', 'get', ['email' => ['<>' => '']]);
$this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
// Case 3: Check with email_id='primary email id'
$result = $this->callAPISuccessGetSingle('contact', ['email_id' => $email1['id']]);
$this->assertEquals($contact1['id'], $result['id']);
// Check no wildcard is appended
$this->callAPISuccessGetCount('Contact', ['email' => 'man3@yahoo.co'], 0);
$this->callAPISuccess('contact', 'delete', $contact1);
}
/**
* Test creating individual by name.
*
* Verify create individual contact with only first and last names succeeds.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testCreateNameIndividual(int $version): void {
$this->_apiversion = $version;
$params = [
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
];
$this->callAPISuccess('contact', 'create', $params);
}
/**
* Test creating individual by display_name.
*
* Display name & sort name should be set.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testCreateDisplayNameIndividual(int $version): void {
$this->_apiversion = $version;
$params = [
'display_name' => 'abc1',
'contact_type' => 'Individual',
];
$contact = $this->callAPISuccess('contact', 'create', $params);
$params['sort_name'] = 'abc1';
$this->getAndCheck($params, $contact['id'], 'contact');
}
/**
* Test that name searches are case insensitive.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testGetNameVariantsCaseInsensitive(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess('contact', 'create', [
'display_name' => 'Abc1',
'contact_type' => 'Individual',
]);
$this->callAPISuccessGetSingle('Contact', ['display_name' => 'aBc1']);
$this->callAPISuccessGetSingle('Contact', ['sort_name' => 'aBc1']);
Civi::settings()->set('includeNickNameInName', TRUE);
$this->callAPISuccessGetSingle('Contact', ['display_name' => 'aBc1']);
$this->callAPISuccessGetSingle('Contact', ['sort_name' => 'aBc1']);
Civi::settings()->set('includeNickNameInName', FALSE);
}
/**
* Test old keys still work.
*
* Verify that attempt to create individual contact with
* first and last names and old key values works
*
* @throws \CRM_Core_Exception
*/
public function testCreateNameIndividualOldKeys(): void {
$params = [
'individual_prefix' => 'Dr.',
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
'individual_suffix' => 'Jr.',
];
$contact = $this->callAPISuccess('contact', 'create', $params);
$result = $this->callAPISuccess('contact', 'getsingle', ['id' => $contact['id']]);
$this->assertArrayKeyExists('prefix_id', $result);
$this->assertArrayKeyExists('suffix_id', $result);
$this->assertArrayKeyExists('gender_id', $result);
$this->assertEquals(4, $result['prefix_id']);
$this->assertEquals(1, $result['suffix_id']);
}
/**
* Test preferred keys work.
*
* Verify that attempt to create individual contact with
* first and last names and old key values works
*
* @throws \CRM_Core_Exception
*/
public function testCreateNameIndividualRecommendedKeys2(): void {
$params = [
'prefix_id' => 'Dr.',
'first_name' => 'abc1',
'contact_type' => 'Individual',
'last_name' => 'xyz1',
'suffix_id' => 'Jr.',
'gender_id' => 'Male',
];
$contact = $this->callAPISuccess('contact', 'create', $params);
$result = $this->callAPISuccess('contact', 'getsingle', ['id' => $contact['id']]);
$this->assertArrayKeyExists('prefix_id', $result);
$this->assertArrayKeyExists('suffix_id', $result);
$this->assertArrayKeyExists('gender_id', $result);
$this->assertEquals(4, $result['prefix_id']);
$this->assertEquals(1, $result['suffix_id']);
}
/**
* Test household name is sufficient for create.
*
* Verify that attempt to create household contact with only
* household name succeeds
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testCreateNameHousehold(int $version): void {
$this->_apiversion = $version;
$params = [
'household_name' => 'The abc Household',
'contact_type' => 'Household',
];
$this->callAPISuccess('contact', 'create', $params);
}
/**
* Test organization name is sufficient for create.
*
* Verify that attempt to create organization contact with only
* organization name succeeds.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testCreateNameOrganization(int $version): void {
$this->_apiversion = $version;
$params = [
'organization_name' => 'The abc Organization',
'contact_type' => 'Organization',
];
$this->callAPISuccess('contact', 'create', $params);
}
/**
* Verify that attempt to create organization contact without organization name fails.
*/
public function testCreateNoNameOrganization(): void {
$params = [
'first_name' => 'The abc Organization',
'contact_type' => 'Organization',
];
$this->callAPIFailure('contact', 'create', $params);
}
/**
* Check that permissions on API key are restricted (CRM-18112).
*
* @param int $version
*
* @throws \CRM_Core_Exception
*
* @dataProvider versionThreeAndFour
*/
public function testCreateApiKey(int $version): void {
$this->_apiversion = $version;
$config = CRM_Core_Config::singleton();
$contactId = $this->individualCreate([
'first_name' => 'A',
'last_name' => 'B',
]);
// Allow edit -- because permissions aren't being checked
$config->userPermissionClass->permissions = [];
$result = $this->callAPISuccess('Contact', 'create', [
'id' => $contactId,
'api_key' => 'original',
]);
$this->assertEquals('original', $result['values'][$contactId]['api_key']);
// Allow edit -- because we have adequate permission
$config->userPermissionClass->permissions = ['access CiviCRM', 'edit all contacts', 'edit api keys'];
$result = $this->callAPISuccess('Contact', 'create', [
'check_permissions' => 1,
'id' => $contactId,
'api_key' => 'abcd1234',
]);
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);
// Disallow edit -- because we don't have permission
$config->userPermissionClass->permissions = ['access CiviCRM', 'edit all contacts'];
$result = $this->callAPIFailure('Contact', 'create', [
'check_permissions' => 1,
'id' => $contactId,
'api_key' => 'defg4321',
]);
$this->assertMatchesRegularExpression(';Permission denied to modify api key;', $result['error_message']);
// Return everything -- because permissions are not being checked
$config->userPermissionClass->permissions = [];
$result = $this->callAPISuccess('Contact', 'create', [
'id' => $contactId,
'first_name' => 'A2',
]);
$this->assertEquals('A2', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);
// Return everything -- because we have adequate permission
$config->userPermissionClass->permissions = ['access CiviCRM', 'edit all contacts', 'edit api keys'];
$result = $this->callAPISuccess('Contact', 'create', [
'check_permissions' => 1,
'id' => $contactId,
'first_name' => 'A3',
]);
$this->assertEquals('A3', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertEquals('abcd1234', $result['values'][$contactId]['api_key']);
// Should also be returned via join
$joinResult = $this->callAPISuccessGetSingle('Email', [
'check_permissions' => 1,
'contact_id' => $contactId,
'return' => 'contact_id.api_key',
]);
$this->assertEquals('abcd1234', $joinResult['contact_id.api_key']);
// Restricted return -- because we don't have permission
$config->userPermissionClass->permissions = ['access CiviCRM', 'view all contacts', 'edit all contacts'];
$result = $this->callAPISuccess('Contact', 'create', [
'check_permissions' => 1,
'id' => $contactId,
'first_name' => 'A4',
]);
$this->assertEquals('A4', $result['values'][$contactId]['first_name']);
$this->assertEquals('B', $result['values'][$contactId]['last_name']);
$this->assertTrue(empty($result['values'][$contactId]['api_key']));
// Should also be restricted via join
$joinResult = $this->callAPISuccessGetSingle('Email', [
'check_permissions' => 1,
'contact_id' => $contactId,
'return' => ['email', 'contact_id.api_key'],
]);
$this->assertTrue(empty($joinResult['contact_id.api_key']));
}
/**
* Check with complete array + custom field.
*
* Note that the test is written on purpose without any
* variables specific to participant so it can be replicated into other entities
* and / or moved to the automated test suite
*
* @param int $version
*
* @dataProvider versionThreeAndFour
*/
public function testCreateWithCustom(int $version): void {
$this->_apiversion = $version;
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$params = $this->_params;
$params['custom_' . $ids['custom_field_id']] = 'custom string';
$result = $this->callAPISuccess($this->_entity, 'create', $params);
$check = $this->callAPISuccess($this->_entity, 'get', [
'return.custom_' . $ids['custom_field_id'] => 1,
'id' => $result['id'],
]);
$this->assertEquals('custom string', $check['values'][$check['id']]['custom_' . $ids['custom_field_id']]);
$this->customFieldDelete($ids['custom_field_id']);
$this->customGroupDelete($ids['custom_group_id']);
}
/**
* CRM-12773 - expectation is that civicrm quietly ignores fields without values.
*
* @throws \CRM_Core_Exception
*/
public function testCreateWithNULLCustomCRM12773(): void {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
$params = $this->_params;
$params['custom_' . $ids['custom_field_id']] = NULL;
$this->callAPISuccess('contact', 'create', $params);
$this->customFieldDelete($ids['custom_field_id']);
$this->customGroupDelete($ids['custom_group_id']);
}
/**
* Test create contact with custom checkbox with empty array
*/
public function testCreateWithEmptyCustomCheckbox(): void {
$this->callAPISuccess('OptionGroup', 'create', [
'name' => 'checkbox_opts',
'title' => 'Checkbox Options',
'data_type' => 'String',
'is_active' => 1,
]);
$this->callAPISuccess('OptionValue', 'create', [
'option_group_id' => 'checkbox_opts',
'name' => 'checkbox_option_one',
'label' => 'Checkbox Option One',
'is_active' => 1,
'value' => 1,
]);
$custom_group_id = $this->createCustomGroup([
'name' => 'checkbox_custom_group',
'title' => 'Checkbox Group',
'extends' => 'Contact',
]);
$custom_field_id = $this->callAPISuccess('CustomField', 'create', [
'name' => 'a_checkbox_field',
'label' => 'A Checkbox Field',
'custom_group_id' => $custom_group_id,
'option_group_id' => 'checkbox_opts',
'html_type' => 'CheckBox',
'data_type' => 'String',
'is_active' => 1,
])['id'];
$params = $this->_params;
$params['custom_' . $custom_field_id] = [];
$contact_id = $this->callAPISuccess('Contact', 'create', $params)['id'];
$result = $this->callAPISuccessGetSingle('Contact', ['id' => $contact_id, 'return' => ['custom_' . $custom_field_id]]);
$this->assertSame('', $result['custom_' . $custom_field_id]);
$this->customFieldDelete($custom_field_id);
$this->customGroupDelete($custom_group_id);
}
/**
* CRM-14232 test preferred language set to site default if not passed.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*
*/
public function testCreatePreferredLanguageUnset(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess('Contact', 'create', [
'first_name' => 'Snoop',
'last_name' => 'Dog',
'contact_type' => 'Individual',
]);
$result = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Dog']);
$this->assertEquals('en_US', $result['preferred_language']);
}
/**
* CRM-14232 test preferred language returns setting if not passed.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*
*/
public function testCreatePreferredLanguageSet(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess('Setting', 'create', ['contact_default_language' => 'fr_FR']);
$this->callAPISuccess('Contact', 'create', [
'first_name' => 'Snoop',
'last_name' => 'Dog',
'contact_type' => 'Individual',
]);
$result = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Dog']);
$this->assertEquals('fr_FR', $result['preferred_language']);
}
/**
* CRM-14232 test preferred language returns setting if not passed where setting is NULL.
* TODO: Api4
*
* @throws \CRM_Core_Exception
*/
public function testCreatePreferredLanguageNull(): void {
$this->callAPISuccess('Setting', 'create', ['contact_default_language' => 'null']);
$this->callAPISuccess('Contact', 'create', [
'first_name' => 'Snoop',
'last_name' => 'Dog',
'contact_type' => 'Individual',
]);
$result = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Dog']);
$this->assertEquals(NULL, $result['preferred_language']);
}
/**
* CRM-14232 test preferred language returns setting if not passed where setting is NULL.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*/
public function testCreatePreferredLanguagePassed(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess('Setting', 'create', ['contact_default_language' => 'null']);
$this->callAPISuccess('Contact', 'create', [
'first_name' => 'Snoop',
'last_name' => 'Dog',
'contact_type' => 'Individual',
'preferred_language' => 'en_AU',
]);
$result = $this->callAPISuccessGetSingle('Contact', ['last_name' => 'Dog']);
$this->assertEquals('en_AU', $result['preferred_language']);
}
/**
* CRM-15792 - create/update datetime field for contact.
*
* @throws \CRM_Core_Exception
*/
public function testCreateContactCustomFldDateTime(): void {
$customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'datetime_test_group']);
$dateTime = CRM_Utils_Date::currentDBDate();
//check date custom field is saved along with time when time_format is set
$params = [
'first_name' => 'abc3',
'last_name' => 'xyz3',
'contact_type' => 'Individual',
'email' => 'man3@yahoo.com',
'api.CustomField.create' => [
'custom_group_id' => $customGroup['id'],
'name' => 'test_datetime',
'label' => 'Demo Date',
'html_type' => 'Select Date',
'data_type' => 'Date',
'time_format' => 2,
'weight' => 4,
'is_required' => 1,
'is_searchable' => 0,
'is_active' => 1,
],
];
$result = $this->callAPISuccess('Contact', 'create', $params);
$customFldId = $result['values'][$result['id']]['api.CustomField.create']['id'];
$this->assertNotNull($result['id']);
$this->assertNotNull($customFldId);
$params = [
'id' => $result['id'],
"custom_$customFldId" => $dateTime,
'api.CustomValue.get' => 1,
];
$result = $this->callAPISuccess('Contact', 'create', $params);
$this->assertNotNull($result['id']);
$customFldDate = date('YmdHis', strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
$this->assertNotNull($customFldDate);
$this->assertEquals($dateTime, $customFldDate);
$customValueId = $result['values'][$result['id']]['api.CustomValue.get']['values'][0]['id'];
$dateTime = date('Ymd');
//date custom field should not contain time part when time_format is null
$params = [
'id' => $result['id'],
'api.CustomField.create' => [
'id' => $customFldId,
'html_type' => 'Select Date',
'data_type' => 'Date',
'time_format' => '',
],
'api.CustomValue.create' => [
'id' => $customValueId,
'entity_id' => $result['id'],
"custom_$customFldId" => $dateTime,
],
'api.CustomValue.get' => 1,
];
$result = $this->callAPISuccess('Contact', 'create', $params);
$this->assertNotNull($result['id']);
$customFldDate = date('Ymd', strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
$customFldTime = date('His', strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
$this->assertNotNull($customFldDate);
$this->assertEquals($dateTime, $customFldDate);
$this->assertEquals(000000, $customFldTime);
$this->callAPISuccess('Contact', 'create', $params);
}
/**
* Test creating a current employer through API.
*/
public function testContactCreateCurrentEmployer(): void {
// Here we will just do the get for set-up purposes.
$count = $this->callAPISuccess('contact', 'getcount', [
'organization_name' => 'new employer org',
'contact_type' => 'Organization',
]);
$this->assertEquals(0, $count);
$employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, [
'current_employer' => 'new employer org',
]));
// do it again as an update to check it doesn't cause an error
$employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, [
'current_employer' => 'new employer org',
'id' => $employerResult['id'],
]));
$expectedCount = 1;
$this->callAPISuccess('contact', 'getcount', [
'organization_name' => 'new employer org',
'contact_type' => 'Organization',
], $expectedCount);
$result = $this->callAPISuccess('contact', 'getsingle', [
'id' => $employerResult['id'],
]);
$this->assertEquals('new employer org', $result['current_employer']);
}
/**
* Test creating a current employer through API.
*
* Check it will re-activate a de-activated employer
*/
public function testContactCreateDuplicateCurrentEmployerEnables(): void {
// Set up - create employer relationship.
$employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, ['current_employer' => 'new employer org']));
$relationship = $this->callAPISuccess('relationship', 'get', [
'contact_id_a' => $employerResult['id'],
]);
//disable & check it is disabled
$this->callAPISuccess('relationship', 'create', ['id' => $relationship['id'], 'is_active' => 0]);
$this->callAPISuccess('relationship', 'getvalue', [
'id' => $relationship['id'],
'return' => 'is_active',
], 0);
// Re-set the current employer - thus enabling the relationship.
$this->callAPISuccess('contact', 'create', array_merge($this->_params, [
'current_employer' => 'new employer org',
'id' => $employerResult['id'],
]));
//check is_active is now 1
$relationship = $this->callAPISuccess('relationship', 'getsingle', ['return' => 'is_active']);
$this->assertEquals(1, $relationship['is_active']);
}
/**
* Check deceased contacts are not retrieved.
*
* Note at time of writing the default is to return default. This should possibly be changed & test added.
*
* @param int $version
*
* @throws \CRM_Core_Exception
* @dataProvider versionThreeAndFour
*
*/
public function testGetDeceasedRetrieved(int $version): void {
$this->_apiversion = $version;
$this->callAPISuccess($this->_entity, 'create', $this->_params);
$c2 = $this->callAPISuccess($this->_entity, 'create', [
'first_name' => 'bb',
'last_name' => 'ccc',
'contact_type' => 'Individual',
'is_deceased' => 1,
]);
$result = $this->callAPISuccess($this->_entity, 'get', ['is_deceased' => 0]);
$this->assertArrayNotHasKey($c2['id'], $result['values']);
}
/**
* Test that sort works - old syntax.
*
* @throws \CRM_Core_Exception
*/
public function testGetSort(): void {
$c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);