2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
5 class CRM_Contact_BAO_ContactTest
extends CiviUnitTestCase
{
8 'name' => 'Contact BAOs',
9 'description' => 'Test all Contact_BAO_Contact methods.',
10 'group' => 'CiviCRM BAO Tests',
19 * test case for add( )
20 * test with empty params.
22 function testAddWithEmptyParams() {
24 $contact = CRM_Contact_BAO_Contact
::add($params);
26 //Now check Contact object
27 $this->assertNull($contact);
31 * test case for add( )
33 * (create and update modes)
35 function testAddWithNames() {
37 $lastName = 'Whatson';
39 'first_name' => $firstName,
40 'last_name' => $lastName,
41 'contact_type' => 'Individual',
44 $contact = CRM_Contact_BAO_Contact
::add($params);
46 //Now check $contact is object of contact DAO..
47 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
48 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
49 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
51 $contactId = $contact->id
;
53 //update and change first name and last name, using add( )
56 'first_name' => $firstName,
57 'contact_type' => 'Individual',
58 'contact_id' => $contactId,
61 $contact = CRM_Contact_BAO_Contact
::add($params);
63 //Now check $contact is object of contact DAO..
64 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
65 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
67 $contactId = $contact->id
;
68 Contact
::delete($contactId);
72 * test case for add( )
73 * test with all contact params
74 * (creat and update modes)
76 function testAddWithAll() {
77 //take the common contact params
78 $params = $this->contactParams();
80 unset($params['location']);
81 $prefComm = $params['preferred_communication_method'];
83 //create the contact using add()
84 $contact = CRM_Contact_BAO_Contact
::add($params);
85 $contactId = $contact->id
;
87 //Now check $contact is object of contact DAO..
88 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
90 //Now check values of object with params.
91 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
92 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
93 $this->assertEquals($params['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
94 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
95 $this->assertEquals('1', $contact->do_not_email
, 'Check for do_not_email creation.');
96 $this->assertEquals('1', $contact->do_not_phone
, 'Check for do_not_phone creation.');
97 $this->assertEquals('1', $contact->do_not_mail
, 'Check for do_not_mail creation.');
98 $this->assertEquals('1', $contact->do_not_trade
, 'Check for do_not_trade creation.');
99 $this->assertEquals('1', $contact->is_opt_out
, 'Check for is_opt_out creation.');
100 $this->assertEquals($params['external_identifier'], $contact->external_identifier
, 'Check for external_identifier creation.');
101 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name
, 'Check for sort_name creation.');
102 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format
,
103 'Check for preferred_mail_format creation.'
105 $this->assertEquals($params['contact_source'], $contact->source
, 'Check for contact_source creation.');
106 $this->assertEquals($params['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
107 $this->assertEquals($params['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
108 $this->assertEquals($params['job_title'], $contact->job_title
, 'Check for job_title creation.');
109 $this->assertEquals($params['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
110 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
111 $this->assertEquals(CRM_Utils_Date
::processDate($params['birth_date']),
112 $contact->birth_date
, 'Check for birth_date creation.'
114 $this->assertEquals(CRM_Utils_Date
::processDate($params['deceased_date']),
115 $contact->deceased_date
, 'Check for deceased_date creation.'
117 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
118 $contact->preferred_communication_method
120 $checkPrefComm = array();
121 foreach ($dbPrefComm as $key => $value) {
123 $checkPrefComm[$value] = 1;
126 $this->assertAttributesEquals($checkPrefComm, $prefComm);
128 //now update the contact using add( )
129 $updateParams = array(
130 'contact_type' => 'Individual',
131 'first_name' => 'Jane',
132 'middle_name' => 'abc',
133 'last_name' => 'Doe',
136 'nick_name' => 'Nick Name Second',
137 'job_title' => 'software Developer',
142 'website_type_id' => 1,
143 'url' => 'http://docs.civicrm.org',
146 'contact_source' => 'test update contact',
147 'external_identifier' => 111111111,
148 'preferred_mail_format' => 'Both',
150 'deceased_date' => '1981-03-03',
151 'birth_date' => '1951-04-04',
158 'preferred_communication_method' => array(
167 $prefComm = $updateParams['preferred_communication_method'];
168 $updateParams['contact_id'] = $contactId;
169 //create the contact using add()
170 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
171 $contactId = $contact->id
;
173 //Now check $contact is object of contact DAO..
174 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
176 //Now check values of object with params.
177 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
178 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
179 $this->assertEquals($updateParams['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
180 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
181 $this->assertEquals('0', $contact->do_not_email
, 'Check for do_not_email creation.');
182 $this->assertEquals('0', $contact->do_not_phone
, 'Check for do_not_phone creation.');
183 $this->assertEquals('0', $contact->do_not_mail
, 'Check for do_not_mail creation.');
184 $this->assertEquals('0', $contact->do_not_trade
, 'Check for do_not_trade creation.');
185 $this->assertEquals('0', $contact->is_opt_out
, 'Check for is_opt_out creation.');
186 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier
,
187 'Check for external_identifier creation.'
189 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
190 $contact->sort_name
, 'Check for sort_name creation.'
192 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format
,
193 'Check for preferred_mail_format creation.'
195 $this->assertEquals($updateParams['contact_source'], $contact->source
, 'Check for contact_source creation.');
196 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
197 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
198 $this->assertEquals($updateParams['job_title'], $contact->job_title
, 'Check for job_title creation.');
199 $this->assertEquals($updateParams['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
200 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
201 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['birth_date']),
202 date('YmdHis', strtotime($contact->birth_date
)), 'Check for birth_date creation.'
204 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['deceased_date']),
205 date('YmdHis', strtotime($contact->deceased_date
)), 'Check for deceased_date creation.'
207 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
208 $contact->preferred_communication_method
210 $checkPrefComm = array();
211 foreach ($dbPrefComm as $key => $value) {
213 $checkPrefComm[$value] = 1;
216 $this->assertAttributesEquals($checkPrefComm, $prefComm);
218 //cleanup DB by deleting the contact
219 Contact
::delete($contactId);
223 * test case for add( )
224 * test with All contact types.
226 function testAddWithAllContactTypes() {
230 'first_name' => $firstName,
231 'last_name' => $lastName,
232 'contact_type' => 'Individual',
235 $contact = CRM_Contact_BAO_Contact
::add($params);
236 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
237 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
239 $contactId = $contact->id
;
241 //update and change first name and last name, using create()
244 'first_name' => $firstName,
245 'contact_type' => 'Individual',
246 'contact_id' => $contactId,
249 $contact = CRM_Contact_BAO_Contact
::add($params);
250 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
251 $contactId = $contact->id
;
252 Contact
::delete($contactId);
254 $householdName = 'Adams house';
256 'household_name' => $householdName,
257 'contact_type' => 'Household',
259 $contact = CRM_Contact_BAO_Contact
::add($params);
260 $this->assertEquals($householdName, $contact->sort_name
, 'Check for created household.');
261 $contactId = $contact->id
;
263 //update and change name of household, using create
264 $householdName = 'Joans home';
266 'household_name' => $householdName,
267 'contact_type' => 'Household',
268 'contact_id' => $contactId,
270 $contact = CRM_Contact_BAO_Contact
::add($params);
271 $this->assertEquals($householdName, $contact->sort_name
, 'Check for updated household.');
272 Contact
::delete($contactId);
274 $organizationName = 'My Organization';
276 'organization_name' => $organizationName,
277 'contact_type' => 'Organization',
279 $contact = CRM_Contact_BAO_Contact
::add($params);
280 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for created organization.');
281 $contactId = $contact->id
;
283 //update and change name of organization, using create
284 $organizationName = 'Your Changed Organization';
286 'organization_name' => $organizationName,
287 'contact_type' => 'Organization',
288 'contact_id' => $contactId,
290 $contact = CRM_Contact_BAO_Contact
::add($params);
291 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for updated organization.');
292 Contact
::delete($contactId);
296 * test case for create( )
297 * test with missing params.
299 function testCreateWithEmptyParams() {
301 'first_name' => 'Bill',
302 'last_name' => 'Adams',
304 $contact = CRM_Contact_BAO_Contact
::create($params);
306 //Now check Contact object
307 $this->assertNull($contact);
311 * test case for create( )
312 * test with all params.
313 * ( create and update modes ).
315 function testCreateWithAll() {
316 //take the common contact params
317 $params = $this->contactParams();
318 $params['note'] = 'test note';
320 //create the contact with given params.
321 $contact = CRM_Contact_BAO_Contact
::create($params);
323 //Now check $contact is object of contact DAO..
324 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
325 $contactId = $contact->id
;
327 //Now check values of contact object with params.
328 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
329 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
330 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
332 //Now check DB for Address
333 $searchParams = array(
334 'contact_id' => $contactId,
335 'location_type_id' => 1,
338 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address', $params['address'][1]),
339 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1',
340 $params['address'][1]
342 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2',
343 $params['address'][1]
345 'city' => CRM_Utils_Array
::value('city', $params['address'][1]),
346 'postal_code' => CRM_Utils_Array
::value('postal_code', $params['address'][1]),
347 'country_id' => CRM_Utils_Array
::value('country_id', $params['address'][1]),
348 'state_province_id' => CRM_Utils_Array
::value('state_province_id',
349 $params['address'][1]
351 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1', $params['address'][1]),
352 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2', $params['address'][1]),
354 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
356 //Now check DB for Email
357 $compareParams = array('email' => CRM_Utils_Array
::value('email', $params['email'][1]));
358 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
360 //Now check DB for openid
361 $compareParams = array('openid' => CRM_Utils_Array
::value('openid', $params['openid'][1]));
362 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
364 //Now check DB for IM
365 $compareParams = array('name' => CRM_Utils_Array
::value('name', $params['im'][1]),
366 'provider_id' => CRM_Utils_Array
::value('provider_id', $params['im'][1]),
368 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
370 //Now check DB for Phone
371 $searchParams = array(
372 'contact_id' => $contactId,
373 'location_type_id' => 1,
375 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][1]),
377 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][1]));
378 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
380 //Now check DB for Mobile
381 $searchParams = array(
382 'contact_id' => $contactId,
383 'location_type_id' => 1,
384 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
386 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][2]));
387 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
389 //Now check DB for Note
390 $searchParams = array(
391 'entity_id' => $contactId,
392 'entity_table' => 'civicrm_contact',
394 $compareParams = array('note' => $params['note']);
395 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
397 //update the contact.
398 $updateParams = array(
399 'first_name' => 'John',
400 'last_name' => 'Doe',
401 'contact_type' => 'Individual',
402 'note' => 'new test note',
404 $updateParams['address'][1] = array(
405 'location_type_id' => 1,
407 'street_address' => 'Oberoi Garden',
408 'supplemental_address_1' => 'A-wing:3037',
409 'supplemental_address_2' => 'Andhery',
411 'postal_code' => '12345',
412 'country_id' => 1228,
413 'state_province_id' => 1004,
414 'geo_code_1' => '31.694842',
415 'geo_code_2' => '-106.29998',
417 $updateParams['email'][1] = array(
418 'location_type_id' => 1,
420 'email' => 'john.doe@example.org',
423 $updateParams['phone'][1] = array(
424 'location_type_id' => 1,
426 'phone_type_id' => 1,
427 'phone' => '02115245336',
429 $updateParams['phone'][2] = array(
430 'location_type_id' => 1,
431 'phone_type_id' => 2,
432 'phone' => '9766323895',
435 $updateParams['contact_id'] = $contactId;
436 //create the contact with given params.
437 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
439 //Now check $contact is object of contact DAO..
440 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
441 $contactId = $contact->id
;
443 //Now check values of contact object with updated params.
444 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
445 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
446 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
448 //Now check DB for updated Address
449 $searchParams = array(
450 'contact_id' => $contactId,
451 'location_type_id' => 1,
454 $compareParams = array(
455 'street_address' => 'Oberoi Garden',
456 'supplemental_address_1' => 'A-wing:3037',
457 'supplemental_address_2' => 'Andhery',
459 'postal_code' => '12345',
460 'country_id' => 1228,
461 'state_province_id' => 1004,
462 'geo_code_1' => '31.694842',
463 'geo_code_2' => '-106.29998',
465 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
467 //Now check DB for updated Email
468 $compareParams = array('email' => 'john.doe@example.org');
469 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
471 //Now check DB for updated Phone
472 $searchParams = array(
473 'contact_id' => $contactId,
474 'location_type_id' => 1,
476 'phone_type_id' => 1,
478 $compareParams = array('phone' => '02115245336');
479 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
481 //Now check DB for updated Mobile
482 $searchParams = array(
483 'contact_id' => $contactId,
484 'location_type_id' => 1,
485 'phone_type_id' => 2,
487 $compareParams = array('phone' => '9766323895');
488 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
489 //As we are not updating note
490 //Now check DB for New Note
491 $noteId = $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
492 'Database check for New created note '
495 //delete all notes related to contact
496 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
499 //cleanup DB by deleting the contact
500 Contact
::delete($contactId);
501 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
505 * test case for resolveDefaults( )
506 * test all pseudoConstant, stateProvince, country.
508 function testResolveDefaults() {
513 'birth_date' => '1983-12-13',
516 $params['address'][1] = array(
517 'location_type_id' => 1,
519 'country_id' => 1228,
520 'state_province_id' => 1004,
522 CRM_Contact_BAO_Contact
::resolveDefaults($params);
524 //check the resolve values.
525 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
526 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
527 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
528 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
529 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
530 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
531 $this->assertEquals(CRM_Core_PseudoConstant
::stateProvince($params['address'][1]['state_province_id']),
532 $params['address'][1]['state_province'],
533 'Check for state province.'
535 $this->assertEquals(CRM_Core_PseudoConstant
::country($params['address'][1]['country_id']),
536 $params['address'][1]['country'],
542 * test case for retrieve( )
543 * test with all values.
545 function testRetrieve() {
546 //take the common contact params
547 $params = $this->contactParams();
548 $params['note'] = 'test note';
550 //create the contact with given params.
551 $contact = CRM_Contact_BAO_Contact
::create($params);
552 //Now check $contact is object of contact DAO..
553 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
554 $contactId = $contact->id
;
555 //create the organization contact with the given params.
557 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
558 'contact_type' => 'Organization',
560 $orgContact = CRM_Contact_BAO_Contact
::add($orgParams);
561 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
563 //create employee of relationship.
564 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $orgContact->id
);
566 //retrieve the contact values from database.
568 $searchParams = array('contact_id' => $contactId);
569 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
571 //Now check $retrieveContact is object of contact DAO..
572 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
575 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
577 //Now check values retrieve from database with params.
578 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
579 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
580 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
582 //Now check values of address
583 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
584 CRM_Utils_Array
::value('1', $values['address'])
587 //Now check values of email
588 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
589 CRM_Utils_Array
::value('1', $values['email'])
592 //Now check values of phone
593 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
594 CRM_Utils_Array
::value('1', $values['phone'])
597 //Now check values of mobile
598 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
599 CRM_Utils_Array
::value('2', $values['phone'])
602 //Now check values of openid
603 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
604 CRM_Utils_Array
::value('1', $values['openid'])
607 //Now check values of im
608 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
609 CRM_Utils_Array
::value('1', $values['im'])
612 //Now check values of Note Count.
613 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
615 foreach ($values['note'] as $key => $val) {
616 $retrieveNote = CRM_Utils_Array
::value('note', $val);
617 //check the note value
618 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
621 //Now check values of Relationship Count.
622 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
623 foreach ($values['relationship']['data'] as $key => $val) {
624 //Now check values of Relationship organization.
625 $this->assertEquals($orgContact->id
, $val['contact_id_b'], 'Check for organization');
626 //Now check values of Relationship type.
627 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
628 //delete the organization.
629 Contact
::delete(CRM_Utils_Array
::value('contact_id_b', $val));
632 //delete all notes related to contact
633 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
636 //cleanup DB by deleting the contact
637 Contact
::delete($contactId);
638 $this->quickCleanup(array('civicrm_contact'));
642 * test case for deleteContact( )
644 function testDeleteContact() {
645 $contactParams = $this->contactParams();
648 require_once 'CiviTest/Custom.php';
649 $customGroup = Custom
::createGroup(array(), 'Individual');
651 'label' => 'testFld',
652 'data_type' => 'String',
653 'html_type' => 'Text',
654 'custom_group_id' => $customGroup->id
,
656 $customField = CRM_Core_BAO_CustomField
::create($fields);
657 $contactParams['custom'] = array(
658 $customField->id
=> array(
660 'value' => 'Test custom value',
662 'custom_field_id' => $customField->id
,
663 'custom_group_id' => $customGroup->id
,
664 'table_name' => $customGroup->table_name
,
665 'column_name' => $customField->column_name
,
672 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
673 $contactId = $contact->id
;
676 CRM_Contact_BAO_Contact
::deleteContact($contactId);
678 //Now check DB for location elements.
679 //Now check DB for Address
681 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
682 'id', 'street_address', 'Database check, Address deleted successfully.'
685 //Now check DB for Email
686 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
687 'id', 'email', 'Database check, Email deleted successfully.'
689 //Now check DB for Phone
690 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
691 'id', 'phone', 'Database check, Phone deleted successfully.'
693 //Now check DB for Mobile
694 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
695 'id', 'phone', 'Database check, Mobile deleted successfully.'
697 //Now check DB for IM
698 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
699 'id', 'name', 'Database check, IM deleted successfully.'
701 //Now check DB for openId
702 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
703 'id', 'openid', 'Database check, openId deleted successfully.'
707 // Check that the custom field value is no longer present
709 'entityID' => $contactId,
710 'custom_' . $customField->id
=> 1,
712 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
713 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
714 'Verify that the data value is empty for contact ' . $contactId
716 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
718 //Now check DB for contact.
719 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
720 'id', 'sort_name', 'Database check, contact deleted successfully.'
722 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
723 Custom
::deleteGroup($customGroup);
727 * test case for createProfileContac( )
728 * test with all params.
729 * ( create and update modes )
731 function testCreateProfileContact() {
732 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
734 //current employer field for individual
735 $fields['organization_name'] = array(
736 'name' => 'organization_name',
737 'where' => 'civicrm_organization.organization_name',
738 'title' => 'Current Employer',
740 //get the common params
741 $contactParams = $this->contactParams();
742 $unsetParams = array('location', 'privacy');
743 foreach ($unsetParams as $param) {
744 unset($contactParams[$param]);
747 $profileParams = array(
748 'organization_name' => 'Yahoo',
752 'city-Primary' => 'Newark',
753 'contact_type' => 'Individual',
754 'country-Primary' => '1228',
755 'do_not_email' => '1',
756 'do_not_mail' => '1',
757 'do_not_phone' => '1',
758 'do_not_trade' => '1',
760 'email-Primary' => 'john.smith@example.org',
761 'geo_code_1-Primary' => '18.219023',
762 'geo_code_2-Primary' => '-105.00973',
763 'im-Primary-provider_id' => '1',
764 'im-Primary' => 'john.smith',
766 'openid' => 'john.smith@example.org',
767 'phone-Primary-1' => '303443689',
768 'phone-Primary-2' => '9833910234',
769 'postal_code-Primary' => '01903',
770 'postal_code_suffix-Primary' => '12345',
771 'state_province-Primary' => '1029',
772 'street_address-Primary' => 'Saint Helier St',
773 'supplemental_address_1-Primary' => 'Hallmark Ct',
774 'supplemental_address_2-Primary' => 'Jersey Village',
775 'user_unique_id' => '123456789',
776 'is_bulkmail' => '1',
777 'world_region' => 'India',
784 $createParams = array_merge($contactParams, $profileParams);
786 //create the contact using create profile contact.
787 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
789 //get the parameters to compare.
790 $params = $this->contactParams();
792 //check the values in DB.
793 foreach ($params as $key => $val) {
794 if (!is_array($params[$key])) {
795 if ($key == 'contact_source') {
796 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
797 'id', $params[$key], "Check for {$key} creation."
801 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
802 'id', $params[$key], "Check for {$key} creation."
808 //check privacy options.
809 foreach ($params['privacy'] as $key => $value) {
810 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
811 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
815 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
816 'id', $profileParams['contact_type'], 'Check for contact type creation.'
818 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
819 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
822 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
823 'id', $params['birth_date'], 'Check for birth_date creation.'
826 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
827 'id', $params['deceased_date'], 'Check for deceased_date creation.'
830 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
831 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
833 $checkPrefComm = array();
834 foreach ($dbPrefComm as $key => $value) {
836 $checkPrefComm[$value] = 1;
839 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
841 //Now check DB for Address
842 $searchParams = array(
843 'contact_id' => $contactId,
844 'location_type_id' => 1,
847 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $profileParams),
848 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $profileParams),
849 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $profileParams),
850 'city' => CRM_Utils_Array
::value('city-Primary', $profileParams),
851 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $profileParams),
852 'country_id' => CRM_Utils_Array
::value('country-Primary', $profileParams),
853 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $profileParams),
854 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $profileParams),
855 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $profileParams),
857 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
859 //Now check DB for Email
860 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $profileParams));
861 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
863 //Now check DB for IM
864 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $profileParams),
865 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $profileParams),
867 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
869 //Now check DB for Phone
870 $searchParams = array(
871 'contact_id' => $contactId,
872 'location_type_id' => 1,
875 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams));
876 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
878 //Now check DB for Mobile
879 $searchParams = array(
880 'contact_id' => $contactId,
881 'location_type_id' => 1,
882 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
884 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams));
886 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
888 //get the value of relationship
890 $searchParams = array('contact_id' => $contactId);
891 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
892 //Now check values of Relationship Count.
893 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
894 foreach ($values['relationship']['data'] as $key => $val) {
895 //Now check values of Relationship organization.
896 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
897 //Now check values of Relationship type.
898 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
899 //delete the organization.
900 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
903 //Now check values of tag ids.
904 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
905 foreach ($tags as $key => $val) {
909 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
911 //update Contact mode
912 $updateCParams = array(
913 'first_name' => 'john',
914 'last_name' => 'doe',
915 'contact_type' => 'Individual',
916 'middle_name' => 'abc',
919 'nick_name' => 'Nick Name Updated',
920 'job_title' => 'software Developer',
925 'website_type_id' => 1,
926 'url' => 'http://civicrmUpdate.org',
929 'contact_source' => 'test contact',
930 'external_identifier' => 111222333,
931 'preferred_mail_format' => 'Both',
933 'legal_identifier' => '123123123123',
934 'image_URL' => 'http://imageupdate.com',
935 'deceased_date' => '1981-10-10',
936 'birth_date' => '1951-11-11',
941 'preferred_communication_method' => array(
950 $updatePfParams = array(
951 'organization_name' => 'Google',
952 'city-Primary' => 'Mumbai',
953 'contact_type' => 'Individual',
954 'country-Primary' => '1228',
955 'do_not_email' => '1',
956 'do_not_mail' => '1',
957 'do_not_phone' => '1',
958 'do_not_trade' => '1',
960 'email-Primary' => 'john.doe@example.org',
961 'geo_code_1-Primary' => '31.694842',
962 'geo_code_2-Primary' => '-106.29998',
963 'im-Primary-provider_id' => '1',
964 'im-Primary' => 'john.doe',
966 'openid' => 'john.doe@example.org',
967 'phone-Primary-1' => '02115245336',
968 'phone-Primary-2' => '9766323895',
969 'postal_code-Primary' => '12345',
970 'postal_code_suffix-Primary' => '123',
971 'state_province-Primary' => '1004',
972 'street_address-Primary' => 'Oberoi Garden',
973 'supplemental_address_1-Primary' => 'A-wing:3037',
974 'supplemental_address_2-Primary' => 'Andhery',
975 'user_unique_id' => '1122334455',
976 'is_bulkmail' => '1',
977 'world_region' => 'India',
984 $createParams = array_merge($updateCParams, $updatePfParams);
986 //create the contact using create profile contact.
987 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
988 NULL, NULL, NULL, TRUE
991 //check the contact ids
992 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
994 //check the values in DB.
995 foreach ($updateCParams as $key => $val) {
996 if (!is_array($updateCParams[$key])) {
997 if ($key == 'contact_source') {
998 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
999 'id', $updateCParams[$key], "Check for {$key} creation."
1003 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1004 'id', $updateCParams[$key], "Check for {$key} creation."
1010 //check privacy options.
1011 foreach ($updateCParams['privacy'] as $key => $value) {
1012 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1013 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1017 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1018 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1020 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1021 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1024 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1025 $updateCParams['birth_date'], 'Check for birth_date creation.'
1028 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1029 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1032 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1033 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1035 $checkPrefComm = array();
1036 foreach ($dbPrefComm as $key => $value) {
1038 $checkPrefComm[$value] = 1;
1041 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1043 //Now check DB for Address
1044 $searchParams = array(
1045 'contact_id' => $contactId,
1046 'location_type_id' => 1,
1049 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $updatePfParams),
1050 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $updatePfParams),
1051 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $updatePfParams),
1052 'city' => CRM_Utils_Array
::value('city-Primary', $updatePfParams),
1053 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $updatePfParams),
1054 'country_id' => CRM_Utils_Array
::value('country-Primary', $updatePfParams),
1055 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $updatePfParams),
1056 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $updatePfParams),
1057 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $updatePfParams),
1059 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1061 //Now check DB for Email
1062 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams));
1063 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1065 //Now check DB for IM
1066 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $updatePfParams),
1067 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $updatePfParams),
1069 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1071 //Now check DB for Phone
1072 $searchParams = array(
1073 'contact_id' => $contactId,
1074 'location_type_id' => 1,
1077 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams));
1078 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1080 //Now check DB for Mobile
1081 $searchParams = array(
1082 'contact_id' => $contactId,
1083 'location_type_id' => 1,
1084 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
1086 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams));
1087 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1089 //get the value of relationship
1091 $searchParams = array('contact_id' => $contactId);
1092 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1093 //Now check values of Relationship Count.
1094 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1095 foreach ($values['relationship']['data'] as $key => $val) {
1096 //Now check values of Relationship organization.
1097 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1098 //Now check values of Relationship type.
1099 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1100 //delete the organization.
1101 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
1104 //Now check values of tag ids.
1105 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1106 foreach ($tags as $key => $val) {
1109 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1111 //cleanup DB by deleting the contact
1112 Contact
::delete($contactId);
1116 * test case for getContactDetails( )
1118 function testGetContactDetails() {
1119 //get the contact params
1120 $params = $this->contactParams();
1123 $contact = CRM_Contact_BAO_Contact
::create($params);
1124 $contactId = $contact->id
;
1126 //get the contact details
1127 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1128 $compareParams = array(
1129 $params['first_name'] . ' ' . $params['last_name'],
1130 CRM_Utils_Array
::value('email', $params['email'][1]),
1131 (bool ) $params['privacy']['do_not_email'],
1133 //Now check the contact details
1134 $this->assertAttributesEquals($compareParams, $contactDetails);
1136 //cleanup DB by deleting the contact
1137 Contact
::delete($contactId);
1138 $this->quickCleanup(array('civicrm_contact'));
1143 * importableFields( ) and exportableFields( )
1145 function testFields() {
1146 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1147 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1148 //Now check all fields
1149 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1151 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1152 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1153 //Now check Individual fields
1154 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1156 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1157 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1158 //Now check Household fields
1159 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1161 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1162 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1163 //Now check Organization fields
1164 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1168 * test case for getPrimaryEmail( )
1171 function testGetPrimaryEmail() {
1172 //get the contact params
1173 $params = $this->contactParams();
1174 $params['email'][2] = $params['email'][1];
1175 $params['email'][2]['email'] = 'primarymail@example.org';
1176 unset($params['email'][1]['is_primary']);
1179 $contact = CRM_Contact_BAO_Contact
::create($params);
1180 $contactId = $contact->id
;
1181 //get the primary email.
1182 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1183 //Now check the primary email
1184 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1186 //cleanup DB by deleting the contact
1187 Contact
::delete($contactId);
1188 $this->quickCleanup(array('civicrm_contact'));
1192 * test case for getPrimaryOpenId( )
1195 function testGetPrimaryOpenId() {
1196 //get the contact params
1197 $params = $this->contactParams();
1198 $params['openid'][2] = $params['openid'][1];
1199 $params['openid'][2]['location_type_id'] = 2;
1200 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1201 unset($params['openid'][1]['is_primary']);
1204 $contact = CRM_Contact_BAO_Contact
::create($params);
1205 $contactId = $contact->id
;
1206 //get the primary openid
1207 $openID = CRM_Contact_BAO_Contact
::getPrimaryOpenId($contactId);
1209 //Now check the primary openid
1210 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1212 //cleanup DB by deleting the contact
1213 Contact
::delete($contactId);
1217 * test case for matchContactOnEmail( )
1220 function testMatchContactOnEmail() {
1221 //get the contact params
1222 $params = $this->contactParams();
1224 $contact = CRM_Contact_BAO_Contact
::create($params);
1225 $contactId = $contact->id
;
1227 //get the matching contact.
1228 $match = CRM_Contact_BAO_Contact
::matchContactOnEmail(CRM_Utils_Array
::value('email', $params['email'][1]),
1231 $this->assertEquals($contactId, $match->contact_id
, 'Check For Matching Contact');
1233 //cleanup DB by deleting the contact
1234 Contact
::delete($contactId);
1235 $this->quickCleanup(array('civicrm_contact'));
1239 * test case for getContactType( )
1242 function testGetContactType() {
1243 //get the contact params
1244 $params = $this->contactParams();
1246 $contact = CRM_Contact_BAO_Contact
::create($params);
1247 $contactId = $contact->id
;
1250 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1251 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1253 //cleanup DB by deleting the contact
1254 Contact
::delete($contactId);
1255 $this->quickCleanup(array('civicrm_contact'));
1259 * test case for displayName( )
1262 function testDisplayName() {
1263 //get the contact params
1264 $params = $this->contactParams();
1267 $contact = CRM_Contact_BAO_Contact
::create($params);
1268 $contactId = $contact->id
;
1271 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1273 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1274 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1276 //build display name
1277 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1279 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1281 //cleanup DB by deleting the contact
1282 Contact
::delete($contactId);
1283 $this->quickCleanup(array('civicrm_contact'));
1287 * test case for getDisplayAndImage( )
1290 function testGetDisplayAndImage() {
1291 //get the contact params
1292 $params = $this->contactParams();
1295 $contact = CRM_Contact_BAO_Contact
::create($params);
1296 $contactId = $contact->id
;
1298 //get DisplayAndImage.
1299 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1301 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1303 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1304 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1306 //build display name
1307 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1309 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1310 $this->assertEquals($image, $checkImage, 'Check For Image');
1312 //cleanup DB by deleting the contact
1313 Contact
::delete($contactId);
1317 // * test logged in user has permissions for
1318 // * various operation types
1320 // function testPermissionedContact()
1322 // $contactId = Contact::createIndividual();
1323 // require_once 'CRM/Contact/BAO/Contact/Permission.php';
1324 // $userViewPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId);
1325 // $this->assertEquals( $userViewPermission, true, 'Check user for view contact permission.' );
1327 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1328 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1330 // Contact::delete($contactId);
1334 * function to build common params
1337 private function contactParams() {
1340 'first_name' => 'john',
1341 'last_name' => 'smith',
1342 'contact_type' => 'Individual',
1343 'middle_name' => 'xyz',
1346 'nick_name' => 'Nick Name',
1347 'job_title' => 'software engg',
1352 'website_type_id' => 1,
1353 'url' => 'http://civicrm.org',
1356 'contact_source' => 'test contact',
1357 'external_identifier' => 123456789,
1358 'preferred_mail_format' => 'Both',
1360 'legal_identifier' => '123456789',
1361 'image_URL' => 'http://image.com',
1362 'deceased_date' => '1991-07-07',
1363 'birth_date' => '1961-06-06',
1365 'do_not_phone' => 1,
1366 'do_not_email' => 1,
1368 'do_not_trade' => 1,
1370 'preferred_communication_method' => array(
1379 $params['address'] = array();
1380 $params['address'][1] = array(
1381 'location_type_id' => 1,
1383 'street_address' => 'Saint Helier St',
1384 'supplemental_address_1' => 'Hallmark Ct',
1385 'supplemental_address_2' => 'Jersey Village',
1387 'postal_code' => '01903',
1388 'country_id' => 1228,
1389 'state_province_id' => 1029,
1390 'geo_code_1' => '18.219023',
1391 'geo_code_2' => '-105.00973',
1394 $params['email'] = array();
1395 $params['email'][1] = array(
1396 'location_type_id' => 1,
1398 'email' => 'john.smith@example.org',
1402 $params['phone'] = array();
1403 $params['phone'][1] = array(
1404 'location_type_id' => 1,
1406 'phone_type_id' => 1,
1407 'phone' => '303443689',
1409 $params['phone'][2] = array(
1410 'location_type_id' => 1,
1411 'phone_type_id' => 2,
1412 'phone' => '9833910234',
1415 $params['openid'] = array();
1416 $params['openid'][1] = array(
1417 'location_type_id' => 1,
1419 'openid' => 'http://civicrm.org/',
1422 $params['im'] = array();
1423 $params['im'][1] = array(
1424 'location_type_id' => 1,
1426 'name' => 'john.smith',
1434 * Ensure that created_date and modified_date are set
1436 function testTimestamps_contact() {
1438 $this->_testTimestamps(array(
1439 'UPDATE' => function ($contactId) use ($test) {
1441 'first_name' => 'Testing',
1442 'contact_type' => 'Individual',
1443 'contact_id' => $contactId,
1445 $contact = CRM_Contact_BAO_Contact
::add($params);
1446 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1452 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1454 function testTimestamps_email() {
1456 $this->_testTimestamps(array(
1457 'INSERT' => function ($contactId) use ($test) {
1459 'email' => 'ex-1@example.com',
1461 'location_type_id' => 1,
1462 'contact_id' => $contactId,
1464 CRM_Core_BAO_Email
::add($params);
1465 $test->assertDBQuery('ex-1@example.com',
1466 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1467 array(1 => array($contactId, 'Integer'))
1471 'UPDATE' => function ($contactId) use ($test) {
1472 CRM_Core_DAO
::executeQuery(
1473 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1474 array(1 => array($contactId, 'Integer'))
1478 'DELETE' => function ($contactId) use ($test) {
1479 CRM_Core_DAO
::executeQuery(
1480 'DELETE FROM civicrm_email WHERE contact_id = %1',
1481 array(1 => array($contactId, 'Integer'))
1488 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1490 function testTimestamps_phone() {
1492 $this->_testTimestamps(array(
1493 'INSERT' => function ($contactId) use ($test) {
1495 'phone' => '202-555-1000',
1497 'location_type_id' => 1,
1498 'contact_id' => $contactId,
1500 CRM_Core_BAO_Phone
::add($params);
1501 $test->assertDBQuery('202-555-1000',
1502 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1503 array(1 => array($contactId, 'Integer'))
1507 'UPDATE' => function ($contactId) use ($test) {
1508 CRM_Core_DAO
::executeQuery(
1509 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1510 array(1 => array($contactId, 'Integer'))
1514 'DELETE' => function ($contactId) use ($test) {
1515 CRM_Core_DAO
::executeQuery(
1516 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1517 array(1 => array($contactId, 'Integer'))
1524 * Ensure that civicrm_contact.modified_date is updated when contact-related
1527 function testTimestamps_custom() {
1528 $customGroup = Custom
::createGroup(array(), 'Individual');
1529 $this->assertNotNull($customGroup);
1531 'custom_group_id' => $customGroup->id
,
1532 'data_type' => 'String',
1533 'html_type' => 'Text',
1535 $customField = $this->customFieldCreate($fields);
1536 $customField = $customField['values'][$customField['id']];
1538 $this->_testTimestamps(array(
1539 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1540 $result = civicrm_api3('contact', 'create', array(
1541 'contact_id' => $contactId,
1542 'custom_' . $customField['id'] => 'test-1',
1545 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1546 CRM_Core_DAO
::executeQuery(
1547 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1548 array(1 => array($contactId, 'Integer'))
1551 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1552 CRM_Core_DAO
::executeQuery(
1553 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1554 array(1 => array($contactId, 'Integer'))
1559 Custom
::deleteGroup($customGroup);
1563 * Helper for testing timestamp manipulation
1565 * Create a contact and perform a series of steps with it; after each
1566 * step, ensure that the contact's modified_date has increased.
1568 * @param $callbacks array ($name => $callable)
1570 function _testTimestamps($callbacks) {
1571 CRM_Core_DAO
::triggerRebuild();
1572 $contactId = Contact
::createIndividual();
1574 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1575 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1576 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1577 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1579 $prevTimestamps = $origTimestamps;
1580 foreach ($callbacks as $callbackName => $callback) {
1581 sleep(1); // advance clock by 1 second to ensure timestamps change
1583 $callback($contactId);
1584 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1585 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1586 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1587 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1588 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1590 $prevTimestamps = $newTimestamps;
1593 Contact
::delete($contactId);