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