Merge pull request #2753 from fuzionnz/CRM-14394
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ContactTest.php
1 <?php
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 {
6 function get_info() {
7 return array(
8 'name' => 'Contact BAOs',
9 'description' => 'Test all Contact_BAO_Contact methods.',
10 'group' => 'CiviCRM BAO Tests',
11 );
12 }
13
14 function setUp() {
15 parent::setUp();
16 }
17
18 /**
19 * test case for add( )
20 * test with empty params.
21 */
22 function testAddWithEmptyParams() {
23 $params = array( );
24 $contact = CRM_Contact_BAO_Contact::add($params);
25
26 //Now check Contact object
27 $this->assertNull($contact);
28 }
29
30 /**
31 * test case for add( )
32 * test with names
33 * (create and update modes)
34 */
35 function testAddWithNames() {
36 $firstName = 'Shane';
37 $lastName = 'Whatson';
38 $params = array(
39 'first_name' => $firstName,
40 'last_name' => $lastName,
41 'contact_type' => 'Individual',
42 );
43
44 $contact = CRM_Contact_BAO_Contact::add($params);
45
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.');
50
51 $contactId = $contact->id;
52
53 //update and change first name and last name, using add( )
54 $firstName = 'Jane';
55 $params = array(
56 'first_name' => $firstName,
57 'contact_type' => 'Individual',
58 'contact_id' => $contactId,
59 );
60
61 $contact = CRM_Contact_BAO_Contact::add($params);
62
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.');
66
67 $contactId = $contact->id;
68 Contact::delete($contactId);
69 }
70
71 /**
72 * test case for add( )
73 * test with all contact params
74 * (creat and update modes)
75 */
76 function testAddWithAll() {
77 //take the common contact params
78 $params = $this->contactParams();
79
80 unset($params['location']);
81 $prefComm = $params['preferred_communication_method'];
82
83 //create the contact using add()
84 $contact = CRM_Contact_BAO_Contact::add($params);
85 $contactId = $contact->id;
86
87 //Now check $contact is object of contact DAO..
88 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
89
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.'
104 );
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.'
113 );
114 $this->assertEquals(CRM_Utils_Date::processDate($params['deceased_date']),
115 $contact->deceased_date, 'Check for deceased_date creation.'
116 );
117 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
118 $contact->preferred_communication_method
119 );
120 $checkPrefComm = array();
121 foreach ($dbPrefComm as $key => $value) {
122 if ($value) {
123 $checkPrefComm[$value] = 1;
124 }
125 }
126 $this->assertAttributesEquals($checkPrefComm, $prefComm);
127
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',
134 'prefix_id' => 2,
135 'suffix_id' => 3,
136 'nick_name' => 'Nick Name Second',
137 'job_title' => 'software Developer',
138 'gender_id' => 1,
139 'is_deceased' => 1,
140 'website' => array(
141 1 => array(
142 'website_type_id' => 1,
143 'url' => 'http://docs.civicrm.org',
144 ),
145 ),
146 'contact_source' => 'test update contact',
147 'external_identifier' => 111111111,
148 'preferred_mail_format' => 'Both',
149 'is_opt_out' => 0,
150 'deceased_date' => '1981-03-03',
151 'birth_date' => '1951-04-04',
152 'privacy' => array(
153 'do_not_phone' => 0,
154 'do_not_email' => 0,
155 'do_not_mail' => 0,
156 'do_not_trade' => 0,
157 ),
158 'preferred_communication_method' => array(
159 '1' => 0,
160 '2' => 1,
161 '3' => 0,
162 '4' => 1,
163 '5' => 0,
164 ),
165 );
166
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;
172
173 //Now check $contact is object of contact DAO..
174 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
175
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.'
188 );
189 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
190 $contact->sort_name, 'Check for sort_name creation.'
191 );
192 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format,
193 'Check for preferred_mail_format creation.'
194 );
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.'
203 );
204 $this->assertEquals(CRM_Utils_Date::processDate($updateParams['deceased_date']),
205 date('YmdHis', strtotime($contact->deceased_date)), 'Check for deceased_date creation.'
206 );
207 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
208 $contact->preferred_communication_method
209 );
210 $checkPrefComm = array();
211 foreach ($dbPrefComm as $key => $value) {
212 if ($value) {
213 $checkPrefComm[$value] = 1;
214 }
215 }
216 $this->assertAttributesEquals($checkPrefComm, $prefComm);
217
218 //cleanup DB by deleting the contact
219 Contact::delete($contactId);
220 }
221
222 /**
223 * test case for add( )
224 * test with All contact types.
225 */
226 function testAddWithAllContactTypes() {
227 $firstName = 'Bill';
228 $lastName = 'Adams';
229 $params = array(
230 'first_name' => $firstName,
231 'last_name' => $lastName,
232 'contact_type' => 'Individual',
233 );
234
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.');
238
239 $contactId = $contact->id;
240
241 //update and change first name and last name, using create()
242 $firstName = 'Joan';
243 $params = array(
244 'first_name' => $firstName,
245 'contact_type' => 'Individual',
246 'contact_id' => $contactId,
247 );
248
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);
253
254 $householdName = 'Adams house';
255 $params = array(
256 'household_name' => $householdName,
257 'contact_type' => 'Household',
258 );
259 $contact = CRM_Contact_BAO_Contact::add($params);
260 $this->assertEquals($householdName, $contact->sort_name, 'Check for created household.');
261 $contactId = $contact->id;
262
263 //update and change name of household, using create
264 $householdName = 'Joans home';
265 $params = array(
266 'household_name' => $householdName,
267 'contact_type' => 'Household',
268 'contact_id' => $contactId,
269 );
270 $contact = CRM_Contact_BAO_Contact::add($params);
271 $this->assertEquals($householdName, $contact->sort_name, 'Check for updated household.');
272 Contact::delete($contactId);
273
274 $organizationName = 'My Organization';
275 $params = array(
276 'organization_name' => $organizationName,
277 'contact_type' => 'Organization',
278 );
279 $contact = CRM_Contact_BAO_Contact::add($params);
280 $this->assertEquals($organizationName, $contact->sort_name, 'Check for created organization.');
281 $contactId = $contact->id;
282
283 //update and change name of organization, using create
284 $organizationName = 'Your Changed Organization';
285 $params = array(
286 'organization_name' => $organizationName,
287 'contact_type' => 'Organization',
288 'contact_id' => $contactId,
289 );
290 $contact = CRM_Contact_BAO_Contact::add($params);
291 $this->assertEquals($organizationName, $contact->sort_name, 'Check for updated organization.');
292 Contact::delete($contactId);
293 }
294
295 /**
296 * test case for create( )
297 * test with missing params.
298 */
299 function testCreateWithEmptyParams() {
300 $params = array(
301 'first_name' => 'Bill',
302 'last_name' => 'Adams',
303 );
304 $contact = CRM_Contact_BAO_Contact::create($params);
305
306 //Now check Contact object
307 $this->assertNull($contact);
308 }
309
310 /**
311 * test case for create( )
312 * test with all params.
313 * ( create and update modes ).
314 */
315 function testCreateWithAll() {
316 //take the common contact params
317 $params = $this->contactParams();
318 $params['note'] = 'test note';
319
320 //create the contact with given params.
321 $contact = CRM_Contact_BAO_Contact::create($params);
322
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;
326
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.');
331
332 //Now check DB for Address
333 $searchParams = array(
334 'contact_id' => $contactId,
335 'location_type_id' => 1,
336 'is_primary' => 1,
337 );
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]
341 ),
342 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2',
343 $params['address'][1]
344 ),
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]
350 ),
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]),
353 );
354 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
355
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);
359
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);
363
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]),
367 );
368 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
369
370 //Now check DB for Phone
371 $searchParams = array(
372 'contact_id' => $contactId,
373 'location_type_id' => 1,
374 'is_primary' => 1,
375 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][1]),
376 );
377 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][1]));
378 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
379
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]),
385 );
386 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][2]));
387 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
388
389 //Now check DB for Note
390 $searchParams = array(
391 'entity_id' => $contactId,
392 'entity_table' => 'civicrm_contact',
393 );
394 $compareParams = array('note' => $params['note']);
395 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
396
397 //update the contact.
398 $updateParams = array(
399 'first_name' => 'John',
400 'last_name' => 'Doe',
401 'contact_type' => 'Individual',
402 'note' => 'new test note',
403 );
404 $updateParams['address'][1] = array(
405 'location_type_id' => 1,
406 'is_primary' => 1,
407 'street_address' => 'Oberoi Garden',
408 'supplemental_address_1' => 'A-wing:3037',
409 'supplemental_address_2' => 'Andhery',
410 'city' => 'Mumbai',
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',
416 );
417 $updateParams['email'][1] = array(
418 'location_type_id' => 1,
419 'is_primary' => 1,
420 'email' => 'john.doe@example.org',
421 );
422
423 $updateParams['phone'][1] = array(
424 'location_type_id' => 1,
425 'is_primary' => 1,
426 'phone_type_id' => 1,
427 'phone' => '02115245336',
428 );
429 $updateParams['phone'][2] = array(
430 'location_type_id' => 1,
431 'phone_type_id' => 2,
432 'phone' => '9766323895',
433 );
434
435 $updateParams['contact_id'] = $contactId;
436 //create the contact with given params.
437 $contact = CRM_Contact_BAO_Contact::create($updateParams);
438
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;
442
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.');
447
448 //Now check DB for updated Address
449 $searchParams = array(
450 'contact_id' => $contactId,
451 'location_type_id' => 1,
452 'is_primary' => 1,
453 );
454 $compareParams = array(
455 'street_address' => 'Oberoi Garden',
456 'supplemental_address_1' => 'A-wing:3037',
457 'supplemental_address_2' => 'Andhery',
458 'city' => 'Mumbai',
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',
464 );
465 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
466
467 //Now check DB for updated Email
468 $compareParams = array('email' => 'john.doe@example.org');
469 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
470
471 //Now check DB for updated Phone
472 $searchParams = array(
473 'contact_id' => $contactId,
474 'location_type_id' => 1,
475 'is_primary' => 1,
476 'phone_type_id' => 1,
477 );
478 $compareParams = array('phone' => '02115245336');
479 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
480
481 //Now check DB for updated Mobile
482 $searchParams = array(
483 'contact_id' => $contactId,
484 'location_type_id' => 1,
485 'phone_type_id' => 2,
486 );
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 '
493 );
494
495 //delete all notes related to contact
496 CRM_Core_BAO_Note::cleanContactNotes($contactId);
497
498
499 //cleanup DB by deleting the contact
500 Contact::delete($contactId);
501 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
502 }
503
504 /**
505 * test case for resolveDefaults( )
506 * test all pseudoConstant, stateProvince, country.
507 */
508 function testResolveDefaults() {
509 $params = array(
510 'prefix_id' => 3,
511 'suffix_id' => 2,
512 'gender_id' => 2,
513 'birth_date' => '1983-12-13',
514 );
515
516 $params['address'][1] = array(
517 'location_type_id' => 1,
518 'is_primary' => 1,
519 'country_id' => 1228,
520 'state_province_id' => 1004,
521 );
522 CRM_Contact_BAO_Contact::resolveDefaults($params);
523
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.'
534 );
535 $this->assertEquals(CRM_Core_PseudoConstant::country($params['address'][1]['country_id']),
536 $params['address'][1]['country'],
537 'Check for country.'
538 );
539 }
540
541 /**
542 * test case for retrieve( )
543 * test with all values.
544 */
545 function testRetrieve() {
546 //take the common contact params
547 $params = $this->contactParams();
548 $params['note'] = 'test note';
549 $params['create_employer'] = 1;
550
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;
556
557 //create employee of relationship.
558 CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $params['create_employer']);
559
560 //retrieve the contact values from database.
561 $values = array();
562 $searchParams = array('contact_id' => $contactId);
563 $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values);
564
565 //Now check $retrieveContact is object of contact DAO..
566 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
567
568 //Now check the ids.
569 $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id');
570
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.');
575
576 //Now check values of address
577 // $this->assertAttributesEquals( CRM_Utils_Array::value( 'address', $params ),
578 // CRM_Utils_Array::value( 'address', $values ) );
579
580 //Now check values of email
581 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
582 CRM_Utils_Array::value('1', $values['email'])
583 );
584
585 //Now check values of phone
586 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
587 CRM_Utils_Array::value('1', $values['phone'])
588 );
589
590 //Now check values of mobile
591 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
592 CRM_Utils_Array::value('2', $values['phone'])
593 );
594
595 //Now check values of openid
596 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
597 CRM_Utils_Array::value('1', $values['openid'])
598 );
599
600 //Now check values of im
601 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
602 CRM_Utils_Array::value('1', $values['im'])
603 );
604
605 //Now check values of Note Count.
606 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
607
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');
612 }
613
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));
623 }
624
625 //delete all notes related to contact
626 CRM_Core_BAO_Note::cleanContactNotes($contactId);
627
628
629 //cleanup DB by deleting the contact
630 Contact::delete($contactId);
631 $this->quickCleanup(array('civicrm_contact'));
632 }
633
634 /**
635 * test case for deleteContact( )
636 */
637 function testDeleteContact() {
638 $contactParams = $this->contactParams();
639
640 //create custom data
641 require_once 'CiviTest/Custom.php';
642 $customGroup = Custom::createGroup(array(), 'Individual');
643 $fields = array(
644 'label' => 'testFld',
645 'data_type' => 'String',
646 'html_type' => 'Text',
647 'custom_group_id' => $customGroup->id,
648 );
649 $customField = CRM_Core_BAO_CustomField::create($fields);
650 $contactParams['custom'] = array(
651 $customField->id => array(
652 -1 => array(
653 'value' => 'Test custom value',
654 'type' => 'String',
655 'custom_field_id' => $customField->id,
656 'custom_group_id' => $customGroup->id,
657 'table_name' => $customGroup->table_name,
658 'column_name' => $customField->column_name,
659 'file_id' => NULL,
660 ),
661 ),
662 );
663
664 //create contact
665 $contact = CRM_Contact_BAO_Contact::create($contactParams);
666 $contactId = $contact->id;
667
668 //delete contact.
669 CRM_Contact_BAO_Contact::deleteContact($contactId);
670
671 //Now check DB for location elements.
672 //Now check DB for Address
673
674 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
675 'id', 'street_address', 'Database check, Address deleted successfully.'
676 );
677
678 //Now check DB for Email
679 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
680 'id', 'email', 'Database check, Email deleted successfully.'
681 );
682 //Now check DB for Phone
683 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
684 'id', 'phone', 'Database check, Phone deleted successfully.'
685 );
686 //Now check DB for Mobile
687 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
688 'id', 'phone', 'Database check, Mobile deleted successfully.'
689 );
690 //Now check DB for IM
691 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
692 'id', 'name', 'Database check, IM deleted successfully.'
693 );
694 //Now check DB for openId
695 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
696 'id', 'openid', 'Database check, openId deleted successfully.'
697 );
698
699
700 // Check that the custom field value is no longer present
701 $params = array(
702 'entityID' => $contactId,
703 'custom_' . $customField->id => 1,
704 );
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
708 );
709 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
710
711 //Now check DB for contact.
712 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
713 'id', 'sort_name', 'Database check, contact deleted successfully.'
714 );
715 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
716 Custom::deleteGroup($customGroup);
717 }
718
719 /**
720 * test case for createProfileContac( )
721 * test with all params.
722 * ( create and update modes )
723 */
724 function testCreateProfileContact() {
725 $fields = CRM_Contact_BAO_Contact::exportableFields('Individual');
726
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',
732 );
733 //get the common params
734 $contactParams = $this->contactParams();
735 $unsetParams = array('location', 'privacy');
736 foreach ($unsetParams as $param) {
737 unset($contactParams[$param]);
738 }
739
740 $profileParams = array(
741 'organization_name' => 'Yahoo',
742 'gender_id' => '2',
743 'prefix_id' => '3',
744 'suffix_id' => '2',
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',
752 'do_not_sms' => '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',
758 'on_hold' => '1',
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',
771 'tag' => array(
772 '3' => '1',
773 '4' => '1',
774 '1' => '1',
775 ),
776 );
777 $createParams = array_merge($contactParams, $profileParams);
778
779 //create the contact using create profile contact.
780 $contactId = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
781
782 //get the parameters to compare.
783 $params = $this->contactParams();
784
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."
791 );
792 }
793 else {
794 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
795 'id', $params[$key], "Check for {$key} creation."
796 );
797 }
798 }
799 }
800
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.'
805 );
806 }
807
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
809 'id', $profileParams['contact_type'], 'Check for contact type creation.'
810 );
811 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
812 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
813 );
814
815 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
816 'id', $params['birth_date'], 'Check for birth_date creation.'
817 );
818
819 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
820 'id', $params['deceased_date'], 'Check for deceased_date creation.'
821 );
822
823 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
824 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
825 );
826 $checkPrefComm = array();
827 foreach ($dbPrefComm as $key => $value) {
828 if ($value) {
829 $checkPrefComm[$value] = 1;
830 }
831 }
832 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
833
834 //Now check DB for Address
835 $searchParams = array(
836 'contact_id' => $contactId,
837 'location_type_id' => 1,
838 'is_primary' => 1,
839 );
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),
849 );
850 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
851
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);
855
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),
859 );
860 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
861
862 //Now check DB for Phone
863 $searchParams = array(
864 'contact_id' => $contactId,
865 'location_type_id' => 1,
866 'is_primary' => 1,
867 );
868 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams));
869 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
870
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]),
876 );
877 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams));
878
879 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
880
881 //get the value of relationship
882 $values = array();
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));
894 }
895
896 //Now check values of tag ids.
897 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
898 foreach ($tags as $key => $val) {
899 $tagIds[$key] = 1;
900 }
901
902 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
903
904 //update Contact mode
905 $updateCParams = array(
906 'first_name' => 'john',
907 'last_name' => 'doe',
908 'contact_type' => 'Individual',
909 'middle_name' => 'abc',
910 'prefix_id' => 2,
911 'suffix_id' => 3,
912 'nick_name' => 'Nick Name Updated',
913 'job_title' => 'software Developer',
914 'gender_id' => 1,
915 'is_deceased' => 1,
916 'website' => array(
917 1 => array(
918 'website_type_id' => 1,
919 'url' => 'http://civicrmUpdate.org',
920 ),
921 ),
922 'contact_source' => 'test contact',
923 'external_identifier' => 111222333,
924 'preferred_mail_format' => 'Both',
925 'is_opt_out' => 0,
926 'legal_identifier' => '123123123123',
927 'image_URL' => 'http://imageupdate.com',
928 'deceased_date' => '1981-10-10',
929 'birth_date' => '1951-11-11',
930 'privacy' => array(
931 'do_not_phone' => 1,
932 'do_not_email' => 1,
933 ),
934 'preferred_communication_method' => array(
935 '1' => 0,
936 '2' => 1,
937 '3' => 0,
938 '4' => 1,
939 '5' => 0,
940 ),
941 );
942
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',
952 'do_not_sms' => '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',
958 'on_hold' => '1',
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',
971 'tag' => array(
972 '2' => '1',
973 '5' => '1',
974 ),
975 );
976
977 $createParams = array_merge($updateCParams, $updatePfParams);
978
979 //create the contact using create profile contact.
980 $contactID = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, $contactId,
981 NULL, NULL, NULL, TRUE
982 );
983
984 //check the contact ids
985 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
986
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."
993 );
994 }
995 else {
996 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
997 'id', $updateCParams[$key], "Check for {$key} creation."
998 );
999 }
1000 }
1001 }
1002
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.'
1007 );
1008 }
1009
1010 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1011 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1012 );
1013 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1014 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1015 );
1016
1017 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1018 $updateCParams['birth_date'], 'Check for birth_date creation.'
1019 );
1020
1021 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1022 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1023 );
1024
1025 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
1026 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1027 );
1028 $checkPrefComm = array();
1029 foreach ($dbPrefComm as $key => $value) {
1030 if ($value) {
1031 $checkPrefComm[$value] = 1;
1032 }
1033 }
1034 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1035
1036 //Now check DB for Address
1037 $searchParams = array(
1038 'contact_id' => $contactId,
1039 'location_type_id' => 1,
1040 'is_primary' => 1,
1041 );
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),
1051 );
1052 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1053
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);
1057
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),
1061 );
1062 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1063
1064 //Now check DB for Phone
1065 $searchParams = array(
1066 'contact_id' => $contactId,
1067 'location_type_id' => 1,
1068 'is_primary' => 1,
1069 );
1070 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams));
1071 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1072
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]),
1078 );
1079 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams));
1080 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1081
1082 //get the value of relationship
1083 $values = array();
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));
1095 }
1096
1097 //Now check values of tag ids.
1098 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
1099 foreach ($tags as $key => $val) {
1100 $tagIds[$key] = 1;
1101 }
1102 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1103
1104 //cleanup DB by deleting the contact
1105 Contact::delete($contactId);
1106 }
1107
1108 /**
1109 * test case for getContactDetails( )
1110 */
1111 function testGetContactDetails() {
1112 //get the contact params
1113 $params = $this->contactParams();
1114
1115 //create contact
1116 $contact = CRM_Contact_BAO_Contact::create($params);
1117 $contactId = $contact->id;
1118
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'],
1125 );
1126 //Now check the contact details
1127 $this->assertAttributesEquals($compareParams, $contactDetails);
1128
1129 //cleanup DB by deleting the contact
1130 Contact::delete($contactId);
1131 $this->quickCleanup(array('civicrm_contact'));
1132 }
1133
1134 /**
1135 * test case for
1136 * importableFields( ) and exportableFields( )
1137 */
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);
1143
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);
1148
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);
1153
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);
1158 }
1159
1160 /**
1161 * test case for getPrimaryEmail( )
1162 *
1163 */
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']);
1170
1171 //create contact
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');
1178
1179 //cleanup DB by deleting the contact
1180 Contact::delete($contactId);
1181 $this->quickCleanup(array('civicrm_contact'));
1182 }
1183
1184 /**
1185 * test case for getPrimaryOpenId( )
1186 *
1187 */
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']);
1195
1196 //create contact
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);
1201
1202 //Now check the primary openid
1203 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1204
1205 //cleanup DB by deleting the contact
1206 Contact::delete($contactId);
1207 }
1208
1209 /**
1210 * test case for matchContactOnEmail( )
1211 *
1212 */
1213 function testMatchContactOnEmail() {
1214 //get the contact params
1215 $params = $this->contactParams();
1216 //create contact
1217 $contact = CRM_Contact_BAO_Contact::create($params);
1218 $contactId = $contact->id;
1219
1220 //get the matching contact.
1221 $match = CRM_Contact_BAO_Contact::matchContactOnEmail(CRM_Utils_Array::value('email', $params['email'][1]),
1222 'Individual'
1223 );
1224 $this->assertEquals($contactId, $match->contact_id, 'Check For Matching Contact');
1225
1226 //cleanup DB by deleting the contact
1227 Contact::delete($contactId);
1228 $this->quickCleanup(array('civicrm_contact'));
1229 }
1230
1231 /**
1232 * test case for getContactType( )
1233 *
1234 */
1235 function testGetContactType() {
1236 //get the contact params
1237 $params = $this->contactParams();
1238 //create contact
1239 $contact = CRM_Contact_BAO_Contact::create($params);
1240 $contactId = $contact->id;
1241
1242 //get contact type.
1243 $contactType = CRM_Contact_BAO_Contact::getContactType($contactId);
1244 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1245
1246 //cleanup DB by deleting the contact
1247 Contact::delete($contactId);
1248 $this->quickCleanup(array('civicrm_contact'));
1249 }
1250
1251 /**
1252 * test case for displayName( )
1253 *
1254 */
1255 function testDisplayName() {
1256 //get the contact params
1257 $params = $this->contactParams();
1258
1259 //create contact
1260 $contact = CRM_Contact_BAO_Contact::create($params);
1261 $contactId = $contact->id;
1262
1263 //get display name.
1264 $dbDisplayName = CRM_Contact_BAO_Contact::displayName($contactId);
1265
1266 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1267 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1268
1269 //build display name
1270 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1271
1272 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1273
1274 //cleanup DB by deleting the contact
1275 Contact::delete($contactId);
1276 $this->quickCleanup(array('civicrm_contact'));
1277 }
1278
1279 /**
1280 * test case for getDisplayAndImage( )
1281 *
1282 */
1283 function testGetDisplayAndImage() {
1284 //get the contact params
1285 $params = $this->contactParams();
1286
1287 //create contact
1288 $contact = CRM_Contact_BAO_Contact::create($params);
1289 $contactId = $contact->id;
1290
1291 //get DisplayAndImage.
1292 list($displayName, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId);
1293
1294 $checkImage = CRM_Contact_BAO_Contact_Utils::getImage($params['contact_type'], FALSE, $contactId);
1295
1296 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1297 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1298
1299 //build display name
1300 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1301
1302 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1303 $this->assertEquals($image, $checkImage, 'Check For Image');
1304
1305 //cleanup DB by deleting the contact
1306 Contact::delete($contactId);
1307 }
1308
1309 // /**
1310 // * test logged in user has permissions for
1311 // * various operation types
1312 // */
1313 // function testPermissionedContact()
1314 // {
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.' );
1319 //
1320 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1321 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1322 //
1323 // Contact::delete($contactId);
1324 // }
1325
1326 /**
1327 * function to build common params
1328 *
1329 */
1330 private function contactParams() {
1331
1332 $params = array(
1333 'first_name' => 'john',
1334 'last_name' => 'smith',
1335 'contact_type' => 'Individual',
1336 'middle_name' => 'xyz',
1337 'prefix_id' => 3,
1338 'suffix_id' => 2,
1339 'nick_name' => 'Nick Name',
1340 'job_title' => 'software engg',
1341 'gender_id' => 2,
1342 'is_deceased' => 1,
1343 'website' => array(
1344 1 => array(
1345 'website_type_id' => 1,
1346 'url' => 'http://civicrm.org',
1347 ),
1348 ),
1349 'contact_source' => 'test contact',
1350 'external_identifier' => 123456789,
1351 'preferred_mail_format' => 'Both',
1352 'is_opt_out' => 1,
1353 'legal_identifier' => '123456789',
1354 'image_URL' => 'http://image.com',
1355 'deceased_date' => '1991-07-07',
1356 'birth_date' => '1961-06-06',
1357 'privacy' => array(
1358 'do_not_phone' => 1,
1359 'do_not_email' => 1,
1360 'do_not_mail' => 1,
1361 'do_not_trade' => 1,
1362 ),
1363 'preferred_communication_method' => array(
1364 '1' => 1,
1365 '2' => 0,
1366 '3' => 1,
1367 '4' => 0,
1368 '5' => 1,
1369 ),
1370 );
1371
1372 $params['address'] = array();
1373 $params['address'][1] = array(
1374 'location_type_id' => 1,
1375 'is_primary' => 1,
1376 'street_address' => 'Saint Helier St',
1377 'supplemental_address_1' => 'Hallmark Ct',
1378 'supplemental_address_2' => 'Jersey Village',
1379 'city' => 'Newark',
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',
1385 );
1386
1387 $params['email'] = array();
1388 $params['email'][1] = array(
1389 'location_type_id' => 1,
1390 'is_primary' => 1,
1391 'email' => 'john.smith@example.org',
1392 );
1393
1394
1395 $params['phone'] = array();
1396 $params['phone'][1] = array(
1397 'location_type_id' => 1,
1398 'is_primary' => 1,
1399 'phone_type_id' => 1,
1400 'phone' => '303443689',
1401 );
1402 $params['phone'][2] = array(
1403 'location_type_id' => 1,
1404 'phone_type_id' => 2,
1405 'phone' => '9833910234',
1406 );
1407
1408 $params['openid'] = array();
1409 $params['openid'][1] = array(
1410 'location_type_id' => 1,
1411 'is_primary' => 1,
1412 'openid' => 'http://civicrm.org/',
1413 );
1414
1415 $params['im'] = array();
1416 $params['im'][1] = array(
1417 'location_type_id' => 1,
1418 'is_primary' => 1,
1419 'name' => 'john.smith',
1420 'provider_id' => 1,
1421 );
1422
1423 return $params;
1424 }
1425
1426 /**
1427 * Ensure that created_date and modified_date are set
1428 */
1429 function testTimestamps_contact() {
1430 $test = $this;
1431 $this->_testTimestamps(array(
1432 'UPDATE' => function ($contactId) use ($test) {
1433 $params = array(
1434 'first_name' => 'Testing',
1435 'contact_type' => 'Individual',
1436 'contact_id' => $contactId,
1437 );
1438 $contact = CRM_Contact_BAO_Contact::add($params);
1439 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1440 },
1441 ));
1442 }
1443
1444 /**
1445 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1446 */
1447 function testTimestamps_email() {
1448 $test = $this;
1449 $this->_testTimestamps(array(
1450 'INSERT' => function ($contactId) use ($test) {
1451 $params = array(
1452 'email' => 'ex-1@example.com',
1453 'is_primary' => 1,
1454 'location_type_id' => 1,
1455 'contact_id' => $contactId,
1456 );
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'))
1461 );
1462 },
1463
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'))
1468 );
1469 },
1470
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'))
1475 );
1476 },
1477 ));
1478 }
1479
1480 /**
1481 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1482 */
1483 function testTimestamps_phone() {
1484 $test = $this;
1485 $this->_testTimestamps(array(
1486 'INSERT' => function ($contactId) use ($test) {
1487 $params = array(
1488 'phone' => '202-555-1000',
1489 'is_primary' => 1,
1490 'location_type_id' => 1,
1491 'contact_id' => $contactId,
1492 );
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'))
1497 );
1498 },
1499
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'))
1504 );
1505 },
1506
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'))
1511 );
1512 },
1513 ));
1514 }
1515
1516 /**
1517 * Ensure that civicrm_contact.modified_date is updated when contact-related
1518 * custom data
1519 */
1520 function testTimestamps_custom() {
1521 $customGroup = Custom::createGroup(array(), 'Individual');
1522 $this->assertNotNull($customGroup);
1523 $fields = array(
1524 'custom_group_id' => $customGroup->id,
1525 'data_type' => 'String',
1526 'html_type' => 'Text',
1527 );
1528 $customField = $this->customFieldCreate($fields);
1529 $customField = $customField['values'][$customField['id']];
1530 $test = $this;
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',
1536 ));
1537 },
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'))
1542 );
1543 },
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'))
1548 );
1549 },
1550 ));
1551
1552 Custom::deleteGroup($customGroup);
1553 }
1554
1555 /**
1556 * Helper for testing timestamp manipulation
1557 *
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.
1560 *
1561 * @param $callbacks array ($name => $callable)
1562 */
1563 function _testTimestamps($callbacks) {
1564 CRM_Core_DAO::triggerRebuild();
1565 $contactId = Contact::createIndividual();
1566
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']);
1571
1572 $prevTimestamps = $origTimestamps;
1573 foreach ($callbacks as $callbackName => $callback) {
1574 sleep(1); // advance clock by 1 second to ensure timestamps change
1575
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)");
1582
1583 $prevTimestamps = $newTimestamps;
1584 }
1585
1586 Contact::delete($contactId);
1587 }
1588 }
1589
1590
1591