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
{
49 'name' => 'Location BAOs',
50 'description' => 'Test all Core_BAO_Location methods.',
51 'group' => 'CiviCRM BAO Tests',
58 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_address', 'civicrm_loc_block', 'civicrm_email', 'civicrm_phone', 'civicrm_im' ) );
62 * Tears down the fixture, for example, closes a network connection.
63 * This method is called after a test is executed.
68 $tablesToTruncate = array(
73 $this->quickCleanup($tablesToTruncate);
76 function testCreateWithMissingParams() {
77 $contactId = Contact
::createIndividual();
79 'contact_id' => $contactId,
80 'street_address' => 'Saint Helier St',
83 CRM_Core_BAO_Location
::create($params);
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.'
90 //cleanup DB by deleting the contact
91 Contact
::delete($contactId);
96 * create various elements of location block
97 * without civicrm_loc_block entry
99 function testCreateWithoutLocBlock() {
100 $contactId = Contact
::createIndividual();
102 //create various element of location block
103 //like address, phone, email, openid, im.
107 'street_address' => 'Saint Helier St',
108 'supplemental_address_1' => 'Hallmark Ct',
109 'supplemental_address_2' => 'Jersey Village',
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',
117 'location_type_id' => 1,
122 'email' => 'john.smith@example.org',
124 'location_type_id' => 1,
129 'phone_type_id' => 1,
130 'phone' => '303443689',
132 'location_type_id' => 1,
135 'phone_type_id' => 2,
136 'phone' => '9833910234',
137 'location_type_id' => 1,
142 'openid' => 'http://civicrm.org/',
143 'location_type_id' => 1,
149 'name' => 'jane.doe',
151 'location_type_id' => 1,
157 $params['contact_id'] = $contactId;
159 $location = CRM_Core_BAO_Location
::create($params);
161 $locBlockId = CRM_Utils_Array
::value('id', $location);
163 //Now check DB for contact
164 $searchParams = array(
165 'contact_id' => $contactId,
166 'location_type_id' => 1,
169 $compareParams = array(
170 'street_address' => 'Saint Helier St',
171 'supplemental_address_1' => 'Hallmark Ct',
172 'supplemental_address_2' => 'Jersey Village',
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',
180 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
182 $compareParams = array('email' => 'john.smith@example.org');
183 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
185 $compareParams = array('openid' => 'http://civicrm.org/');
186 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
188 $compareParams = array(
189 'name' => 'jane.doe',
192 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
194 $searchParams = array(
195 'contact_id' => $contactId,
196 'location_type_id' => 1,
198 'phone_type_id' => 1,
200 $compareParams = array('phone' => '303443689');
201 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
203 $searchParams = array(
204 'contact_id' => $contactId,
205 'location_type_id' => 1,
206 'phone_type_id' => 2,
208 $compareParams = array('phone' => '9833910234');
209 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
211 //delete the location block
212 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
214 //cleanup DB by deleting the contact
215 Contact
::delete($contactId);
220 * create various elements of location block
221 * with civicrm_loc_block
223 function testCreateWithLocBlock() {
224 $this->_contactId
= Contact
::createIndividual();
225 //create test event record.
226 $eventId = Event
::create($this->_contactId
);
230 'street_address' => 'Saint Helier St',
231 'supplemental_address_1' => 'Hallmark Ct',
232 'supplemental_address_2' => 'Jersey Village',
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',
240 'location_type_id' => 1,
245 'email' => 'john.smith@example.org',
247 'location_type_id' => 1,
252 'phone_type_id' => 1,
253 'phone' => '303443689',
255 'location_type_id' => 1,
258 'phone_type_id' => 2,
259 'phone' => '9833910234',
260 'location_type_id' => 1,
265 'name' => 'jane.doe',
267 'location_type_id' => 1,
273 $params['entity_id'] = $eventId;
274 $params['entity_table'] = 'civicrm_event';
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);
282 //update event record with location block id
283 $eventParams = array(
285 'loc_block_id' => $locBlockId,
288 CRM_Event_BAO_Event
::add($eventParams);
290 //Now check DB for location block
292 $this->assertDBCompareValue('CRM_Event_DAO_Event',
297 'Checking database for the record.'
299 $locElementIds = array();
300 $locParams = array('id' => $locBlockId);
301 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
306 //Now check DB for location elements.
307 $searchParams = array('id' => CRM_Utils_Array
::value('address_id', $locElementIds),
308 'location_type_id' => 1,
311 $compareParams = array(
312 'street_address' => 'Saint Helier St',
313 'supplemental_address_1' => 'Hallmark Ct',
314 'supplemental_address_2' => 'Jersey Village',
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',
322 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
324 $searchParams = array('id' => CRM_Utils_Array
::value('email_id', $locElementIds),
325 'location_type_id' => 1,
328 $compareParams = array('email' => 'john.smith@example.org');
329 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
332 $searchParams = array('id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
333 'location_type_id' => 1,
335 'phone_type_id' => 1,
337 $compareParams = array('phone' => '303443689');
338 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
340 $searchParams = array('id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
341 'location_type_id' => 1,
342 'phone_type_id' => 2,
344 $compareParams = array('phone' => '9833910234');
345 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
347 $searchParams = array('id' => CRM_Utils_Array
::value('im_id', $locElementIds),
348 'location_type_id' => 1,
351 $compareParams = array(
352 'name' => 'jane.doe',
355 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
357 //delete the location block
358 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
360 //cleanup DB by deleting the record.
361 Event
::delete($eventId);
362 Contact
::delete($this->_contactId
);
366 * DeleteLocBlock() method
367 * delete the location block
368 * created with various elements.
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,
379 'street_address' => 'Saint Helier St',
380 'supplemental_address_1' => 'Hallmark Ct',
381 'supplemental_address_2' => 'Jersey Village',
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',
390 '1' => array('email' => 'john.smith@example.org'),
394 'phone_type_id' => 1,
395 'phone' => '303443689',
398 'phone_type_id' => 2,
399 'phone' => '9833910234',
404 'name' => 'jane.doe',
409 $params['entity_id'] = $eventId;
410 $params['entity_table'] = 'civicrm_event';
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(
420 'loc_block_id' => $locBlockId,
422 CRM_Event_BAO_Event
::add($eventParams);
424 //delete the location block
425 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
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.'
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.'
436 //Now check DB for Phone
437 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
438 'Database check, Phone deleted successfully.'
440 //Now check DB for Mobile
441 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
442 'Database check, Mobile deleted successfully.'
444 //Now check DB for IM
445 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
446 'Database check, IM deleted successfully.'
449 //cleanup DB by deleting the record.
450 Event
::delete($eventId);
451 Contact
::delete($this->_contactId
);
453 //Now check DB for Event
454 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
455 'Database check, Event deleted successfully.'
461 * get the values of various location elements
463 function testLocBlockgetValues() {
464 $contactId = Contact
::createIndividual();
466 //create various element of location block
467 //like address, phone, email, openid, im.
471 'street_address' => 'Saint Helier St',
472 'supplemental_address_1' => 'Hallmark Ct',
473 'supplemental_address_2' => 'Jersey Village',
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',
481 'location_type_id' => 1,
486 'email' => 'john.smith@example.org',
488 'location_type_id' => 1,
493 'phone_type_id' => 1,
494 'phone' => '303443689',
496 'location_type_id' => 1,
499 'phone_type_id' => 2,
500 'phone' => '9833910234',
501 'location_type_id' => 1,
506 'openid' => 'http://civicrm.org/',
507 'location_type_id' => 1,
513 'name' => 'jane.doe',
515 'location_type_id' => 1,
521 $params['contact_id'] = $contactId;
523 //create location elements.
524 CRM_Core_BAO_Location
::create($params);
526 //get the values from DB
527 $values = CRM_Core_BAO_Location
::getValues($params);
529 //Now check values of address
530 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
531 CRM_Utils_Array
::value('1', $values['address'])
534 //Now check values of email
535 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
536 CRM_Utils_Array
::value('1', $values['email'])
539 //Now check values of phone
540 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
541 CRM_Utils_Array
::value('1', $values['phone'])
544 //Now check values of mobile
545 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
546 CRM_Utils_Array
::value('2', $values['phone'])
549 //Now check values of openid
550 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
551 CRM_Utils_Array
::value('1', $values['openid'])
554 //Now check values of im
555 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
556 CRM_Utils_Array
::value('1', $values['im'])
559 //cleanup DB by deleting the contact
560 Contact
::delete($contactId);