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