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';
39 class CRM_Core_BAO_LocationTest
extends CiviUnitTestCase
{
42 'name' => 'Location BAOs',
43 'description' => 'Test all Core_BAO_Location methods.',
44 'group' => 'CiviCRM BAO Tests',
51 $this->quickCleanup( array( 'civicrm_contact', 'civicrm_address', 'civicrm_loc_block', 'civicrm_email', 'civicrm_phone', 'civicrm_im' ) );
55 * Tears down the fixture, for example, closes a network connection.
56 * This method is called after a test is executed.
61 $tablesToTruncate = array(
66 $this->quickCleanup($tablesToTruncate);
69 function testCreateWithMissingParams() {
70 $contactId = Contact
::createIndividual();
72 'contact_id' => $contactId,
73 'street_address' => 'Saint Helier St',
76 CRM_Core_BAO_Location
::create($params);
78 //Now check DB for Address
79 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
80 'Database check, Address created successfully.'
83 //cleanup DB by deleting the contact
84 Contact
::delete($contactId);
89 * create various elements of location block
90 * without civicrm_loc_block entry
92 function testCreateWithoutLocBlock() {
93 $contactId = Contact
::createIndividual();
95 //create various element of location block
96 //like address, phone, email, openid, im.
100 'street_address' => 'Saint Helier St',
101 'supplemental_address_1' => 'Hallmark Ct',
102 'supplemental_address_2' => 'Jersey Village',
104 'postal_code' => '01903',
105 'country_id' => 1228,
106 'state_province_id' => 1029,
107 'geo_code_1' => '18.219023',
108 'geo_code_2' => '-105.00973',
110 'location_type_id' => 1,
115 'email' => 'john.smith@example.org',
117 'location_type_id' => 1,
122 'phone_type_id' => 1,
123 'phone' => '303443689',
125 'location_type_id' => 1,
128 'phone_type_id' => 2,
129 'phone' => '9833910234',
130 'location_type_id' => 1,
135 'openid' => 'http://civicrm.org/',
136 'location_type_id' => 1,
142 'name' => 'jane.doe',
144 'location_type_id' => 1,
150 $params['contact_id'] = $contactId;
152 $location = CRM_Core_BAO_Location
::create($params);
154 $locBlockId = CRM_Utils_Array
::value('id', $location);
156 //Now check DB for contact
157 $searchParams = array(
158 'contact_id' => $contactId,
159 'location_type_id' => 1,
162 $compareParams = array(
163 'street_address' => 'Saint Helier St',
164 'supplemental_address_1' => 'Hallmark Ct',
165 'supplemental_address_2' => 'Jersey Village',
167 'postal_code' => '01903',
168 'country_id' => 1228,
169 'state_province_id' => 1029,
170 'geo_code_1' => '18.219023',
171 'geo_code_2' => '-105.00973',
173 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
175 $compareParams = array('email' => 'john.smith@example.org');
176 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
178 $compareParams = array('openid' => 'http://civicrm.org/');
179 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
181 $compareParams = array(
182 'name' => 'jane.doe',
185 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
187 $searchParams = array(
188 'contact_id' => $contactId,
189 'location_type_id' => 1,
191 'phone_type_id' => 1,
193 $compareParams = array('phone' => '303443689');
194 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
196 $searchParams = array(
197 'contact_id' => $contactId,
198 'location_type_id' => 1,
199 'phone_type_id' => 2,
201 $compareParams = array('phone' => '9833910234');
202 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
204 //delete the location block
205 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
207 //cleanup DB by deleting the contact
208 Contact
::delete($contactId);
213 * create various elements of location block
214 * with civicrm_loc_block
216 function testCreateWithLocBlock() {
217 $this->_contactId
= Contact
::createIndividual();
218 //create test event record.
219 $eventId = Event
::create($this->_contactId
);
223 'street_address' => 'Saint Helier St',
224 'supplemental_address_1' => 'Hallmark Ct',
225 'supplemental_address_2' => 'Jersey Village',
227 'postal_code' => '01903',
228 'country_id' => 1228,
229 'state_province_id' => 1029,
230 'geo_code_1' => '18.219023',
231 'geo_code_2' => '-105.00973',
233 'location_type_id' => 1,
238 'email' => 'john.smith@example.org',
240 'location_type_id' => 1,
245 'phone_type_id' => 1,
246 'phone' => '303443689',
248 'location_type_id' => 1,
251 'phone_type_id' => 2,
252 'phone' => '9833910234',
253 'location_type_id' => 1,
258 'name' => 'jane.doe',
260 'location_type_id' => 1,
266 $params['entity_id'] = $eventId;
267 $params['entity_table'] = 'civicrm_event';
269 //create location block.
270 //with various element of location block
271 //like address, phone, email, im.
272 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
273 $locBlockId = CRM_Utils_Array
::value('id', $location);
275 //update event record with location block id
276 $eventParams = array(
278 'loc_block_id' => $locBlockId,
281 CRM_Event_BAO_Event
::add($eventParams);
283 //Now check DB for location block
285 $this->assertDBCompareValue('CRM_Event_DAO_Event',
290 'Checking database for the record.'
292 $locElementIds = array();
293 $locParams = array('id' => $locBlockId);
294 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
299 //Now check DB for location elements.
300 $searchParams = array('id' => CRM_Utils_Array
::value('address_id', $locElementIds),
301 'location_type_id' => 1,
304 $compareParams = array(
305 'street_address' => 'Saint Helier St',
306 'supplemental_address_1' => 'Hallmark Ct',
307 'supplemental_address_2' => 'Jersey Village',
309 'postal_code' => '01903',
310 'country_id' => 1228,
311 'state_province_id' => 1029,
312 'geo_code_1' => '18.219023',
313 'geo_code_2' => '-105.00973',
315 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
317 $searchParams = array('id' => CRM_Utils_Array
::value('email_id', $locElementIds),
318 'location_type_id' => 1,
321 $compareParams = array('email' => 'john.smith@example.org');
322 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
325 $searchParams = array('id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
326 'location_type_id' => 1,
328 'phone_type_id' => 1,
330 $compareParams = array('phone' => '303443689');
331 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
333 $searchParams = array('id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
334 'location_type_id' => 1,
335 'phone_type_id' => 2,
337 $compareParams = array('phone' => '9833910234');
338 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
340 $searchParams = array('id' => CRM_Utils_Array
::value('im_id', $locElementIds),
341 'location_type_id' => 1,
344 $compareParams = array(
345 'name' => 'jane.doe',
348 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
350 //delete the location block
351 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
353 //cleanup DB by deleting the record.
354 Event
::delete($eventId);
355 Contact
::delete($this->_contactId
);
359 * deleteLocBlock() method
360 * delete the location block
361 * created with various elements.
364 function testDeleteLocBlock() {
365 $this->_contactId
= Contact
::createIndividual();
366 //create test event record.
367 $eventId = Event
::create($this->_contactId
);
368 $params['location'][1] = array(
369 'location_type_id' => 1,
372 'street_address' => 'Saint Helier St',
373 'supplemental_address_1' => 'Hallmark Ct',
374 'supplemental_address_2' => 'Jersey Village',
376 'postal_code' => '01903',
377 'country_id' => 1228,
378 'state_province_id' => 1029,
379 'geo_code_1' => '18.219023',
380 'geo_code_2' => '-105.00973',
383 '1' => array('email' => 'john.smith@example.org'),
387 'phone_type_id' => 1,
388 'phone' => '303443689',
391 'phone_type_id' => 2,
392 'phone' => '9833910234',
397 'name' => 'jane.doe',
402 $params['entity_id'] = $eventId;
403 $params['entity_table'] = 'civicrm_event';
405 //create location block.
406 //with various elements
407 //like address, phone, email, im.
408 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
409 $locBlockId = CRM_Utils_Array
::value('id', $location);
410 //update event record with location block id
411 $eventParams = array(
413 'loc_block_id' => $locBlockId,
415 CRM_Event_BAO_Event
::add($eventParams);
417 //delete the location block
418 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
420 //Now check DB for location elements.
421 //Now check DB for Address
422 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
423 'Database check, Address deleted successfully.'
425 //Now check DB for Email
426 $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email',
427 'Database check, Email deleted successfully.'
429 //Now check DB for Phone
430 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
431 'Database check, Phone deleted successfully.'
433 //Now check DB for Mobile
434 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
435 'Database check, Mobile deleted successfully.'
437 //Now check DB for IM
438 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
439 'Database check, IM deleted successfully.'
442 //cleanup DB by deleting the record.
443 Event
::delete($eventId);
444 Contact
::delete($this->_contactId
);
446 //Now check DB for Event
447 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
448 'Database check, Event deleted successfully.'
454 * get the values of various location elements
456 function testLocBlockgetValues() {
457 $contactId = Contact
::createIndividual();
459 //create various element of location block
460 //like address, phone, email, openid, im.
464 'street_address' => 'Saint Helier St',
465 'supplemental_address_1' => 'Hallmark Ct',
466 'supplemental_address_2' => 'Jersey Village',
468 'postal_code' => '01903',
469 'country_id' => 1228,
470 'state_province_id' => 1029,
471 'geo_code_1' => '18.219023',
472 'geo_code_2' => '-105.00973',
474 'location_type_id' => 1,
479 'email' => 'john.smith@example.org',
481 'location_type_id' => 1,
486 'phone_type_id' => 1,
487 'phone' => '303443689',
489 'location_type_id' => 1,
492 'phone_type_id' => 2,
493 'phone' => '9833910234',
494 'location_type_id' => 1,
499 'openid' => 'http://civicrm.org/',
500 'location_type_id' => 1,
506 'name' => 'jane.doe',
508 'location_type_id' => 1,
514 $params['contact_id'] = $contactId;
516 //create location elements.
517 CRM_Core_BAO_Location
::create($params);
519 //get the values from DB
520 $values = CRM_Core_BAO_Location
::getValues($params);
522 //Now check values of address
523 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
524 CRM_Utils_Array
::value('1', $values['address'])
527 //Now check values of email
528 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
529 CRM_Utils_Array
::value('1', $values['email'])
532 //Now check values of phone
533 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
534 CRM_Utils_Array
::value('1', $values['phone'])
537 //Now check values of mobile
538 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
539 CRM_Utils_Array
::value('2', $values['phone'])
542 //Now check values of openid
543 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
544 CRM_Utils_Array
::value('1', $values['openid'])
547 //Now check values of im
548 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
549 CRM_Utils_Array
::value('1', $values['im'])
552 //cleanup DB by deleting the contact
553 Contact
::delete($contactId);