3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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
{
44 public function setUp() {
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.
55 public function tearDown() {
56 $tablesToTruncate = array(
61 $this->quickCleanup($tablesToTruncate);
64 public function testCreateWithMissingParams() {
65 $contactId = Contact
::createIndividual();
67 'contact_id' => $contactId,
68 'street_address' => 'Saint Helier St',
71 CRM_Core_BAO_Location
::create($params);
73 //Now check DB for Address
74 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
75 'Database check, Address created successfully.'
78 //cleanup DB by deleting the contact
79 Contact
::delete($contactId);
84 * create various elements of location block
85 * without civicrm_loc_block entry
87 public function testCreateWithoutLocBlock() {
88 $contactId = Contact
::createIndividual();
90 //create various element of location block
91 //like address, phone, email, openid, im.
95 'street_address' => 'Saint Helier St',
96 'supplemental_address_1' => 'Hallmark Ct',
97 'supplemental_address_2' => 'Jersey Village',
99 'postal_code' => '01903',
100 'country_id' => 1228,
101 'state_province_id' => 1029,
102 'geo_code_1' => '18.219023',
103 'geo_code_2' => '-105.00973',
105 'location_type_id' => 1,
110 'email' => 'john.smith@example.org',
112 'location_type_id' => 1,
117 'phone_type_id' => 1,
118 'phone' => '303443689',
120 'location_type_id' => 1,
123 'phone_type_id' => 2,
124 'phone' => '9833910234',
125 'location_type_id' => 1,
130 'openid' => 'http://civicrm.org/',
131 'location_type_id' => 1,
137 'name' => 'jane.doe',
139 'location_type_id' => 1,
145 $params['contact_id'] = $contactId;
147 $location = CRM_Core_BAO_Location
::create($params);
149 $locBlockId = CRM_Utils_Array
::value('id', $location);
151 //Now check DB for contact
152 $searchParams = array(
153 'contact_id' => $contactId,
154 'location_type_id' => 1,
157 $compareParams = array(
158 'street_address' => 'Saint Helier St',
159 'supplemental_address_1' => 'Hallmark Ct',
160 'supplemental_address_2' => 'Jersey Village',
162 'postal_code' => '01903',
163 'country_id' => 1228,
164 'state_province_id' => 1029,
165 'geo_code_1' => '18.219023',
166 'geo_code_2' => '-105.00973',
168 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
170 $compareParams = array('email' => 'john.smith@example.org');
171 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
173 $compareParams = array('openid' => 'http://civicrm.org/');
174 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
176 $compareParams = array(
177 'name' => 'jane.doe',
180 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
182 $searchParams = array(
183 'contact_id' => $contactId,
184 'location_type_id' => 1,
186 'phone_type_id' => 1,
188 $compareParams = array('phone' => '303443689');
189 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
191 $searchParams = array(
192 'contact_id' => $contactId,
193 'location_type_id' => 1,
194 'phone_type_id' => 2,
196 $compareParams = array('phone' => '9833910234');
197 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
199 //delete the location block
200 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
202 //cleanup DB by deleting the contact
203 Contact
::delete($contactId);
208 * create various elements of location block
209 * with civicrm_loc_block
211 public function testCreateWithLocBlock() {
212 $this->_contactId
= Contact
::createIndividual();
213 //create test event record.
214 $eventId = Event
::create($this->_contactId
);
218 'street_address' => 'Saint Helier St',
219 'supplemental_address_1' => 'Hallmark Ct',
220 'supplemental_address_2' => 'Jersey Village',
222 'postal_code' => '01903',
223 'country_id' => 1228,
224 'state_province_id' => 1029,
225 'geo_code_1' => '18.219023',
226 'geo_code_2' => '-105.00973',
228 'location_type_id' => 1,
233 'email' => 'john.smith@example.org',
235 'location_type_id' => 1,
240 'phone_type_id' => 1,
241 'phone' => '303443689',
243 'location_type_id' => 1,
246 'phone_type_id' => 2,
247 'phone' => '9833910234',
248 'location_type_id' => 1,
253 'name' => 'jane.doe',
255 'location_type_id' => 1,
261 $params['entity_id'] = $eventId;
262 $params['entity_table'] = 'civicrm_event';
264 //create location block.
265 //with various element of location block
266 //like address, phone, email, im.
267 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
268 $locBlockId = CRM_Utils_Array
::value('id', $location);
270 //update event record with location block id
271 $eventParams = array(
273 'loc_block_id' => $locBlockId,
276 CRM_Event_BAO_Event
::add($eventParams);
278 //Now check DB for location block
280 $this->assertDBCompareValue('CRM_Event_DAO_Event',
285 'Checking database for the record.'
287 $locElementIds = array();
288 $locParams = array('id' => $locBlockId);
289 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
294 //Now check DB for location elements.
295 $searchParams = array(
296 '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(
314 'id' => CRM_Utils_Array
::value('email_id', $locElementIds),
315 'location_type_id' => 1,
318 $compareParams = array('email' => 'john.smith@example.org');
319 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
321 $searchParams = array(
322 'id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
323 'location_type_id' => 1,
325 'phone_type_id' => 1,
327 $compareParams = array('phone' => '303443689');
328 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
330 $searchParams = array(
331 'id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
332 'location_type_id' => 1,
333 'phone_type_id' => 2,
335 $compareParams = array('phone' => '9833910234');
336 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
338 $searchParams = array(
339 'id' => CRM_Utils_Array
::value('im_id', $locElementIds),
340 'location_type_id' => 1,
343 $compareParams = array(
344 'name' => 'jane.doe',
347 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
349 //delete the location block
350 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
352 //cleanup DB by deleting the record.
353 Event
::delete($eventId);
354 Contact
::delete($this->_contactId
);
358 * DeleteLocBlock() method
359 * delete the location block
360 * created with various elements.
363 public function testDeleteLocBlock() {
364 $this->_contactId
= Contact
::createIndividual();
365 //create test event record.
366 $eventId = Event
::create($this->_contactId
);
367 $params['location'][1] = array(
368 'location_type_id' => 1,
371 'street_address' => 'Saint Helier St',
372 'supplemental_address_1' => 'Hallmark Ct',
373 'supplemental_address_2' => 'Jersey Village',
375 'postal_code' => '01903',
376 'country_id' => 1228,
377 'state_province_id' => 1029,
378 'geo_code_1' => '18.219023',
379 'geo_code_2' => '-105.00973',
382 '1' => array('email' => 'john.smith@example.org'),
386 'phone_type_id' => 1,
387 'phone' => '303443689',
390 'phone_type_id' => 2,
391 'phone' => '9833910234',
396 'name' => 'jane.doe',
401 $params['entity_id'] = $eventId;
402 $params['entity_table'] = 'civicrm_event';
404 //create location block.
405 //with various elements
406 //like address, phone, email, im.
407 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
408 $locBlockId = CRM_Utils_Array
::value('id', $location);
409 //update event record with location block id
410 $eventParams = array(
412 'loc_block_id' => $locBlockId,
414 CRM_Event_BAO_Event
::add($eventParams);
416 //delete the location block
417 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
419 //Now check DB for location elements.
420 //Now check DB for Address
421 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
422 'Database check, Address deleted successfully.'
424 //Now check DB for Email
425 $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email',
426 'Database check, Email deleted successfully.'
428 //Now check DB for Phone
429 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
430 'Database check, Phone deleted successfully.'
432 //Now check DB for Mobile
433 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
434 'Database check, Mobile deleted successfully.'
436 //Now check DB for IM
437 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
438 'Database check, IM deleted successfully.'
441 //cleanup DB by deleting the record.
442 Event
::delete($eventId);
443 Contact
::delete($this->_contactId
);
445 //Now check DB for Event
446 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
447 'Database check, Event deleted successfully.'
453 * get the values of various location elements
455 public function testLocBlockgetValues() {
456 $contactId = Contact
::createIndividual();
458 //create various element of location block
459 //like address, phone, email, openid, im.
463 'street_address' => 'Saint Helier St',
464 'supplemental_address_1' => 'Hallmark Ct',
465 'supplemental_address_2' => 'Jersey Village',
467 'postal_code' => '01903',
468 'country_id' => 1228,
469 'state_province_id' => 1029,
470 'geo_code_1' => '18.219023',
471 'geo_code_2' => '-105.00973',
473 'location_type_id' => 1,
478 'email' => 'john.smith@example.org',
480 'location_type_id' => 1,
485 'phone_type_id' => 1,
486 'phone' => '303443689',
488 'location_type_id' => 1,
491 'phone_type_id' => 2,
492 'phone' => '9833910234',
493 'location_type_id' => 1,
498 'openid' => 'http://civicrm.org/',
499 'location_type_id' => 1,
505 'name' => 'jane.doe',
507 'location_type_id' => 1,
513 $params['contact_id'] = $contactId;
515 //create location elements.
516 CRM_Core_BAO_Location
::create($params);
518 //get the values from DB
519 $values = CRM_Core_BAO_Location
::getValues($params);
521 //Now check values of address
522 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
523 CRM_Utils_Array
::value('1', $values['address'])
526 //Now check values of email
527 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
528 CRM_Utils_Array
::value('1', $values['email'])
531 //Now check values of phone
532 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
533 CRM_Utils_Array
::value('1', $values['phone'])
536 //Now check values of mobile
537 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
538 CRM_Utils_Array
::value('2', $values['phone'])
541 //Now check values of openid
542 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
543 CRM_Utils_Array
::value('1', $values['openid'])
546 //Now check values of im
547 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
548 CRM_Utils_Array
::value('1', $values['im'])
551 //cleanup DB by deleting the contact
552 Contact
::delete($contactId);