Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
0eea664b | 2 | |
aba1cd8b EM |
3 | /** |
4 | * Class CRM_Contact_BAO_ContactTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Contact_BAO_ContactTest extends CiviUnitTestCase { |
22f80e87 EM |
8 | |
9 | /** | |
10 | * Set up function. | |
11 | */ | |
00be9182 | 12 | public function setUp() { |
6a488035 TO |
13 | parent::setUp(); |
14 | } | |
15 | ||
16 | /** | |
22f80e87 EM |
17 | * Test case for add( ). |
18 | * | |
6a488035 TO |
19 | * test with empty params. |
20 | */ | |
00be9182 | 21 | public function testAddWithEmptyParams() { |
9099cab3 | 22 | $params = []; |
6a488035 TO |
23 | $contact = CRM_Contact_BAO_Contact::add($params); |
24 | ||
22f80e87 | 25 | // Now check Contact object. |
6a488035 TO |
26 | $this->assertNull($contact); |
27 | } | |
28 | ||
29 | /** | |
22f80e87 EM |
30 | * Test case for add( ). |
31 | * | |
32 | * Test with names (create and update modes) | |
6a488035 | 33 | */ |
00be9182 | 34 | public function testAddWithNames() { |
6a488035 | 35 | $firstName = 'Shane'; |
92915c55 | 36 | $lastName = 'Whatson'; |
9099cab3 | 37 | $params = [ |
6a488035 TO |
38 | 'first_name' => $firstName, |
39 | 'last_name' => $lastName, | |
40 | 'contact_type' => 'Individual', | |
9099cab3 | 41 | ]; |
6a488035 TO |
42 | |
43 | $contact = CRM_Contact_BAO_Contact::add($params); | |
44 | ||
22f80e87 | 45 | // Now check $contact is object of contact DAO. |
6a488035 TO |
46 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); |
47 | $this->assertEquals($firstName, $contact->first_name, 'Check for first name creation.'); | |
48 | $this->assertEquals($lastName, $contact->last_name, 'Check for last name creation.'); | |
49 | ||
50 | $contactId = $contact->id; | |
51 | ||
22f80e87 | 52 | // Update and change first name and last name, using add( ). |
6a488035 | 53 | $firstName = 'Jane'; |
9099cab3 | 54 | $params = [ |
6a488035 TO |
55 | 'first_name' => $firstName, |
56 | 'contact_type' => 'Individual', | |
57 | 'contact_id' => $contactId, | |
9099cab3 | 58 | ]; |
6a488035 TO |
59 | |
60 | $contact = CRM_Contact_BAO_Contact::add($params); | |
61 | ||
22f80e87 | 62 | // Now check $contact is object of contact DAO. |
6a488035 TO |
63 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); |
64 | $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.'); | |
65 | ||
66 | $contactId = $contact->id; | |
93ac19cd | 67 | $this->contactDelete($contactId); |
6a488035 TO |
68 | } |
69 | ||
70 | /** | |
22f80e87 EM |
71 | * Test case for add. |
72 | * | |
73 | * Test with all contact params | |
74 | * (create and update modes) | |
6a488035 | 75 | */ |
00be9182 | 76 | public function testAddWithAll() { |
22f80e87 | 77 | // Take the common contact params. |
6a488035 TO |
78 | $params = $this->contactParams(); |
79 | ||
80 | unset($params['location']); | |
81 | $prefComm = $params['preferred_communication_method']; | |
6a488035 TO |
82 | $contact = CRM_Contact_BAO_Contact::add($params); |
83 | $contactId = $contact->id; | |
84 | ||
6a488035 TO |
85 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); |
86 | ||
6a488035 TO |
87 | $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.'); |
88 | $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.'); | |
89 | $this->assertEquals($params['middle_name'], $contact->middle_name, 'Check for middle name creation.'); | |
90 | $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.'); | |
91 | $this->assertEquals('1', $contact->do_not_email, 'Check for do_not_email creation.'); | |
92 | $this->assertEquals('1', $contact->do_not_phone, 'Check for do_not_phone creation.'); | |
93 | $this->assertEquals('1', $contact->do_not_mail, 'Check for do_not_mail creation.'); | |
94 | $this->assertEquals('1', $contact->do_not_trade, 'Check for do_not_trade creation.'); | |
95 | $this->assertEquals('1', $contact->is_opt_out, 'Check for is_opt_out creation.'); | |
96 | $this->assertEquals($params['external_identifier'], $contact->external_identifier, 'Check for external_identifier creation.'); | |
97 | $this->assertEquals($params['last_name'] . ', ' . $params['first_name'], $contact->sort_name, 'Check for sort_name creation.'); | |
98 | $this->assertEquals($params['preferred_mail_format'], $contact->preferred_mail_format, | |
99 | 'Check for preferred_mail_format creation.' | |
100 | ); | |
101 | $this->assertEquals($params['contact_source'], $contact->source, 'Check for contact_source creation.'); | |
102 | $this->assertEquals($params['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.'); | |
103 | $this->assertEquals($params['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.'); | |
104 | $this->assertEquals($params['job_title'], $contact->job_title, 'Check for job_title creation.'); | |
105 | $this->assertEquals($params['gender_id'], $contact->gender_id, 'Check for gender_id creation.'); | |
106 | $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.'); | |
107 | $this->assertEquals(CRM_Utils_Date::processDate($params['birth_date']), | |
108 | $contact->birth_date, 'Check for birth_date creation.' | |
109 | ); | |
110 | $this->assertEquals(CRM_Utils_Date::processDate($params['deceased_date']), | |
111 | $contact->deceased_date, 'Check for deceased_date creation.' | |
112 | ); | |
113 | $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
114 | $contact->preferred_communication_method | |
115 | ); | |
9099cab3 | 116 | $checkPrefComm = []; |
6a488035 TO |
117 | foreach ($dbPrefComm as $key => $value) { |
118 | if ($value) { | |
119 | $checkPrefComm[$value] = 1; | |
120 | } | |
121 | } | |
122 | $this->assertAttributesEquals($checkPrefComm, $prefComm); | |
123 | ||
9099cab3 | 124 | $updateParams = [ |
6a488035 TO |
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, | |
9099cab3 CW |
135 | 'website' => [ |
136 | 1 => [ | |
6a488035 TO |
137 | 'website_type_id' => 1, |
138 | 'url' => 'http://docs.civicrm.org', | |
9099cab3 CW |
139 | ], |
140 | ], | |
6a488035 TO |
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', | |
9099cab3 | 147 | 'privacy' => [ |
6a488035 TO |
148 | 'do_not_phone' => 0, |
149 | 'do_not_email' => 0, | |
150 | 'do_not_mail' => 0, | |
151 | 'do_not_trade' => 0, | |
9099cab3 CW |
152 | ], |
153 | 'preferred_communication_method' => [ | |
6a488035 TO |
154 | '1' => 0, |
155 | '2' => 1, | |
156 | '3' => 0, | |
157 | '4' => 1, | |
158 | '5' => 0, | |
9099cab3 CW |
159 | ], |
160 | ]; | |
6a488035 TO |
161 | |
162 | $prefComm = $updateParams['preferred_communication_method']; | |
163 | $updateParams['contact_id'] = $contactId; | |
6a488035 TO |
164 | $contact = CRM_Contact_BAO_Contact::create($updateParams); |
165 | $contactId = $contact->id; | |
166 | ||
6a488035 TO |
167 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); |
168 | ||
6a488035 TO |
169 | $this->assertEquals($updateParams['first_name'], $contact->first_name, 'Check for first name creation.'); |
170 | $this->assertEquals($updateParams['last_name'], $contact->last_name, 'Check for last name creation.'); | |
171 | $this->assertEquals($updateParams['middle_name'], $contact->middle_name, 'Check for middle name creation.'); | |
172 | $this->assertEquals($updateParams['contact_type'], $contact->contact_type, 'Check for contact type creation.'); | |
173 | $this->assertEquals('0', $contact->do_not_email, 'Check for do_not_email creation.'); | |
174 | $this->assertEquals('0', $contact->do_not_phone, 'Check for do_not_phone creation.'); | |
175 | $this->assertEquals('0', $contact->do_not_mail, 'Check for do_not_mail creation.'); | |
176 | $this->assertEquals('0', $contact->do_not_trade, 'Check for do_not_trade creation.'); | |
177 | $this->assertEquals('0', $contact->is_opt_out, 'Check for is_opt_out creation.'); | |
178 | $this->assertEquals($updateParams['external_identifier'], $contact->external_identifier, | |
179 | 'Check for external_identifier creation.' | |
180 | ); | |
181 | $this->assertEquals($updateParams['last_name'] . ', ' . $updateParams['first_name'], | |
182 | $contact->sort_name, 'Check for sort_name creation.' | |
183 | ); | |
184 | $this->assertEquals($updateParams['preferred_mail_format'], $contact->preferred_mail_format, | |
185 | 'Check for preferred_mail_format creation.' | |
186 | ); | |
187 | $this->assertEquals($updateParams['contact_source'], $contact->source, 'Check for contact_source creation.'); | |
188 | $this->assertEquals($updateParams['prefix_id'], $contact->prefix_id, 'Check for prefix_id creation.'); | |
189 | $this->assertEquals($updateParams['suffix_id'], $contact->suffix_id, 'Check for suffix_id creation.'); | |
190 | $this->assertEquals($updateParams['job_title'], $contact->job_title, 'Check for job_title creation.'); | |
191 | $this->assertEquals($updateParams['gender_id'], $contact->gender_id, 'Check for gender_id creation.'); | |
192 | $this->assertEquals('1', $contact->is_deceased, 'Check for is_deceased creation.'); | |
193 | $this->assertEquals(CRM_Utils_Date::processDate($updateParams['birth_date']), | |
194 | date('YmdHis', strtotime($contact->birth_date)), 'Check for birth_date creation.' | |
195 | ); | |
196 | $this->assertEquals(CRM_Utils_Date::processDate($updateParams['deceased_date']), | |
197 | date('YmdHis', strtotime($contact->deceased_date)), 'Check for deceased_date creation.' | |
198 | ); | |
199 | $dbPrefComm = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
200 | $contact->preferred_communication_method | |
201 | ); | |
9099cab3 | 202 | $checkPrefComm = []; |
6a488035 TO |
203 | foreach ($dbPrefComm as $key => $value) { |
204 | if ($value) { | |
205 | $checkPrefComm[$value] = 1; | |
206 | } | |
207 | } | |
208 | $this->assertAttributesEquals($checkPrefComm, $prefComm); | |
209 | ||
93ac19cd | 210 | $this->contactDelete($contactId); |
6a488035 TO |
211 | } |
212 | ||
213 | /** | |
22f80e87 | 214 | * Test case for add( ) with All contact types. |
6a488035 | 215 | */ |
00be9182 | 216 | public function testAddWithAllContactTypes() { |
6a488035 | 217 | $firstName = 'Bill'; |
92915c55 | 218 | $lastName = 'Adams'; |
9099cab3 | 219 | $params = [ |
6a488035 TO |
220 | 'first_name' => $firstName, |
221 | 'last_name' => $lastName, | |
222 | 'contact_type' => 'Individual', | |
9099cab3 | 223 | ]; |
6a488035 TO |
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'; | |
9099cab3 | 233 | $params = [ |
6a488035 TO |
234 | 'first_name' => $firstName, |
235 | 'contact_type' => 'Individual', | |
236 | 'contact_id' => $contactId, | |
9099cab3 | 237 | ]; |
6a488035 TO |
238 | |
239 | $contact = CRM_Contact_BAO_Contact::add($params); | |
240 | $this->assertEquals($firstName, $contact->first_name, 'Check for updated first name.'); | |
241 | $contactId = $contact->id; | |
93ac19cd | 242 | $this->contactDelete($contactId); |
6a488035 TO |
243 | |
244 | $householdName = 'Adams house'; | |
9099cab3 | 245 | $params = [ |
6a488035 TO |
246 | 'household_name' => $householdName, |
247 | 'contact_type' => 'Household', | |
9099cab3 | 248 | ]; |
6a488035 TO |
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'; | |
9099cab3 | 255 | $params = [ |
6a488035 TO |
256 | 'household_name' => $householdName, |
257 | 'contact_type' => 'Household', | |
258 | 'contact_id' => $contactId, | |
9099cab3 | 259 | ]; |
6a488035 TO |
260 | $contact = CRM_Contact_BAO_Contact::add($params); |
261 | $this->assertEquals($householdName, $contact->sort_name, 'Check for updated household.'); | |
93ac19cd | 262 | $this->contactDelete($contactId); |
6a488035 TO |
263 | |
264 | $organizationName = 'My Organization'; | |
9099cab3 | 265 | $params = [ |
6a488035 TO |
266 | 'organization_name' => $organizationName, |
267 | 'contact_type' => 'Organization', | |
9099cab3 | 268 | ]; |
6a488035 TO |
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'; | |
9099cab3 | 275 | $params = [ |
6a488035 TO |
276 | 'organization_name' => $organizationName, |
277 | 'contact_type' => 'Organization', | |
278 | 'contact_id' => $contactId, | |
9099cab3 | 279 | ]; |
6a488035 TO |
280 | $contact = CRM_Contact_BAO_Contact::add($params); |
281 | $this->assertEquals($organizationName, $contact->sort_name, 'Check for updated organization.'); | |
93ac19cd | 282 | $this->contactDelete($contactId); |
6a488035 TO |
283 | } |
284 | ||
285 | /** | |
92c99a4a EM |
286 | * Test case for create. |
287 | * | |
288 | * Test with missing params. | |
6a488035 | 289 | */ |
00be9182 | 290 | public function testCreateWithEmptyParams() { |
9099cab3 | 291 | $params = [ |
6a488035 TO |
292 | 'first_name' => 'Bill', |
293 | 'last_name' => 'Adams', | |
9099cab3 | 294 | ]; |
6a488035 TO |
295 | $contact = CRM_Contact_BAO_Contact::create($params); |
296 | ||
297 | //Now check Contact object | |
298 | $this->assertNull($contact); | |
299 | } | |
300 | ||
301 | /** | |
92c99a4a EM |
302 | * Test case for create. |
303 | * | |
304 | * Test with all params. | |
6a488035 TO |
305 | * ( create and update modes ). |
306 | */ | |
00be9182 | 307 | public function testCreateWithAll() { |
6a488035 TO |
308 | //take the common contact params |
309 | $params = $this->contactParams(); | |
310 | $params['note'] = 'test note'; | |
311 | ||
312 | //create the contact with given params. | |
313 | $contact = CRM_Contact_BAO_Contact::create($params); | |
314 | ||
315 | //Now check $contact is object of contact DAO.. | |
316 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); | |
317 | $contactId = $contact->id; | |
318 | ||
319 | //Now check values of contact object with params. | |
320 | $this->assertEquals($params['first_name'], $contact->first_name, 'Check for first name creation.'); | |
321 | $this->assertEquals($params['last_name'], $contact->last_name, 'Check for last name creation.'); | |
322 | $this->assertEquals($params['contact_type'], $contact->contact_type, 'Check for contact type creation.'); | |
323 | ||
324 | //Now check DB for Address | |
9099cab3 | 325 | $searchParams = [ |
6a488035 TO |
326 | 'contact_id' => $contactId, |
327 | 'location_type_id' => 1, | |
328 | 'is_primary' => 1, | |
9099cab3 CW |
329 | ]; |
330 | $compareParams = [ | |
6b409353 | 331 | 'street_address' => $params['address'][1]['street_address'] ?? NULL, |
6a488035 TO |
332 | 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', |
333 | $params['address'][1] | |
334 | ), | |
335 | 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', | |
336 | $params['address'][1] | |
337 | ), | |
207f62c6 AS |
338 | 'supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', |
339 | $params['address'][1] | |
340 | ), | |
6b409353 CW |
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, | |
6a488035 TO |
344 | 'state_province_id' => CRM_Utils_Array::value('state_province_id', |
345 | $params['address'][1] | |
346 | ), | |
6b409353 CW |
347 | 'geo_code_1' => $params['address'][1]['geo_code_1'] ?? NULL, |
348 | 'geo_code_2' => $params['address'][1]['geo_code_2'] ?? NULL, | |
9099cab3 | 349 | ]; |
6a488035 TO |
350 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
351 | ||
352 | //Now check DB for Email | |
9099cab3 | 353 | $compareParams = ['email' => CRM_Utils_Array::value('email', $params['email'][1])]; |
6a488035 TO |
354 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
355 | ||
356 | //Now check DB for openid | |
9099cab3 | 357 | $compareParams = ['openid' => CRM_Utils_Array::value('openid', $params['openid'][1])]; |
6a488035 TO |
358 | $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams); |
359 | ||
360 | //Now check DB for IM | |
9099cab3 | 361 | $compareParams = [ |
6b409353 CW |
362 | 'name' => $params['im'][1]['name'] ?? NULL, |
363 | 'provider_id' => $params['im'][1]['provider_id'] ?? NULL, | |
9099cab3 | 364 | ]; |
6a488035 TO |
365 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
366 | ||
367 | //Now check DB for Phone | |
9099cab3 | 368 | $searchParams = [ |
6a488035 TO |
369 | 'contact_id' => $contactId, |
370 | 'location_type_id' => 1, | |
371 | 'is_primary' => 1, | |
6b409353 | 372 | 'phone_type_id' => $params['phone'][1]['phone_type_id'] ?? NULL, |
9099cab3 CW |
373 | ]; |
374 | $compareParams = ['phone' => CRM_Utils_Array::value('phone', $params['phone'][1])]; | |
6a488035 TO |
375 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
376 | ||
377 | //Now check DB for Mobile | |
9099cab3 | 378 | $searchParams = [ |
6a488035 TO |
379 | 'contact_id' => $contactId, |
380 | 'location_type_id' => 1, | |
6b409353 | 381 | 'phone_type_id' => $params['phone'][2]['phone_type_id'] ?? NULL, |
9099cab3 CW |
382 | ]; |
383 | $compareParams = ['phone' => CRM_Utils_Array::value('phone', $params['phone'][2])]; | |
6a488035 TO |
384 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
385 | ||
386 | //Now check DB for Note | |
9099cab3 | 387 | $searchParams = [ |
6a488035 TO |
388 | 'entity_id' => $contactId, |
389 | 'entity_table' => 'civicrm_contact', | |
9099cab3 CW |
390 | ]; |
391 | $compareParams = ['note' => $params['note']]; | |
6a488035 TO |
392 | $this->assertDBCompareValues('CRM_Core_DAO_Note', $searchParams, $compareParams); |
393 | ||
394 | //update the contact. | |
9099cab3 | 395 | $updateParams = [ |
6a488035 TO |
396 | 'first_name' => 'John', |
397 | 'last_name' => 'Doe', | |
398 | 'contact_type' => 'Individual', | |
399 | 'note' => 'new test note', | |
9099cab3 CW |
400 | ]; |
401 | $updateParams['address'][1] = [ | |
6a488035 TO |
402 | 'location_type_id' => 1, |
403 | 'is_primary' => 1, | |
404 | 'street_address' => 'Oberoi Garden', | |
405 | 'supplemental_address_1' => 'A-wing:3037', | |
406 | 'supplemental_address_2' => 'Andhery', | |
207f62c6 | 407 | 'supplemental_address_3' => 'Anywhere', |
6a488035 TO |
408 | 'city' => 'Mumbai', |
409 | 'postal_code' => '12345', | |
410 | 'country_id' => 1228, | |
411 | 'state_province_id' => 1004, | |
412 | 'geo_code_1' => '31.694842', | |
413 | 'geo_code_2' => '-106.29998', | |
9099cab3 CW |
414 | ]; |
415 | $updateParams['email'][1] = [ | |
6a488035 TO |
416 | 'location_type_id' => 1, |
417 | 'is_primary' => 1, | |
418 | 'email' => 'john.doe@example.org', | |
9099cab3 | 419 | ]; |
6a488035 | 420 | |
9099cab3 | 421 | $updateParams['phone'][1] = [ |
6a488035 TO |
422 | 'location_type_id' => 1, |
423 | 'is_primary' => 1, | |
424 | 'phone_type_id' => 1, | |
425 | 'phone' => '02115245336', | |
9099cab3 CW |
426 | ]; |
427 | $updateParams['phone'][2] = [ | |
6a488035 TO |
428 | 'location_type_id' => 1, |
429 | 'phone_type_id' => 2, | |
430 | 'phone' => '9766323895', | |
9099cab3 | 431 | ]; |
6a488035 TO |
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 | |
9099cab3 | 447 | $searchParams = [ |
6a488035 TO |
448 | 'contact_id' => $contactId, |
449 | 'location_type_id' => 1, | |
450 | 'is_primary' => 1, | |
9099cab3 CW |
451 | ]; |
452 | $compareParams = [ | |
6a488035 TO |
453 | 'street_address' => 'Oberoi Garden', |
454 | 'supplemental_address_1' => 'A-wing:3037', | |
455 | 'supplemental_address_2' => 'Andhery', | |
207f62c6 | 456 | 'supplemental_address_3' => 'Anywhere', |
6a488035 TO |
457 | 'city' => 'Mumbai', |
458 | 'postal_code' => '12345', | |
459 | 'country_id' => 1228, | |
460 | 'state_province_id' => 1004, | |
461 | 'geo_code_1' => '31.694842', | |
462 | 'geo_code_2' => '-106.29998', | |
9099cab3 | 463 | ]; |
6a488035 TO |
464 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
465 | ||
466 | //Now check DB for updated Email | |
9099cab3 | 467 | $compareParams = ['email' => 'john.doe@example.org']; |
6a488035 TO |
468 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
469 | ||
470 | //Now check DB for updated Phone | |
9099cab3 | 471 | $searchParams = [ |
6a488035 TO |
472 | 'contact_id' => $contactId, |
473 | 'location_type_id' => 1, | |
474 | 'is_primary' => 1, | |
475 | 'phone_type_id' => 1, | |
9099cab3 CW |
476 | ]; |
477 | $compareParams = ['phone' => '02115245336']; | |
6a488035 TO |
478 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
479 | ||
480 | //Now check DB for updated Mobile | |
9099cab3 | 481 | $searchParams = [ |
6a488035 TO |
482 | 'contact_id' => $contactId, |
483 | 'location_type_id' => 1, | |
484 | 'phone_type_id' => 2, | |
9099cab3 CW |
485 | ]; |
486 | $compareParams = ['phone' => '9766323895']; | |
6a488035 | 487 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
22f80e87 EM |
488 | // As we are not updating note. |
489 | // Now check DB for New Note. | |
490 | $this->assertDBNotNull('CRM_Core_DAO_Note', $updateParams['note'], 'id', 'note', | |
6a488035 TO |
491 | 'Database check for New created note ' |
492 | ); | |
493 | ||
22f80e87 | 494 | // Delete all notes related to contact. |
6a488035 TO |
495 | CRM_Core_BAO_Note::cleanContactNotes($contactId); |
496 | ||
22f80e87 | 497 | // Cleanup DB by deleting the contact. |
93ac19cd | 498 | $this->contactDelete($contactId); |
9099cab3 | 499 | $this->quickCleanup(['civicrm_contact', 'civicrm_note']); |
6a488035 TO |
500 | } |
501 | ||
502 | /** | |
db7de9c1 EM |
503 | * Test case for resolveDefaults( ). |
504 | * | |
505 | * Test all pseudoConstant, stateProvince, country. | |
6a488035 | 506 | */ |
00be9182 | 507 | public function testResolveDefaults() { |
9099cab3 | 508 | $params = [ |
6a488035 TO |
509 | 'prefix_id' => 3, |
510 | 'suffix_id' => 2, | |
511 | 'gender_id' => 2, | |
512 | 'birth_date' => '1983-12-13', | |
9099cab3 | 513 | ]; |
6a488035 | 514 | |
9099cab3 | 515 | $params['address'][1] = [ |
6a488035 TO |
516 | 'location_type_id' => 1, |
517 | 'is_primary' => 1, | |
518 | 'country_id' => 1228, | |
519 | 'state_province_id' => 1004, | |
9099cab3 | 520 | ]; |
599bbb8d | 521 | // @todo - we are testing this with $reverse = FALSE but it is never called that way! |
6a488035 TO |
522 | CRM_Contact_BAO_Contact::resolveDefaults($params); |
523 | ||
524 | //check the resolve values. | |
26cf88b5 | 525 | $genders = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'gender_id'); |
6a488035 | 526 | $this->assertEquals($genders[$params['gender_id']], $params['gender'], 'Check for gender.'); |
e6c4755b | 527 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
6a488035 | 528 | $this->assertEquals($prefix[$params['prefix_id']], $params['prefix'], 'Check for prefix.'); |
e6c4755b | 529 | $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); |
6a488035 | 530 | $this->assertEquals($suffix[$params['suffix_id']], $params['suffix'], 'Check for suffix.'); |
599bbb8d | 531 | $this->assertEquals(1004, $params['address'][1]['state_province_id']); |
6a488035 TO |
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 | /** | |
442cf836 EM |
539 | * Test case for retrieve( ). |
540 | * | |
541 | * Test with all values. | |
6a488035 | 542 | */ |
00be9182 | 543 | public function testRetrieve() { |
6a488035 TO |
544 | //take the common contact params |
545 | $params = $this->contactParams(); | |
546 | $params['note'] = 'test note'; | |
6a488035 TO |
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; | |
72142c57 | 553 | //create the organization contact with the given params. |
9099cab3 | 554 | $orgParams = [ |
72142c57 AN |
555 | 'organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4), |
556 | 'contact_type' => 'Organization', | |
9099cab3 | 557 | ]; |
72142c57 AN |
558 | $orgContact = CRM_Contact_BAO_Contact::add($orgParams); |
559 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object'); | |
6a488035 TO |
560 | |
561 | //create employee of relationship. | |
72142c57 | 562 | CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id); |
6a488035 TO |
563 | |
564 | //retrieve the contact values from database. | |
9099cab3 CW |
565 | $values = []; |
566 | $searchParams = ['contact_id' => $contactId]; | |
6a488035 TO |
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 | |
72142c57 AN |
581 | $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), |
582 | CRM_Utils_Array::value('1', $values['address']) | |
583 | ); | |
6a488035 TO |
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) { | |
849ac823 | 614 | $retrieveNote = $val['note'] ?? NULL; |
6a488035 TO |
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. | |
72142c57 | 623 | $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization'); |
6a488035 TO |
624 | //Now check values of Relationship type. |
625 | $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); | |
626 | //delete the organization. | |
93ac19cd | 627 | $this->contactDelete(CRM_Utils_Array::value('contact_id_b', $val)); |
6a488035 TO |
628 | } |
629 | ||
630 | //delete all notes related to contact | |
631 | CRM_Core_BAO_Note::cleanContactNotes($contactId); | |
632 | ||
6a488035 | 633 | //cleanup DB by deleting the contact |
93ac19cd | 634 | $this->contactDelete($contactId); |
9099cab3 | 635 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
636 | } |
637 | ||
638 | /** | |
442cf836 | 639 | * Test case for deleteContact( ). |
6a488035 | 640 | */ |
00be9182 | 641 | public function testDeleteContact() { |
6a488035 TO |
642 | $contactParams = $this->contactParams(); |
643 | ||
23ead872 | 644 | $customGroup = $this->customGroupCreate(); |
9099cab3 | 645 | $fields = [ |
6a488035 TO |
646 | 'label' => 'testFld', |
647 | 'data_type' => 'String', | |
648 | 'html_type' => 'Text', | |
23ead872 | 649 | 'custom_group_id' => $customGroup['id'], |
9099cab3 | 650 | ]; |
6a488035 | 651 | $customField = CRM_Core_BAO_CustomField::create($fields); |
9099cab3 CW |
652 | $contactParams['custom'] = [ |
653 | $customField->id => [ | |
654 | -1 => [ | |
6a488035 TO |
655 | 'value' => 'Test custom value', |
656 | 'type' => 'String', | |
657 | 'custom_field_id' => $customField->id, | |
23ead872 | 658 | 'custom_group_id' => $customGroup['id'], |
659 | 'table_name' => $customGroup['values'][$customGroup['id']]['table_name'], | |
6a488035 TO |
660 | 'column_name' => $customField->column_name, |
661 | 'file_id' => NULL, | |
9099cab3 CW |
662 | ], |
663 | ], | |
664 | ]; | |
6a488035 TO |
665 | |
666 | //create contact | |
667 | $contact = CRM_Contact_BAO_Contact::create($contactParams); | |
668 | $contactId = $contact->id; | |
669 | ||
4f5f00c9 | 670 | //delete contact permanently. |
671 | CRM_Contact_BAO_Contact::deleteContact($contactId, FALSE, TRUE); | |
6a488035 TO |
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 | ||
6a488035 | 701 | // Check that the custom field value is no longer present |
9099cab3 | 702 | $params = [ |
6a488035 TO |
703 | 'entityID' => $contactId, |
704 | 'custom_' . $customField->id => 1, | |
9099cab3 | 705 | ]; |
6a488035 TO |
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 | ); | |
9099cab3 | 716 | $this->quickCleanup(['civicrm_contact', 'civicrm_note']); |
23ead872 | 717 | $this->customGroupDelete($customGroup['id']); |
6a488035 TO |
718 | } |
719 | ||
720 | /** | |
55d2c6f1 | 721 | * Test case for createProfileContact. |
6a488035 | 722 | */ |
00be9182 | 723 | public function testCreateProfileContact() { |
6a488035 TO |
724 | $fields = CRM_Contact_BAO_Contact::exportableFields('Individual'); |
725 | ||
726 | //current employer field for individual | |
9099cab3 | 727 | $fields['organization_name'] = [ |
6a488035 TO |
728 | 'name' => 'organization_name', |
729 | 'where' => 'civicrm_organization.organization_name', | |
730 | 'title' => 'Current Employer', | |
9099cab3 | 731 | ]; |
6a488035 TO |
732 | //get the common params |
733 | $contactParams = $this->contactParams(); | |
9099cab3 | 734 | $unsetParams = ['location', 'privacy']; |
6a488035 TO |
735 | foreach ($unsetParams as $param) { |
736 | unset($contactParams[$param]); | |
737 | } | |
738 | ||
9099cab3 | 739 | $profileParams = [ |
6a488035 | 740 | 'organization_name' => 'Yahoo', |
663fecd0 | 741 | 'gender_id' => '2', |
742 | 'prefix_id' => '3', | |
743 | 'suffix_id' => '2', | |
6a488035 TO |
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', | |
207f62c6 | 767 | 'supplemental_address_3-Primary' => 'My Town', |
6a488035 TO |
768 | 'user_unique_id' => '123456789', |
769 | 'is_bulkmail' => '1', | |
770 | 'world_region' => 'India', | |
9099cab3 | 771 | 'tag' => [ |
6a488035 TO |
772 | '3' => '1', |
773 | '4' => '1', | |
774 | '1' => '1', | |
9099cab3 CW |
775 | ], |
776 | ]; | |
6a488035 TO |
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, | |
92915c55 | 824 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE) |
6a488035 | 825 | ); |
9099cab3 | 826 | $checkPrefComm = []; |
6a488035 TO |
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 | |
9099cab3 | 835 | $searchParams = [ |
6a488035 TO |
836 | 'contact_id' => $contactId, |
837 | 'location_type_id' => 1, | |
838 | 'is_primary' => 1, | |
9099cab3 CW |
839 | ]; |
840 | $compareParams = [ | |
6b409353 CW |
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, | |
9099cab3 | 851 | ]; |
6a488035 TO |
852 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
853 | ||
854 | //Now check DB for Email | |
9099cab3 | 855 | $compareParams = ['email' => CRM_Utils_Array::value('email-Primary', $profileParams)]; |
6a488035 TO |
856 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
857 | ||
858 | //Now check DB for IM | |
9099cab3 | 859 | $compareParams = [ |
6b409353 CW |
860 | 'name' => $profileParams['im-Primary'] ?? NULL, |
861 | 'provider_id' => $profileParams['im-Primary-provider_id'] ?? NULL, | |
9099cab3 | 862 | ]; |
6a488035 TO |
863 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
864 | ||
865 | //Now check DB for Phone | |
9099cab3 | 866 | $searchParams = [ |
6a488035 TO |
867 | 'contact_id' => $contactId, |
868 | 'location_type_id' => 1, | |
869 | 'is_primary' => 1, | |
9099cab3 CW |
870 | ]; |
871 | $compareParams = ['phone' => CRM_Utils_Array::value('phone-Primary-1', $profileParams)]; | |
6a488035 TO |
872 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
873 | ||
874 | //Now check DB for Mobile | |
9099cab3 | 875 | $searchParams = [ |
6a488035 TO |
876 | 'contact_id' => $contactId, |
877 | 'location_type_id' => 1, | |
6b409353 | 878 | 'phone_type_id' => $params['phone'][2]['phone_type_id'] ?? NULL, |
9099cab3 CW |
879 | ]; |
880 | $compareParams = ['phone' => CRM_Utils_Array::value('phone-Primary-2', $profileParams)]; | |
6a488035 TO |
881 | |
882 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); | |
883 | ||
884 | //get the value of relationship | |
9099cab3 CW |
885 | $values = []; |
886 | $searchParams = ['contact_id' => $contactId]; | |
6a488035 TO |
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. | |
93ac19cd | 896 | $this->contactDelete(CRM_Utils_Array::value('cid', $val)); |
6a488035 TO |
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 | |
9099cab3 | 908 | $updateCParams = [ |
6a488035 TO |
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, | |
9099cab3 CW |
919 | 'website' => [ |
920 | 1 => [ | |
6a488035 TO |
921 | 'website_type_id' => 1, |
922 | 'url' => 'http://civicrmUpdate.org', | |
9099cab3 CW |
923 | ], |
924 | ], | |
6a488035 TO |
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', | |
9099cab3 | 933 | 'privacy' => [ |
6a488035 TO |
934 | 'do_not_phone' => 1, |
935 | 'do_not_email' => 1, | |
9099cab3 CW |
936 | ], |
937 | 'preferred_communication_method' => [ | |
6a488035 TO |
938 | '1' => 0, |
939 | '2' => 1, | |
940 | '3' => 0, | |
941 | '4' => 1, | |
942 | '5' => 0, | |
9099cab3 CW |
943 | ], |
944 | ]; | |
6a488035 | 945 | |
9099cab3 | 946 | $updatePfParams = [ |
6a488035 | 947 | 'organization_name' => 'Google', |
6a488035 TO |
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', | |
207f62c6 | 971 | 'supplemental_address_3-Primary' => 'Anywhere', |
6a488035 TO |
972 | 'user_unique_id' => '1122334455', |
973 | 'is_bulkmail' => '1', | |
974 | 'world_region' => 'India', | |
9099cab3 | 975 | 'tag' => [ |
6a488035 TO |
976 | '2' => '1', |
977 | '5' => '1', | |
9099cab3 CW |
978 | ], |
979 | ]; | |
6a488035 TO |
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, | |
92915c55 | 1030 | CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactId, 'preferred_communication_method', 'id', TRUE) |
6a488035 | 1031 | ); |
9099cab3 | 1032 | $checkPrefComm = []; |
6a488035 TO |
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 | |
9099cab3 | 1041 | $searchParams = [ |
6a488035 TO |
1042 | 'contact_id' => $contactId, |
1043 | 'location_type_id' => 1, | |
1044 | 'is_primary' => 1, | |
9099cab3 CW |
1045 | ]; |
1046 | $compareParams = [ | |
6b409353 CW |
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, | |
9099cab3 | 1057 | ]; |
6a488035 TO |
1058 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
1059 | ||
1060 | //Now check DB for Email | |
9099cab3 | 1061 | $compareParams = ['email' => CRM_Utils_Array::value('email-Primary', $updatePfParams)]; |
6a488035 TO |
1062 | $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams); |
1063 | ||
1064 | //Now check DB for IM | |
9099cab3 | 1065 | $compareParams = [ |
6b409353 CW |
1066 | 'name' => $updatePfParams['im-Primary'] ?? NULL, |
1067 | 'provider_id' => $updatePfParams['im-Primary-provider_id'] ?? NULL, | |
9099cab3 | 1068 | ]; |
6a488035 TO |
1069 | $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams); |
1070 | ||
1071 | //Now check DB for Phone | |
9099cab3 | 1072 | $searchParams = [ |
6a488035 TO |
1073 | 'contact_id' => $contactId, |
1074 | 'location_type_id' => 1, | |
1075 | 'is_primary' => 1, | |
9099cab3 CW |
1076 | ]; |
1077 | $compareParams = ['phone' => CRM_Utils_Array::value('phone-Primary-1', $updatePfParams)]; | |
6a488035 TO |
1078 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
1079 | ||
1080 | //Now check DB for Mobile | |
9099cab3 | 1081 | $searchParams = [ |
6a488035 TO |
1082 | 'contact_id' => $contactId, |
1083 | 'location_type_id' => 1, | |
6b409353 | 1084 | 'phone_type_id' => $params['phone'][2]['phone_type_id'] ?? NULL, |
9099cab3 CW |
1085 | ]; |
1086 | $compareParams = ['phone' => CRM_Utils_Array::value('phone-Primary-2', $updatePfParams)]; | |
6a488035 TO |
1087 | $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams); |
1088 | ||
1089 | //get the value of relationship | |
9099cab3 CW |
1090 | $values = []; |
1091 | $searchParams = ['contact_id' => $contactId]; | |
6a488035 TO |
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. | |
93ac19cd | 1101 | $this->contactDelete(CRM_Utils_Array::value('cid', $val)); |
6a488035 TO |
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 | |
93ac19cd | 1112 | $this->contactDelete($contactId); |
6a488035 TO |
1113 | } |
1114 | ||
1115 | /** | |
55d2c6f1 | 1116 | * Test case for getContactDetails( ). |
6a488035 | 1117 | */ |
00be9182 | 1118 | public function testGetContactDetails() { |
6a488035 TO |
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); | |
9099cab3 | 1128 | $compareParams = [ |
6a488035 TO |
1129 | $params['first_name'] . ' ' . $params['last_name'], |
1130 | CRM_Utils_Array::value('email', $params['email'][1]), | |
1131 | (bool ) $params['privacy']['do_not_email'], | |
9099cab3 | 1132 | ]; |
6a488035 TO |
1133 | //Now check the contact details |
1134 | $this->assertAttributesEquals($compareParams, $contactDetails); | |
1135 | ||
1136 | //cleanup DB by deleting the contact | |
93ac19cd | 1137 | $this->contactDelete($contactId); |
9099cab3 | 1138 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
1139 | } |
1140 | ||
1141 | /** | |
55d2c6f1 | 1142 | * Test case for importableFields( ) and exportableFields( ). |
6a488035 | 1143 | */ |
00be9182 | 1144 | public function testFields() { |
6a488035 TO |
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 | /** | |
92c99a4a | 1167 | * Test case for getPrimaryEmail. |
6a488035 | 1168 | */ |
00be9182 | 1169 | public function testGetPrimaryEmail() { |
6a488035 TO |
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 | |
93ac19cd | 1185 | $this->contactDelete($contactId); |
9099cab3 | 1186 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
1187 | } |
1188 | ||
1189 | /** | |
442cf836 | 1190 | * Test case for getPrimaryOpenId( ). |
6a488035 | 1191 | */ |
00be9182 | 1192 | public function testGetPrimaryOpenId() { |
6a488035 TO |
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 | |
93ac19cd | 1210 | $this->contactDelete($contactId); |
6a488035 TO |
1211 | } |
1212 | ||
1213 | /** | |
442cf836 | 1214 | * Test case for matchContactOnEmail( ). |
6a488035 | 1215 | */ |
00be9182 | 1216 | public function testMatchContactOnEmail() { |
6a488035 TO |
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 | |
93ac19cd | 1230 | $this->contactDelete($contactId); |
9099cab3 | 1231 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
1232 | } |
1233 | ||
1234 | /** | |
442cf836 | 1235 | * Test case for getContactType( ). |
6a488035 | 1236 | */ |
00be9182 | 1237 | public function testGetContactType() { |
6a488035 TO |
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 | |
93ac19cd | 1249 | $this->contactDelete($contactId); |
9099cab3 | 1250 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
1251 | } |
1252 | ||
1253 | /** | |
442cf836 | 1254 | * Test case for displayName( ). |
6a488035 | 1255 | */ |
00be9182 | 1256 | public function testDisplayName() { |
6a488035 TO |
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 | ||
e6c4755b CW |
1267 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
1268 | $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); | |
6a488035 TO |
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 | |
93ac19cd | 1276 | $this->contactDelete($contactId); |
9099cab3 | 1277 | $this->quickCleanup(['civicrm_contact']); |
6a488035 TO |
1278 | } |
1279 | ||
1280 | /** | |
442cf836 | 1281 | * Test case for getDisplayAndImage( ). |
6a488035 | 1282 | */ |
00be9182 | 1283 | public function testGetDisplayAndImage() { |
6a488035 TO |
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 | ||
e6c4755b CW |
1296 | $prefix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'prefix_id'); |
1297 | $suffix = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'suffix_id'); | |
6a488035 TO |
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 | |
93ac19cd | 1306 | $this->contactDelete($contactId); |
6a488035 TO |
1307 | } |
1308 | ||
6a488035 | 1309 | /** |
442cf836 | 1310 | * Build common params. |
6a488035 TO |
1311 | */ |
1312 | private function contactParams() { | |
1313 | ||
9099cab3 | 1314 | $params = [ |
6a488035 TO |
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, | |
9099cab3 CW |
1325 | 'website' => [ |
1326 | 1 => [ | |
6a488035 TO |
1327 | 'website_type_id' => 1, |
1328 | 'url' => 'http://civicrm.org', | |
9099cab3 CW |
1329 | ], |
1330 | ], | |
6a488035 TO |
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', | |
9099cab3 | 1339 | 'privacy' => [ |
6a488035 TO |
1340 | 'do_not_phone' => 1, |
1341 | 'do_not_email' => 1, | |
1342 | 'do_not_mail' => 1, | |
1343 | 'do_not_trade' => 1, | |
9099cab3 CW |
1344 | ], |
1345 | 'preferred_communication_method' => [ | |
6a488035 TO |
1346 | '1' => 1, |
1347 | '2' => 0, | |
1348 | '3' => 1, | |
1349 | '4' => 0, | |
1350 | '5' => 1, | |
9099cab3 CW |
1351 | ], |
1352 | ]; | |
6a488035 | 1353 | |
9099cab3 CW |
1354 | $params['address'] = []; |
1355 | $params['address'][1] = [ | |
6a488035 TO |
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', | |
207f62c6 | 1361 | 'supplemental_address_3' => 'My Town', |
6a488035 TO |
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', | |
9099cab3 | 1368 | ]; |
6a488035 | 1369 | |
9099cab3 CW |
1370 | $params['email'] = []; |
1371 | $params['email'][1] = [ | |
6a488035 TO |
1372 | 'location_type_id' => 1, |
1373 | 'is_primary' => 1, | |
1374 | 'email' => 'john.smith@example.org', | |
9099cab3 | 1375 | ]; |
6a488035 | 1376 | |
9099cab3 CW |
1377 | $params['phone'] = []; |
1378 | $params['phone'][1] = [ | |
6a488035 TO |
1379 | 'location_type_id' => 1, |
1380 | 'is_primary' => 1, | |
1381 | 'phone_type_id' => 1, | |
1382 | 'phone' => '303443689', | |
9099cab3 CW |
1383 | ]; |
1384 | $params['phone'][2] = [ | |
6a488035 TO |
1385 | 'location_type_id' => 1, |
1386 | 'phone_type_id' => 2, | |
1387 | 'phone' => '9833910234', | |
9099cab3 | 1388 | ]; |
6a488035 | 1389 | |
9099cab3 CW |
1390 | $params['openid'] = []; |
1391 | $params['openid'][1] = [ | |
6a488035 TO |
1392 | 'location_type_id' => 1, |
1393 | 'is_primary' => 1, | |
1394 | 'openid' => 'http://civicrm.org/', | |
9099cab3 | 1395 | ]; |
6a488035 | 1396 | |
9099cab3 CW |
1397 | $params['im'] = []; |
1398 | $params['im'][1] = [ | |
6a488035 TO |
1399 | 'location_type_id' => 1, |
1400 | 'is_primary' => 1, | |
1401 | 'name' => 'john.smith', | |
1402 | 'provider_id' => 1, | |
9099cab3 | 1403 | ]; |
6a488035 TO |
1404 | |
1405 | return $params; | |
1406 | } | |
1407 | ||
1408 | /** | |
55d2c6f1 | 1409 | * Ensure that created_date and modified_date are set. |
6a488035 | 1410 | */ |
55d2c6f1 | 1411 | public function testTimestampContact() { |
6a488035 | 1412 | $test = $this; |
9099cab3 | 1413 | $this->_testTimestamps([ |
92915c55 | 1414 | 'UPDATE' => function ($contactId) use ($test) { |
9099cab3 | 1415 | $params = [ |
6a488035 TO |
1416 | 'first_name' => 'Testing', |
1417 | 'contact_type' => 'Individual', | |
1418 | 'contact_id' => $contactId, | |
9099cab3 | 1419 | ]; |
e70a7fc0 TO |
1420 | $contact = CRM_Contact_BAO_Contact::add($params); |
1421 | $test->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); | |
92915c55 | 1422 | }, |
9099cab3 | 1423 | ]); |
6a488035 TO |
1424 | } |
1425 | ||
1426 | /** | |
55d2c6f1 | 1427 | * Ensure that civicrm_contact.modified_date is updated when manipulating a phone record. |
6a488035 | 1428 | */ |
55d2c6f1 | 1429 | public function testTimestampsEmail() { |
6a488035 | 1430 | $test = $this; |
9099cab3 | 1431 | $this->_testTimestamps([ |
92915c55 | 1432 | 'INSERT' => function ($contactId) use ($test) { |
9099cab3 | 1433 | $params = [ |
6a488035 TO |
1434 | 'email' => 'ex-1@example.com', |
1435 | 'is_primary' => 1, | |
1436 | 'location_type_id' => 1, | |
1437 | 'contact_id' => $contactId, | |
9099cab3 | 1438 | ]; |
92915c55 TO |
1439 | CRM_Core_BAO_Email::add($params); |
1440 | $test->assertDBQuery('ex-1@example.com', | |
6a488035 | 1441 | 'SELECT email FROM civicrm_email WHERE contact_id = %1 ORDER BY id DESC LIMIT 1', |
9099cab3 | 1442 | [1 => [$contactId, 'Integer']] |
6a488035 | 1443 | ); |
92915c55 TO |
1444 | }, |
1445 | 'UPDATE' => function ($contactId) use ($test) { | |
1446 | CRM_Core_DAO::executeQuery( | |
6a488035 | 1447 | 'UPDATE civicrm_email SET email = "ex-2@example.com" WHERE contact_id = %1', |
9099cab3 | 1448 | [1 => [$contactId, 'Integer']] |
6a488035 | 1449 | ); |
92915c55 TO |
1450 | }, |
1451 | 'DELETE' => function ($contactId) use ($test) { | |
1452 | CRM_Core_DAO::executeQuery( | |
6a488035 | 1453 | 'DELETE FROM civicrm_email WHERE contact_id = %1', |
9099cab3 | 1454 | [1 => [$contactId, 'Integer']] |
6a488035 | 1455 | ); |
92915c55 | 1456 | }, |
9099cab3 | 1457 | ]); |
6a488035 TO |
1458 | } |
1459 | ||
1460 | /** | |
442cf836 | 1461 | * Ensure that civicrm_contact.modified_date is updated when manipulating an email. |
6a488035 | 1462 | */ |
92c99a4a | 1463 | public function testTimestampsPhone() { |
6a488035 | 1464 | $test = $this; |
9099cab3 | 1465 | $this->_testTimestamps([ |
92915c55 | 1466 | 'INSERT' => function ($contactId) use ($test) { |
9099cab3 | 1467 | $params = [ |
6a488035 TO |
1468 | 'phone' => '202-555-1000', |
1469 | 'is_primary' => 1, | |
1470 | 'location_type_id' => 1, | |
1471 | 'contact_id' => $contactId, | |
9099cab3 | 1472 | ]; |
92915c55 TO |
1473 | CRM_Core_BAO_Phone::add($params); |
1474 | $test->assertDBQuery('202-555-1000', | |
6a488035 | 1475 | 'SELECT phone FROM civicrm_phone WHERE contact_id = %1 ORDER BY id DESC LIMIT 1', |
9099cab3 | 1476 | [1 => [$contactId, 'Integer']] |
6a488035 | 1477 | ); |
92915c55 TO |
1478 | }, |
1479 | 'UPDATE' => function ($contactId) use ($test) { | |
1480 | CRM_Core_DAO::executeQuery( | |
6a488035 | 1481 | 'UPDATE civicrm_phone SET phone = "202-555-2000" WHERE contact_id = %1', |
9099cab3 | 1482 | [1 => [$contactId, 'Integer']] |
6a488035 | 1483 | ); |
92915c55 TO |
1484 | }, |
1485 | 'DELETE' => function ($contactId) use ($test) { | |
1486 | CRM_Core_DAO::executeQuery( | |
6a488035 | 1487 | 'DELETE FROM civicrm_phone WHERE contact_id = %1', |
9099cab3 | 1488 | [1 => [$contactId, 'Integer']] |
6a488035 | 1489 | ); |
92915c55 | 1490 | }, |
9099cab3 | 1491 | ]); |
6a488035 TO |
1492 | } |
1493 | ||
1494 | /** | |
92c99a4a EM |
1495 | * Ensure that civicrm_contact.modified_date is updated correctly. |
1496 | * | |
1497 | * Looking at it when contact-related custom data is updated. | |
6a488035 | 1498 | */ |
92c99a4a | 1499 | public function testTimestampsCustom() { |
23ead872 | 1500 | $customGroup = $this->customGroupCreate(); |
1501 | $customGroup = $customGroup['values'][$customGroup['id']]; | |
9099cab3 | 1502 | $fields = [ |
23ead872 | 1503 | 'custom_group_id' => $customGroup['id'], |
6a488035 TO |
1504 | 'data_type' => 'String', |
1505 | 'html_type' => 'Text', | |
9099cab3 | 1506 | ]; |
fbd1b89e E |
1507 | $customField = $this->customFieldCreate($fields); |
1508 | $customField = $customField['values'][$customField['id']]; | |
6a488035 | 1509 | $test = $this; |
9099cab3 | 1510 | $this->_testTimestamps([ |
92915c55 | 1511 | 'INSERT' => function ($contactId) use ($test, $customGroup, $customField) { |
9099cab3 | 1512 | civicrm_api3('contact', 'create', [ |
6a488035 | 1513 | 'contact_id' => $contactId, |
fbd1b89e | 1514 | 'custom_' . $customField['id'] => 'test-1', |
9099cab3 | 1515 | ]); |
92915c55 TO |
1516 | }, |
1517 | 'UPDATE' => function ($contactId) use ($test, $customGroup, $customField) { | |
1518 | CRM_Core_DAO::executeQuery( | |
23ead872 | 1519 | "UPDATE {$customGroup['table_name']} SET {$customField['column_name']} = 'test-2' WHERE entity_id = %1", |
9099cab3 | 1520 | [1 => [$contactId, 'Integer']] |
6a488035 | 1521 | ); |
92915c55 TO |
1522 | }, |
1523 | 'DELETE' => function ($contactId) use ($test, $customGroup, $customField) { | |
1524 | CRM_Core_DAO::executeQuery( | |
23ead872 | 1525 | "DELETE FROM {$customGroup['table_name']} WHERE entity_id = %1", |
9099cab3 | 1526 | [1 => [$contactId, 'Integer']] |
6a488035 | 1527 | ); |
92915c55 | 1528 | }, |
9099cab3 CW |
1529 | ]); |
1530 | $this->quickCleanup(['civicrm_contact'], TRUE); | |
6a488035 TO |
1531 | } |
1532 | ||
1533 | /** | |
eceb18cc | 1534 | * Helper for testing timestamp manipulation. |
6a488035 TO |
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 | * | |
5a4f6742 CW |
1539 | * @param array $callbacks |
1540 | * ($name => $callable). | |
6a488035 | 1541 | */ |
00be9182 | 1542 | public function _testTimestamps($callbacks) { |
6a488035 | 1543 | CRM_Core_DAO::triggerRebuild(); |
f2040bc6 | 1544 | $contactId = $this->individualCreate(); |
6a488035 TO |
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) { | |
39b959db SL |
1553 | // advance clock by 1 second to ensure timestamps change |
1554 | sleep(1); | |
6a488035 TO |
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 | ||
93ac19cd | 1566 | $this->contactDelete($contactId); |
6a488035 | 1567 | } |
96025800 | 1568 | |
b795cfc3 SV |
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(); | |
9099cab3 | 1575 | $params = [ |
b795cfc3 SV |
1576 | 'first_name' => $defaults['first_name'], |
1577 | 'last_name' => $defaults['last_name'], | |
1578 | 'contact_type' => 'Individual', | |
9099cab3 CW |
1579 | 'address' => [1 => $defaults['address'][1]], |
1580 | 'phone' => [1 => $defaults['phone'][1]], | |
1581 | ]; | |
b795cfc3 SV |
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 | |
9099cab3 | 1586 | $updatePfParams = [ |
b795cfc3 SV |
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', | |
9099cab3 | 1594 | ]; |
b795cfc3 SV |
1595 | |
1596 | //create the contact using create profile contact. | |
1597 | $fields = CRM_Contact_BAO_Contact::exportableFields('Individual'); | |
1598 | ||
76719580 | 1599 | $this->createLoggedInUser(); |
b795cfc3 SV |
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. | |
9099cab3 | 1612 | $searchParams = [ |
b795cfc3 SV |
1613 | 'contact_id' => $contactId, |
1614 | 'location_type_id' => 1, | |
1615 | 'is_primary' => 1, | |
9099cab3 CW |
1616 | ]; |
1617 | $compareParams = [ | |
6b409353 | 1618 | 'street_address' => $updatePfParams['street_address-Primary'] ?? NULL, |
9099cab3 | 1619 | ]; |
b795cfc3 SV |
1620 | $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams); |
1621 | ||
1622 | //cleanup DB by deleting the contact | |
1623 | $this->contactDelete($contactId); | |
1624 | } | |
1625 | ||
5c1d7fad VP |
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 | ||
9afafd3c AF |
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 | ||
6a488035 | 1683 | } |