Update Unit test styling to cover the future coder version
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / LocationTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
2fe49090 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id$
33 *
34 */
35
4cbe18b8
EM
36/**
37 * Class CRM_Core_BAO_LocationTest
acb109b7 38 * @group headless
4cbe18b8 39 */
6a488035 40class CRM_Core_BAO_LocationTest extends CiviUnitTestCase {
39b959db 41
00be9182 42 public function setUp() {
6a488035
TO
43 parent::setUp();
44
92915c55 45 $this->quickCleanup(array(
39b959db
SL
46 'civicrm_contact',
47 'civicrm_address',
48 'civicrm_loc_block',
49 'civicrm_email',
50 'civicrm_phone',
51 'civicrm_im',
52 ));
6a488035
TO
53 }
54
55 /**
56 * Tears down the fixture, for example, closes a network connection.
57 * This method is called after a test is executed.
6a488035 58 */
00be9182 59 public function tearDown() {
6a488035
TO
60 $tablesToTruncate = array(
61 'civicrm_contact',
62 'civicrm_openid',
63 'civicrm_loc_block',
64 );
65 $this->quickCleanup($tablesToTruncate);
66 }
67
00be9182 68 public function testCreateWithMissingParams() {
f2040bc6 69 $contactId = $this->individualCreate();
6a488035
TO
70 $params = array(
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
93ac19cd 82 $this->contactDelete($contactId);
6a488035
TO
83 }
84
85 /**
100fef9d 86 * Create() method
6a488035
TO
87 * create various elements of location block
88 * without civicrm_loc_block entry
89 */
00be9182 90 public function testCreateWithoutLocBlock() {
f2040bc6 91 $contactId = $this->individualCreate();
6a488035
TO
92
93 //create various element of location block
94 //like address, phone, email, openid, im.
95 $params = array(
96 'address' => array(
97 '1' => array(
98 'street_address' => 'Saint Helier St',
99 'supplemental_address_1' => 'Hallmark Ct',
100 'supplemental_address_2' => 'Jersey Village',
207f62c6 101 'supplemental_address_3' => 'My Town',
6a488035
TO
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' => array(
113 '1' => array(
114 'email' => 'john.smith@example.org',
115 'is_primary' => 1,
116 'location_type_id' => 1,
117 ),
118 ),
119 'phone' => array(
120 '1' => array(
121 'phone_type_id' => 1,
122 'phone' => '303443689',
123 'is_primary' => 1,
124 'location_type_id' => 1,
125 ),
126 '2' => array(
127 'phone_type_id' => 2,
128 'phone' => '9833910234',
129 'location_type_id' => 1,
130 ),
131 ),
132 'openid' => array(
133 '1' => array(
134 'openid' => 'http://civicrm.org/',
135 'location_type_id' => 1,
136 'is_primary' => 1,
137 ),
138 ),
139 'im' => array(
140 '1' => array(
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 $location = CRM_Core_BAO_Location::create($params);
152
153 $locBlockId = CRM_Utils_Array::value('id', $location);
154
155 //Now check DB for contact
156 $searchParams = array(
157 'contact_id' => $contactId,
158 'location_type_id' => 1,
159 'is_primary' => 1,
160 );
161 $compareParams = array(
162 'street_address' => 'Saint Helier St',
163 'supplemental_address_1' => 'Hallmark Ct',
164 'supplemental_address_2' => 'Jersey Village',
207f62c6 165 'supplemental_address_3' => 'My Town',
6a488035
TO
166 'city' => 'Newark',
167 'postal_code' => '01903',
168 'country_id' => 1228,
169 'state_province_id' => 1029,
170 'geo_code_1' => '18.219023',
171 'geo_code_2' => '-105.00973',
172 );
173 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
174
175 $compareParams = array('email' => 'john.smith@example.org');
176 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
177
178 $compareParams = array('openid' => 'http://civicrm.org/');
179 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
180
181 $compareParams = array(
182 'name' => 'jane.doe',
183 'provider_id' => 1,
184 );
185 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
186
187 $searchParams = array(
188 'contact_id' => $contactId,
189 'location_type_id' => 1,
190 'is_primary' => 1,
191 'phone_type_id' => 1,
192 );
193 $compareParams = array('phone' => '303443689');
194 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
195
196 $searchParams = array(
197 'contact_id' => $contactId,
198 'location_type_id' => 1,
199 'phone_type_id' => 2,
200 );
201 $compareParams = array('phone' => '9833910234');
202 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
203
204 //delete the location block
205 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
93ac19cd 206 $this->contactDelete($contactId);
6a488035
TO
207 }
208
209 /**
100fef9d 210 * Create() method
6a488035
TO
211 * create various elements of location block
212 * with civicrm_loc_block
213 */
00be9182 214 public function testCreateWithLocBlock() {
6ae19242 215 $this->_contactId = $this->individualCreate();
216 $event = $this->eventCreate();
6a488035
TO
217 $params = array(
218 'address' => array(
219 '1' => array(
220 'street_address' => 'Saint Helier St',
221 'supplemental_address_1' => 'Hallmark Ct',
222 'supplemental_address_2' => 'Jersey Village',
207f62c6 223 'supplemental_address_3' => 'My Town',
6a488035
TO
224 'city' => 'Newark',
225 'postal_code' => '01903',
226 'country_id' => 1228,
227 'state_province_id' => 1029,
228 'geo_code_1' => '18.219023',
229 'geo_code_2' => '-105.00973',
230 'is_primary' => 1,
231 'location_type_id' => 1,
232 ),
233 ),
234 'email' => array(
235 '1' => array(
236 'email' => 'john.smith@example.org',
237 'is_primary' => 1,
238 'location_type_id' => 1,
239 ),
240 ),
241 'phone' => array(
242 '1' => array(
243 'phone_type_id' => 1,
244 'phone' => '303443689',
245 'is_primary' => 1,
246 'location_type_id' => 1,
247 ),
248 '2' => array(
249 'phone_type_id' => 2,
250 'phone' => '9833910234',
251 'location_type_id' => 1,
252 ),
253 ),
254 'im' => array(
255 '1' => array(
256 'name' => 'jane.doe',
257 'provider_id' => 1,
258 'location_type_id' => 1,
259 'is_primary' => 1,
260 ),
261 ),
262 );
263
6ae19242 264 $params['entity_id'] = $event['id'];
6a488035
TO
265 $params['entity_table'] = 'civicrm_event';
266
267 //create location block.
268 //with various element of location block
269 //like address, phone, email, im.
270 $location = CRM_Core_BAO_Location::create($params, NULL, TRUE);
271 $locBlockId = CRM_Utils_Array::value('id', $location);
272
273 //update event record with location block id
274 $eventParams = array(
6ae19242 275 'id' => $event['id'],
6a488035
TO
276 'loc_block_id' => $locBlockId,
277 );
278
279 CRM_Event_BAO_Event::add($eventParams);
280
281 //Now check DB for location block
282
283 $this->assertDBCompareValue('CRM_Event_DAO_Event',
6ae19242 284 $event['id'],
6a488035
TO
285 'loc_block_id',
286 'id',
287 $locBlockId,
288 'Checking database for the record.'
289 );
290 $locElementIds = array();
291 $locParams = array('id' => $locBlockId);
292 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock',
293 $locParams,
294 $locElementIds
295 );
296
297 //Now check DB for location elements.
6c6e6187 298 $searchParams = array(
92915c55 299 'id' => CRM_Utils_Array::value('address_id', $locElementIds),
6a488035
TO
300 'location_type_id' => 1,
301 'is_primary' => 1,
302 );
303 $compareParams = array(
304 'street_address' => 'Saint Helier St',
305 'supplemental_address_1' => 'Hallmark Ct',
306 'supplemental_address_2' => 'Jersey Village',
207f62c6 307 'supplemental_address_3' => 'My Town',
6a488035
TO
308 'city' => 'Newark',
309 'postal_code' => '01903',
310 'country_id' => 1228,
311 'state_province_id' => 1029,
312 'geo_code_1' => '18.219023',
313 'geo_code_2' => '-105.00973',
314 );
315 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
316
6c6e6187 317 $searchParams = array(
92915c55 318 'id' => CRM_Utils_Array::value('email_id', $locElementIds),
6a488035
TO
319 'location_type_id' => 1,
320 'is_primary' => 1,
321 );
322 $compareParams = array('email' => 'john.smith@example.org');
323 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
324
6c6e6187 325 $searchParams = array(
92915c55 326 'id' => CRM_Utils_Array::value('phone_id', $locElementIds),
6a488035
TO
327 'location_type_id' => 1,
328 'is_primary' => 1,
329 'phone_type_id' => 1,
330 );
331 $compareParams = array('phone' => '303443689');
332 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
333
6c6e6187 334 $searchParams = array(
92915c55 335 'id' => CRM_Utils_Array::value('phone_2_id', $locElementIds),
6a488035
TO
336 'location_type_id' => 1,
337 'phone_type_id' => 2,
338 );
339 $compareParams = array('phone' => '9833910234');
340 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
341
6c6e6187 342 $searchParams = array(
92915c55 343 'id' => CRM_Utils_Array::value('im_id', $locElementIds),
6a488035
TO
344 'location_type_id' => 1,
345 'is_primary' => 1,
346 );
347 $compareParams = array(
348 'name' => 'jane.doe',
349 'provider_id' => 1,
350 );
351 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
352
6ae19242 353 // Cleanup.
6a488035 354 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
6ae19242 355 $this->eventDelete($event['id']);
93ac19cd 356 $this->contactDelete($this->_contactId);
6a488035
TO
357 }
358
359 /**
100fef9d 360 * DeleteLocBlock() method
6a488035
TO
361 * delete the location block
362 * created with various elements.
6a488035 363 */
00be9182 364 public function testDeleteLocBlock() {
6ae19242 365 $this->_contactId = $this->individualCreate();
6a488035 366 //create test event record.
6ae19242 367 $event = $this->eventCreate();
6a488035
TO
368 $params['location'][1] = array(
369 'location_type_id' => 1,
370 'is_primary' => 1,
371 'address' => array(
372 'street_address' => 'Saint Helier St',
373 'supplemental_address_1' => 'Hallmark Ct',
374 'supplemental_address_2' => 'Jersey Village',
207f62c6 375 'supplemental_address_3' => 'My Town',
6a488035
TO
376 'city' => 'Newark',
377 'postal_code' => '01903',
378 'country_id' => 1228,
379 'state_province_id' => 1029,
380 'geo_code_1' => '18.219023',
381 'geo_code_2' => '-105.00973',
382 ),
383 'email' => array(
384 '1' => array('email' => 'john.smith@example.org'),
385 ),
386 'phone' => array(
387 '1' => array(
388 'phone_type_id' => 1,
389 'phone' => '303443689',
390 ),
391 '2' => array(
392 'phone_type_id' => 2,
393 'phone' => '9833910234',
394 ),
395 ),
396 'im' => array(
397 '1' => array(
398 'name' => 'jane.doe',
399 'provider_id' => 1,
400 ),
401 ),
402 );
6ae19242 403 $params['entity_id'] = $event['id'];
6a488035
TO
404 $params['entity_table'] = 'civicrm_event';
405
406 //create location block.
407 //with various elements
408 //like address, phone, email, im.
409 $location = CRM_Core_BAO_Location::create($params, NULL, TRUE);
410 $locBlockId = CRM_Utils_Array::value('id', $location);
411 //update event record with location block id
412 $eventParams = array(
6ae19242 413 'id' => $event['id'],
6a488035
TO
414 'loc_block_id' => $locBlockId,
415 );
416 CRM_Event_BAO_Event::add($eventParams);
417
418 //delete the location block
419 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
420
421 //Now check DB for location elements.
422 //Now check DB for Address
423 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
424 'Database check, Address deleted successfully.'
425 );
426 //Now check DB for Email
427 $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email',
428 'Database check, Email deleted successfully.'
429 );
430 //Now check DB for Phone
431 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
432 'Database check, Phone deleted successfully.'
433 );
434 //Now check DB for Mobile
435 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
436 'Database check, Mobile deleted successfully.'
437 );
438 //Now check DB for IM
439 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
440 'Database check, IM deleted successfully.'
441 );
442
443 //cleanup DB by deleting the record.
6ae19242 444 $this->eventDelete($event['id']);
93ac19cd 445 $this->contactDelete($this->_contactId);
6a488035
TO
446
447 //Now check DB for Event
6ae19242 448 $this->assertDBNull('CRM_Event_DAO_Event', $event['id'], 'id', 'id',
6a488035
TO
449 'Database check, Event deleted successfully.'
450 );
451 }
452
453 /**
100fef9d 454 * GetValues() method
6a488035
TO
455 * get the values of various location elements
456 */
00be9182 457 public function testLocBlockgetValues() {
6ae19242 458 $contactId = $this->individualCreate();
6a488035
TO
459
460 //create various element of location block
461 //like address, phone, email, openid, im.
462 $params = array(
463 'address' => array(
464 '1' => array(
465 'street_address' => 'Saint Helier St',
466 'supplemental_address_1' => 'Hallmark Ct',
467 'supplemental_address_2' => 'Jersey Village',
207f62c6 468 'supplemental_address_3' => 'My Town',
6a488035
TO
469 'city' => 'Newark',
470 'postal_code' => '01903',
471 'country_id' => 1228,
472 'state_province_id' => 1029,
473 'geo_code_1' => '18.219023',
474 'geo_code_2' => '-105.00973',
475 'is_primary' => 1,
476 'location_type_id' => 1,
477 ),
478 ),
479 'email' => array(
480 '1' => array(
481 'email' => 'john.smith@example.org',
482 'is_primary' => 1,
483 'location_type_id' => 1,
484 ),
485 ),
486 'phone' => array(
487 '1' => array(
488 'phone_type_id' => 1,
489 'phone' => '303443689',
490 'is_primary' => 1,
491 'location_type_id' => 1,
492 ),
493 '2' => array(
494 'phone_type_id' => 2,
495 'phone' => '9833910234',
496 'location_type_id' => 1,
497 ),
498 ),
499 'openid' => array(
500 '1' => array(
501 'openid' => 'http://civicrm.org/',
502 'location_type_id' => 1,
503 'is_primary' => 1,
504 ),
505 ),
506 'im' => array(
507 '1' => array(
508 'name' => 'jane.doe',
509 'provider_id' => 1,
510 'location_type_id' => 1,
511 'is_primary' => 1,
512 ),
513 ),
514 );
515
516 $params['contact_id'] = $contactId;
517
518 //create location elements.
519 CRM_Core_BAO_Location::create($params);
520
521 //get the values from DB
522 $values = CRM_Core_BAO_Location::getValues($params);
523
524 //Now check values of address
525 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
526 CRM_Utils_Array::value('1', $values['address'])
527 );
528
529 //Now check values of email
530 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
531 CRM_Utils_Array::value('1', $values['email'])
532 );
533
534 //Now check values of phone
535 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
536 CRM_Utils_Array::value('1', $values['phone'])
537 );
538
539 //Now check values of mobile
540 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
541 CRM_Utils_Array::value('2', $values['phone'])
542 );
543
544 //Now check values of openid
545 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
546 CRM_Utils_Array::value('1', $values['openid'])
547 );
548
549 //Now check values of im
550 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
551 CRM_Utils_Array::value('1', $values['im'])
552 );
93ac19cd 553 $this->contactDelete($contactId);
6a488035 554 }
96025800 555
6a488035 556}