4 * Class CRM_Contact_BAO_ContactTest
7 class CRM_Contact_BAO_ContactTest
extends CiviUnitTestCase
{
10 * Test case for add( ).
12 * test with empty params.
14 public function testAddWithEmptyParams() {
16 $contact = CRM_Contact_BAO_Contact
::add($params);
18 // Now check Contact object.
19 $this->assertNull($contact);
23 * Test case for add( ).
25 * Test with names (create and update modes)
27 public function testAddWithNames() {
29 $lastName = 'Whatson';
31 'first_name' => $firstName,
32 'last_name' => $lastName,
33 'contact_type' => 'Individual',
36 $contact = CRM_Contact_BAO_Contact
::add($params);
38 // Now check $contact is object of contact DAO.
39 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
40 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
41 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
43 $contactId = $contact->id
;
45 // Update and change first name and last name, using add( ).
48 'first_name' => $firstName,
49 'contact_type' => 'Individual',
50 'contact_id' => $contactId,
53 $contact = CRM_Contact_BAO_Contact
::add($params);
55 // Now check $contact is object of contact DAO.
56 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
57 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
59 $contactId = $contact->id
;
60 $this->contactDelete($contactId);
66 * Test with all contact params
67 * (create and update modes)
69 public function testAddWithAll() {
70 // Take the common contact params.
71 $params = $this->contactParams();
73 unset($params['location']);
74 $prefComm = $params['preferred_communication_method'];
75 $contact = CRM_Contact_BAO_Contact
::add($params);
76 $contactId = $contact->id
;
78 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
80 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
81 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
82 $this->assertEquals($params['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
83 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
84 $this->assertEquals('1', $contact->do_not_email
, 'Check for do_not_email creation.');
85 $this->assertEquals('1', $contact->do_not_phone
, 'Check for do_not_phone creation.');
86 $this->assertEquals('1', $contact->do_not_mail
, 'Check for do_not_mail creation.');
87 $this->assertEquals('1', $contact->do_not_trade
, 'Check for do_not_trade creation.');
88 $this->assertEquals('1', $contact->is_opt_out
, 'Check for is_opt_out creation.');
89 $this->assertEquals($params['external_identifier'], $contact->external_identifier
, 'Check for external_identifier creation.');
90 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name
, 'Check for sort_name creation.');
91 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format
,
92 'Check for preferred_mail_format creation.'
94 $this->assertEquals($params['contact_source'], $contact->source
, 'Check for contact_source creation.');
95 $this->assertEquals($params['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
96 $this->assertEquals($params['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
97 $this->assertEquals($params['job_title'], $contact->job_title
, 'Check for job_title creation.');
98 $this->assertEquals($params['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
99 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
100 $this->assertEquals(CRM_Utils_Date
::processDate($params['birth_date']),
101 $contact->birth_date
, 'Check for birth_date creation.'
103 $this->assertEquals(CRM_Utils_Date
::processDate($params['deceased_date']),
104 $contact->deceased_date
, 'Check for deceased_date creation.'
106 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
107 $contact->preferred_communication_method
110 foreach ($dbPrefComm as $key => $value) {
112 $checkPrefComm[$value] = 1;
115 $this->assertAttributesEquals($checkPrefComm, $prefComm);
118 'contact_type' => 'Individual',
119 'first_name' => 'Jane',
120 'middle_name' => 'abc',
121 'last_name' => 'Doe',
124 'nick_name' => 'Nick Name Second',
125 'job_title' => 'software Developer',
130 'website_type_id' => 1,
131 'url' => 'http://docs.civicrm.org',
134 'contact_source' => 'test update contact',
135 'external_identifier' => 111111111,
136 'preferred_mail_format' => 'Both',
138 'deceased_date' => '1981-03-03',
139 'birth_date' => '1951-04-04',
146 'preferred_communication_method' => [
155 $prefComm = $updateParams['preferred_communication_method'];
156 $updateParams['contact_id'] = $contactId;
157 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
158 $contactId = $contact->id
;
160 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
162 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
163 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
164 $this->assertEquals($updateParams['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
165 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
166 $this->assertEquals('0', $contact->do_not_email
, 'Check for do_not_email creation.');
167 $this->assertEquals('0', $contact->do_not_phone
, 'Check for do_not_phone creation.');
168 $this->assertEquals('0', $contact->do_not_mail
, 'Check for do_not_mail creation.');
169 $this->assertEquals('0', $contact->do_not_trade
, 'Check for do_not_trade creation.');
170 $this->assertEquals('0', $contact->is_opt_out
, 'Check for is_opt_out creation.');
171 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier
,
172 'Check for external_identifier creation.'
174 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
175 $contact->sort_name
, 'Check for sort_name creation.'
177 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format
,
178 'Check for preferred_mail_format creation.'
180 $this->assertEquals($updateParams['contact_source'], $contact->source
, 'Check for contact_source creation.');
181 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
182 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
183 $this->assertEquals($updateParams['job_title'], $contact->job_title
, 'Check for job_title creation.');
184 $this->assertEquals($updateParams['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
185 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
186 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['birth_date']),
187 date('YmdHis', strtotime($contact->birth_date
)), 'Check for birth_date creation.'
189 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['deceased_date']),
190 date('YmdHis', strtotime($contact->deceased_date
)), 'Check for deceased_date creation.'
192 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
193 $contact->preferred_communication_method
196 foreach ($dbPrefComm as $key => $value) {
198 $checkPrefComm[$value] = 1;
201 $this->assertAttributesEquals($checkPrefComm, $prefComm);
203 $this->contactDelete($contactId);
207 * Test case for add( ) with All contact types.
209 public function testAddWithAllContactTypes() {
213 'first_name' => $firstName,
214 'last_name' => $lastName,
215 'contact_type' => 'Individual',
218 $contact = CRM_Contact_BAO_Contact
::add($params);
219 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
220 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
222 $contactId = $contact->id
;
224 //update and change first name and last name, using create()
227 'first_name' => $firstName,
228 'contact_type' => 'Individual',
229 'contact_id' => $contactId,
232 $contact = CRM_Contact_BAO_Contact
::add($params);
233 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
234 $contactId = $contact->id
;
235 $this->contactDelete($contactId);
237 $householdName = 'Adams house';
239 'household_name' => $householdName,
240 'contact_type' => 'Household',
242 $contact = CRM_Contact_BAO_Contact
::add($params);
243 $this->assertEquals($householdName, $contact->sort_name
, 'Check for created household.');
244 $contactId = $contact->id
;
246 //update and change name of household, using create
247 $householdName = 'Joans home';
249 'household_name' => $householdName,
250 'contact_type' => 'Household',
251 'contact_id' => $contactId,
253 $contact = CRM_Contact_BAO_Contact
::add($params);
254 $this->assertEquals($householdName, $contact->sort_name
, 'Check for updated household.');
255 $this->contactDelete($contactId);
257 $organizationName = 'My Organization';
259 'organization_name' => $organizationName,
260 'contact_type' => 'Organization',
262 $contact = CRM_Contact_BAO_Contact
::add($params);
263 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for created organization.');
264 $contactId = $contact->id
;
266 //update and change name of organization, using create
267 $organizationName = 'Your Changed Organization';
269 'organization_name' => $organizationName,
270 'contact_type' => 'Organization',
271 'contact_id' => $contactId,
273 $contact = CRM_Contact_BAO_Contact
::add($params);
274 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for updated organization.');
275 $this->contactDelete($contactId);
279 * Test case for create.
281 * Test with missing params.
283 public function testCreateWithEmptyParams() {
285 'first_name' => 'Bill',
286 'last_name' => 'Adams',
288 $contact = CRM_Contact_BAO_Contact
::create($params);
290 //Now check Contact object
291 $this->assertNull($contact);
295 * Test case for create.
297 * Test with all params.
298 * ( create and update modes ).
300 public function testCreateWithAll() {
301 //take the common contact params
302 $params = $this->contactParams();
303 $params['note'] = 'test note';
305 //create the contact with given params.
306 $contact = CRM_Contact_BAO_Contact
::create($params);
308 //Now check $contact is object of contact DAO..
309 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
310 $contactId = $contact->id
;
312 //Now check values of contact object with params.
313 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
314 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
315 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
317 //Now check DB for Address
319 'contact_id' => $contactId,
320 'location_type_id' => 1,
324 'street_address' => $params['address'][1]['street_address'] ??
NULL,
325 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1',
326 $params['address'][1]
328 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2',
329 $params['address'][1]
331 'supplemental_address_3' => CRM_Utils_Array
::value('supplemental_address_3',
332 $params['address'][1]
334 'city' => $params['address'][1]['city'] ??
NULL,
335 'postal_code' => $params['address'][1]['postal_code'] ??
NULL,
336 'country_id' => $params['address'][1]['country_id'] ??
NULL,
337 'state_province_id' => CRM_Utils_Array
::value('state_province_id',
338 $params['address'][1]
340 'geo_code_1' => $params['address'][1]['geo_code_1'] ??
NULL,
341 'geo_code_2' => $params['address'][1]['geo_code_2'] ??
NULL,
343 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
345 //Now check DB for Email
346 $compareParams = ['email' => CRM_Utils_Array
::value('email', $params['email'][1])];
347 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
349 //Now check DB for openid
350 $compareParams = ['openid' => CRM_Utils_Array
::value('openid', $params['openid'][1])];
351 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
353 //Now check DB for IM
355 'name' => $params['im'][1]['name'] ??
NULL,
356 'provider_id' => $params['im'][1]['provider_id'] ??
NULL,
358 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
360 //Now check DB for Phone
362 'contact_id' => $contactId,
363 'location_type_id' => 1,
365 'phone_type_id' => $params['phone'][1]['phone_type_id'] ??
NULL,
367 $compareParams = ['phone' => CRM_Utils_Array
::value('phone', $params['phone'][1])];
368 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
370 //Now check DB for Mobile
372 'contact_id' => $contactId,
373 'location_type_id' => 1,
374 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
376 $compareParams = ['phone' => CRM_Utils_Array
::value('phone', $params['phone'][2])];
377 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
379 //Now check DB for Note
381 'entity_id' => $contactId,
382 'entity_table' => 'civicrm_contact',
384 $compareParams = ['note' => $params['note']];
385 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
387 //update the contact.
389 'first_name' => 'John',
390 'last_name' => 'Doe',
391 'contact_type' => 'Individual',
392 'note' => 'new test note',
394 $updateParams['address'][1] = [
395 'location_type_id' => 1,
397 'street_address' => 'Oberoi Garden',
398 'supplemental_address_1' => 'A-wing:3037',
399 'supplemental_address_2' => 'Andhery',
400 'supplemental_address_3' => 'Anywhere',
402 'postal_code' => '12345',
403 'country_id' => 1228,
404 'state_province_id' => 1004,
405 'geo_code_1' => '31.694842',
406 'geo_code_2' => '-106.29998',
408 $updateParams['email'][1] = [
409 'location_type_id' => 1,
411 'email' => 'john.doe@example.org',
414 $updateParams['phone'][1] = [
415 'location_type_id' => 1,
417 'phone_type_id' => 1,
418 'phone' => '02115245336',
420 $updateParams['phone'][2] = [
421 'location_type_id' => 1,
422 'phone_type_id' => 2,
423 'phone' => '9766323895',
426 $updateParams['contact_id'] = $contactId;
427 //create the contact with given params.
428 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
430 //Now check $contact is object of contact DAO..
431 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
432 $contactId = $contact->id
;
434 //Now check values of contact object with updated params.
435 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
436 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
437 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
439 //Now check DB for updated Address
441 'contact_id' => $contactId,
442 'location_type_id' => 1,
446 'street_address' => 'Oberoi Garden',
447 'supplemental_address_1' => 'A-wing:3037',
448 'supplemental_address_2' => 'Andhery',
449 'supplemental_address_3' => 'Anywhere',
451 'postal_code' => '12345',
452 'country_id' => 1228,
453 'state_province_id' => 1004,
454 'geo_code_1' => '31.694842',
455 'geo_code_2' => '-106.29998',
457 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
459 //Now check DB for updated Email
460 $compareParams = ['email' => 'john.doe@example.org'];
461 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
463 //Now check DB for updated Phone
465 'contact_id' => $contactId,
466 'location_type_id' => 1,
468 'phone_type_id' => 1,
470 $compareParams = ['phone' => '02115245336'];
471 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
473 //Now check DB for updated Mobile
475 'contact_id' => $contactId,
476 'location_type_id' => 1,
477 'phone_type_id' => 2,
479 $compareParams = ['phone' => '9766323895'];
480 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
481 // As we are not updating note.
482 // Now check DB for New Note.
483 $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
484 'Database check for New created note '
487 // Delete all notes related to contact.
488 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
490 // Cleanup DB by deleting the contact.
491 $this->contactDelete($contactId);
492 $this->quickCleanup(['civicrm_contact', 'civicrm_note']);
496 * Test case for resolveDefaults( ).
498 * Test all pseudoConstant, stateProvince, country.
500 public function testResolveDefaults() {
505 'birth_date' => '1983-12-13',
508 $params['address'][1] = [
509 'location_type_id' => 1,
511 'country_id' => 1228,
512 'state_province_id' => 1004,
514 // @todo - we are testing this with $reverse = FALSE but it is never called that way!
515 CRM_Contact_BAO_Contact
::resolveDefaults($params);
517 //check the resolve values.
518 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
519 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
520 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
521 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
522 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
523 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
524 $this->assertEquals(1004, $params['address'][1]['state_province_id']);
525 $this->assertEquals(CRM_Core_PseudoConstant
::country($params['address'][1]['country_id']),
526 $params['address'][1]['country'],
532 * Test case for retrieve( ).
534 * Test with all values.
536 public function testRetrieve() {
537 //take the common contact params
538 $params = $this->contactParams();
539 $params['note'] = 'test note';
541 //create the contact with given params.
542 $contact = CRM_Contact_BAO_Contact
::create($params);
543 //Now check $contact is object of contact DAO..
544 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
545 $contactId = $contact->id
;
546 //create the organization contact with the given params.
548 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
549 'contact_type' => 'Organization',
551 $orgContact = CRM_Contact_BAO_Contact
::add($orgParams);
552 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
554 //create employee of relationship.
555 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $orgContact->id
);
557 //retrieve the contact values from database.
559 $searchParams = ['contact_id' => $contactId];
560 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
562 //Now check $retrieveContact is object of contact DAO..
563 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
566 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
568 //Now check values retrieve from database with params.
569 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
570 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
571 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
573 //Now check values of address
574 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
575 CRM_Utils_Array
::value('1', $values['address'])
578 //Now check values of email
579 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
580 CRM_Utils_Array
::value('1', $values['email'])
583 //Now check values of phone
584 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
585 CRM_Utils_Array
::value('1', $values['phone'])
588 //Now check values of mobile
589 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
590 CRM_Utils_Array
::value('2', $values['phone'])
593 //Now check values of openid
594 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
595 CRM_Utils_Array
::value('1', $values['openid'])
598 //Now check values of im
599 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
600 CRM_Utils_Array
::value('1', $values['im'])
603 //Now check values of Note Count.
604 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
606 foreach ($values['note'] as $key => $val) {
607 $retrieveNote = $val['note'] ??
NULL;
608 //check the note value
609 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
612 //Now check values of Relationship Count.
613 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
614 foreach ($values['relationship']['data'] as $key => $val) {
615 //Now check values of Relationship organization.
616 $this->assertEquals($orgContact->id
, $val['contact_id_b'], 'Check for organization');
617 //Now check values of Relationship type.
618 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
619 //delete the organization.
620 $this->contactDelete(CRM_Utils_Array
::value('contact_id_b', $val));
623 //delete all notes related to contact
624 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
626 //cleanup DB by deleting the contact
627 $this->contactDelete($contactId);
628 $this->quickCleanup(['civicrm_contact']);
632 * Test case for deleteContact( ).
634 public function testDeleteContact() {
635 $contactParams = $this->contactParams();
637 $customGroup = $this->customGroupCreate();
639 'label' => 'testFld',
640 'data_type' => 'String',
641 'html_type' => 'Text',
642 'custom_group_id' => $customGroup['id'],
644 $customField = CRM_Core_BAO_CustomField
::create($fields);
645 $contactParams['custom'] = [
646 $customField->id
=> [
648 'value' => 'Test custom value',
650 'custom_field_id' => $customField->id
,
651 'custom_group_id' => $customGroup['id'],
652 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'],
653 'column_name' => $customField->column_name
,
660 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
661 $contactId = $contact->id
;
663 //delete contact permanently.
664 $this->contactDelete($contactId);
666 //Now check DB for location elements.
667 //Now check DB for Address
669 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
670 'id', 'street_address', 'Database check, Address deleted successfully.'
673 //Now check DB for Email
674 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
675 'id', 'email', 'Database check, Email deleted successfully.'
677 //Now check DB for Phone
678 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
679 'id', 'phone', 'Database check, Phone deleted successfully.'
681 //Now check DB for Mobile
682 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
683 'id', 'phone', 'Database check, Mobile deleted successfully.'
685 //Now check DB for IM
686 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
687 'id', 'name', 'Database check, IM deleted successfully.'
689 //Now check DB for openId
690 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
691 'id', 'openid', 'Database check, openId deleted successfully.'
694 // Check that the custom field value is no longer present
696 'entityID' => $contactId,
697 'custom_' . $customField->id
=> 1,
699 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
700 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
701 'Verify that the data value is empty for contact ' . $contactId
703 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
705 //Now check DB for contact.
706 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
707 'id', 'sort_name', 'Database check, contact deleted successfully.'
709 $this->quickCleanup(['civicrm_contact', 'civicrm_note']);
710 $this->customGroupDelete($customGroup['id']);
714 * Test case for createProfileContact.
716 public function testCreateProfileContact() {
717 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
719 //current employer field for individual
720 $fields['organization_name'] = [
721 'name' => 'organization_name',
722 'where' => 'civicrm_organization.organization_name',
723 'title' => 'Current Employer',
725 //get the common params
726 $contactParams = $this->contactParams();
727 $unsetParams = ['location', 'privacy'];
728 foreach ($unsetParams as $param) {
729 unset($contactParams[$param]);
733 'organization_name' => 'Yahoo',
737 'city-Primary' => 'Newark',
738 'contact_type' => 'Individual',
739 'country-Primary' => '1228',
740 'do_not_email' => '1',
741 'do_not_mail' => '1',
742 'do_not_phone' => '1',
743 'do_not_trade' => '1',
745 'email-Primary' => 'john.smith@example.org',
746 'geo_code_1-Primary' => '18.219023',
747 'geo_code_2-Primary' => '-105.00973',
748 'im-Primary-provider_id' => '1',
749 'im-Primary' => 'john.smith',
751 'openid' => 'john.smith@example.org',
752 'phone-Primary-1' => '303443689',
753 'phone-Primary-2' => '9833910234',
754 'postal_code-Primary' => '01903',
755 'postal_code_suffix-Primary' => '12345',
756 'state_province-Primary' => '1029',
757 'street_address-Primary' => 'Saint Helier St',
758 'supplemental_address_1-Primary' => 'Hallmark Ct',
759 'supplemental_address_2-Primary' => 'Jersey Village',
760 'supplemental_address_3-Primary' => 'My Town',
761 'user_unique_id' => '123456789',
762 'is_bulkmail' => '1',
763 'world_region' => 'India',
770 $createParams = array_merge($contactParams, $profileParams);
772 //create the contact using create profile contact.
773 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
775 //get the parameters to compare.
776 $params = $this->contactParams();
778 //check the values in DB.
779 foreach ($params as $key => $val) {
780 if (!is_array($params[$key])) {
781 if ($key == 'contact_source') {
782 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
783 'id', $params[$key], "Check for {$key} creation."
787 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
788 'id', $params[$key], "Check for {$key} creation."
794 //check privacy options.
795 foreach ($params['privacy'] as $key => $value) {
796 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
797 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
801 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
802 'id', $profileParams['contact_type'], 'Check for contact type creation.'
804 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
805 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
809 'id', $params['birth_date'], 'Check for birth_date creation.'
812 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
813 'id', $params['deceased_date'], 'Check for deceased_date creation.'
816 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
817 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
820 foreach ($dbPrefComm as $key => $value) {
822 $checkPrefComm[$value] = 1;
825 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
827 //Now check DB for Address
829 'contact_id' => $contactId,
830 'location_type_id' => 1,
834 'street_address' => $profileParams['street_address-Primary'] ??
NULL,
835 'supplemental_address_1' => $profileParams['supplemental_address_1-Primary'] ??
NULL,
836 'supplemental_address_2' => $profileParams['supplemental_address_2-Primary'] ??
NULL,
837 'supplemental_address_3' => $profileParams['supplemental_address_3-Primary'] ??
NULL,
838 'city' => $profileParams['city-Primary'] ??
NULL,
839 'postal_code' => $profileParams['postal_code-Primary'] ??
NULL,
840 'country_id' => $profileParams['country-Primary'] ??
NULL,
841 'state_province_id' => $profileParams['state_province-Primary'] ??
NULL,
842 'geo_code_1' => $profileParams['geo_code_1-Primary'] ??
NULL,
843 'geo_code_2' => $profileParams['geo_code_2-Primary'] ??
NULL,
845 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
847 //Now check DB for Email
848 $compareParams = ['email' => CRM_Utils_Array
::value('email-Primary', $profileParams)];
849 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
851 //Now check DB for IM
853 'name' => $profileParams['im-Primary'] ??
NULL,
854 'provider_id' => $profileParams['im-Primary-provider_id'] ??
NULL,
856 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
858 //Now check DB for Phone
860 'contact_id' => $contactId,
861 'location_type_id' => 1,
864 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams)];
865 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
867 //Now check DB for Mobile
869 'contact_id' => $contactId,
870 'location_type_id' => 1,
871 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
873 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams)];
875 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
877 //get the value of relationship
879 $searchParams = ['contact_id' => $contactId];
880 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
881 //Now check values of Relationship Count.
882 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
883 foreach ($values['relationship']['data'] as $key => $val) {
884 //Now check values of Relationship organization.
885 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
886 //Now check values of Relationship type.
887 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
888 //delete the organization.
889 $this->contactDelete(CRM_Utils_Array
::value('cid', $val));
892 //Now check values of tag ids.
893 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
894 foreach ($tags as $key => $val) {
898 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
900 //update Contact mode
902 'first_name' => 'john',
903 'last_name' => 'doe',
904 'contact_type' => 'Individual',
905 'middle_name' => 'abc',
908 'nick_name' => 'Nick Name Updated',
909 'job_title' => 'software Developer',
914 'website_type_id' => 1,
915 'url' => 'http://civicrmUpdate.org',
918 'contact_source' => 'test contact',
919 'external_identifier' => 111222333,
920 'preferred_mail_format' => 'Both',
922 'legal_identifier' => '123123123123',
923 'image_URL' => 'http://imageupdate.com',
924 'deceased_date' => '1981-10-10',
925 'birth_date' => '1951-11-11',
930 'preferred_communication_method' => [
940 'organization_name' => 'Google',
941 'city-Primary' => 'Mumbai',
942 'contact_type' => 'Individual',
943 'country-Primary' => '1228',
944 'do_not_email' => '1',
945 'do_not_mail' => '1',
946 'do_not_phone' => '1',
947 'do_not_trade' => '1',
949 'email-Primary' => 'john.doe@example.org',
950 'geo_code_1-Primary' => '31.694842',
951 'geo_code_2-Primary' => '-106.29998',
952 'im-Primary-provider_id' => '1',
953 'im-Primary' => 'john.doe',
955 'openid' => 'john.doe@example.org',
956 'phone-Primary-1' => '02115245336',
957 'phone-Primary-2' => '9766323895',
958 'postal_code-Primary' => '12345',
959 'postal_code_suffix-Primary' => '123',
960 'state_province-Primary' => '1004',
961 'street_address-Primary' => 'Oberoi Garden',
962 'supplemental_address_1-Primary' => 'A-wing:3037',
963 'supplemental_address_2-Primary' => 'Andhery',
964 'supplemental_address_3-Primary' => 'Anywhere',
965 'user_unique_id' => '1122334455',
966 'is_bulkmail' => '1',
967 'world_region' => 'India',
974 $createParams = array_merge($updateCParams, $updatePfParams);
976 //create the contact using create profile contact.
977 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
978 NULL, NULL, NULL, TRUE
981 //check the contact ids
982 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
984 //check the values in DB.
985 foreach ($updateCParams as $key => $val) {
986 if (!is_array($updateCParams[$key])) {
987 if ($key == 'contact_source') {
988 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
989 'id', $updateCParams[$key], "Check for {$key} creation."
993 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
994 'id', $updateCParams[$key], "Check for {$key} creation."
1000 //check privacy options.
1001 foreach ($updateCParams['privacy'] as $key => $value) {
1002 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1003 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1007 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1008 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1010 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1011 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1014 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1015 $updateCParams['birth_date'], 'Check for birth_date creation.'
1018 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1019 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1022 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1023 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
1025 $checkPrefComm = [];
1026 foreach ($dbPrefComm as $key => $value) {
1028 $checkPrefComm[$value] = 1;
1031 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1033 //Now check DB for Address
1035 'contact_id' => $contactId,
1036 'location_type_id' => 1,
1040 'street_address' => $updatePfParams['street_address-Primary'] ??
NULL,
1041 'supplemental_address_1' => $updatePfParams['supplemental_address_1-Primary'] ??
NULL,
1042 'supplemental_address_2' => $updatePfParams['supplemental_address_2-Primary'] ??
NULL,
1043 'supplemental_address_3' => $updatePfParams['supplemental_address_3-Primary'] ??
NULL,
1044 'city' => $updatePfParams['city-Primary'] ??
NULL,
1045 'postal_code' => $updatePfParams['postal_code-Primary'] ??
NULL,
1046 'country_id' => $updatePfParams['country-Primary'] ??
NULL,
1047 'state_province_id' => $updatePfParams['state_province-Primary'] ??
NULL,
1048 'geo_code_1' => $updatePfParams['geo_code_1-Primary'] ??
NULL,
1049 'geo_code_2' => $updatePfParams['geo_code_2-Primary'] ??
NULL,
1051 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1053 //Now check DB for Email
1054 $compareParams = ['email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams)];
1055 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1057 //Now check DB for IM
1059 'name' => $updatePfParams['im-Primary'] ??
NULL,
1060 'provider_id' => $updatePfParams['im-Primary-provider_id'] ??
NULL,
1062 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1064 //Now check DB for Phone
1066 'contact_id' => $contactId,
1067 'location_type_id' => 1,
1070 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams)];
1071 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1073 //Now check DB for Mobile
1075 'contact_id' => $contactId,
1076 'location_type_id' => 1,
1077 'phone_type_id' => $params['phone'][2]['phone_type_id'] ??
NULL,
1079 $compareParams = ['phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams)];
1080 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1082 //get the value of relationship
1084 $searchParams = ['contact_id' => $contactId];
1085 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1086 //Now check values of Relationship Count.
1087 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1088 foreach ($values['relationship']['data'] as $key => $val) {
1089 //Now check values of Relationship organization.
1090 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1091 //Now check values of Relationship type.
1092 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1093 //delete the organization.
1094 $this->contactDelete(CRM_Utils_Array
::value('cid', $val));
1097 //Now check values of tag ids.
1098 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1099 foreach ($tags as $key => $val) {
1102 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1104 //cleanup DB by deleting the contact
1105 $this->contactDelete($contactId);
1109 * Test case for getContactDetails( ).
1111 public function testGetContactDetails() {
1112 //get the contact params
1113 $params = $this->contactParams();
1116 $contact = CRM_Contact_BAO_Contact
::create($params);
1117 $contactId = $contact->id
;
1119 //get the contact details
1120 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1122 $params['first_name'] . ' ' . $params['last_name'],
1123 CRM_Utils_Array
::value('email', $params['email'][1]),
1124 (bool ) $params['privacy']['do_not_email'],
1126 //Now check the contact details
1127 $this->assertAttributesEquals($compareParams, $contactDetails);
1129 //cleanup DB by deleting the contact
1130 $this->contactDelete($contactId);
1131 $this->quickCleanup(['civicrm_contact']);
1135 * Test case for importableFields( ) and exportableFields( ).
1137 public function testFields() {
1138 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1139 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1140 //Now check all fields
1141 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1143 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1144 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1145 //Now check Individual fields
1146 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1148 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1149 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1150 //Now check Household fields
1151 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1153 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1154 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1155 //Now check Organization fields
1156 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1160 * Test case for getPrimaryEmail.
1162 public function testGetPrimaryEmail() {
1163 //get the contact params
1164 $params = $this->contactParams();
1165 $params['email'][2] = $params['email'][1];
1166 $params['email'][2]['email'] = 'primarymail@example.org';
1167 unset($params['email'][1]['is_primary']);
1170 $contact = CRM_Contact_BAO_Contact
::create($params);
1171 $contactId = $contact->id
;
1172 //get the primary email.
1173 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1174 //Now check the primary email
1175 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1177 //cleanup DB by deleting the contact
1178 $this->contactDelete($contactId);
1179 $this->quickCleanup(['civicrm_contact']);
1183 * Test case for matchContactOnEmail( ).
1185 public function testMatchContactOnEmail() {
1186 //get the contact params
1187 $params = $this->contactParams();
1189 $contact = CRM_Contact_BAO_Contact
::create($params);
1190 $contactId = $contact->id
;
1192 //get the matching contact.
1193 $match = CRM_Contact_BAO_Contact
::matchContactOnEmail(CRM_Utils_Array
::value('email', $params['email'][1]),
1196 $this->assertEquals($contactId, $match->contact_id
, 'Check For Matching Contact');
1198 //cleanup DB by deleting the contact
1199 $this->contactDelete($contactId);
1200 $this->quickCleanup(['civicrm_contact']);
1204 * Test case for getContactType( ).
1206 public function testGetContactType() {
1207 //get the contact params
1208 $params = $this->contactParams();
1210 $contact = CRM_Contact_BAO_Contact
::create($params);
1211 $contactId = $contact->id
;
1214 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1215 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1217 //cleanup DB by deleting the contact
1218 $this->contactDelete($contactId);
1219 $this->quickCleanup(['civicrm_contact']);
1223 * Test case for displayName( ).
1225 public function testDisplayName() {
1226 //get the contact params
1227 $params = $this->contactParams();
1230 $contact = CRM_Contact_BAO_Contact
::create($params);
1231 $contactId = $contact->id
;
1234 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1236 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1237 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1239 //build display name
1240 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1242 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1244 //cleanup DB by deleting the contact
1245 $this->contactDelete($contactId);
1246 $this->quickCleanup(['civicrm_contact']);
1250 * Test case for getDisplayAndImage( ).
1252 public function testGetDisplayAndImage() {
1253 //get the contact params
1254 $params = $this->contactParams();
1257 $contact = CRM_Contact_BAO_Contact
::create($params);
1258 $contactId = $contact->id
;
1260 //get DisplayAndImage.
1261 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1263 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1265 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1266 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1268 //build display name
1269 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1271 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1272 $this->assertEquals($image, $checkImage, 'Check For Image');
1274 //cleanup DB by deleting the contact
1275 $this->contactDelete($contactId);
1279 * Build common params.
1281 private function contactParams() {
1284 'first_name' => 'john',
1285 'last_name' => 'smith',
1286 'contact_type' => 'Individual',
1287 'middle_name' => 'xyz',
1290 'nick_name' => 'Nick Name',
1291 'job_title' => 'software engg',
1296 'website_type_id' => 1,
1297 'url' => 'http://civicrm.org',
1300 'contact_source' => 'test contact',
1301 'external_identifier' => 123456789,
1302 'preferred_mail_format' => 'Both',
1304 'legal_identifier' => '123456789',
1305 'image_URL' => 'http://image.com',
1306 'deceased_date' => '1991-07-07',
1307 'birth_date' => '1961-06-06',
1309 'do_not_phone' => 1,
1310 'do_not_email' => 1,
1312 'do_not_trade' => 1,
1314 'preferred_communication_method' => [
1323 $params['address'] = [];
1324 $params['address'][1] = [
1325 'location_type_id' => 1,
1327 'street_address' => 'Saint Helier St',
1328 'supplemental_address_1' => 'Hallmark Ct',
1329 'supplemental_address_2' => 'Jersey Village',
1330 'supplemental_address_3' => 'My Town',
1332 'postal_code' => '01903',
1333 'country_id' => 1228,
1334 'state_province_id' => 1029,
1335 'geo_code_1' => '18.219023',
1336 'geo_code_2' => '-105.00973',
1339 $params['email'] = [];
1340 $params['email'][1] = [
1341 'location_type_id' => 1,
1343 'email' => 'john.smith@example.org',
1346 $params['phone'] = [];
1347 $params['phone'][1] = [
1348 'location_type_id' => 1,
1350 'phone_type_id' => 1,
1351 'phone' => '303443689',
1353 $params['phone'][2] = [
1354 'location_type_id' => 1,
1355 'phone_type_id' => 2,
1356 'phone' => '9833910234',
1359 $params['openid'] = [];
1360 $params['openid'][1] = [
1361 'location_type_id' => 1,
1363 'openid' => 'http://civicrm.org/',
1367 $params['im'][1] = [
1368 'location_type_id' => 1,
1370 'name' => 'john.smith',
1378 * Ensure that created_date and modified_date are set.
1380 public function testTimestampContact() {
1382 $this->_testTimestamps([
1383 'UPDATE' => function ($contactId) use ($test) {
1385 'first_name' => 'Testing',
1386 'contact_type' => 'Individual',
1387 'contact_id' => $contactId,
1389 $contact = CRM_Contact_BAO_Contact
::add($params);
1390 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1396 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record.
1398 public function testTimestampsEmail() {
1400 $this->_testTimestamps([
1401 'INSERT' => function ($contactId) use ($test) {
1403 'email' => 'ex-1@example.com',
1405 'location_type_id' => 1,
1406 'contact_id' => $contactId,
1408 $this->callAPISuccess('Email', 'create', $params);
1409 $test->assertDBQuery('ex-1@example.com',
1410 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1411 [1 => [$contactId, 'Integer']]
1414 'UPDATE' => function ($contactId) use ($test) {
1415 CRM_Core_DAO
::executeQuery(
1416 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1417 [1 => [$contactId, 'Integer']]
1420 'DELETE' => function ($contactId) use ($test) {
1421 CRM_Core_DAO
::executeQuery(
1422 'DELETE FROM civicrm_email WHERE contact_id = %1',
1423 [1 => [$contactId, 'Integer']]
1430 * Ensure that civicrm_contact.modified_date is updated when manipulating an email.
1432 public function testTimestampsPhone() {
1434 $this->_testTimestamps([
1435 'INSERT' => function ($contactId) use ($test) {
1437 'phone' => '202-555-1000',
1439 'location_type_id' => 1,
1440 'contact_id' => $contactId,
1442 CRM_Core_BAO_Phone
::create($params);
1443 $test->assertDBQuery('202-555-1000',
1444 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1445 [1 => [$contactId, 'Integer']]
1448 'UPDATE' => function ($contactId) use ($test) {
1449 CRM_Core_DAO
::executeQuery(
1450 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1451 [1 => [$contactId, 'Integer']]
1454 'DELETE' => function ($contactId) use ($test) {
1455 CRM_Core_DAO
::executeQuery(
1456 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1457 [1 => [$contactId, 'Integer']]
1464 * Ensure that civicrm_contact.modified_date is updated correctly.
1466 * Looking at it when contact-related custom data is updated.
1468 public function testTimestampsCustom() {
1469 $customGroup = $this->customGroupCreate();
1470 $customGroup = $customGroup['values'][$customGroup['id']];
1472 'custom_group_id' => $customGroup['id'],
1473 'data_type' => 'String',
1474 'html_type' => 'Text',
1476 $customField = $this->customFieldCreate($fields);
1477 $customField = $customField['values'][$customField['id']];
1479 $this->_testTimestamps([
1480 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1481 civicrm_api3('contact', 'create', [
1482 'contact_id' => $contactId,
1483 'custom_' . $customField['id'] => 'test-1',
1486 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1487 CRM_Core_DAO
::executeQuery(
1488 "UPDATE {$customGroup['table_name']} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1489 [1 => [$contactId, 'Integer']]
1492 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1493 CRM_Core_DAO
::executeQuery(
1494 "DELETE FROM {$customGroup['table_name']} WHERE entity_id = %1",
1495 [1 => [$contactId, 'Integer']]
1499 $this->quickCleanup(['civicrm_contact'], TRUE);
1503 * Helper for testing timestamp manipulation.
1505 * Create a contact and perform a series of steps with it; after each
1506 * step, ensure that the contact's modified_date has increased.
1508 * @param array $callbacks
1509 * ($name => $callable).
1511 public function _testTimestamps($callbacks) {
1512 CRM_Core_DAO
::triggerRebuild();
1513 $contactId = $this->individualCreate();
1515 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1516 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1517 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1518 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1520 $prevTimestamps = $origTimestamps;
1521 foreach ($callbacks as $callbackName => $callback) {
1522 // advance clock by 1 second to ensure timestamps change
1525 $callback($contactId);
1526 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1527 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1528 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1529 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1530 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1532 $prevTimestamps = $newTimestamps;
1535 $this->contactDelete($contactId);
1539 * Test case for UpdateProfileLocationLeak (CRM-20598).
1541 public function testUpdateProfileLocationLeak() {
1542 // create a simple contact with address and phone that share the same location type
1543 $defaults = $this->contactParams();
1545 'first_name' => $defaults['first_name'],
1546 'last_name' => $defaults['last_name'],
1547 'contact_type' => 'Individual',
1548 'address' => [1 => $defaults['address'][1]],
1549 'phone' => [1 => $defaults['phone'][1]],
1551 $contact = CRM_Contact_BAO_Contact
::create($params);
1552 $contactId = $contact->id
;
1554 // now, update using a profile with phone, email, address... that share the same location type
1556 'first_name' => $params['first_name'],
1557 'last_name' => $params['first_name'],
1558 'street_address-Primary' => $params['address'][1]['street_address'],
1559 'state_province-Primary' => $params['address'][1]['state_province_id'],
1560 'country-Primary' => $params['address'][1]['country_id'],
1561 'phone-Primary-1' => $params['phone'][1]['phone'],
1562 'phone_ext-Primary-1' => '345',
1565 //create the contact using create profile contact.
1566 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
1568 $this->createLoggedInUser();
1569 // now, emulate the contact update using a profile
1570 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($updatePfParams, $fields, $contactId,
1571 NULL, NULL, NULL, TRUE
1574 //check the contact ids
1575 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
1576 $phone = $this->callAPISuccess('Phone', 'getsingle', ['contact_id' => $contactID]);
1577 $this->assertEquals('345', $phone['phone_ext']);
1578 $this->assertEquals($params['phone'][1]['phone'], $phone['phone']);
1580 //check the values in DB.
1582 'contact_id' => $contactId,
1583 'location_type_id' => 1,
1587 'street_address' => $updatePfParams['street_address-Primary'] ??
NULL,
1589 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1591 //cleanup DB by deleting the contact
1592 $this->contactDelete($contactId);
1596 * Test that contact details are still displayed if no email is present.
1598 * @throws \Exception
1600 public function testContactEmailDetailsWithNoPrimaryEmail() {
1601 $params = $this->contactParams();
1602 unset($params['email']);
1603 $contact = CRM_Contact_BAO_Contact
::create($params);
1604 $contactId = $contact->id
;
1605 $result = CRM_Contact_BAO_Contact_Location
::getEmailDetails($contactId);
1606 $this->assertEquals([$contact->display_name
, NULL, NULL, NULL], $result);
1610 * dev/core#1605 State/province not copied on shared address
1611 * 1. First, create contacts: A and B
1612 * 2. Create an address for contact A
1613 * 3. Use contact A's address for contact B's address
1614 * ALL the address fields on address A should be copied to address B
1616 public function testSharedAddressCopiesAllAddressFields() {
1617 $contactIdA = $this->individualCreate([], 0);
1618 $contactIdB = $this->individualCreate([], 1);
1621 'street_address' => '123 Fake St.',
1622 'location_type_id' => '1',
1623 'is_primary' => '1',
1624 'contact_id' => $contactIdA,
1625 'street_name' => 'Ambachtstraat',
1626 'street_number' => '23',
1627 'street_address' => 'Ambachtstraat 23',
1628 'postal_code' => '6971 BN',
1629 'country_id' => '1152',
1630 'city' => 'Brummen',
1632 'state_province_id' => '3934',
1634 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
1636 $addressParamsB[1] = [
1637 'contact_id' => $contactIdB,
1638 'master_id' => $addAddressA->id
,
1639 'use_shared_address' => 1,
1642 CRM_Contact_BAO_Contact_Utils
::processSharedAddress($addressParamsB);
1643 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB[1], FALSE);
1645 foreach ($addAddressA as $key => $value) {
1646 if (!in_array($key, ['id', 'contact_id', 'master_id', 'is_primary', 'is_billing', 'location_type_id', 'manual_geo_code'])) {
1647 $this->assertEquals($addAddressA->$key, $addAddressB->$key);
1653 * Test that long unicode individual names are truncated properly when
1654 * creating sort/display name.
1656 * @dataProvider longUnicodeIndividualNames
1658 * @param array $input
1659 * @param array $expected
1661 public function testLongUnicodeIndividualName(array $input, array $expected) {
1662 // needs to be passed by reference
1664 'contact_type' => 'Individual',
1665 'first_name' => $input['first_name'],
1666 'last_name' => $input['last_name'],
1668 $contact = CRM_Contact_BAO_Contact
::add($params);
1670 $this->assertEquals($expected['sort_name'], $contact->sort_name
);
1671 $this->assertEquals($expected['display_name'], $contact->display_name
);
1673 $this->contactDelete($contact->id
);
1677 * Data provider for testLongUnicodeIndividualName
1680 public function longUnicodeIndividualNames():array {
1682 'much less than 128' => [
1684 'first_name' => 'асдадасда',
1685 'last_name' => 'лнплнплнп',
1688 'sort_name' => 'лнплнплнп, асдадасда',
1689 'display_name' => 'асдадасда лнплнплнп',
1692 'less than 128 but still too big' => [
1694 'first_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдаш',
1695 'last_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпш',
1698 'sort_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпш, асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдаш',
1699 'display_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдаш лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпш',
1702 // note we have to account for the comma and space
1703 'equal 128 sort_name' => [
1705 'first_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасд',
1706 'last_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп',
1709 'sort_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп, асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасд',
1710 'display_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасд лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп',
1713 // note we have to account for the space
1714 'equal 128 display_name' => [
1716 'first_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдa',
1717 'last_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп',
1720 'sort_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп, асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасд',
1721 'display_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдa лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнп',
1724 'longer than 128' => [
1726 'first_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдаш',
1727 'last_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпш',
1730 'sort_name' => 'лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпш, асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдада',
1731 'display_name' => 'асдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдашасдадасдаш лнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнплнпшлнплнпл',
1738 * Test that long unicode org names are truncated properly when creating
1739 * sort/display name.
1741 * @dataProvider longUnicodeOrgNames
1743 * @param string $input
1744 * @param string $expected
1746 public function testLongUnicodeOrgName(string $input, string $expected) {
1747 // needs to be passed by reference
1749 'contact_type' => 'Organization',
1750 'organization_name' => $input,
1752 $contact = CRM_Contact_BAO_Contact
::add($params);
1754 $this->assertEquals($expected, $contact->sort_name
);
1755 $this->assertEquals($expected, $contact->display_name
);
1757 $this->contactDelete($contact->id
);
1761 * Data provider for testLongUnicodeOrgName
1764 public function longUnicodeOrgNames():array {
1766 'much less than 128' => [
1767 'асдадасда шшшшшшшшшш',
1768 'асдадасда шшшшшшшшшш',
1770 'less than 128 but still too big' => [
1771 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшасд',
1772 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшасд',
1775 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшасд',
1776 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшасд',
1778 'longer than 128' => [
1779 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшасдасд',
1780 'асдадасда шшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшшш...',
1786 * Show age of contact on Deceased date
1788 public function testAgeOfDeceasedContact() {
1789 $birthDate = '1961-06-06';
1790 $deceasedDate = '1991-07-07';
1791 $age = CRM_Utils_Date
::calculateAge($birthDate, $deceasedDate);
1792 $this->assertEquals('30', $age['years']);
1796 * Show age of Contact with current date
1798 public function testAgeOfNormalContact() {
1799 $birthDate = '1961-06-06';
1800 $age = CRM_Utils_Date
::calculateAge($birthDate);
1801 $this->assertGreaterThanOrEqual('59', $age['years']);
1805 * Test invalidateChecksum hook.
1807 public function testInvalidateChecksumHook() {
1808 $contact_id = $this->individualCreate();
1809 $checksum = CRM_Contact_BAO_Contact_Utils
::generateChecksum($contact_id);
1810 // without the hook it's valid
1811 $this->assertTrue(CRM_Contact_BAO_Contact_Utils
::validChecksum($contact_id, $checksum));
1812 $this->hookClass
->setHook('civicrm_invalidateChecksum', [$this, 'hookForInvalidateChecksum']);
1813 // with the hook it should be invalid, because our hook implementation says so
1814 $this->assertFalse(CRM_Contact_BAO_Contact_Utils
::validChecksum($contact_id, $checksum));
1818 * Hook for invalidateChecksum.
1820 * @param int $contactID
1821 * @param string $inputCheck
1822 * @param bool $invalid
1824 public function hookForInvalidateChecksum(int $contactID, string $inputCheck, bool &$invalid) {
1825 // invalidate all checksums