3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2014
36 require_once 'CiviTest/CiviUnitTestCase.php';
37 require_once 'CiviTest/Contact.php';
38 require_once 'CiviTest/Event.php';
41 * Class CRM_Core_BAO_LocationTest
43 class CRM_Core_BAO_LocationTest
extends CiviUnitTestCase
{
47 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_address', 'civicrm_loc_block', 'civicrm_email', 'civicrm_phone', 'civicrm_im' ) );
51 * Tears down the fixture, for example, closes a network connection.
52 * This method is called after a test is executed.
57 $tablesToTruncate = array(
62 $this->quickCleanup($tablesToTruncate);
65 function testCreateWithMissingParams() {
66 $contactId = Contact
::createIndividual();
68 'contact_id' => $contactId,
69 'street_address' => 'Saint Helier St',
72 CRM_Core_BAO_Location
::create($params);
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.'
79 //cleanup DB by deleting the contact
80 Contact
::delete($contactId);
85 * create various elements of location block
86 * without civicrm_loc_block entry
88 function testCreateWithoutLocBlock() {
89 $contactId = Contact
::createIndividual();
91 //create various element of location block
92 //like address, phone, email, openid, im.
96 'street_address' => 'Saint Helier St',
97 'supplemental_address_1' => 'Hallmark Ct',
98 'supplemental_address_2' => 'Jersey Village',
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',
106 'location_type_id' => 1,
111 'email' => 'john.smith@example.org',
113 'location_type_id' => 1,
118 'phone_type_id' => 1,
119 'phone' => '303443689',
121 'location_type_id' => 1,
124 'phone_type_id' => 2,
125 'phone' => '9833910234',
126 'location_type_id' => 1,
131 'openid' => 'http://civicrm.org/',
132 'location_type_id' => 1,
138 'name' => 'jane.doe',
140 'location_type_id' => 1,
146 $params['contact_id'] = $contactId;
148 $location = CRM_Core_BAO_Location
::create($params);
150 $locBlockId = CRM_Utils_Array
::value('id', $location);
152 //Now check DB for contact
153 $searchParams = array(
154 'contact_id' => $contactId,
155 'location_type_id' => 1,
158 $compareParams = array(
159 'street_address' => 'Saint Helier St',
160 'supplemental_address_1' => 'Hallmark Ct',
161 'supplemental_address_2' => 'Jersey Village',
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',
169 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
171 $compareParams = array('email' => 'john.smith@example.org');
172 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
174 $compareParams = array('openid' => 'http://civicrm.org/');
175 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
177 $compareParams = array(
178 'name' => 'jane.doe',
181 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
183 $searchParams = array(
184 'contact_id' => $contactId,
185 'location_type_id' => 1,
187 'phone_type_id' => 1,
189 $compareParams = array('phone' => '303443689');
190 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
192 $searchParams = array(
193 'contact_id' => $contactId,
194 'location_type_id' => 1,
195 'phone_type_id' => 2,
197 $compareParams = array('phone' => '9833910234');
198 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
200 //delete the location block
201 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
203 //cleanup DB by deleting the contact
204 Contact
::delete($contactId);
209 * create various elements of location block
210 * with civicrm_loc_block
212 function testCreateWithLocBlock() {
213 $this->_contactId
= Contact
::createIndividual();
214 //create test event record.
215 $eventId = Event
::create($this->_contactId
);
219 'street_address' => 'Saint Helier St',
220 'supplemental_address_1' => 'Hallmark Ct',
221 'supplemental_address_2' => 'Jersey Village',
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',
229 'location_type_id' => 1,
234 'email' => 'john.smith@example.org',
236 'location_type_id' => 1,
241 'phone_type_id' => 1,
242 'phone' => '303443689',
244 'location_type_id' => 1,
247 'phone_type_id' => 2,
248 'phone' => '9833910234',
249 'location_type_id' => 1,
254 'name' => 'jane.doe',
256 'location_type_id' => 1,
262 $params['entity_id'] = $eventId;
263 $params['entity_table'] = 'civicrm_event';
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);
271 //update event record with location block id
272 $eventParams = array(
274 'loc_block_id' => $locBlockId,
277 CRM_Event_BAO_Event
::add($eventParams);
279 //Now check DB for location block
281 $this->assertDBCompareValue('CRM_Event_DAO_Event',
286 'Checking database for the record.'
288 $locElementIds = array();
289 $locParams = array('id' => $locBlockId);
290 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
295 //Now check DB for location elements.
296 $searchParams = array('id' => CRM_Utils_Array
::value('address_id', $locElementIds),
297 'location_type_id' => 1,
300 $compareParams = array(
301 'street_address' => 'Saint Helier St',
302 'supplemental_address_1' => 'Hallmark Ct',
303 'supplemental_address_2' => 'Jersey Village',
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',
311 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
313 $searchParams = array('id' => CRM_Utils_Array
::value('email_id', $locElementIds),
314 'location_type_id' => 1,
317 $compareParams = array('email' => 'john.smith@example.org');
318 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
321 $searchParams = array('id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
322 'location_type_id' => 1,
324 'phone_type_id' => 1,
326 $compareParams = array('phone' => '303443689');
327 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
329 $searchParams = array('id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
330 'location_type_id' => 1,
331 'phone_type_id' => 2,
333 $compareParams = array('phone' => '9833910234');
334 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
336 $searchParams = array('id' => CRM_Utils_Array
::value('im_id', $locElementIds),
337 'location_type_id' => 1,
340 $compareParams = array(
341 'name' => 'jane.doe',
344 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
346 //delete the location block
347 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
349 //cleanup DB by deleting the record.
350 Event
::delete($eventId);
351 Contact
::delete($this->_contactId
);
355 * DeleteLocBlock() method
356 * delete the location block
357 * created with various elements.
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,
368 'street_address' => 'Saint Helier St',
369 'supplemental_address_1' => 'Hallmark Ct',
370 'supplemental_address_2' => 'Jersey Village',
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',
379 '1' => array('email' => 'john.smith@example.org'),
383 'phone_type_id' => 1,
384 'phone' => '303443689',
387 'phone_type_id' => 2,
388 'phone' => '9833910234',
393 'name' => 'jane.doe',
398 $params['entity_id'] = $eventId;
399 $params['entity_table'] = 'civicrm_event';
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(
409 'loc_block_id' => $locBlockId,
411 CRM_Event_BAO_Event
::add($eventParams);
413 //delete the location block
414 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
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.'
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.'
425 //Now check DB for Phone
426 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
427 'Database check, Phone deleted successfully.'
429 //Now check DB for Mobile
430 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
431 'Database check, Mobile deleted successfully.'
433 //Now check DB for IM
434 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
435 'Database check, IM deleted successfully.'
438 //cleanup DB by deleting the record.
439 Event
::delete($eventId);
440 Contact
::delete($this->_contactId
);
442 //Now check DB for Event
443 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
444 'Database check, Event deleted successfully.'
450 * get the values of various location elements
452 function testLocBlockgetValues() {
453 $contactId = Contact
::createIndividual();
455 //create various element of location block
456 //like address, phone, email, openid, im.
460 'street_address' => 'Saint Helier St',
461 'supplemental_address_1' => 'Hallmark Ct',
462 'supplemental_address_2' => 'Jersey Village',
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',
470 'location_type_id' => 1,
475 'email' => 'john.smith@example.org',
477 'location_type_id' => 1,
482 'phone_type_id' => 1,
483 'phone' => '303443689',
485 'location_type_id' => 1,
488 'phone_type_id' => 2,
489 'phone' => '9833910234',
490 'location_type_id' => 1,
495 'openid' => 'http://civicrm.org/',
496 'location_type_id' => 1,
502 'name' => 'jane.doe',
504 'location_type_id' => 1,
510 $params['contact_id'] = $contactId;
512 //create location elements.
513 CRM_Core_BAO_Location
::create($params);
515 //get the values from DB
516 $values = CRM_Core_BAO_Location
::getValues($params);
518 //Now check values of address
519 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
520 CRM_Utils_Array
::value('1', $values['address'])
523 //Now check values of email
524 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
525 CRM_Utils_Array
::value('1', $values['email'])
528 //Now check values of phone
529 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
530 CRM_Utils_Array
::value('1', $values['phone'])
533 //Now check values of mobile
534 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
535 CRM_Utils_Array
::value('2', $values['phone'])
538 //Now check values of openid
539 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
540 CRM_Utils_Array
::value('1', $values['openid'])
543 //Now check values of im
544 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
545 CRM_Utils_Array
::value('1', $values['im'])
548 //cleanup DB by deleting the contact
549 Contact
::delete($contactId);