Merge pull request #2845 from elcapo/activity-contact-api
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / LocationTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 class CRM_Core_BAO_LocationTest extends CiviUnitTestCase {
40 function get_info() {
41 return array(
42 'name' => 'Location BAOs',
43 'description' => 'Test all Core_BAO_Location methods.',
44 'group' => 'CiviCRM BAO Tests',
45 );
46 }
47
48 function setUp() {
49 parent::setUp();
50
51 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_address', 'civicrm_loc_block', 'civicrm_email', 'civicrm_phone', 'civicrm_im' ) );
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 * @access protected
59 */
60 function tearDown() {
61 $tablesToTruncate = array(
62 'civicrm_contact',
63 'civicrm_openid',
64 'civicrm_loc_block',
65 );
66 $this->quickCleanup($tablesToTruncate);
67 }
68
69 function testCreateWithMissingParams() {
70 $contactId = Contact::createIndividual();
71 $params = array(
72 'contact_id' => $contactId,
73 'street_address' => 'Saint Helier St',
74 );
75
76 CRM_Core_BAO_Location::create($params);
77
78 //Now check DB for Address
79 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
80 'Database check, Address created successfully.'
81 );
82
83 //cleanup DB by deleting the contact
84 Contact::delete($contactId);
85 }
86
87 /**
88 * create() method
89 * create various elements of location block
90 * without civicrm_loc_block entry
91 */
92 function testCreateWithoutLocBlock() {
93 $contactId = Contact::createIndividual();
94
95 //create various element of location block
96 //like address, phone, email, openid, im.
97 $params = array(
98 'address' => array(
99 '1' => array(
100 'street_address' => 'Saint Helier St',
101 'supplemental_address_1' => 'Hallmark Ct',
102 'supplemental_address_2' => 'Jersey Village',
103 'city' => 'Newark',
104 'postal_code' => '01903',
105 'country_id' => 1228,
106 'state_province_id' => 1029,
107 'geo_code_1' => '18.219023',
108 'geo_code_2' => '-105.00973',
109 'is_primary' => 1,
110 'location_type_id' => 1,
111 ),
112 ),
113 'email' => array(
114 '1' => array(
115 'email' => 'john.smith@example.org',
116 'is_primary' => 1,
117 'location_type_id' => 1,
118 ),
119 ),
120 'phone' => array(
121 '1' => array(
122 'phone_type_id' => 1,
123 'phone' => '303443689',
124 'is_primary' => 1,
125 'location_type_id' => 1,
126 ),
127 '2' => array(
128 'phone_type_id' => 2,
129 'phone' => '9833910234',
130 'location_type_id' => 1,
131 ),
132 ),
133 'openid' => array(
134 '1' => array(
135 'openid' => 'http://civicrm.org/',
136 'location_type_id' => 1,
137 'is_primary' => 1,
138 ),
139 ),
140 'im' => array(
141 '1' => array(
142 'name' => 'jane.doe',
143 'provider_id' => 1,
144 'location_type_id' => 1,
145 'is_primary' => 1,
146 ),
147 ),
148 );
149
150 $params['contact_id'] = $contactId;
151
152 $location = CRM_Core_BAO_Location::create($params);
153
154 $locBlockId = CRM_Utils_Array::value('id', $location);
155
156 //Now check DB for contact
157 $searchParams = array(
158 'contact_id' => $contactId,
159 'location_type_id' => 1,
160 'is_primary' => 1,
161 );
162 $compareParams = array(
163 'street_address' => 'Saint Helier St',
164 'supplemental_address_1' => 'Hallmark Ct',
165 'supplemental_address_2' => 'Jersey Village',
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);
206
207 //cleanup DB by deleting the contact
208 Contact::delete($contactId);
209 }
210
211 /**
212 * create() method
213 * create various elements of location block
214 * with civicrm_loc_block
215 */
216 function testCreateWithLocBlock() {
217 $this->_contactId = Contact::createIndividual();
218 //create test event record.
219 $eventId = Event::create($this->_contactId);
220 $params = array(
221 'address' => array(
222 '1' => array(
223 'street_address' => 'Saint Helier St',
224 'supplemental_address_1' => 'Hallmark Ct',
225 'supplemental_address_2' => 'Jersey Village',
226 'city' => 'Newark',
227 'postal_code' => '01903',
228 'country_id' => 1228,
229 'state_province_id' => 1029,
230 'geo_code_1' => '18.219023',
231 'geo_code_2' => '-105.00973',
232 'is_primary' => 1,
233 'location_type_id' => 1,
234 ),
235 ),
236 'email' => array(
237 '1' => array(
238 'email' => 'john.smith@example.org',
239 'is_primary' => 1,
240 'location_type_id' => 1,
241 ),
242 ),
243 'phone' => array(
244 '1' => array(
245 'phone_type_id' => 1,
246 'phone' => '303443689',
247 'is_primary' => 1,
248 'location_type_id' => 1,
249 ),
250 '2' => array(
251 'phone_type_id' => 2,
252 'phone' => '9833910234',
253 'location_type_id' => 1,
254 ),
255 ),
256 'im' => array(
257 '1' => array(
258 'name' => 'jane.doe',
259 'provider_id' => 1,
260 'location_type_id' => 1,
261 'is_primary' => 1,
262 ),
263 ),
264 );
265
266 $params['entity_id'] = $eventId;
267 $params['entity_table'] = 'civicrm_event';
268
269 //create location block.
270 //with various element of location block
271 //like address, phone, email, im.
272 $location = CRM_Core_BAO_Location::create($params, NULL, TRUE);
273 $locBlockId = CRM_Utils_Array::value('id', $location);
274
275 //update event record with location block id
276 $eventParams = array(
277 'id' => $eventId,
278 'loc_block_id' => $locBlockId,
279 );
280
281 CRM_Event_BAO_Event::add($eventParams);
282
283 //Now check DB for location block
284
285 $this->assertDBCompareValue('CRM_Event_DAO_Event',
286 $eventId,
287 'loc_block_id',
288 'id',
289 $locBlockId,
290 'Checking database for the record.'
291 );
292 $locElementIds = array();
293 $locParams = array('id' => $locBlockId);
294 CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_LocBlock',
295 $locParams,
296 $locElementIds
297 );
298
299 //Now check DB for location elements.
300 $searchParams = array('id' => CRM_Utils_Array::value('address_id', $locElementIds),
301 'location_type_id' => 1,
302 'is_primary' => 1,
303 );
304 $compareParams = array(
305 'street_address' => 'Saint Helier St',
306 'supplemental_address_1' => 'Hallmark Ct',
307 'supplemental_address_2' => 'Jersey Village',
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
317 $searchParams = array('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
325 $searchParams = array('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('id' => CRM_Utils_Array::value('phone_2_id', $locElementIds),
334 'location_type_id' => 1,
335 'phone_type_id' => 2,
336 );
337 $compareParams = array('phone' => '9833910234');
338 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
339
340 $searchParams = array('id' => CRM_Utils_Array::value('im_id', $locElementIds),
341 'location_type_id' => 1,
342 'is_primary' => 1,
343 );
344 $compareParams = array(
345 'name' => 'jane.doe',
346 'provider_id' => 1,
347 );
348 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
349
350 //delete the location block
351 CRM_Core_BAO_Location::deleteLocBlock($locBlockId);
352
353 //cleanup DB by deleting the record.
354 Event::delete($eventId);
355 Contact::delete($this->_contactId);
356 }
357
358 /**
359 * deleteLocBlock() method
360 * delete the location block
361 * created with various elements.
362 *
363 */
364 function testDeleteLocBlock() {
365 $this->_contactId = Contact::createIndividual();
366 //create test event record.
367 $eventId = Event::create($this->_contactId);
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',
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'] = $eventId;
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' => $eventId,
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 Event::delete($eventId);
444 Contact::delete($this->_contactId);
445
446 //Now check DB for Event
447 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, '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 function testLocBlockgetValues() {
457 $contactId = Contact::createIndividual();
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 'city' => 'Newark',
468 'postal_code' => '01903',
469 'country_id' => 1228,
470 'state_province_id' => 1029,
471 'geo_code_1' => '18.219023',
472 'geo_code_2' => '-105.00973',
473 'is_primary' => 1,
474 'location_type_id' => 1,
475 ),
476 ),
477 'email' => array(
478 '1' => array(
479 'email' => 'john.smith@example.org',
480 'is_primary' => 1,
481 'location_type_id' => 1,
482 ),
483 ),
484 'phone' => array(
485 '1' => array(
486 'phone_type_id' => 1,
487 'phone' => '303443689',
488 'is_primary' => 1,
489 'location_type_id' => 1,
490 ),
491 '2' => array(
492 'phone_type_id' => 2,
493 'phone' => '9833910234',
494 'location_type_id' => 1,
495 ),
496 ),
497 'openid' => array(
498 '1' => array(
499 'openid' => 'http://civicrm.org/',
500 'location_type_id' => 1,
501 'is_primary' => 1,
502 ),
503 ),
504 'im' => array(
505 '1' => array(
506 'name' => 'jane.doe',
507 'provider_id' => 1,
508 'location_type_id' => 1,
509 'is_primary' => 1,
510 ),
511 ),
512 );
513
514 $params['contact_id'] = $contactId;
515
516 //create location elements.
517 CRM_Core_BAO_Location::create($params);
518
519 //get the values from DB
520 $values = CRM_Core_BAO_Location::getValues($params);
521
522 //Now check values of address
523 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']),
524 CRM_Utils_Array::value('1', $values['address'])
525 );
526
527 //Now check values of email
528 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']),
529 CRM_Utils_Array::value('1', $values['email'])
530 );
531
532 //Now check values of phone
533 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']),
534 CRM_Utils_Array::value('1', $values['phone'])
535 );
536
537 //Now check values of mobile
538 $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']),
539 CRM_Utils_Array::value('2', $values['phone'])
540 );
541
542 //Now check values of openid
543 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']),
544 CRM_Utils_Array::value('1', $values['openid'])
545 );
546
547 //Now check values of im
548 $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']),
549 CRM_Utils_Array::value('1', $values['im'])
550 );
551
552 //cleanup DB by deleting the contact
553 Contact::delete($contactId);
554 }
555 }
556