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