Merge pull request #17253 from mattwire/utf8convertblocksize
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / LocationTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 * $Id$
17 *
18 */
19
4cbe18b8
EM
20/**
21 * Class CRM_Core_BAO_LocationTest
acb109b7 22 * @group headless
4cbe18b8 23 */
6a488035 24class CRM_Core_BAO_LocationTest extends CiviUnitTestCase {
39b959db 25
00be9182 26 public function setUp() {
6a488035
TO
27 parent::setUp();
28
9099cab3 29 $this->quickCleanup([
39b959db
SL
30 'civicrm_contact',
31 'civicrm_address',
32 'civicrm_loc_block',
33 'civicrm_email',
34 'civicrm_phone',
35 'civicrm_im',
9099cab3 36 ]);
6a488035
TO
37 }
38
39 /**
40 * Tears down the fixture, for example, closes a network connection.
41 * This method is called after a test is executed.
6a488035 42 */
00be9182 43 public function tearDown() {
9099cab3 44 $tablesToTruncate = [
6a488035
TO
45 'civicrm_contact',
46 'civicrm_openid',
47 'civicrm_loc_block',
9099cab3 48 ];
6a488035
TO
49 $this->quickCleanup($tablesToTruncate);
50 }
51
00be9182 52 public function testCreateWithMissingParams() {
f2040bc6 53 $contactId = $this->individualCreate();
9099cab3 54 $params = [
6a488035
TO
55 'contact_id' => $contactId,
56 'street_address' => 'Saint Helier St',
9099cab3 57 ];
6a488035
TO
58
59 CRM_Core_BAO_Location::create($params);
60
61 //Now check DB for Address
62 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
63 'Database check, Address created successfully.'
64 );
65
93ac19cd 66 $this->contactDelete($contactId);
6a488035
TO
67 }
68
69 /**
100fef9d 70 * Create() method
6a488035
TO
71 * create various elements of location block
72 * without civicrm_loc_block entry
73 */
00be9182 74 public function testCreateWithoutLocBlock() {
f2040bc6 75 $contactId = $this->individualCreate();
6a488035
TO
76
77 //create various element of location block
78 //like address, phone, email, openid, im.
9099cab3
CW
79 $params = [
80 'address' => [
81 '1' => [
6a488035
TO
82 'street_address' => 'Saint Helier St',
83 'supplemental_address_1' => 'Hallmark Ct',
84 'supplemental_address_2' => 'Jersey Village',
207f62c6 85 'supplemental_address_3' => 'My Town',
6a488035
TO
86 'city' => 'Newark',
87 'postal_code' => '01903',
88 'country_id' => 1228,
89 'state_province_id' => 1029,
90 'geo_code_1' => '18.219023',
91 'geo_code_2' => '-105.00973',
92 'is_primary' => 1,
93 'location_type_id' => 1,
9099cab3
CW
94 ],
95 ],
96 'email' => [
97 '1' => [
6a488035
TO
98 'email' => 'john.smith@example.org',
99 'is_primary' => 1,
100 'location_type_id' => 1,
9099cab3
CW
101 ],
102 ],
103 'phone' => [
104 '1' => [
6a488035
TO
105 'phone_type_id' => 1,
106 'phone' => '303443689',
107 'is_primary' => 1,
108 'location_type_id' => 1,
9099cab3
CW
109 ],
110 '2' => [
6a488035
TO
111 'phone_type_id' => 2,
112 'phone' => '9833910234',
113 'location_type_id' => 1,
9099cab3
CW
114 ],
115 ],
116 'openid' => [
117 '1' => [
6a488035
TO
118 'openid' => 'http://civicrm.org/',
119 'location_type_id' => 1,
120 'is_primary' => 1,
9099cab3
CW
121 ],
122 ],
123 'im' => [
124 '1' => [
6a488035
TO
125 'name' => 'jane.doe',
126 'provider_id' => 1,
127 'location_type_id' => 1,
128 'is_primary' => 1,
9099cab3
CW
129 ],
130 ],
131 ];
6a488035
TO
132
133 $params['contact_id'] = $contactId;
134
9a350644 135 $locBlockId = CRM_Core_BAO_Location::create($params);
6a488035
TO
136
137 //Now check DB for contact
9099cab3 138 $searchParams = [
6a488035
TO
139 'contact_id' => $contactId,
140 'location_type_id' => 1,
141 'is_primary' => 1,
9099cab3
CW
142 ];
143 $compareParams = [
6a488035
TO
144 'street_address' => 'Saint Helier St',
145 'supplemental_address_1' => 'Hallmark Ct',
146 'supplemental_address_2' => 'Jersey Village',
207f62c6 147 'supplemental_address_3' => 'My Town',
6a488035
TO
148 'city' => 'Newark',
149 'postal_code' => '01903',
150 'country_id' => 1228,
151 'state_province_id' => 1029,
152 'geo_code_1' => '18.219023',
153 'geo_code_2' => '-105.00973',
9099cab3 154 ];
6a488035
TO
155 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
156
9099cab3 157 $compareParams = ['email' => 'john.smith@example.org'];
6a488035
TO
158 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
159
9099cab3 160 $compareParams = ['openid' => 'http://civicrm.org/'];
6a488035
TO
161 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
162
9099cab3 163 $compareParams = [
6a488035
TO
164 'name' => 'jane.doe',
165 'provider_id' => 1,
9099cab3 166 ];
6a488035
TO
167 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
168
9099cab3 169 $searchParams = [
6a488035
TO
170 'contact_id' => $contactId,
171 'location_type_id' => 1,
172 'is_primary' => 1,
173 'phone_type_id' => 1,
9099cab3
CW
174 ];
175 $compareParams = ['phone' => '303443689'];
6a488035
TO
176 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
177
9099cab3 178 $searchParams = [
6a488035
TO
179 'contact_id' => $contactId,
180 'location_type_id' => 1,
181 'phone_type_id' => 2,
9099cab3
CW
182 ];
183 $compareParams = ['phone' => '9833910234'];
6a488035
TO
184 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
185
93ac19cd 186 $this->contactDelete($contactId);
6a488035
TO
187 }
188
189 /**
100fef9d 190 * Create() method
6a488035
TO
191 * create various elements of location block
192 * with civicrm_loc_block
193 */
00be9182 194 public function testCreateWithLocBlock() {
6ae19242 195 $this->_contactId = $this->individualCreate();
196 $event = $this->eventCreate();
9099cab3
CW
197 $params = [
198 'address' => [
199 '1' => [
6a488035
TO
200 'street_address' => 'Saint Helier St',
201 'supplemental_address_1' => 'Hallmark Ct',
202 'supplemental_address_2' => 'Jersey Village',
207f62c6 203 'supplemental_address_3' => 'My Town',
6a488035
TO
204 'city' => 'Newark',
205 'postal_code' => '01903',
206 'country_id' => 1228,
207 'state_province_id' => 1029,
208 'geo_code_1' => '18.219023',
209 'geo_code_2' => '-105.00973',
210 'is_primary' => 1,
211 'location_type_id' => 1,
9099cab3
CW
212 ],
213 ],
214 'email' => [
215 '1' => [
6a488035
TO
216 'email' => 'john.smith@example.org',
217 'is_primary' => 1,
218 'location_type_id' => 1,
9099cab3
CW
219 ],
220 ],
221 'phone' => [
222 '1' => [
6a488035
TO
223 'phone_type_id' => 1,
224 'phone' => '303443689',
225 'is_primary' => 1,
226 'location_type_id' => 1,
9099cab3
CW
227 ],
228 '2' => [
6a488035
TO
229 'phone_type_id' => 2,
230 'phone' => '9833910234',
231 'location_type_id' => 1,
9099cab3
CW
232 ],
233 ],
234 'im' => [
235 '1' => [
6a488035
TO
236 'name' => 'jane.doe',
237 'provider_id' => 1,
238 'location_type_id' => 1,
239 'is_primary' => 1,
9099cab3
CW
240 ],
241 ],
242 ];
6a488035 243
6ae19242 244 $params['entity_id'] = $event['id'];
6a488035
TO
245 $params['entity_table'] = 'civicrm_event';
246
247 //create location block.
248 //with various element of location block
249 //like address, phone, email, im.
9a350644 250 $locBlockId = CRM_Core_BAO_Location::create($params, NULL, TRUE)['id'];
6a488035
TO
251
252 //update event record with location block id
9099cab3 253 $eventParams = [
6ae19242 254 'id' => $event['id'],
6a488035 255 'loc_block_id' => $locBlockId,
9099cab3 256 ];
6a488035
TO
257
258 CRM_Event_BAO_Event::add($eventParams);
259
260 //Now check DB for location block
261
262 $this->assertDBCompareValue('CRM_Event_DAO_Event',
6ae19242 263 $event['id'],
6a488035
TO
264 'loc_block_id',
265 'id',
266 $locBlockId,
267 'Checking database for the record.'
268 );
9099cab3
CW
269 $locElementIds = [];
270 $locParams = ['id' => $locBlockId];
6a488035
TO
271 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock',
272 $locParams,
273 $locElementIds
274 );
275
276 //Now check DB for location elements.
9099cab3 277 $searchParams = [
6b409353 278 'id' => $locElementIds['address_id'] ?? NULL,
6a488035
TO
279 'location_type_id' => 1,
280 'is_primary' => 1,
9099cab3
CW
281 ];
282 $compareParams = [
6a488035
TO
283 'street_address' => 'Saint Helier St',
284 'supplemental_address_1' => 'Hallmark Ct',
285 'supplemental_address_2' => 'Jersey Village',
207f62c6 286 'supplemental_address_3' => 'My Town',
6a488035
TO
287 'city' => 'Newark',
288 'postal_code' => '01903',
289 'country_id' => 1228,
290 'state_province_id' => 1029,
291 'geo_code_1' => '18.219023',
292 'geo_code_2' => '-105.00973',
9099cab3 293 ];
6a488035
TO
294 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
295
9099cab3 296 $searchParams = [
6b409353 297 'id' => $locElementIds['email_id'] ?? NULL,
6a488035
TO
298 'location_type_id' => 1,
299 'is_primary' => 1,
9099cab3
CW
300 ];
301 $compareParams = ['email' => 'john.smith@example.org'];
6a488035
TO
302 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
303
9099cab3 304 $searchParams = [
6b409353 305 'id' => $locElementIds['phone_id'] ?? NULL,
6a488035
TO
306 'location_type_id' => 1,
307 'is_primary' => 1,
308 'phone_type_id' => 1,
9099cab3
CW
309 ];
310 $compareParams = ['phone' => '303443689'];
6a488035
TO
311 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
312
9099cab3 313 $searchParams = [
6b409353 314 'id' => $locElementIds['phone_2_id'] ?? NULL,
6a488035
TO
315 'location_type_id' => 1,
316 'phone_type_id' => 2,
9099cab3
CW
317 ];
318 $compareParams = ['phone' => '9833910234'];
6a488035
TO
319 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
320
9099cab3 321 $searchParams = [
6b409353 322 'id' => $locElementIds['im_id'] ?? NULL,
6a488035
TO
323 'location_type_id' => 1,
324 'is_primary' => 1,
9099cab3
CW
325 ];
326 $compareParams = [
6a488035
TO
327 'name' => 'jane.doe',
328 'provider_id' => 1,
9099cab3 329 ];
6a488035
TO
330 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
331
6ae19242 332 // Cleanup.
6a488035 333 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
6ae19242 334 $this->eventDelete($event['id']);
93ac19cd 335 $this->contactDelete($this->_contactId);
6a488035
TO
336 }
337
6a488035 338 /**
100fef9d 339 * GetValues() method
6a488035
TO
340 * get the values of various location elements
341 */
00be9182 342 public function testLocBlockgetValues() {
6ae19242 343 $contactId = $this->individualCreate();
6a488035
TO
344
345 //create various element of location block
346 //like address, phone, email, openid, im.
9099cab3
CW
347 $params = [
348 'address' => [
349 '1' => [
6a488035
TO
350 'street_address' => 'Saint Helier St',
351 'supplemental_address_1' => 'Hallmark Ct',
352 'supplemental_address_2' => 'Jersey Village',
207f62c6 353 'supplemental_address_3' => 'My Town',
6a488035
TO
354 'city' => 'Newark',
355 'postal_code' => '01903',
356 'country_id' => 1228,
357 'state_province_id' => 1029,
358 'geo_code_1' => '18.219023',
359 'geo_code_2' => '-105.00973',
360 'is_primary' => 1,
361 'location_type_id' => 1,
9099cab3
CW
362 ],
363 ],
364 'email' => [
365 '1' => [
6a488035
TO
366 'email' => 'john.smith@example.org',
367 'is_primary' => 1,
368 'location_type_id' => 1,
9099cab3
CW
369 ],
370 ],
371 'phone' => [
372 '1' => [
6a488035
TO
373 'phone_type_id' => 1,
374 'phone' => '303443689',
375 'is_primary' => 1,
376 'location_type_id' => 1,
9099cab3
CW
377 ],
378 '2' => [
6a488035
TO
379 'phone_type_id' => 2,
380 'phone' => '9833910234',
381 'location_type_id' => 1,
9099cab3
CW
382 ],
383 ],
384 'openid' => [
385 '1' => [
6a488035
TO
386 'openid' => 'http://civicrm.org/',
387 'location_type_id' => 1,
388 'is_primary' => 1,
9099cab3
CW
389 ],
390 ],
391 'im' => [
392 '1' => [
6a488035
TO
393 'name' => 'jane.doe',
394 'provider_id' => 1,
395 'location_type_id' => 1,
396 'is_primary' => 1,
9099cab3
CW
397 ],
398 ],
399 ];
6a488035
TO
400
401 $params['contact_id'] = $contactId;
402
403 //create location elements.
404 CRM_Core_BAO_Location::create($params);
405
406 //get the values from DB
407 $values = CRM_Core_BAO_Location::getValues($params);
408
409 //Now check values of address
410 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
411 CRM_Utils_Array::value('1', $values['address'])
412 );
413
414 //Now check values of email
415 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
416 CRM_Utils_Array::value('1', $values['email'])
417 );
418
419 //Now check values of phone
420 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
421 CRM_Utils_Array::value('1', $values['phone'])
422 );
423
424 //Now check values of mobile
425 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
426 CRM_Utils_Array::value('2', $values['phone'])
427 );
428
429 //Now check values of openid
430 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
431 CRM_Utils_Array::value('1', $values['openid'])
432 );
433
434 //Now check values of im
435 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
436 CRM_Utils_Array::value('1', $values['im'])
437 );
93ac19cd 438 $this->contactDelete($contactId);
6a488035 439 }
96025800 440
6a488035 441}