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