Merge pull request #7680 from JMAConsulting/CRM-16259-9
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / LocationTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
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 {
00be9182 41 public function setUp() {
6a488035
TO
42 parent::setUp();
43
92915c55
TO
44 $this->quickCleanup(array(
45 'civicrm_contact',
46 'civicrm_address',
47 'civicrm_loc_block',
48 'civicrm_email',
49 'civicrm_phone',
af9b09df 50 'civicrm_im',
92915c55 51 ));
6a488035
TO
52 }
53
54 /**
55 * Tears down the fixture, for example, closes a network connection.
56 * This method is called after a test is executed.
6a488035 57 */
00be9182 58 public function tearDown() {
6a488035
TO
59 $tablesToTruncate = array(
60 'civicrm_contact',
61 'civicrm_openid',
62 'civicrm_loc_block',
63 );
64 $this->quickCleanup($tablesToTruncate);
65 }
66
00be9182 67 public function testCreateWithMissingParams() {
6a488035
TO
68 $contactId = Contact::createIndividual();
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 //cleanup DB by deleting the contact
82 Contact::delete($contactId);
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() {
6a488035
TO
91 $contactId = Contact::createIndividual();
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',
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 '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 = array('email' => 'john.smith@example.org');
174 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
175
176 $compareParams = array('openid' => 'http://civicrm.org/');
177 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
178
179 $compareParams = array(
180 'name' => 'jane.doe',
181 'provider_id' => 1,
182 );
183 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
184
185 $searchParams = array(
186 'contact_id' => $contactId,
187 'location_type_id' => 1,
188 'is_primary' => 1,
189 'phone_type_id' => 1,
190 );
191 $compareParams = array('phone' => '303443689');
192 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
193
194 $searchParams = array(
195 'contact_id' => $contactId,
196 'location_type_id' => 1,
197 'phone_type_id' => 2,
198 );
199 $compareParams = array('phone' => '9833910234');
200 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
201
202 //delete the location block
203 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
204
205 //cleanup DB by deleting the contact
206 Contact::delete($contactId);
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() {
6a488035
TO
215 $this->_contactId = Contact::createIndividual();
216 //create test event record.
217 $eventId = Event::create($this->_contactId);
218 $params = array(
219 'address' => array(
220 '1' => array(
221 'street_address' => 'Saint Helier St',
222 'supplemental_address_1' => 'Hallmark Ct',
223 'supplemental_address_2' => 'Jersey Village',
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
264 $params['entity_id'] = $eventId;
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(
275 'id' => $eventId,
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',
284 $eventId,
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',
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
6c6e6187 316 $searchParams = array(
92915c55 317 'id' => CRM_Utils_Array::value('email_id', $locElementIds),
6a488035
TO
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
6c6e6187 324 $searchParams = array(
92915c55 325 'id' => CRM_Utils_Array::value('phone_id', $locElementIds),
6a488035
TO
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
6c6e6187 333 $searchParams = array(
92915c55 334 'id' => CRM_Utils_Array::value('phone_2_id', $locElementIds),
6a488035
TO
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
6c6e6187 341 $searchParams = array(
92915c55 342 'id' => CRM_Utils_Array::value('im_id', $locElementIds),
6a488035
TO
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 //delete the location block
353 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
354
355 //cleanup DB by deleting the record.
356 Event::delete($eventId);
357 Contact::delete($this->_contactId);
358 }
359
360 /**
100fef9d 361 * DeleteLocBlock() method
6a488035
TO
362 * delete the location block
363 * created with various elements.
6a488035 364 */
00be9182 365 public function testDeleteLocBlock() {
6a488035
TO
366 $this->_contactId = Contact::createIndividual();
367 //create test event record.
368 $eventId = Event::create($this->_contactId);
369 $params['location'][1] = array(
370 'location_type_id' => 1,
371 'is_primary' => 1,
372 'address' => array(
373 'street_address' => 'Saint Helier St',
374 'supplemental_address_1' => 'Hallmark Ct',
375 'supplemental_address_2' => 'Jersey Village',
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 );
403 $params['entity_id'] = $eventId;
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(
413 'id' => $eventId,
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.
444 Event::delete($eventId);
445 Contact::delete($this->_contactId);
446
447 //Now check DB for Event
448 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
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() {
6a488035
TO
458 $contactId = Contact::createIndividual();
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',
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
553 //cleanup DB by deleting the contact
554 Contact::delete($contactId);
555 }
96025800 556
6a488035 557}