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