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';
549 $params['create_employer'] = 1;
551 //create the contact with given params.
552 $contact = CRM_Contact_BAO_Contact
::create($params);
553 //Now check $contact is object of contact DAO..
554 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
555 $contactId = $contact->id
;
557 //create employee of relationship.
558 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $params['create_employer']);
560 //retrieve the contact values from database.
562 $searchParams = array('contact_id' => $contactId);
563 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
565 //Now check $retrieveContact is object of contact DAO..
566 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
569 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
571 //Now check values retrieve from database with params.
572 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
573 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
574 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
576 //Now check values of address
577 // $this->assertAttributesEquals( CRM_Utils_Array::value( 'address', $params ),
578 // CRM_Utils_Array::value( 'address', $values ) );
580 //Now check values of email
581 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
582 CRM_Utils_Array
::value('1', $values['email'])
585 //Now check values of phone
586 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
587 CRM_Utils_Array
::value('1', $values['phone'])
590 //Now check values of mobile
591 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
592 CRM_Utils_Array
::value('2', $values['phone'])
595 //Now check values of openid
596 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
597 CRM_Utils_Array
::value('1', $values['openid'])
600 //Now check values of im
601 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
602 CRM_Utils_Array
::value('1', $values['im'])
605 //Now check values of Note Count.
606 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
608 foreach ($values['note'] as $key => $val) {
609 $retrieveNote = CRM_Utils_Array
::value('note', $val);
610 //check the note value
611 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
614 //Now check values of Relationship Count.
615 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
616 foreach ($values['relationship']['data'] as $key => $val) {
617 //Now check values of Relationship organization.
618 $this->assertEquals($params['create_employer'], $val['id'], 'Check for organization');
619 //Now check values of Relationship type.
620 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
621 //delete the organization.
622 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
625 //delete all notes related to contact
626 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
629 //cleanup DB by deleting the contact
630 Contact
::delete($contactId);
631 $this->quickCleanup(array('civicrm_contact'));
635 * test case for deleteContact( )
637 function testDeleteContact() {
638 $contactParams = $this->contactParams();
641 require_once 'CiviTest/Custom.php';
642 $customGroup = Custom
::createGroup(array(), 'Individual');
644 'label' => 'testFld',
645 'data_type' => 'String',
646 'html_type' => 'Text',
647 'custom_group_id' => $customGroup->id
,
649 $customField = CRM_Core_BAO_CustomField
::create($fields);
650 $contactParams['custom'] = array(
651 $customField->id
=> array(
653 'value' => 'Test custom value',
655 'custom_field_id' => $customField->id
,
656 'custom_group_id' => $customGroup->id
,
657 'table_name' => $customGroup->table_name
,
658 'column_name' => $customField->column_name
,
665 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
666 $contactId = $contact->id
;
669 CRM_Contact_BAO_Contact
::deleteContact($contactId);
671 //Now check DB for location elements.
672 //Now check DB for Address
674 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
675 'id', 'street_address', 'Database check, Address deleted successfully.'
678 //Now check DB for Email
679 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
680 'id', 'email', 'Database check, Email deleted successfully.'
682 //Now check DB for Phone
683 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
684 'id', 'phone', 'Database check, Phone deleted successfully.'
686 //Now check DB for Mobile
687 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
688 'id', 'phone', 'Database check, Mobile deleted successfully.'
690 //Now check DB for IM
691 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
692 'id', 'name', 'Database check, IM deleted successfully.'
694 //Now check DB for openId
695 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
696 'id', 'openid', 'Database check, openId deleted successfully.'
700 // Check that the custom field value is no longer present
702 'entityID' => $contactId,
703 'custom_' . $customField->id
=> 1,
705 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
706 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
707 'Verify that the data value is empty for contact ' . $contactId
709 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
711 //Now check DB for contact.
712 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
713 'id', 'sort_name', 'Database check, contact deleted successfully.'
715 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
716 Custom
::deleteGroup($customGroup);
720 * test case for createProfileContac( )
721 * test with all params.
722 * ( create and update modes )
724 function testCreateProfileContact() {
725 $fields = CRM_Contact_BAO_Contact
::exportableFields('Individual');
727 //current employer field for individual
728 $fields['organization_name'] = array(
729 'name' => 'organization_name',
730 'where' => 'civicrm_organization.organization_name',
731 'title' => 'Current Employer',
733 //get the common params
734 $contactParams = $this->contactParams();
735 $unsetParams = array('location', 'privacy');
736 foreach ($unsetParams as $param) {
737 unset($contactParams[$param]);
740 $profileParams = array(
741 'organization_name' => 'Yahoo',
745 'city-Primary' => 'Newark',
746 'contact_type' => 'Individual',
747 'country-Primary' => '1228',
748 'do_not_email' => '1',
749 'do_not_mail' => '1',
750 'do_not_phone' => '1',
751 'do_not_trade' => '1',
753 'email-Primary' => 'john.smith@example.org',
754 'geo_code_1-Primary' => '18.219023',
755 'geo_code_2-Primary' => '-105.00973',
756 'im-Primary-provider_id' => '1',
757 'im-Primary' => 'john.smith',
759 'openid' => 'john.smith@example.org',
760 'phone-Primary-1' => '303443689',
761 'phone-Primary-2' => '9833910234',
762 'postal_code-Primary' => '01903',
763 'postal_code_suffix-Primary' => '12345',
764 'state_province-Primary' => '1029',
765 'street_address-Primary' => 'Saint Helier St',
766 'supplemental_address_1-Primary' => 'Hallmark Ct',
767 'supplemental_address_2-Primary' => 'Jersey Village',
768 'user_unique_id' => '123456789',
769 'is_bulkmail' => '1',
770 'world_region' => 'India',
777 $createParams = array_merge($contactParams, $profileParams);
779 //create the contact using create profile contact.
780 $contactId = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
782 //get the parameters to compare.
783 $params = $this->contactParams();
785 //check the values in DB.
786 foreach ($params as $key => $val) {
787 if (!is_array($params[$key])) {
788 if ($key == 'contact_source') {
789 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
790 'id', $params[$key], "Check for {$key} creation."
794 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
795 'id', $params[$key], "Check for {$key} creation."
801 //check privacy options.
802 foreach ($params['privacy'] as $key => $value) {
803 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
804 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
809 'id', $profileParams['contact_type'], 'Check for contact type creation.'
811 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
812 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
815 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
816 'id', $params['birth_date'], 'Check for birth_date creation.'
819 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
820 'id', $params['deceased_date'], 'Check for deceased_date creation.'
823 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
824 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
826 $checkPrefComm = array();
827 foreach ($dbPrefComm as $key => $value) {
829 $checkPrefComm[$value] = 1;
832 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
834 //Now check DB for Address
835 $searchParams = array(
836 'contact_id' => $contactId,
837 'location_type_id' => 1,
840 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $profileParams),
841 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $profileParams),
842 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $profileParams),
843 'city' => CRM_Utils_Array
::value('city-Primary', $profileParams),
844 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $profileParams),
845 'country_id' => CRM_Utils_Array
::value('country-Primary', $profileParams),
846 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $profileParams),
847 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $profileParams),
848 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $profileParams),
850 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
852 //Now check DB for Email
853 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $profileParams));
854 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
856 //Now check DB for IM
857 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $profileParams),
858 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $profileParams),
860 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
862 //Now check DB for Phone
863 $searchParams = array(
864 'contact_id' => $contactId,
865 'location_type_id' => 1,
868 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams));
869 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
871 //Now check DB for Mobile
872 $searchParams = array(
873 'contact_id' => $contactId,
874 'location_type_id' => 1,
875 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
877 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams));
879 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
881 //get the value of relationship
883 $searchParams = array('contact_id' => $contactId);
884 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
885 //Now check values of Relationship Count.
886 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
887 foreach ($values['relationship']['data'] as $key => $val) {
888 //Now check values of Relationship organization.
889 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
890 //Now check values of Relationship type.
891 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
892 //delete the organization.
893 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
896 //Now check values of tag ids.
897 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
898 foreach ($tags as $key => $val) {
902 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
904 //update Contact mode
905 $updateCParams = array(
906 'first_name' => 'john',
907 'last_name' => 'doe',
908 'contact_type' => 'Individual',
909 'middle_name' => 'abc',
912 'nick_name' => 'Nick Name Updated',
913 'job_title' => 'software Developer',
918 'website_type_id' => 1,
919 'url' => 'http://civicrmUpdate.org',
922 'contact_source' => 'test contact',
923 'external_identifier' => 111222333,
924 'preferred_mail_format' => 'Both',
926 'legal_identifier' => '123123123123',
927 'image_URL' => 'http://imageupdate.com',
928 'deceased_date' => '1981-10-10',
929 'birth_date' => '1951-11-11',
934 'preferred_communication_method' => array(
943 $updatePfParams = array(
944 'organization_name' => 'Google',
945 'city-Primary' => 'Mumbai',
946 'contact_type' => 'Individual',
947 'country-Primary' => '1228',
948 'do_not_email' => '1',
949 'do_not_mail' => '1',
950 'do_not_phone' => '1',
951 'do_not_trade' => '1',
953 'email-Primary' => 'john.doe@example.org',
954 'geo_code_1-Primary' => '31.694842',
955 'geo_code_2-Primary' => '-106.29998',
956 'im-Primary-provider_id' => '1',
957 'im-Primary' => 'john.doe',
959 'openid' => 'john.doe@example.org',
960 'phone-Primary-1' => '02115245336',
961 'phone-Primary-2' => '9766323895',
962 'postal_code-Primary' => '12345',
963 'postal_code_suffix-Primary' => '123',
964 'state_province-Primary' => '1004',
965 'street_address-Primary' => 'Oberoi Garden',
966 'supplemental_address_1-Primary' => 'A-wing:3037',
967 'supplemental_address_2-Primary' => 'Andhery',
968 'user_unique_id' => '1122334455',
969 'is_bulkmail' => '1',
970 'world_region' => 'India',
977 $createParams = array_merge($updateCParams, $updatePfParams);
979 //create the contact using create profile contact.
980 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
981 NULL, NULL, NULL, TRUE
984 //check the contact ids
985 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
987 //check the values in DB.
988 foreach ($updateCParams as $key => $val) {
989 if (!is_array($updateCParams[$key])) {
990 if ($key == 'contact_source') {
991 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
992 'id', $updateCParams[$key], "Check for {$key} creation."
996 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
997 'id', $updateCParams[$key], "Check for {$key} creation."
1003 //check privacy options.
1004 foreach ($updateCParams['privacy'] as $key => $value) {
1005 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1006 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1010 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1011 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1013 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1014 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1017 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1018 $updateCParams['birth_date'], 'Check for birth_date creation.'
1021 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1022 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1025 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1026 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1028 $checkPrefComm = array();
1029 foreach ($dbPrefComm as $key => $value) {
1031 $checkPrefComm[$value] = 1;
1034 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1036 //Now check DB for Address
1037 $searchParams = array(
1038 'contact_id' => $contactId,
1039 'location_type_id' => 1,
1042 $compareParams = array('street_address' => CRM_Utils_Array
::value('street_address-Primary', $updatePfParams),
1043 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $updatePfParams),
1044 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $updatePfParams),
1045 'city' => CRM_Utils_Array
::value('city-Primary', $updatePfParams),
1046 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $updatePfParams),
1047 'country_id' => CRM_Utils_Array
::value('country-Primary', $updatePfParams),
1048 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $updatePfParams),
1049 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $updatePfParams),
1050 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $updatePfParams),
1052 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1054 //Now check DB for Email
1055 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams));
1056 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1058 //Now check DB for IM
1059 $compareParams = array('name' => CRM_Utils_Array
::value('im-Primary', $updatePfParams),
1060 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $updatePfParams),
1062 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1064 //Now check DB for Phone
1065 $searchParams = array(
1066 'contact_id' => $contactId,
1067 'location_type_id' => 1,
1070 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams));
1071 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1073 //Now check DB for Mobile
1074 $searchParams = array(
1075 'contact_id' => $contactId,
1076 'location_type_id' => 1,
1077 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
1079 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams));
1080 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1082 //get the value of relationship
1084 $searchParams = array('contact_id' => $contactId);
1085 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1086 //Now check values of Relationship Count.
1087 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1088 foreach ($values['relationship']['data'] as $key => $val) {
1089 //Now check values of Relationship organization.
1090 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1091 //Now check values of Relationship type.
1092 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1093 //delete the organization.
1094 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
1097 //Now check values of tag ids.
1098 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1099 foreach ($tags as $key => $val) {
1102 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1104 //cleanup DB by deleting the contact
1105 Contact
::delete($contactId);
1109 * test case for getContactDetails( )
1111 function testGetContactDetails() {
1112 //get the contact params
1113 $params = $this->contactParams();
1116 $contact = CRM_Contact_BAO_Contact
::create($params);
1117 $contactId = $contact->id
;
1119 //get the contact details
1120 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1121 $compareParams = array(
1122 $params['first_name'] . ' ' . $params['last_name'],
1123 CRM_Utils_Array
::value('email', $params['email'][1]),
1124 (bool ) $params['privacy']['do_not_email'],
1126 //Now check the contact details
1127 $this->assertAttributesEquals($compareParams, $contactDetails);
1129 //cleanup DB by deleting the contact
1130 Contact
::delete($contactId);
1131 $this->quickCleanup(array('civicrm_contact'));
1136 * importableFields( ) and exportableFields( )
1138 function testFields() {
1139 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1140 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1141 //Now check all fields
1142 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1144 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1145 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1146 //Now check Individual fields
1147 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1149 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1150 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1151 //Now check Household fields
1152 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1154 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1155 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1156 //Now check Organization fields
1157 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1161 * test case for getPrimaryEmail( )
1164 function testGetPrimaryEmail() {
1165 //get the contact params
1166 $params = $this->contactParams();
1167 $params['email'][2] = $params['email'][1];
1168 $params['email'][2]['email'] = 'primarymail@example.org';
1169 unset($params['email'][1]['is_primary']);
1172 $contact = CRM_Contact_BAO_Contact
::create($params);
1173 $contactId = $contact->id
;
1174 //get the primary email.
1175 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1176 //Now check the primary email
1177 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1179 //cleanup DB by deleting the contact
1180 Contact
::delete($contactId);
1181 $this->quickCleanup(array('civicrm_contact'));
1185 * test case for getPrimaryOpenId( )
1188 function testGetPrimaryOpenId() {
1189 //get the contact params
1190 $params = $this->contactParams();
1191 $params['openid'][2] = $params['openid'][1];
1192 $params['openid'][2]['location_type_id'] = 2;
1193 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1194 unset($params['openid'][1]['is_primary']);
1197 $contact = CRM_Contact_BAO_Contact
::create($params);
1198 $contactId = $contact->id
;
1199 //get the primary openid
1200 $openID = CRM_Contact_BAO_Contact
::getPrimaryOpenId($contactId);
1202 //Now check the primary openid
1203 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1205 //cleanup DB by deleting the contact
1206 Contact
::delete($contactId);
1210 * test case for matchContactOnEmail( )
1213 function testMatchContactOnEmail() {
1214 //get the contact params
1215 $params = $this->contactParams();
1217 $contact = CRM_Contact_BAO_Contact
::create($params);
1218 $contactId = $contact->id
;
1220 //get the matching contact.
1221 $match = CRM_Contact_BAO_Contact
::matchContactOnEmail(CRM_Utils_Array
::value('email', $params['email'][1]),
1224 $this->assertEquals($contactId, $match->contact_id
, 'Check For Matching Contact');
1226 //cleanup DB by deleting the contact
1227 Contact
::delete($contactId);
1228 $this->quickCleanup(array('civicrm_contact'));
1232 * test case for getContactType( )
1235 function testGetContactType() {
1236 //get the contact params
1237 $params = $this->contactParams();
1239 $contact = CRM_Contact_BAO_Contact
::create($params);
1240 $contactId = $contact->id
;
1243 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1244 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1246 //cleanup DB by deleting the contact
1247 Contact
::delete($contactId);
1248 $this->quickCleanup(array('civicrm_contact'));
1252 * test case for displayName( )
1255 function testDisplayName() {
1256 //get the contact params
1257 $params = $this->contactParams();
1260 $contact = CRM_Contact_BAO_Contact
::create($params);
1261 $contactId = $contact->id
;
1264 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1266 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1267 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1269 //build display name
1270 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1272 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1274 //cleanup DB by deleting the contact
1275 Contact
::delete($contactId);
1276 $this->quickCleanup(array('civicrm_contact'));
1280 * test case for getDisplayAndImage( )
1283 function testGetDisplayAndImage() {
1284 //get the contact params
1285 $params = $this->contactParams();
1288 $contact = CRM_Contact_BAO_Contact
::create($params);
1289 $contactId = $contact->id
;
1291 //get DisplayAndImage.
1292 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1294 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1296 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1297 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1299 //build display name
1300 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1302 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1303 $this->assertEquals($image, $checkImage, 'Check For Image');
1305 //cleanup DB by deleting the contact
1306 Contact
::delete($contactId);
1310 // * test logged in user has permissions for
1311 // * various operation types
1313 // function testPermissionedContact()
1315 // $contactId = Contact::createIndividual();
1316 // require_once 'CRM/Contact/BAO/Contact/Permission.php';
1317 // $userViewPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId);
1318 // $this->assertEquals( $userViewPermission, true, 'Check user for view contact permission.' );
1320 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1321 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1323 // Contact::delete($contactId);
1327 * function to build common params
1330 private function contactParams() {
1333 'first_name' => 'john',
1334 'last_name' => 'smith',
1335 'contact_type' => 'Individual',
1336 'middle_name' => 'xyz',
1339 'nick_name' => 'Nick Name',
1340 'job_title' => 'software engg',
1345 'website_type_id' => 1,
1346 'url' => 'http://civicrm.org',
1349 'contact_source' => 'test contact',
1350 'external_identifier' => 123456789,
1351 'preferred_mail_format' => 'Both',
1353 'legal_identifier' => '123456789',
1354 'image_URL' => 'http://image.com',
1355 'deceased_date' => '1991-07-07',
1356 'birth_date' => '1961-06-06',
1358 'do_not_phone' => 1,
1359 'do_not_email' => 1,
1361 'do_not_trade' => 1,
1363 'preferred_communication_method' => array(
1372 $params['address'] = array();
1373 $params['address'][1] = array(
1374 'location_type_id' => 1,
1376 'street_address' => 'Saint Helier St',
1377 'supplemental_address_1' => 'Hallmark Ct',
1378 'supplemental_address_2' => 'Jersey Village',
1380 'postal_code' => '01903',
1381 'country_id' => 1228,
1382 'state_province_id' => 1029,
1383 'geo_code_1' => '18.219023',
1384 'geo_code_2' => '-105.00973',
1387 $params['email'] = array();
1388 $params['email'][1] = array(
1389 'location_type_id' => 1,
1391 'email' => 'john.smith@example.org',
1395 $params['phone'] = array();
1396 $params['phone'][1] = array(
1397 'location_type_id' => 1,
1399 'phone_type_id' => 1,
1400 'phone' => '303443689',
1402 $params['phone'][2] = array(
1403 'location_type_id' => 1,
1404 'phone_type_id' => 2,
1405 'phone' => '9833910234',
1408 $params['openid'] = array();
1409 $params['openid'][1] = array(
1410 'location_type_id' => 1,
1412 'openid' => 'http://civicrm.org/',
1415 $params['im'] = array();
1416 $params['im'][1] = array(
1417 'location_type_id' => 1,
1419 'name' => 'john.smith',
1427 * Ensure that created_date and modified_date are set
1429 function testTimestamps_contact() {
1431 $this->_testTimestamps(array(
1432 'UPDATE' => function ($contactId) use ($test) {
1434 'first_name' => 'Testing',
1435 'contact_type' => 'Individual',
1436 'contact_id' => $contactId,
1438 $contact = CRM_Contact_BAO_Contact
::add($params);
1439 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1445 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1447 function testTimestamps_email() {
1449 $this->_testTimestamps(array(
1450 'INSERT' => function ($contactId) use ($test) {
1452 'email' => 'ex-1@example.com',
1454 'location_type_id' => 1,
1455 'contact_id' => $contactId,
1457 CRM_Core_BAO_Email
::add($params);
1458 $test->assertDBQuery('ex-1@example.com',
1459 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1460 array(1 => array($contactId, 'Integer'))
1464 'UPDATE' => function ($contactId) use ($test) {
1465 CRM_Core_DAO
::executeQuery(
1466 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1467 array(1 => array($contactId, 'Integer'))
1471 'DELETE' => function ($contactId) use ($test) {
1472 CRM_Core_DAO
::executeQuery(
1473 'DELETE FROM civicrm_email WHERE contact_id = %1',
1474 array(1 => array($contactId, 'Integer'))
1481 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1483 function testTimestamps_phone() {
1485 $this->_testTimestamps(array(
1486 'INSERT' => function ($contactId) use ($test) {
1488 'phone' => '202-555-1000',
1490 'location_type_id' => 1,
1491 'contact_id' => $contactId,
1493 CRM_Core_BAO_Phone
::add($params);
1494 $test->assertDBQuery('202-555-1000',
1495 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1496 array(1 => array($contactId, 'Integer'))
1500 'UPDATE' => function ($contactId) use ($test) {
1501 CRM_Core_DAO
::executeQuery(
1502 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1503 array(1 => array($contactId, 'Integer'))
1507 'DELETE' => function ($contactId) use ($test) {
1508 CRM_Core_DAO
::executeQuery(
1509 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1510 array(1 => array($contactId, 'Integer'))
1517 * Ensure that civicrm_contact.modified_date is updated when contact-related
1520 function testTimestamps_custom() {
1521 $customGroup = Custom
::createGroup(array(), 'Individual');
1522 $this->assertNotNull($customGroup);
1524 'custom_group_id' => $customGroup->id
,
1525 'data_type' => 'String',
1526 'html_type' => 'Text',
1528 $customField = $this->customFieldCreate($fields);
1529 $customField = $customField['values'][$customField['id']];
1531 $this->_testTimestamps(array(
1532 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1533 $result = civicrm_api3('contact', 'create', array(
1534 'contact_id' => $contactId,
1535 'custom_' . $customField['id'] => 'test-1',
1538 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1539 CRM_Core_DAO
::executeQuery(
1540 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1541 array(1 => array($contactId, 'Integer'))
1544 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1545 CRM_Core_DAO
::executeQuery(
1546 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1547 array(1 => array($contactId, 'Integer'))
1552 Custom
::deleteGroup($customGroup);
1556 * Helper for testing timestamp manipulation
1558 * Create a contact and perform a series of steps with it; after each
1559 * step, ensure that the contact's modified_date has increased.
1561 * @param $callbacks array ($name => $callable)
1563 function _testTimestamps($callbacks) {
1564 CRM_Core_DAO
::triggerRebuild();
1565 $contactId = Contact
::createIndividual();
1567 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1568 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1569 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1570 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1572 $prevTimestamps = $origTimestamps;
1573 foreach ($callbacks as $callbackName => $callback) {
1574 sleep(1); // advance clock by 1 second to ensure timestamps change
1576 $callback($contactId);
1577 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1578 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1579 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1580 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1581 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1583 $prevTimestamps = $newTimestamps;
1586 Contact
::delete($contactId);