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