4 * Class CRM_Contact_BAO_ContactTest
7 class CRM_Contact_BAO_ContactTest
extends CiviUnitTestCase
{
12 public function setUp() {
17 * Test case for add( ).
19 * test with empty params.
21 public function testAddWithEmptyParams() {
23 $contact = CRM_Contact_BAO_Contact
::add($params);
25 // Now check Contact object.
26 $this->assertNull($contact);
30 * Test case for add( ).
32 * Test with names (create and update modes)
34 public function testAddWithNames() {
36 $lastName = 'Whatson';
38 'first_name' => $firstName,
39 'last_name' => $lastName,
40 'contact_type' => 'Individual',
43 $contact = CRM_Contact_BAO_Contact
::add($params);
45 // Now check $contact is object of contact DAO.
46 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
47 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
48 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
50 $contactId = $contact->id
;
52 // Update and change first name and last name, using add( ).
55 'first_name' => $firstName,
56 'contact_type' => 'Individual',
57 'contact_id' => $contactId,
60 $contact = CRM_Contact_BAO_Contact
::add($params);
62 // Now check $contact is object of contact DAO.
63 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
64 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
66 $contactId = $contact->id
;
67 $this->contactDelete($contactId);
73 * Test with all contact params
74 * (create and update modes)
76 public function testAddWithAll() {
77 // Take the common contact params.
78 $params = $this->contactParams();
80 unset($params['location']);
81 $prefComm = $params['preferred_communication_method'];
82 $contact = CRM_Contact_BAO_Contact
::add($params);
83 $contactId = $contact->id
;
85 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
87 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
88 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
89 $this->assertEquals($params['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
90 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
91 $this->assertEquals('1', $contact->do_not_email
, 'Check for do_not_email creation.');
92 $this->assertEquals('1', $contact->do_not_phone
, 'Check for do_not_phone creation.');
93 $this->assertEquals('1', $contact->do_not_mail
, 'Check for do_not_mail creation.');
94 $this->assertEquals('1', $contact->do_not_trade
, 'Check for do_not_trade creation.');
95 $this->assertEquals('1', $contact->is_opt_out
, 'Check for is_opt_out creation.');
96 $this->assertEquals($params['external_identifier'], $contact->external_identifier
, 'Check for external_identifier creation.');
97 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name
, 'Check for sort_name creation.');
98 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format
,
99 'Check for preferred_mail_format creation.'
101 $this->assertEquals($params['contact_source'], $contact->source
, 'Check for contact_source creation.');
102 $this->assertEquals($params['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
103 $this->assertEquals($params['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
104 $this->assertEquals($params['job_title'], $contact->job_title
, 'Check for job_title creation.');
105 $this->assertEquals($params['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
106 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
107 $this->assertEquals(CRM_Utils_Date
::processDate($params['birth_date']),
108 $contact->birth_date
, 'Check for birth_date creation.'
110 $this->assertEquals(CRM_Utils_Date
::processDate($params['deceased_date']),
111 $contact->deceased_date
, 'Check for deceased_date creation.'
113 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
114 $contact->preferred_communication_method
117 foreach ($dbPrefComm as $key => $value) {
119 $checkPrefComm[$value] = 1;
122 $this->assertAttributesEquals($checkPrefComm, $prefComm);
125 'contact_type' => 'Individual',
126 'first_name' => 'Jane',
127 'middle_name' => 'abc',
128 'last_name' => 'Doe',
131 'nick_name' => 'Nick Name Second',
132 'job_title' => 'software Developer',
137 'website_type_id' => 1,
138 'url' => 'http://docs.civicrm.org',
141 'contact_source' => 'test update contact',
142 'external_identifier' => 111111111,
143 'preferred_mail_format' => 'Both',
145 'deceased_date' => '1981-03-03',
146 'birth_date' => '1951-04-04',
153 'preferred_communication_method' => [
162 $prefComm = $updateParams['preferred_communication_method'];
163 $updateParams['contact_id'] = $contactId;
164 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
165 $contactId = $contact->id
;
167 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
169 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
170 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
171 $this->assertEquals($updateParams['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
172 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
173 $this->assertEquals('0', $contact->do_not_email
, 'Check for do_not_email creation.');
174 $this->assertEquals('0', $contact->do_not_phone
, 'Check for do_not_phone creation.');
175 $this->assertEquals('0', $contact->do_not_mail
, 'Check for do_not_mail creation.');
176 $this->assertEquals('0', $contact->do_not_trade
, 'Check for do_not_trade creation.');
177 $this->assertEquals('0', $contact->is_opt_out
, 'Check for is_opt_out creation.');
178 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier
,
179 'Check for external_identifier creation.'
181 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
182 $contact->sort_name
, 'Check for sort_name creation.'
184 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format
,
185 'Check for preferred_mail_format creation.'
187 $this->assertEquals($updateParams['contact_source'], $contact->source
, 'Check for contact_source creation.');
188 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
189 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
190 $this->assertEquals($updateParams['job_title'], $contact->job_title
, 'Check for job_title creation.');
191 $this->assertEquals($updateParams['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
192 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
193 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['birth_date']),
194 date('YmdHis', strtotime($contact->birth_date
)), 'Check for birth_date creation.'
196 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['deceased_date']),
197 date('YmdHis', strtotime($contact->deceased_date
)), 'Check for deceased_date creation.'
199 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
200 $contact->preferred_communication_method
203 foreach ($dbPrefComm as $key => $value) {
205 $checkPrefComm[$value] = 1;
208 $this->assertAttributesEquals($checkPrefComm, $prefComm);
210 $this->contactDelete($contactId);
214 * Test case for add( ) with All contact types.
216 public function testAddWithAllContactTypes() {
220 'first_name' => $firstName,
221 'last_name' => $lastName,
222 'contact_type' => 'Individual',
225 $contact = CRM_Contact_BAO_Contact
::add($params);
226 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
227 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
229 $contactId = $contact->id
;
231 //update and change first name and last name, using create()
234 'first_name' => $firstName,
235 'contact_type' => 'Individual',
236 'contact_id' => $contactId,
239 $contact = CRM_Contact_BAO_Contact
::add($params);
240 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
241 $contactId = $contact->id
;
242 $this->contactDelete($contactId);
244 $householdName = 'Adams house';
246 'household_name' => $householdName,
247 'contact_type' => 'Household',
249 $contact = CRM_Contact_BAO_Contact
::add($params);
250 $this->assertEquals($householdName, $contact->sort_name
, 'Check for created household.');
251 $contactId = $contact->id
;
253 //update and change name of household, using create
254 $householdName = 'Joans home';
256 'household_name' => $householdName,
257 'contact_type' => 'Household',
258 'contact_id' => $contactId,
260 $contact = CRM_Contact_BAO_Contact
::add($params);
261 $this->assertEquals($householdName, $contact->sort_name
, 'Check for updated household.');
262 $this->contactDelete($contactId);
264 $organizationName = 'My Organization';
266 'organization_name' => $organizationName,
267 'contact_type' => 'Organization',
269 $contact = CRM_Contact_BAO_Contact
::add($params);
270 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for created organization.');
271 $contactId = $contact->id
;
273 //update and change name of organization, using create
274 $organizationName = 'Your Changed Organization';
276 'organization_name' => $organizationName,
277 'contact_type' => 'Organization',
278 'contact_id' => $contactId,
280 $contact = CRM_Contact_BAO_Contact
::add($params);
281 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for updated organization.');
282 $this->contactDelete($contactId);
286 * Test case for create.
288 * Test with missing params.
290 public function testCreateWithEmptyParams() {
292 'first_name' => 'Bill',
293 'last_name' => 'Adams',
295 $contact = CRM_Contact_BAO_Contact
::create($params);
297 //Now check Contact object
298 $this->assertNull($contact);
302 * Test case for create.
304 * Test with all params.
305 * ( create and update modes ).
307 public function testCreateWithAll() {
308 //take the common contact params
309 $params = $this->contactParams();
310 $params['note'] = 'test note';
312 //create the contact with given params.
313 $contact = CRM_Contact_BAO_Contact
::create($params);
315 //Now check $contact is object of contact DAO..
316 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
317 $contactId = $contact->id
;
319 //Now check values of contact object with params.
320 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
321 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
322 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
324 //Now check DB for Address
326 'contact_id' => $contactId,
327 'location_type_id' => 1,
331 'street_address' => $params['address'][1]['street_address'] ??
NULL,
332 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1',
333 $params['address'][1]
335 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2',
336 $params['address'][1]
338 'supplemental_address_3' => CRM_Utils_Array
::value('supplemental_address_3',
339 $params['address'][1]
341 'city' => $params['address'][1]['city'] ??
NULL,
342 'postal_code' => $params['address'][1]['postal_code'] ??
NULL,
343 'country_id' => $params['address'][1]['country_id'] ??
NULL,
344 'state_province_id' => CRM_Utils_Array
::value('state_province_id',
345 $params['address'][1]
347 'geo_code_1' => $params['address'][1]['geo_code_1'] ??
NULL,
348 'geo_code_2' => $params['address'][1]['geo_code_2'] ??
NULL,
350 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
352 //Now check DB for Email
353 $compareParams = ['email' => CRM_Utils_Array
::value('email', $params['email'][1])];
354 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
356 //Now check DB for openid
357 $compareParams = ['openid' => CRM_Utils_Array
::value('openid', $params['openid'][1])];
358 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
360 //Now check DB for IM
362 'name' => $params['im'][1]['name'] ??
NULL,
363 'provider_id' => $params['im'][1]['provider_id'] ??
NULL,
365 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
367 //Now check DB for Phone
369 'contact_id' => $contactId,
370 'location_type_id' => 1,
372 'phone_type_id' => $params['phone'][1]['phone_type_id'] ??
NULL,
374 $compareParams = ['phone' => CRM_Utils_Array
::value('phone', $params['phone'][1])];
375 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
377 //Now check DB for Mobile
379 'contact_id' => $contactId,
380 'location_type_id' => 1,
381 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
383 $compareParams = ['phone' => CRM_Utils_Array
::value('phone', $params['phone'][2])];
384 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
386 //Now check DB for Note
388 'entity_id' => $contactId,
389 'entity_table' => 'civicrm_contact',
391 $compareParams = ['note' => $params['note']];
392 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
394 //update the contact.
396 'first_name' => 'John',
397 'last_name' => 'Doe',
398 'contact_type' => 'Individual',
399 'note' => 'new test note',
401 $updateParams['address'][1] = [
402 'location_type_id' => 1,
404 'street_address' => 'Oberoi Garden',
405 'supplemental_address_1' => 'A-wing:3037',
406 'supplemental_address_2' => 'Andhery',
407 'supplemental_address_3' => 'Anywhere',
409 'postal_code' => '12345',
410 'country_id' => 1228,
411 'state_province_id' => 1004,
412 'geo_code_1' => '31.694842',
413 'geo_code_2' => '-106.29998',
415 $updateParams['email'][1] = [
416 'location_type_id' => 1,
418 'email' => 'john.doe@example.org',
421 $updateParams['phone'][1] = [
422 'location_type_id' => 1,
424 'phone_type_id' => 1,
425 'phone' => '02115245336',
427 $updateParams['phone'][2] = [
428 'location_type_id' => 1,
429 'phone_type_id' => 2,
430 'phone' => '9766323895',
433 $updateParams['contact_id'] = $contactId;
434 //create the contact with given params.
435 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
437 //Now check $contact is object of contact DAO..
438 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
439 $contactId = $contact->id
;
441 //Now check values of contact object with updated params.
442 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
443 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
444 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
446 //Now check DB for updated Address
448 'contact_id' => $contactId,
449 'location_type_id' => 1,
453 'street_address' => 'Oberoi Garden',
454 'supplemental_address_1' => 'A-wing:3037',
455 'supplemental_address_2' => 'Andhery',
456 'supplemental_address_3' => 'Anywhere',
458 'postal_code' => '12345',
459 'country_id' => 1228,
460 'state_province_id' => 1004,
461 'geo_code_1' => '31.694842',
462 'geo_code_2' => '-106.29998',
464 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
466 //Now check DB for updated Email
467 $compareParams = ['email' => 'john.doe@example.org'];
468 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
470 //Now check DB for updated Phone
472 'contact_id' => $contactId,
473 'location_type_id' => 1,
475 'phone_type_id' => 1,
477 $compareParams = ['phone' => '02115245336'];
478 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
480 //Now check DB for updated Mobile
482 'contact_id' => $contactId,
483 'location_type_id' => 1,
484 'phone_type_id' => 2,
486 $compareParams = ['phone' => '9766323895'];
487 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
488 // As we are not updating note.
489 // Now check DB for New Note.
490 $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
491 'Database check for New created note '
494 // Delete all notes related to contact.
495 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
497 // Cleanup DB by deleting the contact.
498 $this->contactDelete($contactId);
499 $this->quickCleanup(['civicrm_contact', 'civicrm_note']);
503 * Test case for resolveDefaults( ).
505 * Test all pseudoConstant, stateProvince, country.
507 public function testResolveDefaults() {
512 'birth_date' => '1983-12-13',
515 $params['address'][1] = [
516 'location_type_id' => 1,
518 'country_id' => 1228,
519 'state_province_id' => 1004,
521 // @todo - we are testing this with $reverse = FALSE but it is never called that way!
522 CRM_Contact_BAO_Contact
::resolveDefaults($params);
524 //check the resolve values.
525 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
526 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
527 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
528 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
529 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
530 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
531 $this->assertEquals(1004, $params['address'][1]['state_province_id']);
532 $this->assertEquals(CRM_Core_PseudoConstant
::country($params['address'][1]['country_id']),
533 $params['address'][1]['country'],
539 * Test case for retrieve( ).
541 * Test with all values.
543 public function testRetrieve() {
544 //take the common contact params
545 $params = $this->contactParams();
546 $params['note'] = 'test note';
548 //create the contact with given params.
549 $contact = CRM_Contact_BAO_Contact
::create($params);
550 //Now check $contact is object of contact DAO..
551 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
552 $contactId = $contact->id
;
553 //create the organization contact with the given params.
555 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
556 'contact_type' => 'Organization',
558 $orgContact = CRM_Contact_BAO_Contact
::add($orgParams);
559 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
561 //create employee of relationship.
562 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $orgContact->id
);
564 //retrieve the contact values from database.
566 $searchParams = ['contact_id' => $contactId];
567 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
569 //Now check $retrieveContact is object of contact DAO..
570 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
573 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
575 //Now check values retrieve from database with params.
576 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
577 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
578 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
580 //Now check values of address
581 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
582 CRM_Utils_Array
::value('1', $values['address'])
585 //Now check values of email
586 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
587 CRM_Utils_Array
::value('1', $values['email'])
590 //Now check values of phone
591 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
592 CRM_Utils_Array
::value('1', $values['phone'])
595 //Now check values of mobile
596 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
597 CRM_Utils_Array
::value('2', $values['phone'])
600 //Now check values of openid
601 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
602 CRM_Utils_Array
::value('1', $values['openid'])
605 //Now check values of im
606 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
607 CRM_Utils_Array
::value('1', $values['im'])
610 //Now check values of Note Count.
611 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
613 foreach ($values['note'] as $key => $val) {
614 $retrieveNote = $val['note'] ??
NULL;
615 //check the note value
616 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
619 //Now check values of Relationship Count.
620 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
621 foreach ($values['relationship']['data'] as $key => $val) {
622 //Now check values of Relationship organization.
623 $this->assertEquals($orgContact->id
, $val['contact_id_b'], 'Check for organization');
624 //Now check values of Relationship type.
625 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
626 //delete the organization.
627 $this->contactDelete(CRM_Utils_Array
::value('contact_id_b', $val));
630 //delete all notes related to contact
631 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
633 //cleanup DB by deleting the contact
634 $this->contactDelete($contactId);
635 $this->quickCleanup(['civicrm_contact']);
639 * Test case for deleteContact( ).
641 public function testDeleteContact() {
642 $contactParams = $this->contactParams();
644 $customGroup = $this->customGroupCreate();
646 'label' => 'testFld',
647 'data_type' => 'String',
648 'html_type' => 'Text',
649 'custom_group_id' => $customGroup['id'],
651 $customField = CRM_Core_BAO_CustomField
::create($fields);
652 $contactParams['custom'] = [
653 $customField->id
=> [
655 'value' => 'Test custom value',
657 'custom_field_id' => $customField->id
,
658 'custom_group_id' => $customGroup['id'],
659 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
660 'column_name' => $customField->column_name
,
667 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
668 $contactId = $contact->id
;
670 //delete contact permanently.
671 CRM_Contact_BAO_Contact
::deleteContact($contactId, FALSE, TRUE);
673 //Now check DB for location elements.
674 //Now check DB for Address
676 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
677 'id', 'street_address', 'Database check, Address deleted successfully.'
680 //Now check DB for Email
681 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
682 'id', 'email', 'Database check, Email deleted successfully.'
684 //Now check DB for Phone
685 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
686 'id', 'phone', 'Database check, Phone deleted successfully.'
688 //Now check DB for Mobile
689 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
690 'id', 'phone', 'Database check, Mobile deleted successfully.'
692 //Now check DB for IM
693 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
694 'id', 'name', 'Database check, IM deleted successfully.'
696 //Now check DB for openId
697 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
698 'id', 'openid', 'Database check, openId deleted successfully.'
701 // Check that the custom field value is no longer present
703 'entityID' => $contactId,
704 'custom_' . $customField->id
=> 1,
706 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
707 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
708 'Verify that the data value is empty for contact ' . $contactId
710 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
712 //Now check DB for contact.
713 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
714 'id', 'sort_name', 'Database check, contact deleted successfully.'
716 $this->quickCleanup(['civicrm_contact', 'civicrm_note']);
717 $this->customGroupDelete($customGroup['id']);
721 * Test case for createProfileContact.
723 public function testCreateProfileContact() {
724 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
726 //current employer field for individual
727 $fields['organization_name'] = [
728 'name' => 'organization_name',
729 'where' => 'civicrm_organization.organization_name',
730 'title' => 'Current Employer',
732 //get the common params
733 $contactParams = $this->contactParams();
734 $unsetParams = ['location', 'privacy'];
735 foreach ($unsetParams as $param) {
736 unset($contactParams[$param]);
740 'organization_name' => 'Yahoo',
744 'city-Primary' => 'Newark',
745 'contact_type' => 'Individual',
746 'country-Primary' => '1228',
747 'do_not_email' => '1',
748 'do_not_mail' => '1',
749 'do_not_phone' => '1',
750 'do_not_trade' => '1',
752 'email-Primary' => 'john.smith@example.org',
753 'geo_code_1-Primary' => '18.219023',
754 'geo_code_2-Primary' => '-105.00973',
755 'im-Primary-provider_id' => '1',
756 'im-Primary' => 'john.smith',
758 'openid' => 'john.smith@example.org',
759 'phone-Primary-1' => '303443689',
760 'phone-Primary-2' => '9833910234',
761 'postal_code-Primary' => '01903',
762 'postal_code_suffix-Primary' => '12345',
763 'state_province-Primary' => '1029',
764 'street_address-Primary' => 'Saint Helier St',
765 'supplemental_address_1-Primary' => 'Hallmark Ct',
766 'supplemental_address_2-Primary' => 'Jersey Village',
767 'supplemental_address_3-Primary' => 'My Town',
768 'user_unique_id' => '123456789',
769 'is_bulkmail' => '1',
770 'world_region' => 'India',
777 $createParams = array_merge($contactParams, $profileParams);
779 //create the contact using create profile contact.
780 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
782 //get the parameters to compare.
783 $params = $this->contactParams();
785 //check the values in DB.
786 foreach ($params as $key => $val) {
787 if (!is_array($params[$key])) {
788 if ($key == 'contact_source') {
789 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
790 'id', $params[$key], "Check for {$key} creation."
794 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
795 'id', $params[$key], "Check for {$key} creation."
801 //check privacy options.
802 foreach ($params['privacy'] as $key => $value) {
803 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
804 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
809 'id', $profileParams['contact_type'], 'Check for contact type creation.'
811 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
812 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
815 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
816 'id', $params['birth_date'], 'Check for birth_date creation.'
819 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
820 'id', $params['deceased_date'], 'Check for deceased_date creation.'
823 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
824 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
827 foreach ($dbPrefComm as $key => $value) {
829 $checkPrefComm[$value] = 1;
832 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
834 //Now check DB for Address
836 'contact_id' => $contactId,
837 'location_type_id' => 1,
841 'street_address' => $profileParams['street_address-Primary'] ??
NULL,
842 'supplemental_address_1' => $profileParams['supplemental_address_1-Primary'] ??
NULL,
843 'supplemental_address_2' => $profileParams['supplemental_address_2-Primary'] ??
NULL,
844 'supplemental_address_3' => $profileParams['supplemental_address_3-Primary'] ??
NULL,
845 'city' => $profileParams['city-Primary'] ??
NULL,
846 'postal_code' => $profileParams['postal_code-Primary'] ??
NULL,
847 'country_id' => $profileParams['country-Primary'] ??
NULL,
848 'state_province_id' => $profileParams['state_province-Primary'] ??
NULL,
849 'geo_code_1' => $profileParams['geo_code_1-Primary'] ??
NULL,
850 'geo_code_2' => $profileParams['geo_code_2-Primary'] ??
NULL,
852 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
854 //Now check DB for Email
855 $compareParams = ['email' => CRM_Utils_Array
::value('email-Primary', $profileParams)];
856 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
858 //Now check DB for IM
860 'name' => $profileParams['im-Primary'] ??
NULL,
861 'provider_id' => $profileParams['im-Primary-provider_id'] ??
NULL,
863 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
865 //Now check DB for Phone
867 'contact_id' => $contactId,
868 'location_type_id' => 1,
871 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams)];
872 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
874 //Now check DB for Mobile
876 'contact_id' => $contactId,
877 'location_type_id' => 1,
878 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
880 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams)];
882 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
884 //get the value of relationship
886 $searchParams = ['contact_id' => $contactId];
887 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
888 //Now check values of Relationship Count.
889 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
890 foreach ($values['relationship']['data'] as $key => $val) {
891 //Now check values of Relationship organization.
892 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
893 //Now check values of Relationship type.
894 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
895 //delete the organization.
896 $this->contactDelete(CRM_Utils_Array
::value('cid', $val));
899 //Now check values of tag ids.
900 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
901 foreach ($tags as $key => $val) {
905 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
907 //update Contact mode
909 'first_name' => 'john',
910 'last_name' => 'doe',
911 'contact_type' => 'Individual',
912 'middle_name' => 'abc',
915 'nick_name' => 'Nick Name Updated',
916 'job_title' => 'software Developer',
921 'website_type_id' => 1,
922 'url' => 'http://civicrmUpdate.org',
925 'contact_source' => 'test contact',
926 'external_identifier' => 111222333,
927 'preferred_mail_format' => 'Both',
929 'legal_identifier' => '123123123123',
930 'image_URL' => 'http://imageupdate.com',
931 'deceased_date' => '1981-10-10',
932 'birth_date' => '1951-11-11',
937 'preferred_communication_method' => [
947 'organization_name' => 'Google',
948 'city-Primary' => 'Mumbai',
949 'contact_type' => 'Individual',
950 'country-Primary' => '1228',
951 'do_not_email' => '1',
952 'do_not_mail' => '1',
953 'do_not_phone' => '1',
954 'do_not_trade' => '1',
956 'email-Primary' => 'john.doe@example.org',
957 'geo_code_1-Primary' => '31.694842',
958 'geo_code_2-Primary' => '-106.29998',
959 'im-Primary-provider_id' => '1',
960 'im-Primary' => 'john.doe',
962 'openid' => 'john.doe@example.org',
963 'phone-Primary-1' => '02115245336',
964 'phone-Primary-2' => '9766323895',
965 'postal_code-Primary' => '12345',
966 'postal_code_suffix-Primary' => '123',
967 'state_province-Primary' => '1004',
968 'street_address-Primary' => 'Oberoi Garden',
969 'supplemental_address_1-Primary' => 'A-wing:3037',
970 'supplemental_address_2-Primary' => 'Andhery',
971 'supplemental_address_3-Primary' => 'Anywhere',
972 'user_unique_id' => '1122334455',
973 'is_bulkmail' => '1',
974 'world_region' => 'India',
981 $createParams = array_merge($updateCParams, $updatePfParams);
983 //create the contact using create profile contact.
984 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
985 NULL, NULL, NULL, TRUE
988 //check the contact ids
989 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
991 //check the values in DB.
992 foreach ($updateCParams as $key => $val) {
993 if (!is_array($updateCParams[$key])) {
994 if ($key == 'contact_source') {
995 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
996 'id', $updateCParams[$key], "Check for {$key} creation."
1000 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1001 'id', $updateCParams[$key], "Check for {$key} creation."
1007 //check privacy options.
1008 foreach ($updateCParams['privacy'] as $key => $value) {
1009 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1010 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1014 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1015 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1017 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1018 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1021 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1022 $updateCParams['birth_date'], 'Check for birth_date creation.'
1025 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1026 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1029 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1030 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
1032 $checkPrefComm = [];
1033 foreach ($dbPrefComm as $key => $value) {
1035 $checkPrefComm[$value] = 1;
1038 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1040 //Now check DB for Address
1042 'contact_id' => $contactId,
1043 'location_type_id' => 1,
1047 'street_address' => $updatePfParams['street_address-Primary'] ??
NULL,
1048 'supplemental_address_1' => $updatePfParams['supplemental_address_1-Primary'] ??
NULL,
1049 'supplemental_address_2' => $updatePfParams['supplemental_address_2-Primary'] ??
NULL,
1050 'supplemental_address_3' => $updatePfParams['supplemental_address_3-Primary'] ??
NULL,
1051 'city' => $updatePfParams['city-Primary'] ??
NULL,
1052 'postal_code' => $updatePfParams['postal_code-Primary'] ??
NULL,
1053 'country_id' => $updatePfParams['country-Primary'] ??
NULL,
1054 'state_province_id' => $updatePfParams['state_province-Primary'] ??
NULL,
1055 'geo_code_1' => $updatePfParams['geo_code_1-Primary'] ??
NULL,
1056 'geo_code_2' => $updatePfParams['geo_code_2-Primary'] ??
NULL,
1058 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1060 //Now check DB for Email
1061 $compareParams = ['email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams)];
1062 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1064 //Now check DB for IM
1066 'name' => $updatePfParams['im-Primary'] ??
NULL,
1067 'provider_id' => $updatePfParams['im-Primary-provider_id'] ??
NULL,
1069 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1071 //Now check DB for Phone
1073 'contact_id' => $contactId,
1074 'location_type_id' => 1,
1077 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams)];
1078 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1080 //Now check DB for Mobile
1082 'contact_id' => $contactId,
1083 'location_type_id' => 1,
1084 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
1086 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams)];
1087 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1089 //get the value of relationship
1091 $searchParams = ['contact_id' => $contactId];
1092 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1093 //Now check values of Relationship Count.
1094 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1095 foreach ($values['relationship']['data'] as $key => $val) {
1096 //Now check values of Relationship organization.
1097 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1098 //Now check values of Relationship type.
1099 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1100 //delete the organization.
1101 $this->contactDelete(CRM_Utils_Array
::value('cid', $val));
1104 //Now check values of tag ids.
1105 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1106 foreach ($tags as $key => $val) {
1109 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1111 //cleanup DB by deleting the contact
1112 $this->contactDelete($contactId);
1116 * Test case for getContactDetails( ).
1118 public function testGetContactDetails() {
1119 //get the contact params
1120 $params = $this->contactParams();
1123 $contact = CRM_Contact_BAO_Contact
::create($params);
1124 $contactId = $contact->id
;
1126 //get the contact details
1127 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1129 $params['first_name'] . ' ' . $params['last_name'],
1130 CRM_Utils_Array
::value('email', $params['email'][1]),
1131 (bool ) $params['privacy']['do_not_email'],
1133 //Now check the contact details
1134 $this->assertAttributesEquals($compareParams, $contactDetails);
1136 //cleanup DB by deleting the contact
1137 $this->contactDelete($contactId);
1138 $this->quickCleanup(['civicrm_contact']);
1142 * Test case for importableFields( ) and exportableFields( ).
1144 public function testFields() {
1145 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1146 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1147 //Now check all fields
1148 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1150 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1151 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1152 //Now check Individual fields
1153 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1155 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1156 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1157 //Now check Household fields
1158 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1160 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1161 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1162 //Now check Organization fields
1163 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1167 * Test case for getPrimaryEmail.
1169 public function testGetPrimaryEmail() {
1170 //get the contact params
1171 $params = $this->contactParams();
1172 $params['email'][2] = $params['email'][1];
1173 $params['email'][2]['email'] = 'primarymail@example.org';
1174 unset($params['email'][1]['is_primary']);
1177 $contact = CRM_Contact_BAO_Contact
::create($params);
1178 $contactId = $contact->id
;
1179 //get the primary email.
1180 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1181 //Now check the primary email
1182 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1184 //cleanup DB by deleting the contact
1185 $this->contactDelete($contactId);
1186 $this->quickCleanup(['civicrm_contact']);
1190 * Test case for getPrimaryOpenId( ).
1192 public function testGetPrimaryOpenId() {
1193 //get the contact params
1194 $params = $this->contactParams();
1195 $params['openid'][2] = $params['openid'][1];
1196 $params['openid'][2]['location_type_id'] = 2;
1197 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1198 unset($params['openid'][1]['is_primary']);
1201 $contact = CRM_Contact_BAO_Contact
::create($params);
1202 $contactId = $contact->id
;
1203 //get the primary openid
1204 $openID = CRM_Contact_BAO_Contact
::getPrimaryOpenId($contactId);
1206 //Now check the primary openid
1207 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1209 //cleanup DB by deleting the contact
1210 $this->contactDelete($contactId);
1214 * Test case for matchContactOnEmail( ).
1216 public function testMatchContactOnEmail() {
1217 //get the contact params
1218 $params = $this->contactParams();
1220 $contact = CRM_Contact_BAO_Contact
::create($params);
1221 $contactId = $contact->id
;
1223 //get the matching contact.
1224 $match = CRM_Contact_BAO_Contact
::matchContactOnEmail(CRM_Utils_Array
::value('email', $params['email'][1]),
1227 $this->assertEquals($contactId, $match->contact_id
, 'Check For Matching Contact');
1229 //cleanup DB by deleting the contact
1230 $this->contactDelete($contactId);
1231 $this->quickCleanup(['civicrm_contact']);
1235 * Test case for getContactType( ).
1237 public function testGetContactType() {
1238 //get the contact params
1239 $params = $this->contactParams();
1241 $contact = CRM_Contact_BAO_Contact
::create($params);
1242 $contactId = $contact->id
;
1245 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1246 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1248 //cleanup DB by deleting the contact
1249 $this->contactDelete($contactId);
1250 $this->quickCleanup(['civicrm_contact']);
1254 * Test case for displayName( ).
1256 public function testDisplayName() {
1257 //get the contact params
1258 $params = $this->contactParams();
1261 $contact = CRM_Contact_BAO_Contact
::create($params);
1262 $contactId = $contact->id
;
1265 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1267 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1268 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1270 //build display name
1271 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1273 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1275 //cleanup DB by deleting the contact
1276 $this->contactDelete($contactId);
1277 $this->quickCleanup(['civicrm_contact']);
1281 * Test case for getDisplayAndImage( ).
1283 public function testGetDisplayAndImage() {
1284 //get the contact params
1285 $params = $this->contactParams();
1288 $contact = CRM_Contact_BAO_Contact
::create($params);
1289 $contactId = $contact->id
;
1291 //get DisplayAndImage.
1292 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1294 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1296 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1297 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1299 //build display name
1300 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1302 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1303 $this->assertEquals($image, $checkImage, 'Check For Image');
1305 //cleanup DB by deleting the contact
1306 $this->contactDelete($contactId);
1310 * Build common params.
1312 private function contactParams() {
1315 'first_name' => 'john',
1316 'last_name' => 'smith',
1317 'contact_type' => 'Individual',
1318 'middle_name' => 'xyz',
1321 'nick_name' => 'Nick Name',
1322 'job_title' => 'software engg',
1327 'website_type_id' => 1,
1328 'url' => 'http://civicrm.org',
1331 'contact_source' => 'test contact',
1332 'external_identifier' => 123456789,
1333 'preferred_mail_format' => 'Both',
1335 'legal_identifier' => '123456789',
1336 'image_URL' => 'http://image.com',
1337 'deceased_date' => '1991-07-07',
1338 'birth_date' => '1961-06-06',
1340 'do_not_phone' => 1,
1341 'do_not_email' => 1,
1343 'do_not_trade' => 1,
1345 'preferred_communication_method' => [
1354 $params['address'] = [];
1355 $params['address'][1] = [
1356 'location_type_id' => 1,
1358 'street_address' => 'Saint Helier St',
1359 'supplemental_address_1' => 'Hallmark Ct',
1360 'supplemental_address_2' => 'Jersey Village',
1361 'supplemental_address_3' => 'My Town',
1363 'postal_code' => '01903',
1364 'country_id' => 1228,
1365 'state_province_id' => 1029,
1366 'geo_code_1' => '18.219023',
1367 'geo_code_2' => '-105.00973',
1370 $params['email'] = [];
1371 $params['email'][1] = [
1372 'location_type_id' => 1,
1374 'email' => 'john.smith@example.org',
1377 $params['phone'] = [];
1378 $params['phone'][1] = [
1379 'location_type_id' => 1,
1381 'phone_type_id' => 1,
1382 'phone' => '303443689',
1384 $params['phone'][2] = [
1385 'location_type_id' => 1,
1386 'phone_type_id' => 2,
1387 'phone' => '9833910234',
1390 $params['openid'] = [];
1391 $params['openid'][1] = [
1392 'location_type_id' => 1,
1394 'openid' => 'http://civicrm.org/',
1398 $params['im'][1] = [
1399 'location_type_id' => 1,
1401 'name' => 'john.smith',
1409 * Ensure that created_date and modified_date are set.
1411 public function testTimestampContact() {
1413 $this->_testTimestamps([
1414 'UPDATE' => function ($contactId) use ($test) {
1416 'first_name' => 'Testing',
1417 'contact_type' => 'Individual',
1418 'contact_id' => $contactId,
1420 $contact = CRM_Contact_BAO_Contact
::add($params);
1421 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1427 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record.
1429 public function testTimestampsEmail() {
1431 $this->_testTimestamps([
1432 'INSERT' => function ($contactId) use ($test) {
1434 'email' => 'ex-1@example.com',
1436 'location_type_id' => 1,
1437 'contact_id' => $contactId,
1439 CRM_Core_BAO_Email
::add($params);
1440 $test->assertDBQuery('ex-1@example.com',
1441 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1442 [1 => [$contactId, 'Integer']]
1445 'UPDATE' => function ($contactId) use ($test) {
1446 CRM_Core_DAO
::executeQuery(
1447 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1448 [1 => [$contactId, 'Integer']]
1451 'DELETE' => function ($contactId) use ($test) {
1452 CRM_Core_DAO
::executeQuery(
1453 'DELETE FROM civicrm_email WHERE contact_id = %1',
1454 [1 => [$contactId, 'Integer']]
1461 * Ensure that civicrm_contact.modified_date is updated when manipulating an email.
1463 public function testTimestampsPhone() {
1465 $this->_testTimestamps([
1466 'INSERT' => function ($contactId) use ($test) {
1468 'phone' => '202-555-1000',
1470 'location_type_id' => 1,
1471 'contact_id' => $contactId,
1473 CRM_Core_BAO_Phone
::add($params);
1474 $test->assertDBQuery('202-555-1000',
1475 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1476 [1 => [$contactId, 'Integer']]
1479 'UPDATE' => function ($contactId) use ($test) {
1480 CRM_Core_DAO
::executeQuery(
1481 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1482 [1 => [$contactId, 'Integer']]
1485 'DELETE' => function ($contactId) use ($test) {
1486 CRM_Core_DAO
::executeQuery(
1487 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1488 [1 => [$contactId, 'Integer']]
1495 * Ensure that civicrm_contact.modified_date is updated correctly.
1497 * Looking at it when contact-related custom data is updated.
1499 public function testTimestampsCustom() {
1500 $customGroup = $this->customGroupCreate();
1501 $customGroup = $customGroup['values'][$customGroup['id']];
1503 'custom_group_id' => $customGroup['id'],
1504 'data_type' => 'String',
1505 'html_type' => 'Text',
1507 $customField = $this->customFieldCreate($fields);
1508 $customField = $customField['values'][$customField['id']];
1510 $this->_testTimestamps([
1511 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1512 civicrm_api3('contact', 'create', [
1513 'contact_id' => $contactId,
1514 'custom_' . $customField['id'] => 'test-1',
1517 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1518 CRM_Core_DAO
::executeQuery(
1519 "UPDATE {$customGroup['table_name']} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1520 [1 => [$contactId, 'Integer']]
1523 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1524 CRM_Core_DAO
::executeQuery(
1525 "DELETE FROM {$customGroup['table_name']} WHERE entity_id = %1",
1526 [1 => [$contactId, 'Integer']]
1530 $this->quickCleanup(['civicrm_contact'], TRUE);
1534 * Helper for testing timestamp manipulation.
1536 * Create a contact and perform a series of steps with it; after each
1537 * step, ensure that the contact's modified_date has increased.
1539 * @param array $callbacks
1540 * ($name => $callable).
1542 public function _testTimestamps($callbacks) {
1543 CRM_Core_DAO
::triggerRebuild();
1544 $contactId = $this->individualCreate();
1546 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1547 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1548 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1549 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1551 $prevTimestamps = $origTimestamps;
1552 foreach ($callbacks as $callbackName => $callback) {
1553 // advance clock by 1 second to ensure timestamps change
1556 $callback($contactId);
1557 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1558 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1559 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1560 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1561 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1563 $prevTimestamps = $newTimestamps;
1566 $this->contactDelete($contactId);
1570 * Test case for UpdateProfileLocationLeak (CRM-20598).
1572 public function testUpdateProfileLocationLeak() {
1573 // create a simple contact with address and phone that share the same location type
1574 $defaults = $this->contactParams();
1576 'first_name' => $defaults['first_name'],
1577 'last_name' => $defaults['last_name'],
1578 'contact_type' => 'Individual',
1579 'address' => [1 => $defaults['address'][1]],
1580 'phone' => [1 => $defaults['phone'][1]],
1582 $contact = CRM_Contact_BAO_Contact
::create($params);
1583 $contactId = $contact->id
;
1585 // now, update using a profile with phone, email, address... that share the same location type
1587 'first_name' => $params['first_name'],
1588 'last_name' => $params['first_name'],
1589 'street_address-Primary' => $params['address'][1]['street_address'],
1590 'state_province-Primary' => $params['address'][1]['state_province_id'],
1591 'country-Primary' => $params['address'][1]['country_id'],
1592 'phone-Primary-1' => $params['phone'][1]['phone'],
1593 'phone_ext-Primary-1' => '345',
1596 //create the contact using create profile contact.
1597 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
1599 $this->createLoggedInUser();
1600 // now, emulate the contact update using a profile
1601 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($updatePfParams, $fields, $contactId,
1602 NULL, NULL, NULL, TRUE
1605 //check the contact ids
1606 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
1607 $phone = $this->callAPISuccess('Phone', 'getsingle', ['contact_id' => $contactID]);
1608 $this->assertEquals('345', $phone['phone_ext']);
1609 $this->assertEquals($params['phone'][1]['phone'], $phone['phone']);
1611 //check the values in DB.
1613 'contact_id' => $contactId,
1614 'location_type_id' => 1,
1618 'street_address' => $updatePfParams['street_address-Primary'] ??
NULL,
1620 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1622 //cleanup DB by deleting the contact
1623 $this->contactDelete($contactId);
1627 * Test that contact details are still displayed if no email is present.
1629 * @throws \Exception
1631 public function testContactEmailDetailsWithNoPrimaryEmail() {
1632 $params = $this->contactParams();
1633 unset($params['email']);
1634 $contact = CRM_Contact_BAO_Contact
::create($params);
1635 $contactId = $contact->id
;
1636 $result = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
1637 $this->assertEquals([$contact->display_name
, NULL, NULL, NULL], $result);
1641 * dev/core#1605 State/province not copied on shared address
1642 * 1. First, create contacts: A and B
1643 * 2. Create an address for contact A
1644 * 3. Use contact A's address for contact B's address
1645 * ALL the address fields on address A should be copied to address B
1647 public function testSharedAddressCopiesAllAddressFields() {
1648 $contactIdA = $this->individualCreate([], 0);
1649 $contactIdB = $this->individualCreate([], 1);
1652 'street_address' => '123 Fake St.',
1653 'location_type_id' => '1',
1654 'is_primary' => '1',
1655 'contact_id' => $contactIdA,
1656 'street_name' => 'Ambachtstraat',
1657 'street_number' => '23',
1658 'street_address' => 'Ambachtstraat 23',
1659 'postal_code' => '6971 BN',
1660 'country_id' => '1152',
1661 'city' => 'Brummen',
1663 'state_province_id' => '3934',
1665 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
1667 $addressParamsB[1] = [
1668 'contact_id' => $contactIdB,
1669 'master_id' => $addAddressA->id
,
1670 'use_shared_address' => 1,
1673 CRM_Contact_BAO_Contact_Utils
::processSharedAddress($addressParamsB);
1674 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB[1], FALSE);
1676 foreach ($addAddressA as $key => $value) {
1677 if (!in_array($key, ['id', 'contact_id', 'master_id', 'is_primary', 'is_billing', 'location_type_id', 'manual_geo_code'])) {
1678 $this->assertEquals($addAddressA->$key, $addAddressB->$key);