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