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('id' => CRM_Utils_Array
::value('address_id', $locElementIds),
296 'location_type_id' => 1,
299 $compareParams = array(
300 'street_address' => 'Saint Helier St',
301 'supplemental_address_1' => 'Hallmark Ct',
302 'supplemental_address_2' => 'Jersey Village',
304 'postal_code' => '01903',
305 'country_id' => 1228,
306 'state_province_id' => 1029,
307 'geo_code_1' => '18.219023',
308 'geo_code_2' => '-105.00973',
310 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
312 $searchParams = array('id' => CRM_Utils_Array
::value('email_id', $locElementIds),
313 'location_type_id' => 1,
316 $compareParams = array('email' => 'john.smith@example.org');
317 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
320 $searchParams = array('id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
321 'location_type_id' => 1,
323 'phone_type_id' => 1,
325 $compareParams = array('phone' => '303443689');
326 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
328 $searchParams = array('id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
329 'location_type_id' => 1,
330 'phone_type_id' => 2,
332 $compareParams = array('phone' => '9833910234');
333 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
335 $searchParams = array('id' => CRM_Utils_Array
::value('im_id', $locElementIds),
336 'location_type_id' => 1,
339 $compareParams = array(
340 'name' => 'jane.doe',
343 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
345 //delete the location block
346 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
348 //cleanup DB by deleting the record.
349 Event
::delete($eventId);
350 Contact
::delete($this->_contactId
);
354 * DeleteLocBlock() method
355 * delete the location block
356 * created with various elements.
359 public function testDeleteLocBlock() {
360 $this->_contactId
= Contact
::createIndividual();
361 //create test event record.
362 $eventId = Event
::create($this->_contactId
);
363 $params['location'][1] = array(
364 'location_type_id' => 1,
367 'street_address' => 'Saint Helier St',
368 'supplemental_address_1' => 'Hallmark Ct',
369 'supplemental_address_2' => 'Jersey Village',
371 'postal_code' => '01903',
372 'country_id' => 1228,
373 'state_province_id' => 1029,
374 'geo_code_1' => '18.219023',
375 'geo_code_2' => '-105.00973',
378 '1' => array('email' => 'john.smith@example.org'),
382 'phone_type_id' => 1,
383 'phone' => '303443689',
386 'phone_type_id' => 2,
387 'phone' => '9833910234',
392 'name' => 'jane.doe',
397 $params['entity_id'] = $eventId;
398 $params['entity_table'] = 'civicrm_event';
400 //create location block.
401 //with various elements
402 //like address, phone, email, im.
403 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
404 $locBlockId = CRM_Utils_Array
::value('id', $location);
405 //update event record with location block id
406 $eventParams = array(
408 'loc_block_id' => $locBlockId,
410 CRM_Event_BAO_Event
::add($eventParams);
412 //delete the location block
413 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
415 //Now check DB for location elements.
416 //Now check DB for Address
417 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
418 'Database check, Address deleted successfully.'
420 //Now check DB for Email
421 $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email',
422 'Database check, Email deleted successfully.'
424 //Now check DB for Phone
425 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
426 'Database check, Phone deleted successfully.'
428 //Now check DB for Mobile
429 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
430 'Database check, Mobile deleted successfully.'
432 //Now check DB for IM
433 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
434 'Database check, IM deleted successfully.'
437 //cleanup DB by deleting the record.
438 Event
::delete($eventId);
439 Contact
::delete($this->_contactId
);
441 //Now check DB for Event
442 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
443 'Database check, Event deleted successfully.'
449 * get the values of various location elements
451 public function testLocBlockgetValues() {
452 $contactId = Contact
::createIndividual();
454 //create various element of location block
455 //like address, phone, email, openid, im.
459 'street_address' => 'Saint Helier St',
460 'supplemental_address_1' => 'Hallmark Ct',
461 'supplemental_address_2' => 'Jersey Village',
463 'postal_code' => '01903',
464 'country_id' => 1228,
465 'state_province_id' => 1029,
466 'geo_code_1' => '18.219023',
467 'geo_code_2' => '-105.00973',
469 'location_type_id' => 1,
474 'email' => 'john.smith@example.org',
476 'location_type_id' => 1,
481 'phone_type_id' => 1,
482 'phone' => '303443689',
484 'location_type_id' => 1,
487 'phone_type_id' => 2,
488 'phone' => '9833910234',
489 'location_type_id' => 1,
494 'openid' => 'http://civicrm.org/',
495 'location_type_id' => 1,
501 'name' => 'jane.doe',
503 'location_type_id' => 1,
509 $params['contact_id'] = $contactId;
511 //create location elements.
512 CRM_Core_BAO_Location
::create($params);
514 //get the values from DB
515 $values = CRM_Core_BAO_Location
::getValues($params);
517 //Now check values of address
518 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
519 CRM_Utils_Array
::value('1', $values['address'])
522 //Now check values of email
523 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
524 CRM_Utils_Array
::value('1', $values['email'])
527 //Now check values of phone
528 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
529 CRM_Utils_Array
::value('1', $values['phone'])
532 //Now check values of mobile
533 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
534 CRM_Utils_Array
::value('2', $values['phone'])
537 //Now check values of openid
538 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
539 CRM_Utils_Array
::value('1', $values['openid'])
542 //Now check values of im
543 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
544 CRM_Utils_Array
::value('1', $values['im'])
547 //cleanup DB by deleting the contact
548 Contact
::delete($contactId);