Merge branch '4.4' into master
[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
550 //create the contact with given params.
551 $contact = CRM_Contact_BAO_Contact::create($params);
552 //Now check $contact is object of contact DAO..
553 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
554 $contactId = $contact->id;
555 //create the organization contact with the given params.
556 $orgParams = array(
557 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
558 'contact_type' => 'Organization',
559 );
560 $orgContact = CRM_Contact_BAO_Contact::add($orgParams);
561 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
562
563 //create employee of relationship.
564 CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id);
565
566 //retrieve the contact values from database.
567 $values = array();
568 $searchParams = array('contact_id' => $contactId);
569 $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values);
570
571 //Now check $retrieveContact is object of contact DAO..
572 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
573
574 //Now check the ids.
575 $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id');
576
577 //Now check values retrieve from database with params.
578 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
579 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
580 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
581
582 //Now check values of address
583 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
584 CRM_Utils_Array::value('1', $values['address'])
585 );
586
587 //Now check values of email
588 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
589 CRM_Utils_Array::value('1', $values['email'])
590 );
591
592 //Now check values of phone
593 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
594 CRM_Utils_Array::value('1', $values['phone'])
595 );
596
597 //Now check values of mobile
598 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
599 CRM_Utils_Array::value('2', $values['phone'])
600 );
601
602 //Now check values of openid
603 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
604 CRM_Utils_Array::value('1', $values['openid'])
605 );
606
607 //Now check values of im
608 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
609 CRM_Utils_Array::value('1', $values['im'])
610 );
611
612 //Now check values of Note Count.
613 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
614
615 foreach ($values['note'] as $key => $val) {
616 $retrieveNote = CRM_Utils_Array::value('note', $val);
617 //check the note value
618 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
619 }
620
621 //Now check values of Relationship Count.
622 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
623 foreach ($values['relationship']['data'] as $key => $val) {
624 //Now check values of Relationship organization.
625 $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization');
626 //Now check values of Relationship type.
627 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
628 //delete the organization.
629 Contact::delete(CRM_Utils_Array::value('contact_id_b', $val));
630 }
631
632 //delete all notes related to contact
633 CRM_Core_BAO_Note::cleanContactNotes($contactId);
634
635
636 //cleanup DB by deleting the contact
637 Contact::delete($contactId);
638 $this->quickCleanup(array('civicrm_contact'));
639 }
640
641 /**
642 * test case for deleteContact( )
643 */
644 function testDeleteContact() {
645 $contactParams = $this->contactParams();
646
647 //create custom data
648 require_once 'CiviTest/Custom.php';
649 $customGroup = Custom::createGroup(array(), 'Individual');
650 $fields = array(
651 'label' => 'testFld',
652 'data_type' => 'String',
653 'html_type' => 'Text',
654 'custom_group_id' => $customGroup->id,
655 );
656 $customField = CRM_Core_BAO_CustomField::create($fields);
657 $contactParams['custom'] = array(
658 $customField->id => array(
659 -1 => array(
660 'value' => 'Test custom value',
661 'type' => 'String',
662 'custom_field_id' => $customField->id,
663 'custom_group_id' => $customGroup->id,
664 'table_name' => $customGroup->table_name,
665 'column_name' => $customField->column_name,
666 'file_id' => NULL,
667 ),
668 ),
669 );
670
671 //create contact
672 $contact = CRM_Contact_BAO_Contact::create($contactParams);
673 $contactId = $contact->id;
674
675 //delete contact.
676 CRM_Contact_BAO_Contact::deleteContact($contactId);
677
678 //Now check DB for location elements.
679 //Now check DB for Address
680
681 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
682 'id', 'street_address', 'Database check, Address deleted successfully.'
683 );
684
685 //Now check DB for Email
686 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
687 'id', 'email', 'Database check, Email deleted successfully.'
688 );
689 //Now check DB for Phone
690 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
691 'id', 'phone', 'Database check, Phone deleted successfully.'
692 );
693 //Now check DB for Mobile
694 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
695 'id', 'phone', 'Database check, Mobile deleted successfully.'
696 );
697 //Now check DB for IM
698 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
699 'id', 'name', 'Database check, IM deleted successfully.'
700 );
701 //Now check DB for openId
702 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
703 'id', 'openid', 'Database check, openId deleted successfully.'
704 );
705
706
707 // Check that the custom field value is no longer present
708 $params = array(
709 'entityID' => $contactId,
710 'custom_' . $customField->id => 1,
711 );
712 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
713 $this->assertEquals(CRM_Utils_Array::value("custom_" . $customField->id, $values), '',
714 'Verify that the data value is empty for contact ' . $contactId
715 );
716 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
717
718 //Now check DB for contact.
719 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
720 'id', 'sort_name', 'Database check, contact deleted successfully.'
721 );
722 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
723 Custom::deleteGroup($customGroup);
724 }
725
726 /**
727 * test case for createProfileContac( )
728 * test with all params.
729 * ( create and update modes )
730 */
731 function testCreateProfileContact() {
732 $fields = CRM_Contact_BAO_Contact::exportableFields('Individual');
733
734 //current employer field for individual
735 $fields['organization_name'] = array(
736 'name' => 'organization_name',
737 'where' => 'civicrm_organization.organization_name',
738 'title' => 'Current Employer',
739 );
740 //get the common params
741 $contactParams = $this->contactParams();
742 $unsetParams = array('location', 'privacy');
743 foreach ($unsetParams as $param) {
744 unset($contactParams[$param]);
745 }
746
747 $profileParams = array(
748 'organization_name' => 'Yahoo',
749 'gender_id' => '2',
750 'prefix_id' => '3',
751 'suffix_id' => '2',
752 'city-Primary' => 'Newark',
753 'contact_type' => 'Individual',
754 'country-Primary' => '1228',
755 'do_not_email' => '1',
756 'do_not_mail' => '1',
757 'do_not_phone' => '1',
758 'do_not_trade' => '1',
759 'do_not_sms' => '1',
760 'email-Primary' => 'john.smith@example.org',
761 'geo_code_1-Primary' => '18.219023',
762 'geo_code_2-Primary' => '-105.00973',
763 'im-Primary-provider_id' => '1',
764 'im-Primary' => 'john.smith',
765 'on_hold' => '1',
766 'openid' => 'john.smith@example.org',
767 'phone-Primary-1' => '303443689',
768 'phone-Primary-2' => '9833910234',
769 'postal_code-Primary' => '01903',
770 'postal_code_suffix-Primary' => '12345',
771 'state_province-Primary' => '1029',
772 'street_address-Primary' => 'Saint Helier St',
773 'supplemental_address_1-Primary' => 'Hallmark Ct',
774 'supplemental_address_2-Primary' => 'Jersey Village',
775 'user_unique_id' => '123456789',
776 'is_bulkmail' => '1',
777 'world_region' => 'India',
778 'tag' => array(
779 '3' => '1',
780 '4' => '1',
781 '1' => '1',
782 ),
783 );
784 $createParams = array_merge($contactParams, $profileParams);
785
786 //create the contact using create profile contact.
787 $contactId = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
788
789 //get the parameters to compare.
790 $params = $this->contactParams();
791
792 //check the values in DB.
793 foreach ($params as $key => $val) {
794 if (!is_array($params[$key])) {
795 if ($key == 'contact_source') {
796 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
797 'id', $params[$key], "Check for {$key} creation."
798 );
799 }
800 else {
801 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
802 'id', $params[$key], "Check for {$key} creation."
803 );
804 }
805 }
806 }
807
808 //check privacy options.
809 foreach ($params['privacy'] as $key => $value) {
810 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
811 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
812 );
813 }
814
815 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
816 'id', $profileParams['contact_type'], 'Check for contact type creation.'
817 );
818 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
819 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
820 );
821
822 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
823 'id', $params['birth_date'], 'Check for birth_date creation.'
824 );
825
826 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
827 'id', $params['deceased_date'], 'Check for deceased_date creation.'
828 );
829
830 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
831 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
832 );
833 $checkPrefComm = array();
834 foreach ($dbPrefComm as $key => $value) {
835 if ($value) {
836 $checkPrefComm[$value] = 1;
837 }
838 }
839 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
840
841 //Now check DB for Address
842 $searchParams = array(
843 'contact_id' => $contactId,
844 'location_type_id' => 1,
845 'is_primary' => 1,
846 );
847 $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $profileParams),
848 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $profileParams),
849 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $profileParams),
850 'city' => CRM_Utils_Array::value('city-Primary', $profileParams),
851 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $profileParams),
852 'country_id' => CRM_Utils_Array::value('country-Primary', $profileParams),
853 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $profileParams),
854 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $profileParams),
855 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $profileParams),
856 );
857 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
858
859 //Now check DB for Email
860 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $profileParams));
861 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
862
863 //Now check DB for IM
864 $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $profileParams),
865 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $profileParams),
866 );
867 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
868
869 //Now check DB for Phone
870 $searchParams = array(
871 'contact_id' => $contactId,
872 'location_type_id' => 1,
873 'is_primary' => 1,
874 );
875 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams));
876 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
877
878 //Now check DB for Mobile
879 $searchParams = array(
880 'contact_id' => $contactId,
881 'location_type_id' => 1,
882 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
883 );
884 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams));
885
886 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
887
888 //get the value of relationship
889 $values = array();
890 $searchParams = array('contact_id' => $contactId);
891 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
892 //Now check values of Relationship Count.
893 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
894 foreach ($values['relationship']['data'] as $key => $val) {
895 //Now check values of Relationship organization.
896 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
897 //Now check values of Relationship type.
898 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
899 //delete the organization.
900 Contact::delete(CRM_Utils_Array::value('cid', $val));
901 }
902
903 //Now check values of tag ids.
904 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
905 foreach ($tags as $key => $val) {
906 $tagIds[$key] = 1;
907 }
908
909 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
910
911 //update Contact mode
912 $updateCParams = array(
913 'first_name' => 'john',
914 'last_name' => 'doe',
915 'contact_type' => 'Individual',
916 'middle_name' => 'abc',
917 'prefix_id' => 2,
918 'suffix_id' => 3,
919 'nick_name' => 'Nick Name Updated',
920 'job_title' => 'software Developer',
921 'gender_id' => 1,
922 'is_deceased' => 1,
923 'website' => array(
924 1 => array(
925 'website_type_id' => 1,
926 'url' => 'http://civicrmUpdate.org',
927 ),
928 ),
929 'contact_source' => 'test contact',
930 'external_identifier' => 111222333,
931 'preferred_mail_format' => 'Both',
932 'is_opt_out' => 0,
933 'legal_identifier' => '123123123123',
934 'image_URL' => 'http://imageupdate.com',
935 'deceased_date' => '1981-10-10',
936 'birth_date' => '1951-11-11',
937 'privacy' => array(
938 'do_not_phone' => 1,
939 'do_not_email' => 1,
940 ),
941 'preferred_communication_method' => array(
942 '1' => 0,
943 '2' => 1,
944 '3' => 0,
945 '4' => 1,
946 '5' => 0,
947 ),
948 );
949
950 $updatePfParams = array(
951 'organization_name' => 'Google',
952 'city-Primary' => 'Mumbai',
953 'contact_type' => 'Individual',
954 'country-Primary' => '1228',
955 'do_not_email' => '1',
956 'do_not_mail' => '1',
957 'do_not_phone' => '1',
958 'do_not_trade' => '1',
959 'do_not_sms' => '1',
960 'email-Primary' => 'john.doe@example.org',
961 'geo_code_1-Primary' => '31.694842',
962 'geo_code_2-Primary' => '-106.29998',
963 'im-Primary-provider_id' => '1',
964 'im-Primary' => 'john.doe',
965 'on_hold' => '1',
966 'openid' => 'john.doe@example.org',
967 'phone-Primary-1' => '02115245336',
968 'phone-Primary-2' => '9766323895',
969 'postal_code-Primary' => '12345',
970 'postal_code_suffix-Primary' => '123',
971 'state_province-Primary' => '1004',
972 'street_address-Primary' => 'Oberoi Garden',
973 'supplemental_address_1-Primary' => 'A-wing:3037',
974 'supplemental_address_2-Primary' => 'Andhery',
975 'user_unique_id' => '1122334455',
976 'is_bulkmail' => '1',
977 'world_region' => 'India',
978 'tag' => array(
979 '2' => '1',
980 '5' => '1',
981 ),
982 );
983
984 $createParams = array_merge($updateCParams, $updatePfParams);
985
986 //create the contact using create profile contact.
987 $contactID = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, $contactId,
988 NULL, NULL, NULL, TRUE
989 );
990
991 //check the contact ids
992 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
993
994 //check the values in DB.
995 foreach ($updateCParams as $key => $val) {
996 if (!is_array($updateCParams[$key])) {
997 if ($key == 'contact_source') {
998 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
999 'id', $updateCParams[$key], "Check for {$key} creation."
1000 );
1001 }
1002 else {
1003 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1004 'id', $updateCParams[$key], "Check for {$key} creation."
1005 );
1006 }
1007 }
1008 }
1009
1010 //check privacy options.
1011 foreach ($updateCParams['privacy'] as $key => $value) {
1012 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1013 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1014 );
1015 }
1016
1017 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1018 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1019 );
1020 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1021 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1022 );
1023
1024 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1025 $updateCParams['birth_date'], 'Check for birth_date creation.'
1026 );
1027
1028 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1029 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1030 );
1031
1032 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
1033 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1034 );
1035 $checkPrefComm = array();
1036 foreach ($dbPrefComm as $key => $value) {
1037 if ($value) {
1038 $checkPrefComm[$value] = 1;
1039 }
1040 }
1041 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1042
1043 //Now check DB for Address
1044 $searchParams = array(
1045 'contact_id' => $contactId,
1046 'location_type_id' => 1,
1047 'is_primary' => 1,
1048 );
1049 $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $updatePfParams),
1050 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $updatePfParams),
1051 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $updatePfParams),
1052 'city' => CRM_Utils_Array::value('city-Primary', $updatePfParams),
1053 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $updatePfParams),
1054 'country_id' => CRM_Utils_Array::value('country-Primary', $updatePfParams),
1055 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $updatePfParams),
1056 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $updatePfParams),
1057 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $updatePfParams),
1058 );
1059 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1060
1061 //Now check DB for Email
1062 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $updatePfParams));
1063 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1064
1065 //Now check DB for IM
1066 $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $updatePfParams),
1067 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $updatePfParams),
1068 );
1069 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1070
1071 //Now check DB for Phone
1072 $searchParams = array(
1073 'contact_id' => $contactId,
1074 'location_type_id' => 1,
1075 'is_primary' => 1,
1076 );
1077 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams));
1078 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1079
1080 //Now check DB for Mobile
1081 $searchParams = array(
1082 'contact_id' => $contactId,
1083 'location_type_id' => 1,
1084 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
1085 );
1086 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams));
1087 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1088
1089 //get the value of relationship
1090 $values = array();
1091 $searchParams = array('contact_id' => $contactId);
1092 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
1093 //Now check values of Relationship Count.
1094 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1095 foreach ($values['relationship']['data'] as $key => $val) {
1096 //Now check values of Relationship organization.
1097 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1098 //Now check values of Relationship type.
1099 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1100 //delete the organization.
1101 Contact::delete(CRM_Utils_Array::value('cid', $val));
1102 }
1103
1104 //Now check values of tag ids.
1105 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
1106 foreach ($tags as $key => $val) {
1107 $tagIds[$key] = 1;
1108 }
1109 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1110
1111 //cleanup DB by deleting the contact
1112 Contact::delete($contactId);
1113 }
1114
1115 /**
1116 * test case for getContactDetails( )
1117 */
1118 function testGetContactDetails() {
1119 //get the contact params
1120 $params = $this->contactParams();
1121
1122 //create contact
1123 $contact = CRM_Contact_BAO_Contact::create($params);
1124 $contactId = $contact->id;
1125
1126 //get the contact details
1127 $contactDetails = CRM_Contact_BAO_Contact::getContactDetails($contactId);
1128 $compareParams = array(
1129 $params['first_name'] . ' ' . $params['last_name'],
1130 CRM_Utils_Array::value('email', $params['email'][1]),
1131 (bool ) $params['privacy']['do_not_email'],
1132 );
1133 //Now check the contact details
1134 $this->assertAttributesEquals($compareParams, $contactDetails);
1135
1136 //cleanup DB by deleting the contact
1137 Contact::delete($contactId);
1138 $this->quickCleanup(array('civicrm_contact'));
1139 }
1140
1141 /**
1142 * test case for
1143 * importableFields( ) and exportableFields( )
1144 */
1145 function testFields() {
1146 $allImpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1147 $allExpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1148 //Now check all fields
1149 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1150
1151 $individualImpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1152 $individualExpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1153 //Now check Individual fields
1154 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1155
1156 $householdImpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1157 $householdExpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1158 //Now check Household fields
1159 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1160
1161 $organizationImpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1162 $organizationExpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1163 //Now check Organization fields
1164 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1165 }
1166
1167 /**
1168 * test case for getPrimaryEmail( )
1169 *
1170 */
1171 function testGetPrimaryEmail() {
1172 //get the contact params
1173 $params = $this->contactParams();
1174 $params['email'][2] = $params['email'][1];
1175 $params['email'][2]['email'] = 'primarymail@example.org';
1176 unset($params['email'][1]['is_primary']);
1177
1178 //create contact
1179 $contact = CRM_Contact_BAO_Contact::create($params);
1180 $contactId = $contact->id;
1181 //get the primary email.
1182 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId);
1183 //Now check the primary email
1184 $this->assertEquals($email, CRM_Utils_Array::value('email', $params['email'][2]), 'Check Primary Email');
1185
1186 //cleanup DB by deleting the contact
1187 Contact::delete($contactId);
1188 $this->quickCleanup(array('civicrm_contact'));
1189 }
1190
1191 /**
1192 * test case for getPrimaryOpenId( )
1193 *
1194 */
1195 function testGetPrimaryOpenId() {
1196 //get the contact params
1197 $params = $this->contactParams();
1198 $params['openid'][2] = $params['openid'][1];
1199 $params['openid'][2]['location_type_id'] = 2;
1200 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1201 unset($params['openid'][1]['is_primary']);
1202
1203 //create contact
1204 $contact = CRM_Contact_BAO_Contact::create($params);
1205 $contactId = $contact->id;
1206 //get the primary openid
1207 $openID = CRM_Contact_BAO_Contact::getPrimaryOpenId($contactId);
1208
1209 //Now check the primary openid
1210 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1211
1212 //cleanup DB by deleting the contact
1213 Contact::delete($contactId);
1214 }
1215
1216 /**
1217 * test case for matchContactOnEmail( )
1218 *
1219 */
1220 function testMatchContactOnEmail() {
1221 //get the contact params
1222 $params = $this->contactParams();
1223 //create contact
1224 $contact = CRM_Contact_BAO_Contact::create($params);
1225 $contactId = $contact->id;
1226
1227 //get the matching contact.
1228 $match = CRM_Contact_BAO_Contact::matchContactOnEmail(CRM_Utils_Array::value('email', $params['email'][1]),
1229 'Individual'
1230 );
1231 $this->assertEquals($contactId, $match->contact_id, 'Check For Matching Contact');
1232
1233 //cleanup DB by deleting the contact
1234 Contact::delete($contactId);
1235 $this->quickCleanup(array('civicrm_contact'));
1236 }
1237
1238 /**
1239 * test case for getContactType( )
1240 *
1241 */
1242 function testGetContactType() {
1243 //get the contact params
1244 $params = $this->contactParams();
1245 //create contact
1246 $contact = CRM_Contact_BAO_Contact::create($params);
1247 $contactId = $contact->id;
1248
1249 //get contact type.
1250 $contactType = CRM_Contact_BAO_Contact::getContactType($contactId);
1251 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1252
1253 //cleanup DB by deleting the contact
1254 Contact::delete($contactId);
1255 $this->quickCleanup(array('civicrm_contact'));
1256 }
1257
1258 /**
1259 * test case for displayName( )
1260 *
1261 */
1262 function testDisplayName() {
1263 //get the contact params
1264 $params = $this->contactParams();
1265
1266 //create contact
1267 $contact = CRM_Contact_BAO_Contact::create($params);
1268 $contactId = $contact->id;
1269
1270 //get display name.
1271 $dbDisplayName = CRM_Contact_BAO_Contact::displayName($contactId);
1272
1273 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1274 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1275
1276 //build display name
1277 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1278
1279 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1280
1281 //cleanup DB by deleting the contact
1282 Contact::delete($contactId);
1283 $this->quickCleanup(array('civicrm_contact'));
1284 }
1285
1286 /**
1287 * test case for getDisplayAndImage( )
1288 *
1289 */
1290 function testGetDisplayAndImage() {
1291 //get the contact params
1292 $params = $this->contactParams();
1293
1294 //create contact
1295 $contact = CRM_Contact_BAO_Contact::create($params);
1296 $contactId = $contact->id;
1297
1298 //get DisplayAndImage.
1299 list($displayName, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId);
1300
1301 $checkImage = CRM_Contact_BAO_Contact_Utils::getImage($params['contact_type'], FALSE, $contactId);
1302
1303 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1304 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1305
1306 //build display name
1307 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1308
1309 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1310 $this->assertEquals($image, $checkImage, 'Check For Image');
1311
1312 //cleanup DB by deleting the contact
1313 Contact::delete($contactId);
1314 }
1315
1316 // /**
1317 // * test logged in user has permissions for
1318 // * various operation types
1319 // */
1320 // function testPermissionedContact()
1321 // {
1322 // $contactId = Contact::createIndividual();
1323 // require_once 'CRM/Contact/BAO/Contact/Permission.php';
1324 // $userViewPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId);
1325 // $this->assertEquals( $userViewPermission, true, 'Check user for view contact permission.' );
1326 //
1327 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1328 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1329 //
1330 // Contact::delete($contactId);
1331 // }
1332
1333 /**
1334 * function to build common params
1335 *
1336 */
1337 private function contactParams() {
1338
1339 $params = array(
1340 'first_name' => 'john',
1341 'last_name' => 'smith',
1342 'contact_type' => 'Individual',
1343 'middle_name' => 'xyz',
1344 'prefix_id' => 3,
1345 'suffix_id' => 2,
1346 'nick_name' => 'Nick Name',
1347 'job_title' => 'software engg',
1348 'gender_id' => 2,
1349 'is_deceased' => 1,
1350 'website' => array(
1351 1 => array(
1352 'website_type_id' => 1,
1353 'url' => 'http://civicrm.org',
1354 ),
1355 ),
1356 'contact_source' => 'test contact',
1357 'external_identifier' => 123456789,
1358 'preferred_mail_format' => 'Both',
1359 'is_opt_out' => 1,
1360 'legal_identifier' => '123456789',
1361 'image_URL' => 'http://image.com',
1362 'deceased_date' => '1991-07-07',
1363 'birth_date' => '1961-06-06',
1364 'privacy' => array(
1365 'do_not_phone' => 1,
1366 'do_not_email' => 1,
1367 'do_not_mail' => 1,
1368 'do_not_trade' => 1,
1369 ),
1370 'preferred_communication_method' => array(
1371 '1' => 1,
1372 '2' => 0,
1373 '3' => 1,
1374 '4' => 0,
1375 '5' => 1,
1376 ),
1377 );
1378
1379 $params['address'] = array();
1380 $params['address'][1] = array(
1381 'location_type_id' => 1,
1382 'is_primary' => 1,
1383 'street_address' => 'Saint Helier St',
1384 'supplemental_address_1' => 'Hallmark Ct',
1385 'supplemental_address_2' => 'Jersey Village',
1386 'city' => 'Newark',
1387 'postal_code' => '01903',
1388 'country_id' => 1228,
1389 'state_province_id' => 1029,
1390 'geo_code_1' => '18.219023',
1391 'geo_code_2' => '-105.00973',
1392 );
1393
1394 $params['email'] = array();
1395 $params['email'][1] = array(
1396 'location_type_id' => 1,
1397 'is_primary' => 1,
1398 'email' => 'john.smith@example.org',
1399 );
1400
1401
1402 $params['phone'] = array();
1403 $params['phone'][1] = array(
1404 'location_type_id' => 1,
1405 'is_primary' => 1,
1406 'phone_type_id' => 1,
1407 'phone' => '303443689',
1408 );
1409 $params['phone'][2] = array(
1410 'location_type_id' => 1,
1411 'phone_type_id' => 2,
1412 'phone' => '9833910234',
1413 );
1414
1415 $params['openid'] = array();
1416 $params['openid'][1] = array(
1417 'location_type_id' => 1,
1418 'is_primary' => 1,
1419 'openid' => 'http://civicrm.org/',
1420 );
1421
1422 $params['im'] = array();
1423 $params['im'][1] = array(
1424 'location_type_id' => 1,
1425 'is_primary' => 1,
1426 'name' => 'john.smith',
1427 'provider_id' => 1,
1428 );
1429
1430 return $params;
1431 }
1432
1433 /**
1434 * Ensure that created_date and modified_date are set
1435 */
1436 function testTimestamps_contact() {
1437 $test = $this;
1438 $this->_testTimestamps(array(
1439 'UPDATE' => function ($contactId) use ($test) {
1440 $params = array(
1441 'first_name' => 'Testing',
1442 'contact_type' => 'Individual',
1443 'contact_id' => $contactId,
1444 );
1445 $contact = CRM_Contact_BAO_Contact::add($params);
1446 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1447 },
1448 ));
1449 }
1450
1451 /**
1452 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1453 */
1454 function testTimestamps_email() {
1455 $test = $this;
1456 $this->_testTimestamps(array(
1457 'INSERT' => function ($contactId) use ($test) {
1458 $params = array(
1459 'email' => 'ex-1@example.com',
1460 'is_primary' => 1,
1461 'location_type_id' => 1,
1462 'contact_id' => $contactId,
1463 );
1464 CRM_Core_BAO_Email::add($params);
1465 $test->assertDBQuery('ex-1@example.com',
1466 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1467 array(1 => array($contactId, 'Integer'))
1468 );
1469 },
1470
1471 'UPDATE' => function ($contactId) use ($test) {
1472 CRM_Core_DAO::executeQuery(
1473 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1474 array(1 => array($contactId, 'Integer'))
1475 );
1476 },
1477
1478 'DELETE' => function ($contactId) use ($test) {
1479 CRM_Core_DAO::executeQuery(
1480 'DELETE FROM civicrm_email WHERE contact_id = %1',
1481 array(1 => array($contactId, 'Integer'))
1482 );
1483 },
1484 ));
1485 }
1486
1487 /**
1488 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1489 */
1490 function testTimestamps_phone() {
1491 $test = $this;
1492 $this->_testTimestamps(array(
1493 'INSERT' => function ($contactId) use ($test) {
1494 $params = array(
1495 'phone' => '202-555-1000',
1496 'is_primary' => 1,
1497 'location_type_id' => 1,
1498 'contact_id' => $contactId,
1499 );
1500 CRM_Core_BAO_Phone::add($params);
1501 $test->assertDBQuery('202-555-1000',
1502 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1503 array(1 => array($contactId, 'Integer'))
1504 );
1505 },
1506
1507 'UPDATE' => function ($contactId) use ($test) {
1508 CRM_Core_DAO::executeQuery(
1509 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1510 array(1 => array($contactId, 'Integer'))
1511 );
1512 },
1513
1514 'DELETE' => function ($contactId) use ($test) {
1515 CRM_Core_DAO::executeQuery(
1516 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1517 array(1 => array($contactId, 'Integer'))
1518 );
1519 },
1520 ));
1521 }
1522
1523 /**
1524 * Ensure that civicrm_contact.modified_date is updated when contact-related
1525 * custom data
1526 */
1527 function testTimestamps_custom() {
1528 $customGroup = Custom::createGroup(array(), 'Individual');
1529 $this->assertNotNull($customGroup);
1530 $fields = array(
1531 'custom_group_id' => $customGroup->id,
1532 'data_type' => 'String',
1533 'html_type' => 'Text',
1534 );
1535 $customField = $this->customFieldCreate($fields);
1536 $customField = $customField['values'][$customField['id']];
1537 $test = $this;
1538 $this->_testTimestamps(array(
1539 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1540 $result = civicrm_api3('contact', 'create', array(
1541 'contact_id' => $contactId,
1542 'custom_' . $customField['id'] => 'test-1',
1543 ));
1544 },
1545 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1546 CRM_Core_DAO::executeQuery(
1547 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1548 array(1 => array($contactId, 'Integer'))
1549 );
1550 },
1551 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1552 CRM_Core_DAO::executeQuery(
1553 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1554 array(1 => array($contactId, 'Integer'))
1555 );
1556 },
1557 ));
1558
1559 Custom::deleteGroup($customGroup);
1560 }
1561
1562 /**
1563 * Helper for testing timestamp manipulation
1564 *
1565 * Create a contact and perform a series of steps with it; after each
1566 * step, ensure that the contact's modified_date has increased.
1567 *
1568 * @param $callbacks array ($name => $callable)
1569 */
1570 function _testTimestamps($callbacks) {
1571 CRM_Core_DAO::triggerRebuild();
1572 $contactId = Contact::createIndividual();
1573
1574 $origTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1575 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1576 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1577 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1578
1579 $prevTimestamps = $origTimestamps;
1580 foreach ($callbacks as $callbackName => $callback) {
1581 sleep(1); // advance clock by 1 second to ensure timestamps change
1582
1583 $callback($contactId);
1584 $newTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1585 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1586 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1587 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1588 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1589
1590 $prevTimestamps = $newTimestamps;
1591 }
1592
1593 Contact::delete($contactId);
1594 }
1595 }
1596
1597
1598