2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
7 * Class CRM_Contact_BAO_ContactTest
9 class CRM_Contact_BAO_ContactTest
extends CiviUnitTestCase
{
15 'name' => 'Contact BAOs',
16 'description' => 'Test all Contact_BAO_Contact methods.',
17 'group' => 'CiviCRM BAO Tests',
26 * test case for add( )
27 * test with empty params.
29 function testAddWithEmptyParams() {
31 $contact = CRM_Contact_BAO_Contact
::add($params);
33 //Now check Contact object
34 $this->assertNull($contact);
38 * test case for add( )
40 * (create and update modes)
42 function testAddWithNames() {
44 $lastName = 'Whatson';
46 'first_name' => $firstName,
47 'last_name' => $lastName,
48 'contact_type' => 'Individual',
51 $contact = CRM_Contact_BAO_Contact
::add($params);
53 //Now check $contact is object of contact DAO..
54 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
55 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
56 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
58 $contactId = $contact->id
;
60 //update and change first name and last name, using add( )
63 'first_name' => $firstName,
64 'contact_type' => 'Individual',
65 'contact_id' => $contactId,
68 $contact = CRM_Contact_BAO_Contact
::add($params);
70 //Now check $contact is object of contact DAO..
71 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
72 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
74 $contactId = $contact->id
;
75 Contact
::delete($contactId);
79 * test case for add( )
80 * test with all contact params
81 * (creat and update modes)
83 function testAddWithAll() {
84 //take the common contact params
85 $params = $this->contactParams();
87 unset($params['location']);
88 $prefComm = $params['preferred_communication_method'];
90 //create the contact using add()
91 $contact = CRM_Contact_BAO_Contact
::add($params);
92 $contactId = $contact->id
;
94 //Now check $contact is object of contact DAO..
95 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
97 //Now check values of object with params.
98 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
99 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
100 $this->assertEquals($params['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
101 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
102 $this->assertEquals('1', $contact->do_not_email
, 'Check for do_not_email creation.');
103 $this->assertEquals('1', $contact->do_not_phone
, 'Check for do_not_phone creation.');
104 $this->assertEquals('1', $contact->do_not_mail
, 'Check for do_not_mail creation.');
105 $this->assertEquals('1', $contact->do_not_trade
, 'Check for do_not_trade creation.');
106 $this->assertEquals('1', $contact->is_opt_out
, 'Check for is_opt_out creation.');
107 $this->assertEquals($params['external_identifier'], $contact->external_identifier
, 'Check for external_identifier creation.');
108 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name
, 'Check for sort_name creation.');
109 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format
,
110 'Check for preferred_mail_format creation.'
112 $this->assertEquals($params['contact_source'], $contact->source
, 'Check for contact_source creation.');
113 $this->assertEquals($params['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
114 $this->assertEquals($params['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
115 $this->assertEquals($params['job_title'], $contact->job_title
, 'Check for job_title creation.');
116 $this->assertEquals($params['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
117 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
118 $this->assertEquals(CRM_Utils_Date
::processDate($params['birth_date']),
119 $contact->birth_date
, 'Check for birth_date creation.'
121 $this->assertEquals(CRM_Utils_Date
::processDate($params['deceased_date']),
122 $contact->deceased_date
, 'Check for deceased_date creation.'
124 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
125 $contact->preferred_communication_method
127 $checkPrefComm = array();
128 foreach ($dbPrefComm as $key => $value) {
130 $checkPrefComm[$value] = 1;
133 $this->assertAttributesEquals($checkPrefComm, $prefComm);
135 //now update the contact using add( )
136 $updateParams = array(
137 'contact_type' => 'Individual',
138 'first_name' => 'Jane',
139 'middle_name' => 'abc',
140 'last_name' => 'Doe',
143 'nick_name' => 'Nick Name Second',
144 'job_title' => 'software Developer',
149 'website_type_id' => 1,
150 'url' => 'http://docs.civicrm.org',
153 'contact_source' => 'test update contact',
154 'external_identifier' => 111111111,
155 'preferred_mail_format' => 'Both',
157 'deceased_date' => '1981-03-03',
158 'birth_date' => '1951-04-04',
165 'preferred_communication_method' => array(
174 $prefComm = $updateParams['preferred_communication_method'];
175 $updateParams['contact_id'] = $contactId;
176 //create the contact using add()
177 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
178 $contactId = $contact->id
;
180 //Now check $contact is object of contact DAO..
181 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
183 //Now check values of object with params.
184 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
185 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
186 $this->assertEquals($updateParams['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
187 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
188 $this->assertEquals('0', $contact->do_not_email
, 'Check for do_not_email creation.');
189 $this->assertEquals('0', $contact->do_not_phone
, 'Check for do_not_phone creation.');
190 $this->assertEquals('0', $contact->do_not_mail
, 'Check for do_not_mail creation.');
191 $this->assertEquals('0', $contact->do_not_trade
, 'Check for do_not_trade creation.');
192 $this->assertEquals('0', $contact->is_opt_out
, 'Check for is_opt_out creation.');
193 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier
,
194 'Check for external_identifier creation.'
196 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
197 $contact->sort_name
, 'Check for sort_name creation.'
199 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format
,
200 'Check for preferred_mail_format creation.'
202 $this->assertEquals($updateParams['contact_source'], $contact->source
, 'Check for contact_source creation.');
203 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
204 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
205 $this->assertEquals($updateParams['job_title'], $contact->job_title
, 'Check for job_title creation.');
206 $this->assertEquals($updateParams['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
207 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
208 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['birth_date']),
209 date('YmdHis', strtotime($contact->birth_date
)), 'Check for birth_date creation.'
211 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['deceased_date']),
212 date('YmdHis', strtotime($contact->deceased_date
)), 'Check for deceased_date creation.'
214 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
215 $contact->preferred_communication_method
217 $checkPrefComm = array();
218 foreach ($dbPrefComm as $key => $value) {
220 $checkPrefComm[$value] = 1;
223 $this->assertAttributesEquals($checkPrefComm, $prefComm);
225 //cleanup DB by deleting the contact
226 Contact
::delete($contactId);
230 * test case for add( )
231 * test with All contact types.
233 function testAddWithAllContactTypes() {
237 'first_name' => $firstName,
238 'last_name' => $lastName,
239 'contact_type' => 'Individual',
242 $contact = CRM_Contact_BAO_Contact
::add($params);
243 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
244 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
246 $contactId = $contact->id
;
248 //update and change first name and last name, using create()
251 'first_name' => $firstName,
252 'contact_type' => 'Individual',
253 'contact_id' => $contactId,
256 $contact = CRM_Contact_BAO_Contact
::add($params);
257 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
258 $contactId = $contact->id
;
259 Contact
::delete($contactId);
261 $householdName = 'Adams house';
263 'household_name' => $householdName,
264 'contact_type' => 'Household',
266 $contact = CRM_Contact_BAO_Contact
::add($params);
267 $this->assertEquals($householdName, $contact->sort_name
, 'Check for created household.');
268 $contactId = $contact->id
;
270 //update and change name of household, using create
271 $householdName = 'Joans home';
273 'household_name' => $householdName,
274 'contact_type' => 'Household',
275 'contact_id' => $contactId,
277 $contact = CRM_Contact_BAO_Contact
::add($params);
278 $this->assertEquals($householdName, $contact->sort_name
, 'Check for updated household.');
279 Contact
::delete($contactId);
281 $organizationName = 'My Organization';
283 'organization_name' => $organizationName,
284 'contact_type' => 'Organization',
286 $contact = CRM_Contact_BAO_Contact
::add($params);
287 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for created organization.');
288 $contactId = $contact->id
;
290 //update and change name of organization, using create
291 $organizationName = 'Your Changed Organization';
293 'organization_name' => $organizationName,
294 'contact_type' => 'Organization',
295 'contact_id' => $contactId,
297 $contact = CRM_Contact_BAO_Contact
::add($params);
298 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for updated organization.');
299 Contact
::delete($contactId);
303 * test case for create( )
304 * test with missing params.
306 function testCreateWithEmptyParams() {
308 'first_name' => 'Bill',
309 'last_name' => 'Adams',
311 $contact = CRM_Contact_BAO_Contact
::create($params);
313 //Now check Contact object
314 $this->assertNull($contact);
318 * test case for create( )
319 * test with all params.
320 * ( create and update modes ).
322 function testCreateWithAll() {
323 //take the common contact params
324 $params = $this->contactParams();
325 $params['note'] = 'test note';
327 //create the contact with given params.
328 $contact = CRM_Contact_BAO_Contact
::create($params);
330 //Now check $contact is object of contact DAO..
331 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
332 $contactId = $contact->id
;
334 //Now check values of contact object with params.
335 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
336 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
337 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
339 //Now check DB for Address
340 $searchParams = array(
341 'contact_id' => $contactId,
342 'location_type_id' => 1,
345 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address', $params['address'][1]),
346 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1',
347 $params['address'][1]
349 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2',
350 $params['address'][1]
352 'city' => CRM_Utils_Array
::value('city', $params['address'][1]),
353 'postal_code' => CRM_Utils_Array
::value('postal_code', $params['address'][1]),
354 'country_id' => CRM_Utils_Array
::value('country_id', $params['address'][1]),
355 'state_province_id' => CRM_Utils_Array
::value('state_province_id',
356 $params['address'][1]
358 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1', $params['address'][1]),
359 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2', $params['address'][1]),
361 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
363 //Now check DB for Email
364 $compareParams = array('email' => CRM_Utils_Array
::value('email', $params['email'][1]));
365 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
367 //Now check DB for openid
368 $compareParams = array('openid' => CRM_Utils_Array
::value('openid', $params['openid'][1]));
369 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
371 //Now check DB for IM
372 $compareParams = array('name' => CRM_Utils_Array
::value('name', $params['im'][1]),
373 'provider_id' => CRM_Utils_Array
::value('provider_id', $params['im'][1]),
375 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
377 //Now check DB for Phone
378 $searchParams = array(
379 'contact_id' => $contactId,
380 'location_type_id' => 1,
382 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][1]),
384 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][1]));
385 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
387 //Now check DB for Mobile
388 $searchParams = array(
389 'contact_id' => $contactId,
390 'location_type_id' => 1,
391 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
393 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][2]));
394 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
396 //Now check DB for Note
397 $searchParams = array(
398 'entity_id' => $contactId,
399 'entity_table' => 'civicrm_contact',
401 $compareParams = array('note' => $params['note']);
402 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
404 //update the contact.
405 $updateParams = array(
406 'first_name' => 'John',
407 'last_name' => 'Doe',
408 'contact_type' => 'Individual',
409 'note' => 'new test note',
411 $updateParams['address'][1] = array(
412 'location_type_id' => 1,
414 'street_address' => 'Oberoi Garden',
415 'supplemental_address_1' => 'A-wing:3037',
416 'supplemental_address_2' => 'Andhery',
418 'postal_code' => '12345',
419 'country_id' => 1228,
420 'state_province_id' => 1004,
421 'geo_code_1' => '31.694842',
422 'geo_code_2' => '-106.29998',
424 $updateParams['email'][1] = array(
425 'location_type_id' => 1,
427 'email' => 'john.doe@example.org',
430 $updateParams['phone'][1] = array(
431 'location_type_id' => 1,
433 'phone_type_id' => 1,
434 'phone' => '02115245336',
436 $updateParams['phone'][2] = array(
437 'location_type_id' => 1,
438 'phone_type_id' => 2,
439 'phone' => '9766323895',
442 $updateParams['contact_id'] = $contactId;
443 //create the contact with given params.
444 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
446 //Now check $contact is object of contact DAO..
447 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
448 $contactId = $contact->id
;
450 //Now check values of contact object with updated params.
451 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
452 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
453 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
455 //Now check DB for updated Address
456 $searchParams = array(
457 'contact_id' => $contactId,
458 'location_type_id' => 1,
461 $compareParams = array(
462 'street_address' => 'Oberoi Garden',
463 'supplemental_address_1' => 'A-wing:3037',
464 'supplemental_address_2' => 'Andhery',
466 'postal_code' => '12345',
467 'country_id' => 1228,
468 'state_province_id' => 1004,
469 'geo_code_1' => '31.694842',
470 'geo_code_2' => '-106.29998',
472 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
474 //Now check DB for updated Email
475 $compareParams = array('email' => 'john.doe@example.org');
476 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
478 //Now check DB for updated Phone
479 $searchParams = array(
480 'contact_id' => $contactId,
481 'location_type_id' => 1,
483 'phone_type_id' => 1,
485 $compareParams = array('phone' => '02115245336');
486 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
488 //Now check DB for updated Mobile
489 $searchParams = array(
490 'contact_id' => $contactId,
491 'location_type_id' => 1,
492 'phone_type_id' => 2,
494 $compareParams = array('phone' => '9766323895');
495 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
496 //As we are not updating note
497 //Now check DB for New Note
498 $noteId = $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
499 'Database check for New created note '
502 //delete all notes related to contact
503 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
506 //cleanup DB by deleting the contact
507 Contact
::delete($contactId);
508 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
512 * test case for resolveDefaults( )
513 * test all pseudoConstant, stateProvince, country.
515 function testResolveDefaults() {
520 'birth_date' => '1983-12-13',
523 $params['address'][1] = array(
524 'location_type_id' => 1,
526 'country_id' => 1228,
527 'state_province_id' => 1004,
529 CRM_Contact_BAO_Contact
::resolveDefaults($params);
531 //check the resolve values.
532 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
533 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
534 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
535 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
536 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
537 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
538 $this->assertEquals(CRM_Core_PseudoConstant
::stateProvince($params['address'][1]['state_province_id']),
539 $params['address'][1]['state_province'],
540 'Check for state province.'
542 $this->assertEquals(CRM_Core_PseudoConstant
::country($params['address'][1]['country_id']),
543 $params['address'][1]['country'],
549 * test case for retrieve( )
550 * test with all values.
552 function testRetrieve() {
553 //take the common contact params
554 $params = $this->contactParams();
555 $params['note'] = 'test note';
557 //create the contact with given params.
558 $contact = CRM_Contact_BAO_Contact
::create($params);
559 //Now check $contact is object of contact DAO..
560 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
561 $contactId = $contact->id
;
562 //create the organization contact with the given params.
564 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
565 'contact_type' => 'Organization',
567 $orgContact = CRM_Contact_BAO_Contact
::add($orgParams);
568 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
570 //create employee of relationship.
571 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $orgContact->id
);
573 //retrieve the contact values from database.
575 $searchParams = array('contact_id' => $contactId);
576 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
578 //Now check $retrieveContact is object of contact DAO..
579 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
582 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
584 //Now check values retrieve from database with params.
585 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
586 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
587 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
589 //Now check values of address
590 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
591 CRM_Utils_Array
::value('1', $values['address'])
594 //Now check values of email
595 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
596 CRM_Utils_Array
::value('1', $values['email'])
599 //Now check values of phone
600 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
601 CRM_Utils_Array
::value('1', $values['phone'])
604 //Now check values of mobile
605 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
606 CRM_Utils_Array
::value('2', $values['phone'])
609 //Now check values of openid
610 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
611 CRM_Utils_Array
::value('1', $values['openid'])
614 //Now check values of im
615 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
616 CRM_Utils_Array
::value('1', $values['im'])
619 //Now check values of Note Count.
620 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
622 foreach ($values['note'] as $key => $val) {
623 $retrieveNote = CRM_Utils_Array
::value('note', $val);
624 //check the note value
625 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
628 //Now check values of Relationship Count.
629 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
630 foreach ($values['relationship']['data'] as $key => $val) {
631 //Now check values of Relationship organization.
632 $this->assertEquals($orgContact->id
, $val['contact_id_b'], 'Check for organization');
633 //Now check values of Relationship type.
634 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
635 //delete the organization.
636 Contact
::delete(CRM_Utils_Array
::value('contact_id_b', $val));
639 //delete all notes related to contact
640 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
643 //cleanup DB by deleting the contact
644 Contact
::delete($contactId);
645 $this->quickCleanup(array('civicrm_contact'));
649 * test case for deleteContact( )
651 function testDeleteContact() {
652 $contactParams = $this->contactParams();
655 require_once 'CiviTest/Custom.php';
656 $customGroup = Custom
::createGroup(array(), 'Individual');
658 'label' => 'testFld',
659 'data_type' => 'String',
660 'html_type' => 'Text',
661 'custom_group_id' => $customGroup->id
,
663 $customField = CRM_Core_BAO_CustomField
::create($fields);
664 $contactParams['custom'] = array(
665 $customField->id
=> array(
667 'value' => 'Test custom value',
669 'custom_field_id' => $customField->id
,
670 'custom_group_id' => $customGroup->id
,
671 'table_name' => $customGroup->table_name
,
672 'column_name' => $customField->column_name
,
679 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
680 $contactId = $contact->id
;
683 CRM_Contact_BAO_Contact
::deleteContact($contactId);
685 //Now check DB for location elements.
686 //Now check DB for Address
688 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
689 'id', 'street_address', 'Database check, Address deleted successfully.'
692 //Now check DB for Email
693 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
694 'id', 'email', 'Database check, Email deleted successfully.'
696 //Now check DB for Phone
697 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
698 'id', 'phone', 'Database check, Phone deleted successfully.'
700 //Now check DB for Mobile
701 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
702 'id', 'phone', 'Database check, Mobile deleted successfully.'
704 //Now check DB for IM
705 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
706 'id', 'name', 'Database check, IM deleted successfully.'
708 //Now check DB for openId
709 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
710 'id', 'openid', 'Database check, openId deleted successfully.'
714 // Check that the custom field value is no longer present
716 'entityID' => $contactId,
717 'custom_' . $customField->id
=> 1,
719 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
720 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
721 'Verify that the data value is empty for contact ' . $contactId
723 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
725 //Now check DB for contact.
726 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
727 'id', 'sort_name', 'Database check, contact deleted successfully.'
729 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
730 Custom
::deleteGroup($customGroup);
734 * test case for createProfileContac( )
735 * test with all params.
736 * ( create and update modes )
738 function testCreateProfileContact() {
739 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
741 //current employer field for individual
742 $fields['organization_name'] = array(
743 'name' => 'organization_name',
744 'where' => 'civicrm_organization.organization_name',
745 'title' => 'Current Employer',
747 //get the common params
748 $contactParams = $this->contactParams();
749 $unsetParams = array('location', 'privacy');
750 foreach ($unsetParams as $param) {
751 unset($contactParams[$param]);
754 $profileParams = array(
755 'organization_name' => 'Yahoo',
759 'city-Primary' => 'Newark',
760 'contact_type' => 'Individual',
761 'country-Primary' => '1228',
762 'do_not_email' => '1',
763 'do_not_mail' => '1',
764 'do_not_phone' => '1',
765 'do_not_trade' => '1',
767 'email-Primary' => 'john.smith@example.org',
768 'geo_code_1-Primary' => '18.219023',
769 'geo_code_2-Primary' => '-105.00973',
770 'im-Primary-provider_id' => '1',
771 'im-Primary' => 'john.smith',
773 'openid' => 'john.smith@example.org',
774 'phone-Primary-1' => '303443689',
775 'phone-Primary-2' => '9833910234',
776 'postal_code-Primary' => '01903',
777 'postal_code_suffix-Primary' => '12345',
778 'state_province-Primary' => '1029',
779 'street_address-Primary' => 'Saint Helier St',
780 'supplemental_address_1-Primary' => 'Hallmark Ct',
781 'supplemental_address_2-Primary' => 'Jersey Village',
782 'user_unique_id' => '123456789',
783 'is_bulkmail' => '1',
784 'world_region' => 'India',
791 $createParams = array_merge($contactParams, $profileParams);
793 //create the contact using create profile contact.
794 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
796 //get the parameters to compare.
797 $params = $this->contactParams();
799 //check the values in DB.
800 foreach ($params as $key => $val) {
801 if (!is_array($params[$key])) {
802 if ($key == 'contact_source') {
803 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
804 'id', $params[$key], "Check for {$key} creation."
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
809 'id', $params[$key], "Check for {$key} creation."
815 //check privacy options.
816 foreach ($params['privacy'] as $key => $value) {
817 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
818 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
822 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
823 'id', $profileParams['contact_type'], 'Check for contact type creation.'
825 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
826 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
829 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
830 'id', $params['birth_date'], 'Check for birth_date creation.'
833 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
834 'id', $params['deceased_date'], 'Check for deceased_date creation.'
837 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
838 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
840 $checkPrefComm = array();
841 foreach ($dbPrefComm as $key => $value) {
843 $checkPrefComm[$value] = 1;
846 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
848 //Now check DB for Address
849 $searchParams = array(
850 'contact_id' => $contactId,
851 'location_type_id' => 1,
854 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $profileParams),
855 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $profileParams),
856 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $profileParams),
857 'city' => CRM_Utils_Array
::value('city-Primary', $profileParams),
858 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $profileParams),
859 'country_id' => CRM_Utils_Array
::value('country-Primary', $profileParams),
860 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $profileParams),
861 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $profileParams),
862 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $profileParams),
864 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
866 //Now check DB for Email
867 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $profileParams));
868 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
870 //Now check DB for IM
871 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $profileParams),
872 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $profileParams),
874 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
876 //Now check DB for Phone
877 $searchParams = array(
878 'contact_id' => $contactId,
879 'location_type_id' => 1,
882 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams));
883 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
885 //Now check DB for Mobile
886 $searchParams = array(
887 'contact_id' => $contactId,
888 'location_type_id' => 1,
889 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
891 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams));
893 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
895 //get the value of relationship
897 $searchParams = array('contact_id' => $contactId);
898 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
899 //Now check values of Relationship Count.
900 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
901 foreach ($values['relationship']['data'] as $key => $val) {
902 //Now check values of Relationship organization.
903 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
904 //Now check values of Relationship type.
905 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
906 //delete the organization.
907 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
910 //Now check values of tag ids.
911 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
912 foreach ($tags as $key => $val) {
916 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
918 //update Contact mode
919 $updateCParams = array(
920 'first_name' => 'john',
921 'last_name' => 'doe',
922 'contact_type' => 'Individual',
923 'middle_name' => 'abc',
926 'nick_name' => 'Nick Name Updated',
927 'job_title' => 'software Developer',
932 'website_type_id' => 1,
933 'url' => 'http://civicrmUpdate.org',
936 'contact_source' => 'test contact',
937 'external_identifier' => 111222333,
938 'preferred_mail_format' => 'Both',
940 'legal_identifier' => '123123123123',
941 'image_URL' => 'http://imageupdate.com',
942 'deceased_date' => '1981-10-10',
943 'birth_date' => '1951-11-11',
948 'preferred_communication_method' => array(
957 $updatePfParams = array(
958 'organization_name' => 'Google',
959 'city-Primary' => 'Mumbai',
960 'contact_type' => 'Individual',
961 'country-Primary' => '1228',
962 'do_not_email' => '1',
963 'do_not_mail' => '1',
964 'do_not_phone' => '1',
965 'do_not_trade' => '1',
967 'email-Primary' => 'john.doe@example.org',
968 'geo_code_1-Primary' => '31.694842',
969 'geo_code_2-Primary' => '-106.29998',
970 'im-Primary-provider_id' => '1',
971 'im-Primary' => 'john.doe',
973 'openid' => 'john.doe@example.org',
974 'phone-Primary-1' => '02115245336',
975 'phone-Primary-2' => '9766323895',
976 'postal_code-Primary' => '12345',
977 'postal_code_suffix-Primary' => '123',
978 'state_province-Primary' => '1004',
979 'street_address-Primary' => 'Oberoi Garden',
980 'supplemental_address_1-Primary' => 'A-wing:3037',
981 'supplemental_address_2-Primary' => 'Andhery',
982 'user_unique_id' => '1122334455',
983 'is_bulkmail' => '1',
984 'world_region' => 'India',
991 $createParams = array_merge($updateCParams, $updatePfParams);
993 //create the contact using create profile contact.
994 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
995 NULL, NULL, NULL, TRUE
998 //check the contact ids
999 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
1001 //check the values in DB.
1002 foreach ($updateCParams as $key => $val) {
1003 if (!is_array($updateCParams[$key])) {
1004 if ($key == 'contact_source') {
1005 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
1006 'id', $updateCParams[$key], "Check for {$key} creation."
1010 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1011 'id', $updateCParams[$key], "Check for {$key} creation."
1017 //check privacy options.
1018 foreach ($updateCParams['privacy'] as $key => $value) {
1019 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1020 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1024 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1025 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1027 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1028 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1031 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1032 $updateCParams['birth_date'], 'Check for birth_date creation.'
1035 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1036 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1039 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1040 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1042 $checkPrefComm = array();
1043 foreach ($dbPrefComm as $key => $value) {
1045 $checkPrefComm[$value] = 1;
1048 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1050 //Now check DB for Address
1051 $searchParams = array(
1052 'contact_id' => $contactId,
1053 'location_type_id' => 1,
1056 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $updatePfParams),
1057 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $updatePfParams),
1058 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $updatePfParams),
1059 'city' => CRM_Utils_Array
::value('city-Primary', $updatePfParams),
1060 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $updatePfParams),
1061 'country_id' => CRM_Utils_Array
::value('country-Primary', $updatePfParams),
1062 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $updatePfParams),
1063 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $updatePfParams),
1064 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $updatePfParams),
1066 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1068 //Now check DB for Email
1069 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams));
1070 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1072 //Now check DB for IM
1073 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $updatePfParams),
1074 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $updatePfParams),
1076 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1078 //Now check DB for Phone
1079 $searchParams = array(
1080 'contact_id' => $contactId,
1081 'location_type_id' => 1,
1084 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams));
1085 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1087 //Now check DB for Mobile
1088 $searchParams = array(
1089 'contact_id' => $contactId,
1090 'location_type_id' => 1,
1091 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
1093 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams));
1094 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1096 //get the value of relationship
1098 $searchParams = array('contact_id' => $contactId);
1099 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1100 //Now check values of Relationship Count.
1101 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1102 foreach ($values['relationship']['data'] as $key => $val) {
1103 //Now check values of Relationship organization.
1104 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1105 //Now check values of Relationship type.
1106 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1107 //delete the organization.
1108 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
1111 //Now check values of tag ids.
1112 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1113 foreach ($tags as $key => $val) {
1116 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1118 //cleanup DB by deleting the contact
1119 Contact
::delete($contactId);
1123 * test case for getContactDetails( )
1125 function testGetContactDetails() {
1126 //get the contact params
1127 $params = $this->contactParams();
1130 $contact = CRM_Contact_BAO_Contact
::create($params);
1131 $contactId = $contact->id
;
1133 //get the contact details
1134 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1135 $compareParams = array(
1136 $params['first_name'] . ' ' . $params['last_name'],
1137 CRM_Utils_Array
::value('email', $params['email'][1]),
1138 (bool ) $params['privacy']['do_not_email'],
1140 //Now check the contact details
1141 $this->assertAttributesEquals($compareParams, $contactDetails);
1143 //cleanup DB by deleting the contact
1144 Contact
::delete($contactId);
1145 $this->quickCleanup(array('civicrm_contact'));
1150 * importableFields( ) and exportableFields( )
1152 function testFields() {
1153 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1154 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1155 //Now check all fields
1156 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1158 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1159 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1160 //Now check Individual fields
1161 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1163 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1164 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1165 //Now check Household fields
1166 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1168 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1169 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1170 //Now check Organization fields
1171 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1175 * test case for getPrimaryEmail( )
1178 function testGetPrimaryEmail() {
1179 //get the contact params
1180 $params = $this->contactParams();
1181 $params['email'][2] = $params['email'][1];
1182 $params['email'][2]['email'] = 'primarymail@example.org';
1183 unset($params['email'][1]['is_primary']);
1186 $contact = CRM_Contact_BAO_Contact
::create($params);
1187 $contactId = $contact->id
;
1188 //get the primary email.
1189 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1190 //Now check the primary email
1191 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1193 //cleanup DB by deleting the contact
1194 Contact
::delete($contactId);
1195 $this->quickCleanup(array('civicrm_contact'));
1199 * test case for getPrimaryOpenId( )
1202 function testGetPrimaryOpenId() {
1203 //get the contact params
1204 $params = $this->contactParams();
1205 $params['openid'][2] = $params['openid'][1];
1206 $params['openid'][2]['location_type_id'] = 2;
1207 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1208 unset($params['openid'][1]['is_primary']);
1211 $contact = CRM_Contact_BAO_Contact
::create($params);
1212 $contactId = $contact->id
;
1213 //get the primary openid
1214 $openID = CRM_Contact_BAO_Contact
::getPrimaryOpenId($contactId);
1216 //Now check the primary openid
1217 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1219 //cleanup DB by deleting the contact
1220 Contact
::delete($contactId);
1224 * test case for matchContactOnEmail( )
1227 function testMatchContactOnEmail() {
1228 //get the contact params
1229 $params = $this->contactParams();
1231 $contact = CRM_Contact_BAO_Contact
::create($params);
1232 $contactId = $contact->id
;
1234 //get the matching contact.
1235 $match = CRM_Contact_BAO_Contact
::matchContactOnEmail(CRM_Utils_Array
::value('email', $params['email'][1]),
1238 $this->assertEquals($contactId, $match->contact_id
, 'Check For Matching Contact');
1240 //cleanup DB by deleting the contact
1241 Contact
::delete($contactId);
1242 $this->quickCleanup(array('civicrm_contact'));
1246 * test case for getContactType( )
1249 function testGetContactType() {
1250 //get the contact params
1251 $params = $this->contactParams();
1253 $contact = CRM_Contact_BAO_Contact
::create($params);
1254 $contactId = $contact->id
;
1257 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1258 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1260 //cleanup DB by deleting the contact
1261 Contact
::delete($contactId);
1262 $this->quickCleanup(array('civicrm_contact'));
1266 * test case for displayName( )
1269 function testDisplayName() {
1270 //get the contact params
1271 $params = $this->contactParams();
1274 $contact = CRM_Contact_BAO_Contact
::create($params);
1275 $contactId = $contact->id
;
1278 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1280 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1281 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1283 //build display name
1284 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1286 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1288 //cleanup DB by deleting the contact
1289 Contact
::delete($contactId);
1290 $this->quickCleanup(array('civicrm_contact'));
1294 * test case for getDisplayAndImage( )
1297 function testGetDisplayAndImage() {
1298 //get the contact params
1299 $params = $this->contactParams();
1302 $contact = CRM_Contact_BAO_Contact
::create($params);
1303 $contactId = $contact->id
;
1305 //get DisplayAndImage.
1306 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1308 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1310 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1311 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1313 //build display name
1314 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1316 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1317 $this->assertEquals($image, $checkImage, 'Check For Image');
1319 //cleanup DB by deleting the contact
1320 Contact
::delete($contactId);
1324 // * test logged in user has permissions for
1325 // * various operation types
1327 // function testPermissionedContact()
1329 // $contactId = Contact::createIndividual();
1330 // require_once 'CRM/Contact/BAO/Contact/Permission.php';
1331 // $userViewPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId);
1332 // $this->assertEquals( $userViewPermission, true, 'Check user for view contact permission.' );
1334 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1335 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1337 // Contact::delete($contactId);
1341 * function to build common params
1344 private function contactParams() {
1347 'first_name' => 'john',
1348 'last_name' => 'smith',
1349 'contact_type' => 'Individual',
1350 'middle_name' => 'xyz',
1353 'nick_name' => 'Nick Name',
1354 'job_title' => 'software engg',
1359 'website_type_id' => 1,
1360 'url' => 'http://civicrm.org',
1363 'contact_source' => 'test contact',
1364 'external_identifier' => 123456789,
1365 'preferred_mail_format' => 'Both',
1367 'legal_identifier' => '123456789',
1368 'image_URL' => 'http://image.com',
1369 'deceased_date' => '1991-07-07',
1370 'birth_date' => '1961-06-06',
1372 'do_not_phone' => 1,
1373 'do_not_email' => 1,
1375 'do_not_trade' => 1,
1377 'preferred_communication_method' => array(
1386 $params['address'] = array();
1387 $params['address'][1] = array(
1388 'location_type_id' => 1,
1390 'street_address' => 'Saint Helier St',
1391 'supplemental_address_1' => 'Hallmark Ct',
1392 'supplemental_address_2' => 'Jersey Village',
1394 'postal_code' => '01903',
1395 'country_id' => 1228,
1396 'state_province_id' => 1029,
1397 'geo_code_1' => '18.219023',
1398 'geo_code_2' => '-105.00973',
1401 $params['email'] = array();
1402 $params['email'][1] = array(
1403 'location_type_id' => 1,
1405 'email' => 'john.smith@example.org',
1409 $params['phone'] = array();
1410 $params['phone'][1] = array(
1411 'location_type_id' => 1,
1413 'phone_type_id' => 1,
1414 'phone' => '303443689',
1416 $params['phone'][2] = array(
1417 'location_type_id' => 1,
1418 'phone_type_id' => 2,
1419 'phone' => '9833910234',
1422 $params['openid'] = array();
1423 $params['openid'][1] = array(
1424 'location_type_id' => 1,
1426 'openid' => 'http://civicrm.org/',
1429 $params['im'] = array();
1430 $params['im'][1] = array(
1431 'location_type_id' => 1,
1433 'name' => 'john.smith',
1441 * Ensure that created_date and modified_date are set
1443 function testTimestamps_contact() {
1445 $this->_testTimestamps(array(
1446 'UPDATE' => function ($contactId) use ($test) {
1448 'first_name' => 'Testing',
1449 'contact_type' => 'Individual',
1450 'contact_id' => $contactId,
1452 $contact = CRM_Contact_BAO_Contact
::add($params);
1453 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1459 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1461 function testTimestamps_email() {
1463 $this->_testTimestamps(array(
1464 'INSERT' => function ($contactId) use ($test) {
1466 'email' => 'ex-1@example.com',
1468 'location_type_id' => 1,
1469 'contact_id' => $contactId,
1471 CRM_Core_BAO_Email
::add($params);
1472 $test->assertDBQuery('ex-1@example.com',
1473 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1474 array(1 => array($contactId, 'Integer'))
1478 'UPDATE' => function ($contactId) use ($test) {
1479 CRM_Core_DAO
::executeQuery(
1480 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1481 array(1 => array($contactId, 'Integer'))
1485 'DELETE' => function ($contactId) use ($test) {
1486 CRM_Core_DAO
::executeQuery(
1487 'DELETE FROM civicrm_email WHERE contact_id = %1',
1488 array(1 => array($contactId, 'Integer'))
1495 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1497 function testTimestamps_phone() {
1499 $this->_testTimestamps(array(
1500 'INSERT' => function ($contactId) use ($test) {
1502 'phone' => '202-555-1000',
1504 'location_type_id' => 1,
1505 'contact_id' => $contactId,
1507 CRM_Core_BAO_Phone
::add($params);
1508 $test->assertDBQuery('202-555-1000',
1509 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1510 array(1 => array($contactId, 'Integer'))
1514 'UPDATE' => function ($contactId) use ($test) {
1515 CRM_Core_DAO
::executeQuery(
1516 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1517 array(1 => array($contactId, 'Integer'))
1521 'DELETE' => function ($contactId) use ($test) {
1522 CRM_Core_DAO
::executeQuery(
1523 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1524 array(1 => array($contactId, 'Integer'))
1531 * Ensure that civicrm_contact.modified_date is updated when contact-related
1534 function testTimestamps_custom() {
1535 $customGroup = Custom
::createGroup(array(), 'Individual');
1536 $this->assertNotNull($customGroup);
1538 'custom_group_id' => $customGroup->id
,
1539 'data_type' => 'String',
1540 'html_type' => 'Text',
1542 $customField = $this->customFieldCreate($fields);
1543 $customField = $customField['values'][$customField['id']];
1545 $this->_testTimestamps(array(
1546 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1547 $result = civicrm_api3('contact', 'create', array(
1548 'contact_id' => $contactId,
1549 'custom_' . $customField['id'] => 'test-1',
1552 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1553 CRM_Core_DAO
::executeQuery(
1554 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1555 array(1 => array($contactId, 'Integer'))
1558 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1559 CRM_Core_DAO
::executeQuery(
1560 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1561 array(1 => array($contactId, 'Integer'))
1566 Custom
::deleteGroup($customGroup);
1570 * Helper for testing timestamp manipulation
1572 * Create a contact and perform a series of steps with it; after each
1573 * step, ensure that the contact's modified_date has increased.
1575 * @param $callbacks array ($name => $callable)
1577 function _testTimestamps($callbacks) {
1578 CRM_Core_DAO
::triggerRebuild();
1579 $contactId = Contact
::createIndividual();
1581 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1582 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1583 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1584 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1586 $prevTimestamps = $origTimestamps;
1587 foreach ($callbacks as $callbackName => $callback) {
1588 sleep(1); // advance clock by 1 second to ensure timestamps change
1590 $callback($contactId);
1591 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1592 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1593 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1594 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1595 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1597 $prevTimestamps = $newTimestamps;
1600 Contact
::delete($contactId);