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