2 require_once 'CiviTest/CiviUnitTestCase.php';
3 require_once 'CiviTest/Contact.php';
4 require_once 'CiviTest/Custom.php';
7 * Class CRM_Contact_BAO_ContactTest
9 class CRM_Contact_BAO_ContactTest
extends CiviUnitTestCase
{
14 public function setUp() {
19 * Test case for add( ).
21 * test with empty params.
23 public function testAddWithEmptyParams() {
25 $contact = CRM_Contact_BAO_Contact
::add($params);
27 // Now check Contact object.
28 $this->assertNull($contact);
32 * Test case for add( ).
34 * Test with names (create and update modes)
36 public function testAddWithNames() {
38 $lastName = 'Whatson';
40 'first_name' => $firstName,
41 'last_name' => $lastName,
42 'contact_type' => 'Individual',
45 $contact = CRM_Contact_BAO_Contact
::add($params);
47 // Now check $contact is object of contact DAO.
48 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
49 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
50 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
52 $contactId = $contact->id
;
54 // Update and change first name and last name, using add( ).
57 'first_name' => $firstName,
58 'contact_type' => 'Individual',
59 'contact_id' => $contactId,
62 $contact = CRM_Contact_BAO_Contact
::add($params);
64 // Now check $contact is object of contact DAO.
65 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
66 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
68 $contactId = $contact->id
;
69 Contact
::delete($contactId);
75 * Test with all contact params
76 * (create and update modes)
78 public function testAddWithAll() {
79 // Take the common contact params.
80 $params = $this->contactParams();
82 unset($params['location']);
83 $prefComm = $params['preferred_communication_method'];
84 $contact = CRM_Contact_BAO_Contact
::add($params);
85 $contactId = $contact->id
;
87 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
89 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
90 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
91 $this->assertEquals($params['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
92 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
93 $this->assertEquals('1', $contact->do_not_email
, 'Check for do_not_email creation.');
94 $this->assertEquals('1', $contact->do_not_phone
, 'Check for do_not_phone creation.');
95 $this->assertEquals('1', $contact->do_not_mail
, 'Check for do_not_mail creation.');
96 $this->assertEquals('1', $contact->do_not_trade
, 'Check for do_not_trade creation.');
97 $this->assertEquals('1', $contact->is_opt_out
, 'Check for is_opt_out creation.');
98 $this->assertEquals($params['external_identifier'], $contact->external_identifier
, 'Check for external_identifier creation.');
99 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name
, 'Check for sort_name creation.');
100 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format
,
101 'Check for preferred_mail_format creation.'
103 $this->assertEquals($params['contact_source'], $contact->source
, 'Check for contact_source creation.');
104 $this->assertEquals($params['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
105 $this->assertEquals($params['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
106 $this->assertEquals($params['job_title'], $contact->job_title
, 'Check for job_title creation.');
107 $this->assertEquals($params['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
108 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
109 $this->assertEquals(CRM_Utils_Date
::processDate($params['birth_date']),
110 $contact->birth_date
, 'Check for birth_date creation.'
112 $this->assertEquals(CRM_Utils_Date
::processDate($params['deceased_date']),
113 $contact->deceased_date
, 'Check for deceased_date creation.'
115 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
116 $contact->preferred_communication_method
118 $checkPrefComm = array();
119 foreach ($dbPrefComm as $key => $value) {
121 $checkPrefComm[$value] = 1;
124 $this->assertAttributesEquals($checkPrefComm, $prefComm);
126 $updateParams = array(
127 'contact_type' => 'Individual',
128 'first_name' => 'Jane',
129 'middle_name' => 'abc',
130 'last_name' => 'Doe',
133 'nick_name' => 'Nick Name Second',
134 'job_title' => 'software Developer',
139 'website_type_id' => 1,
140 'url' => 'http://docs.civicrm.org',
143 'contact_source' => 'test update contact',
144 'external_identifier' => 111111111,
145 'preferred_mail_format' => 'Both',
147 'deceased_date' => '1981-03-03',
148 'birth_date' => '1951-04-04',
155 'preferred_communication_method' => array(
164 $prefComm = $updateParams['preferred_communication_method'];
165 $updateParams['contact_id'] = $contactId;
166 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
167 $contactId = $contact->id
;
169 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
171 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
172 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
173 $this->assertEquals($updateParams['middle_name'], $contact->middle_name
, 'Check for middle name creation.');
174 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
175 $this->assertEquals('0', $contact->do_not_email
, 'Check for do_not_email creation.');
176 $this->assertEquals('0', $contact->do_not_phone
, 'Check for do_not_phone creation.');
177 $this->assertEquals('0', $contact->do_not_mail
, 'Check for do_not_mail creation.');
178 $this->assertEquals('0', $contact->do_not_trade
, 'Check for do_not_trade creation.');
179 $this->assertEquals('0', $contact->is_opt_out
, 'Check for is_opt_out creation.');
180 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier
,
181 'Check for external_identifier creation.'
183 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
184 $contact->sort_name
, 'Check for sort_name creation.'
186 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format
,
187 'Check for preferred_mail_format creation.'
189 $this->assertEquals($updateParams['contact_source'], $contact->source
, 'Check for contact_source creation.');
190 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id
, 'Check for prefix_id creation.');
191 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id
, 'Check for suffix_id creation.');
192 $this->assertEquals($updateParams['job_title'], $contact->job_title
, 'Check for job_title creation.');
193 $this->assertEquals($updateParams['gender_id'], $contact->gender_id
, 'Check for gender_id creation.');
194 $this->assertEquals('1', $contact->is_deceased
, 'Check for is_deceased creation.');
195 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['birth_date']),
196 date('YmdHis', strtotime($contact->birth_date
)), 'Check for birth_date creation.'
198 $this->assertEquals(CRM_Utils_Date
::processDate($updateParams['deceased_date']),
199 date('YmdHis', strtotime($contact->deceased_date
)), 'Check for deceased_date creation.'
201 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
202 $contact->preferred_communication_method
204 $checkPrefComm = array();
205 foreach ($dbPrefComm as $key => $value) {
207 $checkPrefComm[$value] = 1;
210 $this->assertAttributesEquals($checkPrefComm, $prefComm);
212 Contact
::delete($contactId);
216 * Test case for add( ) with All contact types.
218 public function testAddWithAllContactTypes() {
222 'first_name' => $firstName,
223 'last_name' => $lastName,
224 'contact_type' => 'Individual',
227 $contact = CRM_Contact_BAO_Contact
::add($params);
228 $this->assertEquals($firstName, $contact->first_name
, 'Check for first name creation.');
229 $this->assertEquals($lastName, $contact->last_name
, 'Check for last name creation.');
231 $contactId = $contact->id
;
233 //update and change first name and last name, using create()
236 'first_name' => $firstName,
237 'contact_type' => 'Individual',
238 'contact_id' => $contactId,
241 $contact = CRM_Contact_BAO_Contact
::add($params);
242 $this->assertEquals($firstName, $contact->first_name
, 'Check for updated first name.');
243 $contactId = $contact->id
;
244 Contact
::delete($contactId);
246 $householdName = 'Adams house';
248 'household_name' => $householdName,
249 'contact_type' => 'Household',
251 $contact = CRM_Contact_BAO_Contact
::add($params);
252 $this->assertEquals($householdName, $contact->sort_name
, 'Check for created household.');
253 $contactId = $contact->id
;
255 //update and change name of household, using create
256 $householdName = 'Joans home';
258 'household_name' => $householdName,
259 'contact_type' => 'Household',
260 'contact_id' => $contactId,
262 $contact = CRM_Contact_BAO_Contact
::add($params);
263 $this->assertEquals($householdName, $contact->sort_name
, 'Check for updated household.');
264 Contact
::delete($contactId);
266 $organizationName = 'My Organization';
268 'organization_name' => $organizationName,
269 'contact_type' => 'Organization',
271 $contact = CRM_Contact_BAO_Contact
::add($params);
272 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for created organization.');
273 $contactId = $contact->id
;
275 //update and change name of organization, using create
276 $organizationName = 'Your Changed Organization';
278 'organization_name' => $organizationName,
279 'contact_type' => 'Organization',
280 'contact_id' => $contactId,
282 $contact = CRM_Contact_BAO_Contact
::add($params);
283 $this->assertEquals($organizationName, $contact->sort_name
, 'Check for updated organization.');
284 Contact
::delete($contactId);
288 * Test case for create.
290 * Test with missing params.
292 public function testCreateWithEmptyParams() {
294 'first_name' => 'Bill',
295 'last_name' => 'Adams',
297 $contact = CRM_Contact_BAO_Contact
::create($params);
299 //Now check Contact object
300 $this->assertNull($contact);
304 * Test case for create.
306 * Test with all params.
307 * ( create and update modes ).
309 public function testCreateWithAll() {
310 //take the common contact params
311 $params = $this->contactParams();
312 $params['note'] = 'test note';
314 //create the contact with given params.
315 $contact = CRM_Contact_BAO_Contact
::create($params);
317 //Now check $contact is object of contact DAO..
318 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
319 $contactId = $contact->id
;
321 //Now check values of contact object with params.
322 $this->assertEquals($params['first_name'], $contact->first_name
, 'Check for first name creation.');
323 $this->assertEquals($params['last_name'], $contact->last_name
, 'Check for last name creation.');
324 $this->assertEquals($params['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
326 //Now check DB for Address
327 $searchParams = array(
328 'contact_id' => $contactId,
329 'location_type_id' => 1,
332 $compareParams = array(
333 'street_address' => CRM_Utils_Array
::value('street_address', $params['address'][1]),
334 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1',
335 $params['address'][1]
337 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2',
338 $params['address'][1]
340 'city' => CRM_Utils_Array
::value('city', $params['address'][1]),
341 'postal_code' => CRM_Utils_Array
::value('postal_code', $params['address'][1]),
342 'country_id' => CRM_Utils_Array
::value('country_id', $params['address'][1]),
343 'state_province_id' => CRM_Utils_Array
::value('state_province_id',
344 $params['address'][1]
346 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1', $params['address'][1]),
347 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2', $params['address'][1]),
349 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
351 //Now check DB for Email
352 $compareParams = array('email' => CRM_Utils_Array
::value('email', $params['email'][1]));
353 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
355 //Now check DB for openid
356 $compareParams = array('openid' => CRM_Utils_Array
::value('openid', $params['openid'][1]));
357 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
359 //Now check DB for IM
360 $compareParams = array(
361 'name' => CRM_Utils_Array
::value('name', $params['im'][1]),
362 'provider_id' => CRM_Utils_Array
::value('provider_id', $params['im'][1]),
364 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
366 //Now check DB for Phone
367 $searchParams = array(
368 'contact_id' => $contactId,
369 'location_type_id' => 1,
371 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][1]),
373 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][1]));
374 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
376 //Now check DB for Mobile
377 $searchParams = array(
378 'contact_id' => $contactId,
379 'location_type_id' => 1,
380 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
382 $compareParams = array('phone' => CRM_Utils_Array
::value('phone', $params['phone'][2]));
383 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
385 //Now check DB for Note
386 $searchParams = array(
387 'entity_id' => $contactId,
388 'entity_table' => 'civicrm_contact',
390 $compareParams = array('note' => $params['note']);
391 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
393 //update the contact.
394 $updateParams = array(
395 'first_name' => 'John',
396 'last_name' => 'Doe',
397 'contact_type' => 'Individual',
398 'note' => 'new test note',
400 $updateParams['address'][1] = array(
401 'location_type_id' => 1,
403 'street_address' => 'Oberoi Garden',
404 'supplemental_address_1' => 'A-wing:3037',
405 'supplemental_address_2' => 'Andhery',
407 'postal_code' => '12345',
408 'country_id' => 1228,
409 'state_province_id' => 1004,
410 'geo_code_1' => '31.694842',
411 'geo_code_2' => '-106.29998',
413 $updateParams['email'][1] = array(
414 'location_type_id' => 1,
416 'email' => 'john.doe@example.org',
419 $updateParams['phone'][1] = array(
420 'location_type_id' => 1,
422 'phone_type_id' => 1,
423 'phone' => '02115245336',
425 $updateParams['phone'][2] = array(
426 'location_type_id' => 1,
427 'phone_type_id' => 2,
428 'phone' => '9766323895',
431 $updateParams['contact_id'] = $contactId;
432 //create the contact with given params.
433 $contact = CRM_Contact_BAO_Contact
::create($updateParams);
435 //Now check $contact is object of contact DAO..
436 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
437 $contactId = $contact->id
;
439 //Now check values of contact object with updated params.
440 $this->assertEquals($updateParams['first_name'], $contact->first_name
, 'Check for first name creation.');
441 $this->assertEquals($updateParams['last_name'], $contact->last_name
, 'Check for last name creation.');
442 $this->assertEquals($updateParams['contact_type'], $contact->contact_type
, 'Check for contact type creation.');
444 //Now check DB for updated Address
445 $searchParams = array(
446 'contact_id' => $contactId,
447 'location_type_id' => 1,
450 $compareParams = array(
451 'street_address' => 'Oberoi Garden',
452 'supplemental_address_1' => 'A-wing:3037',
453 'supplemental_address_2' => 'Andhery',
455 'postal_code' => '12345',
456 'country_id' => 1228,
457 'state_province_id' => 1004,
458 'geo_code_1' => '31.694842',
459 'geo_code_2' => '-106.29998',
461 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
463 //Now check DB for updated Email
464 $compareParams = array('email' => 'john.doe@example.org');
465 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
467 //Now check DB for updated Phone
468 $searchParams = array(
469 'contact_id' => $contactId,
470 'location_type_id' => 1,
472 'phone_type_id' => 1,
474 $compareParams = array('phone' => '02115245336');
475 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
477 //Now check DB for updated Mobile
478 $searchParams = array(
479 'contact_id' => $contactId,
480 'location_type_id' => 1,
481 'phone_type_id' => 2,
483 $compareParams = array('phone' => '9766323895');
484 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
485 // As we are not updating note.
486 // Now check DB for New Note.
487 $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
488 'Database check for New created note '
491 // Delete all notes related to contact.
492 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
494 // Cleanup DB by deleting the contact.
495 Contact
::delete($contactId);
496 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
500 * Test case for resolveDefaults( ).
502 * Test all pseudoConstant, stateProvince, country.
504 public function testResolveDefaults() {
509 'birth_date' => '1983-12-13',
512 $params['address'][1] = array(
513 'location_type_id' => 1,
515 'country_id' => 1228,
516 'state_province_id' => 1004,
518 CRM_Contact_BAO_Contact
::resolveDefaults($params);
520 //check the resolve values.
521 $genders = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'gender_id');
522 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
523 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
524 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
525 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
526 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
527 $this->assertEquals(CRM_Core_PseudoConstant
::stateProvince($params['address'][1]['state_province_id']),
528 $params['address'][1]['state_province'],
529 'Check for state province.'
531 $this->assertEquals(CRM_Core_PseudoConstant
::country($params['address'][1]['country_id']),
532 $params['address'][1]['country'],
538 * Test case for retrieve( ).
540 * Test with all values.
542 public function testRetrieve() {
543 //take the common contact params
544 $params = $this->contactParams();
545 $params['note'] = 'test note';
547 //create the contact with given params.
548 $contact = CRM_Contact_BAO_Contact
::create($params);
549 //Now check $contact is object of contact DAO..
550 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
551 $contactId = $contact->id
;
552 //create the organization contact with the given params.
554 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
555 'contact_type' => 'Organization',
557 $orgContact = CRM_Contact_BAO_Contact
::add($orgParams);
558 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
560 //create employee of relationship.
561 CRM_Contact_BAO_Contact_Utils
::createCurrentEmployerRelationship($contactId, $orgContact->id
);
563 //retrieve the contact values from database.
565 $searchParams = array('contact_id' => $contactId);
566 $retrieveContact = CRM_Contact_BAO_Contact
::retrieve($searchParams, $values);
568 //Now check $retrieveContact is object of contact DAO..
569 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
572 $this->assertEquals($contactId, $retrieveContact->id
, 'Check for contact id');
574 //Now check values retrieve from database with params.
575 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
576 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
577 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
579 //Now check values of address
580 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
581 CRM_Utils_Array
::value('1', $values['address'])
584 //Now check values of email
585 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
586 CRM_Utils_Array
::value('1', $values['email'])
589 //Now check values of phone
590 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
591 CRM_Utils_Array
::value('1', $values['phone'])
594 //Now check values of mobile
595 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
596 CRM_Utils_Array
::value('2', $values['phone'])
599 //Now check values of openid
600 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
601 CRM_Utils_Array
::value('1', $values['openid'])
604 //Now check values of im
605 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
606 CRM_Utils_Array
::value('1', $values['im'])
609 //Now check values of Note Count.
610 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
612 foreach ($values['note'] as $key => $val) {
613 $retrieveNote = CRM_Utils_Array
::value('note', $val);
614 //check the note value
615 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
618 //Now check values of Relationship Count.
619 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
620 foreach ($values['relationship']['data'] as $key => $val) {
621 //Now check values of Relationship organization.
622 $this->assertEquals($orgContact->id
, $val['contact_id_b'], 'Check for organization');
623 //Now check values of Relationship type.
624 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
625 //delete the organization.
626 Contact
::delete(CRM_Utils_Array
::value('contact_id_b', $val));
629 //delete all notes related to contact
630 CRM_Core_BAO_Note
::cleanContactNotes($contactId);
632 //cleanup DB by deleting the contact
633 Contact
::delete($contactId);
634 $this->quickCleanup(array('civicrm_contact'));
638 * Test case for deleteContact( ).
640 public function testDeleteContact() {
641 $contactParams = $this->contactParams();
644 require_once 'CiviTest/Custom.php';
645 $customGroup = Custom
::createGroup(array(), 'Individual');
647 'label' => 'testFld',
648 'data_type' => 'String',
649 'html_type' => 'Text',
650 'custom_group_id' => $customGroup->id
,
652 $customField = CRM_Core_BAO_CustomField
::create($fields);
653 $contactParams['custom'] = array(
654 $customField->id
=> array(
656 'value' => 'Test custom value',
658 'custom_field_id' => $customField->id
,
659 'custom_group_id' => $customGroup->id
,
660 'table_name' => $customGroup->table_name
,
661 'column_name' => $customField->column_name
,
668 $contact = CRM_Contact_BAO_Contact
::create($contactParams);
669 $contactId = $contact->id
;
671 //delete contact permanently.
672 CRM_Contact_BAO_Contact
::deleteContact($contactId, FALSE, TRUE);
674 //Now check DB for location elements.
675 //Now check DB for Address
677 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
678 'id', 'street_address', 'Database check, Address deleted successfully.'
681 //Now check DB for Email
682 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
683 'id', 'email', 'Database check, Email deleted successfully.'
685 //Now check DB for Phone
686 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
687 'id', 'phone', 'Database check, Phone deleted successfully.'
689 //Now check DB for Mobile
690 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
691 'id', 'phone', 'Database check, Mobile deleted successfully.'
693 //Now check DB for IM
694 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
695 'id', 'name', 'Database check, IM deleted successfully.'
697 //Now check DB for openId
698 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
699 'id', 'openid', 'Database check, openId deleted successfully.'
702 // Check that the custom field value is no longer present
704 'entityID' => $contactId,
705 'custom_' . $customField->id
=> 1,
707 $values = CRM_Core_BAO_CustomValueTable
::getValues($params);
708 $this->assertEquals(CRM_Utils_Array
::value("custom_" . $customField->id
, $values), '',
709 'Verify that the data value is empty for contact ' . $contactId
711 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
713 //Now check DB for contact.
714 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
715 'id', 'sort_name', 'Database check, contact deleted successfully.'
717 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
718 Custom
::deleteGroup($customGroup);
722 * Test case for createProfileContact.
724 public 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(
841 'street_address' => CRM_Utils_Array
::value('street_address-Primary', $profileParams),
842 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $profileParams),
843 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $profileParams),
844 'city' => CRM_Utils_Array
::value('city-Primary', $profileParams),
845 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $profileParams),
846 'country_id' => CRM_Utils_Array
::value('country-Primary', $profileParams),
847 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $profileParams),
848 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $profileParams),
849 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $profileParams),
851 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
853 //Now check DB for Email
854 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $profileParams));
855 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
857 //Now check DB for IM
858 $compareParams = array(
859 'name' => CRM_Utils_Array
::value('im-Primary', $profileParams),
860 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $profileParams),
862 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
864 //Now check DB for Phone
865 $searchParams = array(
866 'contact_id' => $contactId,
867 'location_type_id' => 1,
870 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $profileParams));
871 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
873 //Now check DB for Mobile
874 $searchParams = array(
875 'contact_id' => $contactId,
876 'location_type_id' => 1,
877 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
879 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $profileParams));
881 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
883 //get the value of relationship
885 $searchParams = array('contact_id' => $contactId);
886 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
887 //Now check values of Relationship Count.
888 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
889 foreach ($values['relationship']['data'] as $key => $val) {
890 //Now check values of Relationship organization.
891 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
892 //Now check values of Relationship type.
893 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
894 //delete the organization.
895 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
898 //Now check values of tag ids.
899 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
900 foreach ($tags as $key => $val) {
904 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
906 //update Contact mode
907 $updateCParams = array(
908 'first_name' => 'john',
909 'last_name' => 'doe',
910 'contact_type' => 'Individual',
911 'middle_name' => 'abc',
914 'nick_name' => 'Nick Name Updated',
915 'job_title' => 'software Developer',
920 'website_type_id' => 1,
921 'url' => 'http://civicrmUpdate.org',
924 'contact_source' => 'test contact',
925 'external_identifier' => 111222333,
926 'preferred_mail_format' => 'Both',
928 'legal_identifier' => '123123123123',
929 'image_URL' => 'http://imageupdate.com',
930 'deceased_date' => '1981-10-10',
931 'birth_date' => '1951-11-11',
936 'preferred_communication_method' => array(
945 $updatePfParams = array(
946 'organization_name' => 'Google',
947 'city-Primary' => 'Mumbai',
948 'contact_type' => 'Individual',
949 'country-Primary' => '1228',
950 'do_not_email' => '1',
951 'do_not_mail' => '1',
952 'do_not_phone' => '1',
953 'do_not_trade' => '1',
955 'email-Primary' => 'john.doe@example.org',
956 'geo_code_1-Primary' => '31.694842',
957 'geo_code_2-Primary' => '-106.29998',
958 'im-Primary-provider_id' => '1',
959 'im-Primary' => 'john.doe',
961 'openid' => 'john.doe@example.org',
962 'phone-Primary-1' => '02115245336',
963 'phone-Primary-2' => '9766323895',
964 'postal_code-Primary' => '12345',
965 'postal_code_suffix-Primary' => '123',
966 'state_province-Primary' => '1004',
967 'street_address-Primary' => 'Oberoi Garden',
968 'supplemental_address_1-Primary' => 'A-wing:3037',
969 'supplemental_address_2-Primary' => 'Andhery',
970 'user_unique_id' => '1122334455',
971 'is_bulkmail' => '1',
972 'world_region' => 'India',
979 $createParams = array_merge($updateCParams, $updatePfParams);
981 //create the contact using create profile contact.
982 $contactID = CRM_Contact_BAO_Contact
::createProfileContact($createParams, $fields, $contactId,
983 NULL, NULL, NULL, TRUE
986 //check the contact ids
987 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
989 //check the values in DB.
990 foreach ($updateCParams as $key => $val) {
991 if (!is_array($updateCParams[$key])) {
992 if ($key == 'contact_source') {
993 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
994 'id', $updateCParams[$key], "Check for {$key} creation."
998 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
999 'id', $updateCParams[$key], "Check for {$key} creation."
1005 //check privacy options.
1006 foreach ($updateCParams['privacy'] as $key => $value) {
1007 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1008 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1012 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1013 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1015 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1016 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1019 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1020 $updateCParams['birth_date'], 'Check for birth_date creation.'
1023 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1024 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1027 $dbPrefComm = explode(CRM_Core_DAO
::VALUE_SEPARATOR
,
1028 CRM_Core_DAO
::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE)
1030 $checkPrefComm = array();
1031 foreach ($dbPrefComm as $key => $value) {
1033 $checkPrefComm[$value] = 1;
1036 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1038 //Now check DB for Address
1039 $searchParams = array(
1040 'contact_id' => $contactId,
1041 'location_type_id' => 1,
1044 $compareParams = array(
1045 'street_address' => CRM_Utils_Array
::value('street_address-Primary', $updatePfParams),
1046 'supplemental_address_1' => CRM_Utils_Array
::value('supplemental_address_1-Primary', $updatePfParams),
1047 'supplemental_address_2' => CRM_Utils_Array
::value('supplemental_address_2-Primary', $updatePfParams),
1048 'city' => CRM_Utils_Array
::value('city-Primary', $updatePfParams),
1049 'postal_code' => CRM_Utils_Array
::value('postal_code-Primary', $updatePfParams),
1050 'country_id' => CRM_Utils_Array
::value('country-Primary', $updatePfParams),
1051 'state_province_id' => CRM_Utils_Array
::value('state_province-Primary', $updatePfParams),
1052 'geo_code_1' => CRM_Utils_Array
::value('geo_code_1-Primary', $updatePfParams),
1053 'geo_code_2' => CRM_Utils_Array
::value('geo_code_2-Primary', $updatePfParams),
1055 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1057 //Now check DB for Email
1058 $compareParams = array('email' => CRM_Utils_Array
::value('email-Primary', $updatePfParams));
1059 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1061 //Now check DB for IM
1062 $compareParams = array(
1063 'name' => CRM_Utils_Array
::value('im-Primary', $updatePfParams),
1064 'provider_id' => CRM_Utils_Array
::value('im-Primary-provider_id', $updatePfParams),
1066 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1068 //Now check DB for Phone
1069 $searchParams = array(
1070 'contact_id' => $contactId,
1071 'location_type_id' => 1,
1074 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-1', $updatePfParams));
1075 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1077 //Now check DB for Mobile
1078 $searchParams = array(
1079 'contact_id' => $contactId,
1080 'location_type_id' => 1,
1081 'phone_type_id' => CRM_Utils_Array
::value('phone_type_id', $params['phone'][2]),
1083 $compareParams = array('phone' => CRM_Utils_Array
::value('phone-Primary-2', $updatePfParams));
1084 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1086 //get the value of relationship
1088 $searchParams = array('contact_id' => $contactId);
1089 $relationship = CRM_Contact_BAO_Relationship
::getValues($searchParams, $values);
1090 //Now check values of Relationship Count.
1091 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1092 foreach ($values['relationship']['data'] as $key => $val) {
1093 //Now check values of Relationship organization.
1094 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1095 //Now check values of Relationship type.
1096 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1097 //delete the organization.
1098 Contact
::delete(CRM_Utils_Array
::value('cid', $val));
1101 //Now check values of tag ids.
1102 $tags = CRM_Core_BAO_EntityTag
::getTag($contactId);
1103 foreach ($tags as $key => $val) {
1106 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1108 //cleanup DB by deleting the contact
1109 Contact
::delete($contactId);
1113 * Test case for getContactDetails( ).
1115 public function testGetContactDetails() {
1116 //get the contact params
1117 $params = $this->contactParams();
1120 $contact = CRM_Contact_BAO_Contact
::create($params);
1121 $contactId = $contact->id
;
1123 //get the contact details
1124 $contactDetails = CRM_Contact_BAO_Contact
::getContactDetails($contactId);
1125 $compareParams = array(
1126 $params['first_name'] . ' ' . $params['last_name'],
1127 CRM_Utils_Array
::value('email', $params['email'][1]),
1128 (bool ) $params['privacy']['do_not_email'],
1130 //Now check the contact details
1131 $this->assertAttributesEquals($compareParams, $contactDetails);
1133 //cleanup DB by deleting the contact
1134 Contact
::delete($contactId);
1135 $this->quickCleanup(array('civicrm_contact'));
1139 * Test case for importableFields( ) and exportableFields( ).
1141 public function testFields() {
1142 $allImpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1143 $allExpFileds = CRM_Contact_BAO_Contact
::importableFields('All');
1144 //Now check all fields
1145 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1147 $individualImpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1148 $individualExpFileds = CRM_Contact_BAO_Contact
::importableFields('Individual');
1149 //Now check Individual fields
1150 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1152 $householdImpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1153 $householdExpFileds = CRM_Contact_BAO_Contact
::importableFields('Household');
1154 //Now check Household fields
1155 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1157 $organizationImpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1158 $organizationExpFileds = CRM_Contact_BAO_Contact
::importableFields('Organization');
1159 //Now check Organization fields
1160 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1164 * Test case for getPrimaryEmail.
1166 public function testGetPrimaryEmail() {
1167 //get the contact params
1168 $params = $this->contactParams();
1169 $params['email'][2] = $params['email'][1];
1170 $params['email'][2]['email'] = 'primarymail@example.org';
1171 unset($params['email'][1]['is_primary']);
1174 $contact = CRM_Contact_BAO_Contact
::create($params);
1175 $contactId = $contact->id
;
1176 //get the primary email.
1177 $email = CRM_Contact_BAO_Contact
::getPrimaryEmail($contactId);
1178 //Now check the primary email
1179 $this->assertEquals($email, CRM_Utils_Array
::value('email', $params['email'][2]), 'Check Primary Email');
1181 //cleanup DB by deleting the contact
1182 Contact
::delete($contactId);
1183 $this->quickCleanup(array('civicrm_contact'));
1187 * Test case for getPrimaryOpenId( ).
1189 public function testGetPrimaryOpenId() {
1190 //get the contact params
1191 $params = $this->contactParams();
1192 $params['openid'][2] = $params['openid'][1];
1193 $params['openid'][2]['location_type_id'] = 2;
1194 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1195 unset($params['openid'][1]['is_primary']);
1198 $contact = CRM_Contact_BAO_Contact
::create($params);
1199 $contactId = $contact->id
;
1200 //get the primary openid
1201 $openID = CRM_Contact_BAO_Contact
::getPrimaryOpenId($contactId);
1203 //Now check the primary openid
1204 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1206 //cleanup DB by deleting the contact
1207 Contact
::delete($contactId);
1211 * Test case for matchContactOnEmail( ).
1213 public 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( ).
1234 public function testGetContactType() {
1235 //get the contact params
1236 $params = $this->contactParams();
1238 $contact = CRM_Contact_BAO_Contact
::create($params);
1239 $contactId = $contact->id
;
1242 $contactType = CRM_Contact_BAO_Contact
::getContactType($contactId);
1243 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1245 //cleanup DB by deleting the contact
1246 Contact
::delete($contactId);
1247 $this->quickCleanup(array('civicrm_contact'));
1251 * Test case for displayName( ).
1253 public function testDisplayName() {
1254 //get the contact params
1255 $params = $this->contactParams();
1258 $contact = CRM_Contact_BAO_Contact
::create($params);
1259 $contactId = $contact->id
;
1262 $dbDisplayName = CRM_Contact_BAO_Contact
::displayName($contactId);
1264 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1265 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1267 //build display name
1268 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1270 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1272 //cleanup DB by deleting the contact
1273 Contact
::delete($contactId);
1274 $this->quickCleanup(array('civicrm_contact'));
1278 * Test case for getDisplayAndImage( ).
1280 public function testGetDisplayAndImage() {
1281 //get the contact params
1282 $params = $this->contactParams();
1285 $contact = CRM_Contact_BAO_Contact
::create($params);
1286 $contactId = $contact->id
;
1288 //get DisplayAndImage.
1289 list($displayName, $image) = CRM_Contact_BAO_Contact
::getDisplayAndImage($contactId);
1291 $checkImage = CRM_Contact_BAO_Contact_Utils
::getImage($params['contact_type'], FALSE, $contactId);
1293 $prefix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'prefix_id');
1294 $suffix = CRM_Core_PseudoConstant
::get('CRM_Contact_DAO_Contact', 'suffix_id');
1296 //build display name
1297 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1299 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1300 $this->assertEquals($image, $checkImage, 'Check For Image');
1302 //cleanup DB by deleting the contact
1303 Contact
::delete($contactId);
1307 * Build common params.
1309 private function contactParams() {
1312 'first_name' => 'john',
1313 'last_name' => 'smith',
1314 'contact_type' => 'Individual',
1315 'middle_name' => 'xyz',
1318 'nick_name' => 'Nick Name',
1319 'job_title' => 'software engg',
1324 'website_type_id' => 1,
1325 'url' => 'http://civicrm.org',
1328 'contact_source' => 'test contact',
1329 'external_identifier' => 123456789,
1330 'preferred_mail_format' => 'Both',
1332 'legal_identifier' => '123456789',
1333 'image_URL' => 'http://image.com',
1334 'deceased_date' => '1991-07-07',
1335 'birth_date' => '1961-06-06',
1337 'do_not_phone' => 1,
1338 'do_not_email' => 1,
1340 'do_not_trade' => 1,
1342 'preferred_communication_method' => array(
1351 $params['address'] = array();
1352 $params['address'][1] = array(
1353 'location_type_id' => 1,
1355 'street_address' => 'Saint Helier St',
1356 'supplemental_address_1' => 'Hallmark Ct',
1357 'supplemental_address_2' => 'Jersey Village',
1359 'postal_code' => '01903',
1360 'country_id' => 1228,
1361 'state_province_id' => 1029,
1362 'geo_code_1' => '18.219023',
1363 'geo_code_2' => '-105.00973',
1366 $params['email'] = array();
1367 $params['email'][1] = array(
1368 'location_type_id' => 1,
1370 'email' => 'john.smith@example.org',
1373 $params['phone'] = array();
1374 $params['phone'][1] = array(
1375 'location_type_id' => 1,
1377 'phone_type_id' => 1,
1378 'phone' => '303443689',
1380 $params['phone'][2] = array(
1381 'location_type_id' => 1,
1382 'phone_type_id' => 2,
1383 'phone' => '9833910234',
1386 $params['openid'] = array();
1387 $params['openid'][1] = array(
1388 'location_type_id' => 1,
1390 'openid' => 'http://civicrm.org/',
1393 $params['im'] = array();
1394 $params['im'][1] = array(
1395 'location_type_id' => 1,
1397 'name' => 'john.smith',
1405 * Ensure that created_date and modified_date are set.
1407 public function testTimestampContact() {
1409 $this->_testTimestamps(array(
1410 'UPDATE' => function ($contactId) use ($test) {
1412 'first_name' => 'Testing',
1413 'contact_type' => 'Individual',
1414 'contact_id' => $contactId,
1416 $contact = CRM_Contact_BAO_Contact
::add($params);
1417 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1423 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record.
1425 public function testTimestampsEmail() {
1427 $this->_testTimestamps(array(
1428 'INSERT' => function ($contactId) use ($test) {
1430 'email' => 'ex-1@example.com',
1432 'location_type_id' => 1,
1433 'contact_id' => $contactId,
1435 CRM_Core_BAO_Email
::add($params);
1436 $test->assertDBQuery('ex-1@example.com',
1437 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1438 array(1 => array($contactId, 'Integer'))
1441 'UPDATE' => function ($contactId) use ($test) {
1442 CRM_Core_DAO
::executeQuery(
1443 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1444 array(1 => array($contactId, 'Integer'))
1447 'DELETE' => function ($contactId) use ($test) {
1448 CRM_Core_DAO
::executeQuery(
1449 'DELETE FROM civicrm_email WHERE contact_id = %1',
1450 array(1 => array($contactId, 'Integer'))
1457 * Ensure that civicrm_contact.modified_date is updated when manipulating an email.
1459 public function testTimestampsPhone() {
1461 $this->_testTimestamps(array(
1462 'INSERT' => function ($contactId) use ($test) {
1464 'phone' => '202-555-1000',
1466 'location_type_id' => 1,
1467 'contact_id' => $contactId,
1469 CRM_Core_BAO_Phone
::add($params);
1470 $test->assertDBQuery('202-555-1000',
1471 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1472 array(1 => array($contactId, 'Integer'))
1475 'UPDATE' => function ($contactId) use ($test) {
1476 CRM_Core_DAO
::executeQuery(
1477 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1478 array(1 => array($contactId, 'Integer'))
1481 'DELETE' => function ($contactId) use ($test) {
1482 CRM_Core_DAO
::executeQuery(
1483 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1484 array(1 => array($contactId, 'Integer'))
1491 * Ensure that civicrm_contact.modified_date is updated correctly.
1493 * Looking at it when contact-related custom data is updated.
1495 public function testTimestampsCustom() {
1496 $customGroup = Custom
::createGroup(array(), 'Individual');
1497 $this->assertNotNull($customGroup);
1499 'custom_group_id' => $customGroup->id
,
1500 'data_type' => 'String',
1501 'html_type' => 'Text',
1503 $customField = $this->customFieldCreate($fields);
1504 $customField = $customField['values'][$customField['id']];
1506 $this->_testTimestamps(array(
1507 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1508 $result = civicrm_api3('contact', 'create', array(
1509 'contact_id' => $contactId,
1510 'custom_' . $customField['id'] => 'test-1',
1513 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1514 CRM_Core_DAO
::executeQuery(
1515 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1516 array(1 => array($contactId, 'Integer'))
1519 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1520 CRM_Core_DAO
::executeQuery(
1521 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1522 array(1 => array($contactId, 'Integer'))
1527 Custom
::deleteGroup($customGroup);
1531 * Helper for testing timestamp manipulation.
1533 * Create a contact and perform a series of steps with it; after each
1534 * step, ensure that the contact's modified_date has increased.
1536 * @param array $callbacks
1537 * ($name => $callable).
1539 public function _testTimestamps($callbacks) {
1540 CRM_Core_DAO
::triggerRebuild();
1541 $contactId = Contact
::createIndividual();
1543 $origTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1544 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1545 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1546 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1548 $prevTimestamps = $origTimestamps;
1549 foreach ($callbacks as $callbackName => $callback) {
1550 sleep(1); // advance clock by 1 second to ensure timestamps change
1552 $callback($contactId);
1553 $newTimestamps = CRM_Contact_BAO_Contact
::getTimestamps($contactId);
1554 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1555 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1556 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1557 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1559 $prevTimestamps = $newTimestamps;
1562 Contact
::delete($contactId);