3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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(
58 * Tears down the fixture, for example, closes a network connection.
59 * This method is called after a test is executed.
61 public function tearDown() {
62 $tablesToTruncate = array(
67 $this->quickCleanup($tablesToTruncate);
70 public function testCreateWithMissingParams() {
71 $contactId = Contact
::createIndividual();
73 'contact_id' => $contactId,
74 'street_address' => 'Saint Helier St',
77 CRM_Core_BAO_Location
::create($params);
79 //Now check DB for Address
80 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
81 'Database check, Address created successfully.'
84 //cleanup DB by deleting the contact
85 Contact
::delete($contactId);
90 * create various elements of location block
91 * without civicrm_loc_block entry
93 public function testCreateWithoutLocBlock() {
94 $contactId = Contact
::createIndividual();
96 //create various element of location block
97 //like address, phone, email, openid, im.
101 'street_address' => 'Saint Helier St',
102 'supplemental_address_1' => 'Hallmark Ct',
103 'supplemental_address_2' => 'Jersey Village',
105 'postal_code' => '01903',
106 'country_id' => 1228,
107 'state_province_id' => 1029,
108 'geo_code_1' => '18.219023',
109 'geo_code_2' => '-105.00973',
111 'location_type_id' => 1,
116 'email' => 'john.smith@example.org',
118 'location_type_id' => 1,
123 'phone_type_id' => 1,
124 'phone' => '303443689',
126 'location_type_id' => 1,
129 'phone_type_id' => 2,
130 'phone' => '9833910234',
131 'location_type_id' => 1,
136 'openid' => 'http://civicrm.org/',
137 'location_type_id' => 1,
143 'name' => 'jane.doe',
145 'location_type_id' => 1,
151 $params['contact_id'] = $contactId;
153 $location = CRM_Core_BAO_Location
::create($params);
155 $locBlockId = CRM_Utils_Array
::value('id', $location);
157 //Now check DB for contact
158 $searchParams = array(
159 'contact_id' => $contactId,
160 'location_type_id' => 1,
163 $compareParams = array(
164 'street_address' => 'Saint Helier St',
165 'supplemental_address_1' => 'Hallmark Ct',
166 'supplemental_address_2' => 'Jersey Village',
168 'postal_code' => '01903',
169 'country_id' => 1228,
170 'state_province_id' => 1029,
171 'geo_code_1' => '18.219023',
172 'geo_code_2' => '-105.00973',
174 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
176 $compareParams = array('email' => 'john.smith@example.org');
177 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
179 $compareParams = array('openid' => 'http://civicrm.org/');
180 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
182 $compareParams = array(
183 'name' => 'jane.doe',
186 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
188 $searchParams = array(
189 'contact_id' => $contactId,
190 'location_type_id' => 1,
192 'phone_type_id' => 1,
194 $compareParams = array('phone' => '303443689');
195 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
197 $searchParams = array(
198 'contact_id' => $contactId,
199 'location_type_id' => 1,
200 'phone_type_id' => 2,
202 $compareParams = array('phone' => '9833910234');
203 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
205 //delete the location block
206 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
208 //cleanup DB by deleting the contact
209 Contact
::delete($contactId);
214 * create various elements of location block
215 * with civicrm_loc_block
217 public function testCreateWithLocBlock() {
218 $this->_contactId
= Contact
::createIndividual();
219 //create test event record.
220 $eventId = Event
::create($this->_contactId
);
224 'street_address' => 'Saint Helier St',
225 'supplemental_address_1' => 'Hallmark Ct',
226 'supplemental_address_2' => 'Jersey Village',
228 'postal_code' => '01903',
229 'country_id' => 1228,
230 'state_province_id' => 1029,
231 'geo_code_1' => '18.219023',
232 'geo_code_2' => '-105.00973',
234 'location_type_id' => 1,
239 'email' => 'john.smith@example.org',
241 'location_type_id' => 1,
246 'phone_type_id' => 1,
247 'phone' => '303443689',
249 'location_type_id' => 1,
252 'phone_type_id' => 2,
253 'phone' => '9833910234',
254 'location_type_id' => 1,
259 'name' => 'jane.doe',
261 'location_type_id' => 1,
267 $params['entity_id'] = $eventId;
268 $params['entity_table'] = 'civicrm_event';
270 //create location block.
271 //with various element of location block
272 //like address, phone, email, im.
273 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
274 $locBlockId = CRM_Utils_Array
::value('id', $location);
276 //update event record with location block id
277 $eventParams = array(
279 'loc_block_id' => $locBlockId,
282 CRM_Event_BAO_Event
::add($eventParams);
284 //Now check DB for location block
286 $this->assertDBCompareValue('CRM_Event_DAO_Event',
291 'Checking database for the record.'
293 $locElementIds = array();
294 $locParams = array('id' => $locBlockId);
295 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
300 //Now check DB for location elements.
301 $searchParams = array(
302 'id' => CRM_Utils_Array
::value('address_id', $locElementIds),
303 'location_type_id' => 1,
306 $compareParams = array(
307 'street_address' => 'Saint Helier St',
308 'supplemental_address_1' => 'Hallmark Ct',
309 'supplemental_address_2' => 'Jersey Village',
311 'postal_code' => '01903',
312 'country_id' => 1228,
313 'state_province_id' => 1029,
314 'geo_code_1' => '18.219023',
315 'geo_code_2' => '-105.00973',
317 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
319 $searchParams = array(
320 'id' => CRM_Utils_Array
::value('email_id', $locElementIds),
321 'location_type_id' => 1,
324 $compareParams = array('email' => 'john.smith@example.org');
325 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
327 $searchParams = array(
328 'id' => CRM_Utils_Array
::value('phone_id', $locElementIds),
329 'location_type_id' => 1,
331 'phone_type_id' => 1,
333 $compareParams = array('phone' => '303443689');
334 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
336 $searchParams = array(
337 'id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
338 'location_type_id' => 1,
339 'phone_type_id' => 2,
341 $compareParams = array('phone' => '9833910234');
342 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
344 $searchParams = array(
345 'id' => CRM_Utils_Array
::value('im_id', $locElementIds),
346 'location_type_id' => 1,
349 $compareParams = array(
350 'name' => 'jane.doe',
353 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
355 //delete the location block
356 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
358 //cleanup DB by deleting the record.
359 Event
::delete($eventId);
360 Contact
::delete($this->_contactId
);
364 * DeleteLocBlock() method
365 * delete the location block
366 * created with various elements.
368 public function testDeleteLocBlock() {
369 $this->_contactId
= Contact
::createIndividual();
370 //create test event record.
371 $eventId = Event
::create($this->_contactId
);
372 $params['location'][1] = array(
373 'location_type_id' => 1,
376 'street_address' => 'Saint Helier St',
377 'supplemental_address_1' => 'Hallmark Ct',
378 'supplemental_address_2' => 'Jersey Village',
380 'postal_code' => '01903',
381 'country_id' => 1228,
382 'state_province_id' => 1029,
383 'geo_code_1' => '18.219023',
384 'geo_code_2' => '-105.00973',
387 '1' => array('email' => 'john.smith@example.org'),
391 'phone_type_id' => 1,
392 'phone' => '303443689',
395 'phone_type_id' => 2,
396 'phone' => '9833910234',
401 'name' => 'jane.doe',
406 $params['entity_id'] = $eventId;
407 $params['entity_table'] = 'civicrm_event';
409 //create location block.
410 //with various elements
411 //like address, phone, email, im.
412 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
413 $locBlockId = CRM_Utils_Array
::value('id', $location);
414 //update event record with location block id
415 $eventParams = array(
417 'loc_block_id' => $locBlockId,
419 CRM_Event_BAO_Event
::add($eventParams);
421 //delete the location block
422 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
424 //Now check DB for location elements.
425 //Now check DB for Address
426 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
427 'Database check, Address deleted successfully.'
429 //Now check DB for Email
430 $this->assertDBNull('CRM_Core_DAO_Email', 'john.smith@example.org', 'id', 'email',
431 'Database check, Email deleted successfully.'
433 //Now check DB for Phone
434 $this->assertDBNull('CRM_Core_DAO_Phone', '303443689', 'id', 'phone',
435 'Database check, Phone deleted successfully.'
437 //Now check DB for Mobile
438 $this->assertDBNull('CRM_Core_DAO_Phone', '9833910234', 'id', 'phone',
439 'Database check, Mobile deleted successfully.'
441 //Now check DB for IM
442 $this->assertDBNull('CRM_Core_DAO_IM', 'jane.doe', 'id', 'name',
443 'Database check, IM deleted successfully.'
446 //cleanup DB by deleting the record.
447 Event
::delete($eventId);
448 Contact
::delete($this->_contactId
);
450 //Now check DB for Event
451 $this->assertDBNull('CRM_Event_DAO_Event', $eventId, 'id', 'id',
452 'Database check, Event deleted successfully.'
458 * get the values of various location elements
460 public function testLocBlockgetValues() {
461 $contactId = Contact
::createIndividual();
463 //create various element of location block
464 //like address, phone, email, openid, im.
468 'street_address' => 'Saint Helier St',
469 'supplemental_address_1' => 'Hallmark Ct',
470 'supplemental_address_2' => 'Jersey Village',
472 'postal_code' => '01903',
473 'country_id' => 1228,
474 'state_province_id' => 1029,
475 'geo_code_1' => '18.219023',
476 'geo_code_2' => '-105.00973',
478 'location_type_id' => 1,
483 'email' => 'john.smith@example.org',
485 'location_type_id' => 1,
490 'phone_type_id' => 1,
491 'phone' => '303443689',
493 'location_type_id' => 1,
496 'phone_type_id' => 2,
497 'phone' => '9833910234',
498 'location_type_id' => 1,
503 'openid' => 'http://civicrm.org/',
504 'location_type_id' => 1,
510 'name' => 'jane.doe',
512 'location_type_id' => 1,
518 $params['contact_id'] = $contactId;
520 //create location elements.
521 CRM_Core_BAO_Location
::create($params);
523 //get the values from DB
524 $values = CRM_Core_BAO_Location
::getValues($params);
526 //Now check values of address
527 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
528 CRM_Utils_Array
::value('1', $values['address'])
531 //Now check values of email
532 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
533 CRM_Utils_Array
::value('1', $values['email'])
536 //Now check values of phone
537 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
538 CRM_Utils_Array
::value('1', $values['phone'])
541 //Now check values of mobile
542 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
543 CRM_Utils_Array
::value('2', $values['phone'])
546 //Now check values of openid
547 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
548 CRM_Utils_Array
::value('1', $values['openid'])
551 //Now check values of im
552 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
553 CRM_Utils_Array
::value('1', $values['im'])
556 //cleanup DB by deleting the contact
557 Contact
::delete($contactId);