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