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