Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-08-25-10-57-01
[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
6 /**
7 * Class CRM_Contact_BAO_ContactTest
8 */
9 class CRM_Contact_BAO_ContactTest extends CiviUnitTestCase {
10 /**
11 * @return array
12 */
13 function get_info() {
14 return array(
15 'name' => 'Contact BAOs',
16 'description' => 'Test all Contact_BAO_Contact methods.',
17 'group' => 'CiviCRM BAO Tests',
18 );
19 }
20
21 function setUp() {
22 parent::setUp();
23 }
24
25 /**
26 * test case for add( )
27 * test with empty params.
28 */
29 function testAddWithEmptyParams() {
30 $params = array( );
31 $contact = CRM_Contact_BAO_Contact::add($params);
32
33 //Now check Contact object
34 $this->assertNull($contact);
35 }
36
37 /**
38 * test case for add( )
39 * test with names
40 * (create and update modes)
41 */
42 function testAddWithNames() {
43 $firstName = 'Shane';
44 $lastName = 'Whatson';
45 $params = array(
46 'first_name' => $firstName,
47 'last_name' => $lastName,
48 'contact_type' => 'Individual',
49 );
50
51 $contact = CRM_Contact_BAO_Contact::add($params);
52
53 //Now check $contact is object of contact DAO..
54 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
55 $this->assertEquals($firstName, $contact->first_name, 'Check for first name creation.');
56 $this->assertEquals($lastName, $contact->last_name, 'Check for last name creation.');
57
58 $contactId = $contact->id;
59
60 //update and change first name and last name, using add( )
61 $firstName = 'Jane';
62 $params = array(
63 'first_name' => $firstName,
64 'contact_type' => 'Individual',
65 'contact_id' => $contactId,
66 );
67
68 $contact = CRM_Contact_BAO_Contact::add($params);
69
70 //Now check $contact is object of contact DAO..
71 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
72 $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.');
73
74 $contactId = $contact->id;
75 Contact::delete($contactId);
76 }
77
78 /**
79 * test case for add( )
80 * test with all contact params
81 * (creat and update modes)
82 */
83 function testAddWithAll() {
84 //take the common contact params
85 $params = $this->contactParams();
86
87 unset($params['location']);
88 $prefComm = $params['preferred_communication_method'];
89
90 //create the contact using add()
91 $contact = CRM_Contact_BAO_Contact::add($params);
92 $contactId = $contact->id;
93
94 //Now check $contact is object of contact DAO..
95 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
96
97 //Now check values of object with params.
98 $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.');
99 $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.');
100 $this->assertEquals($params['middle_name'], $contact->middle_name, 'Check for middle name creation.');
101 $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.');
102 $this->assertEquals('1', $contact->do_not_email, 'Check for do_not_email creation.');
103 $this->assertEquals('1', $contact->do_not_phone, 'Check for do_not_phone creation.');
104 $this->assertEquals('1', $contact->do_not_mail, 'Check for do_not_mail creation.');
105 $this->assertEquals('1', $contact->do_not_trade, 'Check for do_not_trade creation.');
106 $this->assertEquals('1', $contact->is_opt_out, 'Check for is_opt_out creation.');
107 $this->assertEquals($params['external_identifier'], $contact->external_identifier, 'Check for external_identifier creation.');
108 $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name, 'Check for sort_name creation.');
109 $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format,
110 'Check for preferred_mail_format creation.'
111 );
112 $this->assertEquals($params['contact_source'], $contact->source, 'Check for contact_source creation.');
113 $this->assertEquals($params['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.');
114 $this->assertEquals($params['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.');
115 $this->assertEquals($params['job_title'], $contact->job_title, 'Check for job_title creation.');
116 $this->assertEquals($params['gender_id'], $contact->gender_id, 'Check for gender_id creation.');
117 $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.');
118 $this->assertEquals(CRM_Utils_Date::processDate($params['birth_date']),
119 $contact->birth_date, 'Check for birth_date creation.'
120 );
121 $this->assertEquals(CRM_Utils_Date::processDate($params['deceased_date']),
122 $contact->deceased_date, 'Check for deceased_date creation.'
123 );
124 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
125 $contact->preferred_communication_method
126 );
127 $checkPrefComm = array();
128 foreach ($dbPrefComm as $key => $value) {
129 if ($value) {
130 $checkPrefComm[$value] = 1;
131 }
132 }
133 $this->assertAttributesEquals($checkPrefComm, $prefComm);
134
135 //now update the contact using add( )
136 $updateParams = array(
137 'contact_type' => 'Individual',
138 'first_name' => 'Jane',
139 'middle_name' => 'abc',
140 'last_name' => 'Doe',
141 'prefix_id' => 2,
142 'suffix_id' => 3,
143 'nick_name' => 'Nick Name Second',
144 'job_title' => 'software Developer',
145 'gender_id' => 1,
146 'is_deceased' => 1,
147 'website' => array(
148 1 => array(
149 'website_type_id' => 1,
150 'url' => 'http://docs.civicrm.org',
151 ),
152 ),
153 'contact_source' => 'test update contact',
154 'external_identifier' => 111111111,
155 'preferred_mail_format' => 'Both',
156 'is_opt_out' => 0,
157 'deceased_date' => '1981-03-03',
158 'birth_date' => '1951-04-04',
159 'privacy' => array(
160 'do_not_phone' => 0,
161 'do_not_email' => 0,
162 'do_not_mail' => 0,
163 'do_not_trade' => 0,
164 ),
165 'preferred_communication_method' => array(
166 '1' => 0,
167 '2' => 1,
168 '3' => 0,
169 '4' => 1,
170 '5' => 0,
171 ),
172 );
173
174 $prefComm = $updateParams['preferred_communication_method'];
175 $updateParams['contact_id'] = $contactId;
176 //create the contact using add()
177 $contact = CRM_Contact_BAO_Contact::create($updateParams);
178 $contactId = $contact->id;
179
180 //Now check $contact is object of contact DAO..
181 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
182
183 //Now check values of object with params.
184 $this->assertEquals($updateParams['first_name'], $contact->first_name, 'Check for first name creation.');
185 $this->assertEquals($updateParams['last_name'], $contact->last_name, 'Check for last name creation.');
186 $this->assertEquals($updateParams['middle_name'], $contact->middle_name, 'Check for middle name creation.');
187 $this->assertEquals($updateParams['contact_type'], $contact->contact_type, 'Check for contact type creation.');
188 $this->assertEquals('0', $contact->do_not_email, 'Check for do_not_email creation.');
189 $this->assertEquals('0', $contact->do_not_phone, 'Check for do_not_phone creation.');
190 $this->assertEquals('0', $contact->do_not_mail, 'Check for do_not_mail creation.');
191 $this->assertEquals('0', $contact->do_not_trade, 'Check for do_not_trade creation.');
192 $this->assertEquals('0', $contact->is_opt_out, 'Check for is_opt_out creation.');
193 $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier,
194 'Check for external_identifier creation.'
195 );
196 $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'],
197 $contact->sort_name, 'Check for sort_name creation.'
198 );
199 $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format,
200 'Check for preferred_mail_format creation.'
201 );
202 $this->assertEquals($updateParams['contact_source'], $contact->source, 'Check for contact_source creation.');
203 $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.');
204 $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.');
205 $this->assertEquals($updateParams['job_title'], $contact->job_title, 'Check for job_title creation.');
206 $this->assertEquals($updateParams['gender_id'], $contact->gender_id, 'Check for gender_id creation.');
207 $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.');
208 $this->assertEquals(CRM_Utils_Date::processDate($updateParams['birth_date']),
209 date('YmdHis', strtotime($contact->birth_date)), 'Check for birth_date creation.'
210 );
211 $this->assertEquals(CRM_Utils_Date::processDate($updateParams['deceased_date']),
212 date('YmdHis', strtotime($contact->deceased_date)), 'Check for deceased_date creation.'
213 );
214 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
215 $contact->preferred_communication_method
216 );
217 $checkPrefComm = array();
218 foreach ($dbPrefComm as $key => $value) {
219 if ($value) {
220 $checkPrefComm[$value] = 1;
221 }
222 }
223 $this->assertAttributesEquals($checkPrefComm, $prefComm);
224
225 //cleanup DB by deleting the contact
226 Contact::delete($contactId);
227 }
228
229 /**
230 * test case for add( )
231 * test with All contact types.
232 */
233 function testAddWithAllContactTypes() {
234 $firstName = 'Bill';
235 $lastName = 'Adams';
236 $params = array(
237 'first_name' => $firstName,
238 'last_name' => $lastName,
239 'contact_type' => 'Individual',
240 );
241
242 $contact = CRM_Contact_BAO_Contact::add($params);
243 $this->assertEquals($firstName, $contact->first_name, 'Check for first name creation.');
244 $this->assertEquals($lastName, $contact->last_name, 'Check for last name creation.');
245
246 $contactId = $contact->id;
247
248 //update and change first name and last name, using create()
249 $firstName = 'Joan';
250 $params = array(
251 'first_name' => $firstName,
252 'contact_type' => 'Individual',
253 'contact_id' => $contactId,
254 );
255
256 $contact = CRM_Contact_BAO_Contact::add($params);
257 $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.');
258 $contactId = $contact->id;
259 Contact::delete($contactId);
260
261 $householdName = 'Adams house';
262 $params = array(
263 'household_name' => $householdName,
264 'contact_type' => 'Household',
265 );
266 $contact = CRM_Contact_BAO_Contact::add($params);
267 $this->assertEquals($householdName, $contact->sort_name, 'Check for created household.');
268 $contactId = $contact->id;
269
270 //update and change name of household, using create
271 $householdName = 'Joans home';
272 $params = array(
273 'household_name' => $householdName,
274 'contact_type' => 'Household',
275 'contact_id' => $contactId,
276 );
277 $contact = CRM_Contact_BAO_Contact::add($params);
278 $this->assertEquals($householdName, $contact->sort_name, 'Check for updated household.');
279 Contact::delete($contactId);
280
281 $organizationName = 'My Organization';
282 $params = array(
283 'organization_name' => $organizationName,
284 'contact_type' => 'Organization',
285 );
286 $contact = CRM_Contact_BAO_Contact::add($params);
287 $this->assertEquals($organizationName, $contact->sort_name, 'Check for created organization.');
288 $contactId = $contact->id;
289
290 //update and change name of organization, using create
291 $organizationName = 'Your Changed Organization';
292 $params = array(
293 'organization_name' => $organizationName,
294 'contact_type' => 'Organization',
295 'contact_id' => $contactId,
296 );
297 $contact = CRM_Contact_BAO_Contact::add($params);
298 $this->assertEquals($organizationName, $contact->sort_name, 'Check for updated organization.');
299 Contact::delete($contactId);
300 }
301
302 /**
303 * test case for create( )
304 * test with missing params.
305 */
306 function testCreateWithEmptyParams() {
307 $params = array(
308 'first_name' => 'Bill',
309 'last_name' => 'Adams',
310 );
311 $contact = CRM_Contact_BAO_Contact::create($params);
312
313 //Now check Contact object
314 $this->assertNull($contact);
315 }
316
317 /**
318 * test case for create( )
319 * test with all params.
320 * ( create and update modes ).
321 */
322 function testCreateWithAll() {
323 //take the common contact params
324 $params = $this->contactParams();
325 $params['note'] = 'test note';
326
327 //create the contact with given params.
328 $contact = CRM_Contact_BAO_Contact::create($params);
329
330 //Now check $contact is object of contact DAO..
331 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
332 $contactId = $contact->id;
333
334 //Now check values of contact object with params.
335 $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.');
336 $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.');
337 $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.');
338
339 //Now check DB for Address
340 $searchParams = array(
341 'contact_id' => $contactId,
342 'location_type_id' => 1,
343 'is_primary' => 1,
344 );
345 $compareParams = array('street_address' => CRM_Utils_Array::value('street_address', $params['address'][1]),
346 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1',
347 $params['address'][1]
348 ),
349 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2',
350 $params['address'][1]
351 ),
352 'city' => CRM_Utils_Array::value('city', $params['address'][1]),
353 'postal_code' => CRM_Utils_Array::value('postal_code', $params['address'][1]),
354 'country_id' => CRM_Utils_Array::value('country_id', $params['address'][1]),
355 'state_province_id' => CRM_Utils_Array::value('state_province_id',
356 $params['address'][1]
357 ),
358 'geo_code_1' => CRM_Utils_Array::value('geo_code_1', $params['address'][1]),
359 'geo_code_2' => CRM_Utils_Array::value('geo_code_2', $params['address'][1]),
360 );
361 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
362
363 //Now check DB for Email
364 $compareParams = array('email' => CRM_Utils_Array::value('email', $params['email'][1]));
365 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
366
367 //Now check DB for openid
368 $compareParams = array('openid' => CRM_Utils_Array::value('openid', $params['openid'][1]));
369 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
370
371 //Now check DB for IM
372 $compareParams = array('name' => CRM_Utils_Array::value('name', $params['im'][1]),
373 'provider_id' => CRM_Utils_Array::value('provider_id', $params['im'][1]),
374 );
375 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
376
377 //Now check DB for Phone
378 $searchParams = array(
379 'contact_id' => $contactId,
380 'location_type_id' => 1,
381 'is_primary' => 1,
382 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][1]),
383 );
384 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][1]));
385 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
386
387 //Now check DB for Mobile
388 $searchParams = array(
389 'contact_id' => $contactId,
390 'location_type_id' => 1,
391 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
392 );
393 $compareParams = array('phone' => CRM_Utils_Array::value('phone', $params['phone'][2]));
394 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
395
396 //Now check DB for Note
397 $searchParams = array(
398 'entity_id' => $contactId,
399 'entity_table' => 'civicrm_contact',
400 );
401 $compareParams = array('note' => $params['note']);
402 $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams);
403
404 //update the contact.
405 $updateParams = array(
406 'first_name' => 'John',
407 'last_name' => 'Doe',
408 'contact_type' => 'Individual',
409 'note' => 'new test note',
410 );
411 $updateParams['address'][1] = array(
412 'location_type_id' => 1,
413 'is_primary' => 1,
414 'street_address' => 'Oberoi Garden',
415 'supplemental_address_1' => 'A-wing:3037',
416 'supplemental_address_2' => 'Andhery',
417 'city' => 'Mumbai',
418 'postal_code' => '12345',
419 'country_id' => 1228,
420 'state_province_id' => 1004,
421 'geo_code_1' => '31.694842',
422 'geo_code_2' => '-106.29998',
423 );
424 $updateParams['email'][1] = array(
425 'location_type_id' => 1,
426 'is_primary' => 1,
427 'email' => 'john.doe@example.org',
428 );
429
430 $updateParams['phone'][1] = array(
431 'location_type_id' => 1,
432 'is_primary' => 1,
433 'phone_type_id' => 1,
434 'phone' => '02115245336',
435 );
436 $updateParams['phone'][2] = array(
437 'location_type_id' => 1,
438 'phone_type_id' => 2,
439 'phone' => '9766323895',
440 );
441
442 $updateParams['contact_id'] = $contactId;
443 //create the contact with given params.
444 $contact = CRM_Contact_BAO_Contact::create($updateParams);
445
446 //Now check $contact is object of contact DAO..
447 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
448 $contactId = $contact->id;
449
450 //Now check values of contact object with updated params.
451 $this->assertEquals($updateParams['first_name'], $contact->first_name, 'Check for first name creation.');
452 $this->assertEquals($updateParams['last_name'], $contact->last_name, 'Check for last name creation.');
453 $this->assertEquals($updateParams['contact_type'], $contact->contact_type, 'Check for contact type creation.');
454
455 //Now check DB for updated Address
456 $searchParams = array(
457 'contact_id' => $contactId,
458 'location_type_id' => 1,
459 'is_primary' => 1,
460 );
461 $compareParams = array(
462 'street_address' => 'Oberoi Garden',
463 'supplemental_address_1' => 'A-wing:3037',
464 'supplemental_address_2' => 'Andhery',
465 'city' => 'Mumbai',
466 'postal_code' => '12345',
467 'country_id' => 1228,
468 'state_province_id' => 1004,
469 'geo_code_1' => '31.694842',
470 'geo_code_2' => '-106.29998',
471 );
472 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
473
474 //Now check DB for updated Email
475 $compareParams = array('email' => 'john.doe@example.org');
476 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
477
478 //Now check DB for updated Phone
479 $searchParams = array(
480 'contact_id' => $contactId,
481 'location_type_id' => 1,
482 'is_primary' => 1,
483 'phone_type_id' => 1,
484 );
485 $compareParams = array('phone' => '02115245336');
486 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
487
488 //Now check DB for updated Mobile
489 $searchParams = array(
490 'contact_id' => $contactId,
491 'location_type_id' => 1,
492 'phone_type_id' => 2,
493 );
494 $compareParams = array('phone' => '9766323895');
495 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
496 //As we are not updating note
497 //Now check DB for New Note
498 $noteId = $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note',
499 'Database check for New created note '
500 );
501
502 //delete all notes related to contact
503 CRM_Core_BAO_Note::cleanContactNotes($contactId);
504
505
506 //cleanup DB by deleting the contact
507 Contact::delete($contactId);
508 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
509 }
510
511 /**
512 * test case for resolveDefaults( )
513 * test all pseudoConstant, stateProvince, country.
514 */
515 function testResolveDefaults() {
516 $params = array(
517 'prefix_id' => 3,
518 'suffix_id' => 2,
519 'gender_id' => 2,
520 'birth_date' => '1983-12-13',
521 );
522
523 $params['address'][1] = array(
524 'location_type_id' => 1,
525 'is_primary' => 1,
526 'country_id' => 1228,
527 'state_province_id' => 1004,
528 );
529 CRM_Contact_BAO_Contact::resolveDefaults($params);
530
531 //check the resolve values.
532 $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id');
533 $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.');
534 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
535 $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.');
536 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
537 $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.');
538 $this->assertEquals(CRM_Core_PseudoConstant::stateProvince($params['address'][1]['state_province_id']),
539 $params['address'][1]['state_province'],
540 'Check for state province.'
541 );
542 $this->assertEquals(CRM_Core_PseudoConstant::country($params['address'][1]['country_id']),
543 $params['address'][1]['country'],
544 'Check for country.'
545 );
546 }
547
548 /**
549 * test case for retrieve( )
550 * test with all values.
551 */
552 function testRetrieve() {
553 //take the common contact params
554 $params = $this->contactParams();
555 $params['note'] = 'test note';
556
557 //create the contact with given params.
558 $contact = CRM_Contact_BAO_Contact::create($params);
559 //Now check $contact is object of contact DAO..
560 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
561 $contactId = $contact->id;
562 //create the organization contact with the given params.
563 $orgParams = array(
564 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4),
565 'contact_type' => 'Organization',
566 );
567 $orgContact = CRM_Contact_BAO_Contact::add($orgParams);
568 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object');
569
570 //create employee of relationship.
571 CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id);
572
573 //retrieve the contact values from database.
574 $values = array();
575 $searchParams = array('contact_id' => $contactId);
576 $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values);
577
578 //Now check $retrieveContact is object of contact DAO..
579 $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object');
580
581 //Now check the ids.
582 $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id');
583
584 //Now check values retrieve from database with params.
585 $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.');
586 $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.');
587 $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.');
588
589 //Now check values of address
590 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
591 CRM_Utils_Array::value('1', $values['address'])
592 );
593
594 //Now check values of email
595 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
596 CRM_Utils_Array::value('1', $values['email'])
597 );
598
599 //Now check values of phone
600 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
601 CRM_Utils_Array::value('1', $values['phone'])
602 );
603
604 //Now check values of mobile
605 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
606 CRM_Utils_Array::value('2', $values['phone'])
607 );
608
609 //Now check values of openid
610 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
611 CRM_Utils_Array::value('1', $values['openid'])
612 );
613
614 //Now check values of im
615 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
616 CRM_Utils_Array::value('1', $values['im'])
617 );
618
619 //Now check values of Note Count.
620 $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count');
621
622 foreach ($values['note'] as $key => $val) {
623 $retrieveNote = CRM_Utils_Array::value('note', $val);
624 //check the note value
625 $this->assertEquals($params['note'], $retrieveNote, 'Check for note');
626 }
627
628 //Now check values of Relationship Count.
629 $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count');
630 foreach ($values['relationship']['data'] as $key => $val) {
631 //Now check values of Relationship organization.
632 $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization');
633 //Now check values of Relationship type.
634 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
635 //delete the organization.
636 Contact::delete(CRM_Utils_Array::value('contact_id_b', $val));
637 }
638
639 //delete all notes related to contact
640 CRM_Core_BAO_Note::cleanContactNotes($contactId);
641
642
643 //cleanup DB by deleting the contact
644 Contact::delete($contactId);
645 $this->quickCleanup(array('civicrm_contact'));
646 }
647
648 /**
649 * test case for deleteContact( )
650 */
651 function testDeleteContact() {
652 $contactParams = $this->contactParams();
653
654 //create custom data
655 require_once 'CiviTest/Custom.php';
656 $customGroup = Custom::createGroup(array(), 'Individual');
657 $fields = array(
658 'label' => 'testFld',
659 'data_type' => 'String',
660 'html_type' => 'Text',
661 'custom_group_id' => $customGroup->id,
662 );
663 $customField = CRM_Core_BAO_CustomField::create($fields);
664 $contactParams['custom'] = array(
665 $customField->id => array(
666 -1 => array(
667 'value' => 'Test custom value',
668 'type' => 'String',
669 'custom_field_id' => $customField->id,
670 'custom_group_id' => $customGroup->id,
671 'table_name' => $customGroup->table_name,
672 'column_name' => $customField->column_name,
673 'file_id' => NULL,
674 ),
675 ),
676 );
677
678 //create contact
679 $contact = CRM_Contact_BAO_Contact::create($contactParams);
680 $contactId = $contact->id;
681
682 //delete contact.
683 CRM_Contact_BAO_Contact::deleteContact($contactId);
684
685 //Now check DB for location elements.
686 //Now check DB for Address
687
688 $this->assertDBNull('CRM_Core_DAO_Address', $contactId,
689 'id', 'street_address', 'Database check, Address deleted successfully.'
690 );
691
692 //Now check DB for Email
693 $this->assertDBNull('CRM_Core_DAO_Email', $contactId,
694 'id', 'email', 'Database check, Email deleted successfully.'
695 );
696 //Now check DB for Phone
697 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
698 'id', 'phone', 'Database check, Phone deleted successfully.'
699 );
700 //Now check DB for Mobile
701 $this->assertDBNull('CRM_Core_DAO_Phone', $contactId,
702 'id', 'phone', 'Database check, Mobile deleted successfully.'
703 );
704 //Now check DB for IM
705 $this->assertDBNull('CRM_Core_DAO_IM', $contactId,
706 'id', 'name', 'Database check, IM deleted successfully.'
707 );
708 //Now check DB for openId
709 $this->assertDBNull('CRM_Core_DAO_OpenID', $contactId,
710 'id', 'openid', 'Database check, openId deleted successfully.'
711 );
712
713
714 // Check that the custom field value is no longer present
715 $params = array(
716 'entityID' => $contactId,
717 'custom_' . $customField->id => 1,
718 );
719 $values = CRM_Core_BAO_CustomValueTable::getValues($params);
720 $this->assertEquals(CRM_Utils_Array::value("custom_" . $customField->id, $values), '',
721 'Verify that the data value is empty for contact ' . $contactId
722 );
723 $this->assertEquals($values['is_error'], 1, 'Verify that is_error = 0 (success).');
724
725 //Now check DB for contact.
726 $this->assertDBNull('CRM_Contact_DAO_Contact', $contactId,
727 'id', 'sort_name', 'Database check, contact deleted successfully.'
728 );
729 $this->quickCleanup(array('civicrm_contact', 'civicrm_note'));
730 Custom::deleteGroup($customGroup);
731 }
732
733 /**
734 * test case for createProfileContac( )
735 * test with all params.
736 * ( create and update modes )
737 */
738 function testCreateProfileContact() {
739 $fields = CRM_Contact_BAO_Contact::exportableFields('Individual');
740
741 //current employer field for individual
742 $fields['organization_name'] = array(
743 'name' => 'organization_name',
744 'where' => 'civicrm_organization.organization_name',
745 'title' => 'Current Employer',
746 );
747 //get the common params
748 $contactParams = $this->contactParams();
749 $unsetParams = array('location', 'privacy');
750 foreach ($unsetParams as $param) {
751 unset($contactParams[$param]);
752 }
753
754 $profileParams = array(
755 'organization_name' => 'Yahoo',
756 'gender_id' => '2',
757 'prefix_id' => '3',
758 'suffix_id' => '2',
759 'city-Primary' => 'Newark',
760 'contact_type' => 'Individual',
761 'country-Primary' => '1228',
762 'do_not_email' => '1',
763 'do_not_mail' => '1',
764 'do_not_phone' => '1',
765 'do_not_trade' => '1',
766 'do_not_sms' => '1',
767 'email-Primary' => 'john.smith@example.org',
768 'geo_code_1-Primary' => '18.219023',
769 'geo_code_2-Primary' => '-105.00973',
770 'im-Primary-provider_id' => '1',
771 'im-Primary' => 'john.smith',
772 'on_hold' => '1',
773 'openid' => 'john.smith@example.org',
774 'phone-Primary-1' => '303443689',
775 'phone-Primary-2' => '9833910234',
776 'postal_code-Primary' => '01903',
777 'postal_code_suffix-Primary' => '12345',
778 'state_province-Primary' => '1029',
779 'street_address-Primary' => 'Saint Helier St',
780 'supplemental_address_1-Primary' => 'Hallmark Ct',
781 'supplemental_address_2-Primary' => 'Jersey Village',
782 'user_unique_id' => '123456789',
783 'is_bulkmail' => '1',
784 'world_region' => 'India',
785 'tag' => array(
786 '3' => '1',
787 '4' => '1',
788 '1' => '1',
789 ),
790 );
791 $createParams = array_merge($contactParams, $profileParams);
792
793 //create the contact using create profile contact.
794 $contactId = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, NULL, NULL, NULL, NULL, TRUE);
795
796 //get the parameters to compare.
797 $params = $this->contactParams();
798
799 //check the values in DB.
800 foreach ($params as $key => $val) {
801 if (!is_array($params[$key])) {
802 if ($key == 'contact_source') {
803 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
804 'id', $params[$key], "Check for {$key} creation."
805 );
806 }
807 else {
808 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
809 'id', $params[$key], "Check for {$key} creation."
810 );
811 }
812 }
813 }
814
815 //check privacy options.
816 foreach ($params['privacy'] as $key => $value) {
817 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
818 'id', $params['privacy'][$key], 'Check for do_not_email creation.'
819 );
820 }
821
822 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
823 'id', $profileParams['contact_type'], 'Check for contact type creation.'
824 );
825 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
826 'id', $profileParams['user_unique_id'], 'Check for user_unique_id creation.'
827 );
828
829 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date',
830 'id', $params['birth_date'], 'Check for birth_date creation.'
831 );
832
833 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date',
834 'id', $params['deceased_date'], 'Check for deceased_date creation.'
835 );
836
837 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
838 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
839 );
840 $checkPrefComm = array();
841 foreach ($dbPrefComm as $key => $value) {
842 if ($value) {
843 $checkPrefComm[$value] = 1;
844 }
845 }
846 $this->assertAttributesEquals($checkPrefComm, $params['preferred_communication_method']);
847
848 //Now check DB for Address
849 $searchParams = array(
850 'contact_id' => $contactId,
851 'location_type_id' => 1,
852 'is_primary' => 1,
853 );
854 $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $profileParams),
855 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $profileParams),
856 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $profileParams),
857 'city' => CRM_Utils_Array::value('city-Primary', $profileParams),
858 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $profileParams),
859 'country_id' => CRM_Utils_Array::value('country-Primary', $profileParams),
860 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $profileParams),
861 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $profileParams),
862 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $profileParams),
863 );
864 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
865
866 //Now check DB for Email
867 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $profileParams));
868 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
869
870 //Now check DB for IM
871 $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $profileParams),
872 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $profileParams),
873 );
874 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
875
876 //Now check DB for Phone
877 $searchParams = array(
878 'contact_id' => $contactId,
879 'location_type_id' => 1,
880 'is_primary' => 1,
881 );
882 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams));
883 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
884
885 //Now check DB for Mobile
886 $searchParams = array(
887 'contact_id' => $contactId,
888 'location_type_id' => 1,
889 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
890 );
891 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams));
892
893 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
894
895 //get the value of relationship
896 $values = array();
897 $searchParams = array('contact_id' => $contactId);
898 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
899 //Now check values of Relationship Count.
900 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
901 foreach ($values['relationship']['data'] as $key => $val) {
902 //Now check values of Relationship organization.
903 $this->assertEquals($profileParams['organization_name'], $val['name'], 'Check for organization');
904 //Now check values of Relationship type.
905 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
906 //delete the organization.
907 Contact::delete(CRM_Utils_Array::value('cid', $val));
908 }
909
910 //Now check values of tag ids.
911 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
912 foreach ($tags as $key => $val) {
913 $tagIds[$key] = 1;
914 }
915
916 $this->assertAttributesEquals($profileParams['tag'], $tagIds);
917
918 //update Contact mode
919 $updateCParams = array(
920 'first_name' => 'john',
921 'last_name' => 'doe',
922 'contact_type' => 'Individual',
923 'middle_name' => 'abc',
924 'prefix_id' => 2,
925 'suffix_id' => 3,
926 'nick_name' => 'Nick Name Updated',
927 'job_title' => 'software Developer',
928 'gender_id' => 1,
929 'is_deceased' => 1,
930 'website' => array(
931 1 => array(
932 'website_type_id' => 1,
933 'url' => 'http://civicrmUpdate.org',
934 ),
935 ),
936 'contact_source' => 'test contact',
937 'external_identifier' => 111222333,
938 'preferred_mail_format' => 'Both',
939 'is_opt_out' => 0,
940 'legal_identifier' => '123123123123',
941 'image_URL' => 'http://imageupdate.com',
942 'deceased_date' => '1981-10-10',
943 'birth_date' => '1951-11-11',
944 'privacy' => array(
945 'do_not_phone' => 1,
946 'do_not_email' => 1,
947 ),
948 'preferred_communication_method' => array(
949 '1' => 0,
950 '2' => 1,
951 '3' => 0,
952 '4' => 1,
953 '5' => 0,
954 ),
955 );
956
957 $updatePfParams = array(
958 'organization_name' => 'Google',
959 'city-Primary' => 'Mumbai',
960 'contact_type' => 'Individual',
961 'country-Primary' => '1228',
962 'do_not_email' => '1',
963 'do_not_mail' => '1',
964 'do_not_phone' => '1',
965 'do_not_trade' => '1',
966 'do_not_sms' => '1',
967 'email-Primary' => 'john.doe@example.org',
968 'geo_code_1-Primary' => '31.694842',
969 'geo_code_2-Primary' => '-106.29998',
970 'im-Primary-provider_id' => '1',
971 'im-Primary' => 'john.doe',
972 'on_hold' => '1',
973 'openid' => 'john.doe@example.org',
974 'phone-Primary-1' => '02115245336',
975 'phone-Primary-2' => '9766323895',
976 'postal_code-Primary' => '12345',
977 'postal_code_suffix-Primary' => '123',
978 'state_province-Primary' => '1004',
979 'street_address-Primary' => 'Oberoi Garden',
980 'supplemental_address_1-Primary' => 'A-wing:3037',
981 'supplemental_address_2-Primary' => 'Andhery',
982 'user_unique_id' => '1122334455',
983 'is_bulkmail' => '1',
984 'world_region' => 'India',
985 'tag' => array(
986 '2' => '1',
987 '5' => '1',
988 ),
989 );
990
991 $createParams = array_merge($updateCParams, $updatePfParams);
992
993 //create the contact using create profile contact.
994 $contactID = CRM_Contact_BAO_Contact::createProfileContact($createParams, $fields, $contactId,
995 NULL, NULL, NULL, TRUE
996 );
997
998 //check the contact ids
999 $this->assertEquals($contactId, $contactID, 'check for Contact ids');
1000
1001 //check the values in DB.
1002 foreach ($updateCParams as $key => $val) {
1003 if (!is_array($updateCParams[$key])) {
1004 if ($key == 'contact_source') {
1005 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'source',
1006 'id', $updateCParams[$key], "Check for {$key} creation."
1007 );
1008 }
1009 else {
1010 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1011 'id', $updateCParams[$key], "Check for {$key} creation."
1012 );
1013 }
1014 }
1015 }
1016
1017 //check privacy options.
1018 foreach ($updateCParams['privacy'] as $key => $value) {
1019 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, $key,
1020 'id', $updateCParams['privacy'][$key], 'Check for do_not_email creation.'
1021 );
1022 }
1023
1024 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'contact_type',
1025 'id', $updatePfParams['contact_type'], 'Check for contact type creation.'
1026 );
1027 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'user_unique_id',
1028 'id', $updatePfParams['user_unique_id'], 'Check for user_unique_id creation.'
1029 );
1030
1031 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'birth_date', 'id',
1032 $updateCParams['birth_date'], 'Check for birth_date creation.'
1033 );
1034
1035 $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $contactId, 'deceased_date', 'id',
1036 $updateCParams['deceased_date'], 'Check for deceased_date creation.'
1037 );
1038
1039 $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
1040 CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', true)
1041 );
1042 $checkPrefComm = array();
1043 foreach ($dbPrefComm as $key => $value) {
1044 if ($value) {
1045 $checkPrefComm[$value] = 1;
1046 }
1047 }
1048 $this->assertAttributesEquals($checkPrefComm, $updateCParams['preferred_communication_method']);
1049
1050 //Now check DB for Address
1051 $searchParams = array(
1052 'contact_id' => $contactId,
1053 'location_type_id' => 1,
1054 'is_primary' => 1,
1055 );
1056 $compareParams = array('street_address' => CRM_Utils_Array::value('street_address-Primary', $updatePfParams),
1057 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1-Primary', $updatePfParams),
1058 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2-Primary', $updatePfParams),
1059 'city' => CRM_Utils_Array::value('city-Primary', $updatePfParams),
1060 'postal_code' => CRM_Utils_Array::value('postal_code-Primary', $updatePfParams),
1061 'country_id' => CRM_Utils_Array::value('country-Primary', $updatePfParams),
1062 'state_province_id' => CRM_Utils_Array::value('state_province-Primary', $updatePfParams),
1063 'geo_code_1' => CRM_Utils_Array::value('geo_code_1-Primary', $updatePfParams),
1064 'geo_code_2' => CRM_Utils_Array::value('geo_code_2-Primary', $updatePfParams),
1065 );
1066 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
1067
1068 //Now check DB for Email
1069 $compareParams = array('email' => CRM_Utils_Array::value('email-Primary', $updatePfParams));
1070 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
1071
1072 //Now check DB for IM
1073 $compareParams = array('name' => CRM_Utils_Array::value('im-Primary', $updatePfParams),
1074 'provider_id' => CRM_Utils_Array::value('im-Primary-provider_id', $updatePfParams),
1075 );
1076 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
1077
1078 //Now check DB for Phone
1079 $searchParams = array(
1080 'contact_id' => $contactId,
1081 'location_type_id' => 1,
1082 'is_primary' => 1,
1083 );
1084 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams));
1085 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1086
1087 //Now check DB for Mobile
1088 $searchParams = array(
1089 'contact_id' => $contactId,
1090 'location_type_id' => 1,
1091 'phone_type_id' => CRM_Utils_Array::value('phone_type_id', $params['phone'][2]),
1092 );
1093 $compareParams = array('phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams));
1094 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
1095
1096 //get the value of relationship
1097 $values = array();
1098 $searchParams = array('contact_id' => $contactId);
1099 $relationship = CRM_Contact_BAO_Relationship::getValues($searchParams, $values);
1100 //Now check values of Relationship Count.
1101 $this->assertEquals(0, $values['relationship']['totalCount'], 'Check for total relationship count');
1102 foreach ($values['relationship']['data'] as $key => $val) {
1103 //Now check values of Relationship organization.
1104 $this->assertEquals($updatePfParams['organization_name'], $val['name'], 'Check for organization');
1105 //Now check values of Relationship type.
1106 $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type');
1107 //delete the organization.
1108 Contact::delete(CRM_Utils_Array::value('cid', $val));
1109 }
1110
1111 //Now check values of tag ids.
1112 $tags = CRM_Core_BAO_EntityTag::getTag($contactId);
1113 foreach ($tags as $key => $val) {
1114 $tagIds[$key] = 1;
1115 }
1116 $this->assertAttributesEquals($updatePfParams['tag'], $tagIds);
1117
1118 //cleanup DB by deleting the contact
1119 Contact::delete($contactId);
1120 }
1121
1122 /**
1123 * test case for getContactDetails( )
1124 */
1125 function testGetContactDetails() {
1126 //get the contact params
1127 $params = $this->contactParams();
1128
1129 //create contact
1130 $contact = CRM_Contact_BAO_Contact::create($params);
1131 $contactId = $contact->id;
1132
1133 //get the contact details
1134 $contactDetails = CRM_Contact_BAO_Contact::getContactDetails($contactId);
1135 $compareParams = array(
1136 $params['first_name'] . ' ' . $params['last_name'],
1137 CRM_Utils_Array::value('email', $params['email'][1]),
1138 (bool ) $params['privacy']['do_not_email'],
1139 );
1140 //Now check the contact details
1141 $this->assertAttributesEquals($compareParams, $contactDetails);
1142
1143 //cleanup DB by deleting the contact
1144 Contact::delete($contactId);
1145 $this->quickCleanup(array('civicrm_contact'));
1146 }
1147
1148 /**
1149 * test case for
1150 * importableFields( ) and exportableFields( )
1151 */
1152 function testFields() {
1153 $allImpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1154 $allExpFileds = CRM_Contact_BAO_Contact::importableFields('All');
1155 //Now check all fields
1156 $this->assertAttributesEquals($allImpFileds, $allExpFileds);
1157
1158 $individualImpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1159 $individualExpFileds = CRM_Contact_BAO_Contact::importableFields('Individual');
1160 //Now check Individual fields
1161 $this->assertAttributesEquals($individualImpFileds, $individualExpFileds);
1162
1163 $householdImpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1164 $householdExpFileds = CRM_Contact_BAO_Contact::importableFields('Household');
1165 //Now check Household fields
1166 $this->assertAttributesEquals($householdImpFileds, $householdExpFileds);
1167
1168 $organizationImpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1169 $organizationExpFileds = CRM_Contact_BAO_Contact::importableFields('Organization');
1170 //Now check Organization fields
1171 $this->assertAttributesEquals($organizationImpFileds, $organizationExpFileds);
1172 }
1173
1174 /**
1175 * test case for getPrimaryEmail( )
1176 *
1177 */
1178 function testGetPrimaryEmail() {
1179 //get the contact params
1180 $params = $this->contactParams();
1181 $params['email'][2] = $params['email'][1];
1182 $params['email'][2]['email'] = 'primarymail@example.org';
1183 unset($params['email'][1]['is_primary']);
1184
1185 //create contact
1186 $contact = CRM_Contact_BAO_Contact::create($params);
1187 $contactId = $contact->id;
1188 //get the primary email.
1189 $email = CRM_Contact_BAO_Contact::getPrimaryEmail($contactId);
1190 //Now check the primary email
1191 $this->assertEquals($email, CRM_Utils_Array::value('email', $params['email'][2]), 'Check Primary Email');
1192
1193 //cleanup DB by deleting the contact
1194 Contact::delete($contactId);
1195 $this->quickCleanup(array('civicrm_contact'));
1196 }
1197
1198 /**
1199 * test case for getPrimaryOpenId( )
1200 *
1201 */
1202 function testGetPrimaryOpenId() {
1203 //get the contact params
1204 $params = $this->contactParams();
1205 $params['openid'][2] = $params['openid'][1];
1206 $params['openid'][2]['location_type_id'] = 2;
1207 $params['openid'][2]['openid'] = 'http://primaryopenid.org/';
1208 unset($params['openid'][1]['is_primary']);
1209
1210 //create contact
1211 $contact = CRM_Contact_BAO_Contact::create($params);
1212 $contactId = $contact->id;
1213 //get the primary openid
1214 $openID = CRM_Contact_BAO_Contact::getPrimaryOpenId($contactId);
1215
1216 //Now check the primary openid
1217 $this->assertEquals($openID, strtolower($params['openid'][2]['openid']), 'Check Primary OpenID');
1218
1219 //cleanup DB by deleting the contact
1220 Contact::delete($contactId);
1221 }
1222
1223 /**
1224 * test case for matchContactOnEmail( )
1225 *
1226 */
1227 function testMatchContactOnEmail() {
1228 //get the contact params
1229 $params = $this->contactParams();
1230 //create contact
1231 $contact = CRM_Contact_BAO_Contact::create($params);
1232 $contactId = $contact->id;
1233
1234 //get the matching contact.
1235 $match = CRM_Contact_BAO_Contact::matchContactOnEmail(CRM_Utils_Array::value('email', $params['email'][1]),
1236 'Individual'
1237 );
1238 $this->assertEquals($contactId, $match->contact_id, 'Check For Matching Contact');
1239
1240 //cleanup DB by deleting the contact
1241 Contact::delete($contactId);
1242 $this->quickCleanup(array('civicrm_contact'));
1243 }
1244
1245 /**
1246 * test case for getContactType( )
1247 *
1248 */
1249 function testGetContactType() {
1250 //get the contact params
1251 $params = $this->contactParams();
1252 //create contact
1253 $contact = CRM_Contact_BAO_Contact::create($params);
1254 $contactId = $contact->id;
1255
1256 //get contact type.
1257 $contactType = CRM_Contact_BAO_Contact::getContactType($contactId);
1258 $this->assertEquals($contactType, $params['contact_type'], 'Check For Contact Type');
1259
1260 //cleanup DB by deleting the contact
1261 Contact::delete($contactId);
1262 $this->quickCleanup(array('civicrm_contact'));
1263 }
1264
1265 /**
1266 * test case for displayName( )
1267 *
1268 */
1269 function testDisplayName() {
1270 //get the contact params
1271 $params = $this->contactParams();
1272
1273 //create contact
1274 $contact = CRM_Contact_BAO_Contact::create($params);
1275 $contactId = $contact->id;
1276
1277 //get display name.
1278 $dbDisplayName = CRM_Contact_BAO_Contact::displayName($contactId);
1279
1280 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1281 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1282
1283 //build display name
1284 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1285
1286 $this->assertEquals($dbDisplayName, $paramsDisplayName, 'Check For Display Name');
1287
1288 //cleanup DB by deleting the contact
1289 Contact::delete($contactId);
1290 $this->quickCleanup(array('civicrm_contact'));
1291 }
1292
1293 /**
1294 * test case for getDisplayAndImage( )
1295 *
1296 */
1297 function testGetDisplayAndImage() {
1298 //get the contact params
1299 $params = $this->contactParams();
1300
1301 //create contact
1302 $contact = CRM_Contact_BAO_Contact::create($params);
1303 $contactId = $contact->id;
1304
1305 //get DisplayAndImage.
1306 list($displayName, $image) = CRM_Contact_BAO_Contact::getDisplayAndImage($contactId);
1307
1308 $checkImage = CRM_Contact_BAO_Contact_Utils::getImage($params['contact_type'], FALSE, $contactId);
1309
1310 $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id');
1311 $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id');
1312
1313 //build display name
1314 $paramsDisplayName = $prefix[$params['prefix_id']] . ' ' . $params['first_name'] . ' ' . $params['last_name'] . ' ' . $suffix[$params['suffix_id']];
1315
1316 $this->assertEquals($displayName, $paramsDisplayName, 'Check For Display Name');
1317 $this->assertEquals($image, $checkImage, 'Check For Image');
1318
1319 //cleanup DB by deleting the contact
1320 Contact::delete($contactId);
1321 }
1322
1323 // /**
1324 // * test logged in user has permissions for
1325 // * various operation types
1326 // */
1327 // function testPermissionedContact()
1328 // {
1329 // $contactId = Contact::createIndividual();
1330 // require_once 'CRM/Contact/BAO/Contact/Permission.php';
1331 // $userViewPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId);
1332 // $this->assertEquals( $userViewPermission, true, 'Check user for view contact permission.' );
1333 //
1334 // $userEditPermission = CRM_Contact_BAO_Contact_Permission::allow($contactId, CRM_Core_Permission::EDIT);
1335 // $this->assertEquals( $userEditPermission, true, 'Check user for edit contact permission.' );
1336 //
1337 // Contact::delete($contactId);
1338 // }
1339
1340 /**
1341 * function to build common params
1342 *
1343 */
1344 private function contactParams() {
1345
1346 $params = array(
1347 'first_name' => 'john',
1348 'last_name' => 'smith',
1349 'contact_type' => 'Individual',
1350 'middle_name' => 'xyz',
1351 'prefix_id' => 3,
1352 'suffix_id' => 2,
1353 'nick_name' => 'Nick Name',
1354 'job_title' => 'software engg',
1355 'gender_id' => 2,
1356 'is_deceased' => 1,
1357 'website' => array(
1358 1 => array(
1359 'website_type_id' => 1,
1360 'url' => 'http://civicrm.org',
1361 ),
1362 ),
1363 'contact_source' => 'test contact',
1364 'external_identifier' => 123456789,
1365 'preferred_mail_format' => 'Both',
1366 'is_opt_out' => 1,
1367 'legal_identifier' => '123456789',
1368 'image_URL' => 'http://image.com',
1369 'deceased_date' => '1991-07-07',
1370 'birth_date' => '1961-06-06',
1371 'privacy' => array(
1372 'do_not_phone' => 1,
1373 'do_not_email' => 1,
1374 'do_not_mail' => 1,
1375 'do_not_trade' => 1,
1376 ),
1377 'preferred_communication_method' => array(
1378 '1' => 1,
1379 '2' => 0,
1380 '3' => 1,
1381 '4' => 0,
1382 '5' => 1,
1383 ),
1384 );
1385
1386 $params['address'] = array();
1387 $params['address'][1] = array(
1388 'location_type_id' => 1,
1389 'is_primary' => 1,
1390 'street_address' => 'Saint Helier St',
1391 'supplemental_address_1' => 'Hallmark Ct',
1392 'supplemental_address_2' => 'Jersey Village',
1393 'city' => 'Newark',
1394 'postal_code' => '01903',
1395 'country_id' => 1228,
1396 'state_province_id' => 1029,
1397 'geo_code_1' => '18.219023',
1398 'geo_code_2' => '-105.00973',
1399 );
1400
1401 $params['email'] = array();
1402 $params['email'][1] = array(
1403 'location_type_id' => 1,
1404 'is_primary' => 1,
1405 'email' => 'john.smith@example.org',
1406 );
1407
1408
1409 $params['phone'] = array();
1410 $params['phone'][1] = array(
1411 'location_type_id' => 1,
1412 'is_primary' => 1,
1413 'phone_type_id' => 1,
1414 'phone' => '303443689',
1415 );
1416 $params['phone'][2] = array(
1417 'location_type_id' => 1,
1418 'phone_type_id' => 2,
1419 'phone' => '9833910234',
1420 );
1421
1422 $params['openid'] = array();
1423 $params['openid'][1] = array(
1424 'location_type_id' => 1,
1425 'is_primary' => 1,
1426 'openid' => 'http://civicrm.org/',
1427 );
1428
1429 $params['im'] = array();
1430 $params['im'][1] = array(
1431 'location_type_id' => 1,
1432 'is_primary' => 1,
1433 'name' => 'john.smith',
1434 'provider_id' => 1,
1435 );
1436
1437 return $params;
1438 }
1439
1440 /**
1441 * Ensure that created_date and modified_date are set
1442 */
1443 function testTimestamps_contact() {
1444 $test = $this;
1445 $this->_testTimestamps(array(
1446 'UPDATE' => function ($contactId) use ($test) {
1447 $params = array(
1448 'first_name' => 'Testing',
1449 'contact_type' => 'Individual',
1450 'contact_id' => $contactId,
1451 );
1452 $contact = CRM_Contact_BAO_Contact::add($params);
1453 $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object');
1454 },
1455 ));
1456 }
1457
1458 /**
1459 * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record
1460 */
1461 function testTimestamps_email() {
1462 $test = $this;
1463 $this->_testTimestamps(array(
1464 'INSERT' => function ($contactId) use ($test) {
1465 $params = array(
1466 'email' => 'ex-1@example.com',
1467 'is_primary' => 1,
1468 'location_type_id' => 1,
1469 'contact_id' => $contactId,
1470 );
1471 CRM_Core_BAO_Email::add($params);
1472 $test->assertDBQuery('ex-1@example.com',
1473 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1474 array(1 => array($contactId, 'Integer'))
1475 );
1476 },
1477
1478 'UPDATE' => function ($contactId) use ($test) {
1479 CRM_Core_DAO::executeQuery(
1480 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1',
1481 array(1 => array($contactId, 'Integer'))
1482 );
1483 },
1484
1485 'DELETE' => function ($contactId) use ($test) {
1486 CRM_Core_DAO::executeQuery(
1487 'DELETE FROM civicrm_email WHERE contact_id = %1',
1488 array(1 => array($contactId, 'Integer'))
1489 );
1490 },
1491 ));
1492 }
1493
1494 /**
1495 * Ensure that civicrm_contact.modified_date is updated when manipulating an email
1496 */
1497 function testTimestamps_phone() {
1498 $test = $this;
1499 $this->_testTimestamps(array(
1500 'INSERT' => function ($contactId) use ($test) {
1501 $params = array(
1502 'phone' => '202-555-1000',
1503 'is_primary' => 1,
1504 'location_type_id' => 1,
1505 'contact_id' => $contactId,
1506 );
1507 CRM_Core_BAO_Phone::add($params);
1508 $test->assertDBQuery('202-555-1000',
1509 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1',
1510 array(1 => array($contactId, 'Integer'))
1511 );
1512 },
1513
1514 'UPDATE' => function ($contactId) use ($test) {
1515 CRM_Core_DAO::executeQuery(
1516 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1',
1517 array(1 => array($contactId, 'Integer'))
1518 );
1519 },
1520
1521 'DELETE' => function ($contactId) use ($test) {
1522 CRM_Core_DAO::executeQuery(
1523 'DELETE FROM civicrm_phone WHERE contact_id = %1',
1524 array(1 => array($contactId, 'Integer'))
1525 );
1526 },
1527 ));
1528 }
1529
1530 /**
1531 * Ensure that civicrm_contact.modified_date is updated when contact-related
1532 * custom data
1533 */
1534 function testTimestamps_custom() {
1535 $customGroup = Custom::createGroup(array(), 'Individual');
1536 $this->assertNotNull($customGroup);
1537 $fields = array(
1538 'custom_group_id' => $customGroup->id,
1539 'data_type' => 'String',
1540 'html_type' => 'Text',
1541 );
1542 $customField = $this->customFieldCreate($fields);
1543 $customField = $customField['values'][$customField['id']];
1544 $test = $this;
1545 $this->_testTimestamps(array(
1546 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) {
1547 $result = civicrm_api3('contact', 'create', array(
1548 'contact_id' => $contactId,
1549 'custom_' . $customField['id'] => 'test-1',
1550 ));
1551 },
1552 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) {
1553 CRM_Core_DAO::executeQuery(
1554 "UPDATE {$customGroup->table_name} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1",
1555 array(1 => array($contactId, 'Integer'))
1556 );
1557 },
1558 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) {
1559 CRM_Core_DAO::executeQuery(
1560 "DELETE FROM {$customGroup->table_name} WHERE entity_id = %1",
1561 array(1 => array($contactId, 'Integer'))
1562 );
1563 },
1564 ));
1565
1566 Custom::deleteGroup($customGroup);
1567 }
1568
1569 /**
1570 * Helper for testing timestamp manipulation
1571 *
1572 * Create a contact and perform a series of steps with it; after each
1573 * step, ensure that the contact's modified_date has increased.
1574 *
1575 * @param $callbacks array ($name => $callable)
1576 */
1577 function _testTimestamps($callbacks) {
1578 CRM_Core_DAO::triggerRebuild();
1579 $contactId = Contact::createIndividual();
1580
1581 $origTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1582 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['created_date']);
1583 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $origTimestamps['modified_date']);
1584 $this->assertTrue($origTimestamps['created_date'] <= $origTimestamps['modified_date']);
1585
1586 $prevTimestamps = $origTimestamps;
1587 foreach ($callbacks as $callbackName => $callback) {
1588 sleep(1); // advance clock by 1 second to ensure timestamps change
1589
1590 $callback($contactId);
1591 $newTimestamps = CRM_Contact_BAO_Contact::getTimestamps($contactId);
1592 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['created_date'], "Malformed created_date (after $callbackName)");
1593 $this->assertRegexp('/^\d\d\d\d-\d\d-\d\d /', $newTimestamps['modified_date'], "Malformed modified_date (after $callbackName)");
1594 $this->assertEquals($origTimestamps['created_date'], $newTimestamps['created_date'], "Changed created_date (after $callbackName)");
1595 $this->assertTrue($prevTimestamps['modified_date'] < $newTimestamps['modified_date'], "Misordered modified_date (after $callbackName)");
1596
1597 $prevTimestamps = $newTimestamps;
1598 }
1599
1600 Contact::delete($contactId);
1601 }
1602 }
1603
1604
1605