3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
37 * Class CRM_Core_BAO_LocationTest
40 class CRM_Core_BAO_LocationTest
extends CiviUnitTestCase
{
41 public function setUp() {
44 $this->quickCleanup(array(
55 * Tears down the fixture, for example, closes a network connection.
56 * This method is called after a test is executed.
58 public function tearDown() {
59 $tablesToTruncate = array(
64 $this->quickCleanup($tablesToTruncate);
67 public function testCreateWithMissingParams() {
68 $contactId = $this->individualCreate();
70 'contact_id' => $contactId,
71 'street_address' => 'Saint Helier St',
74 CRM_Core_BAO_Location
::create($params);
76 //Now check DB for Address
77 $this->assertDBNull('CRM_Core_DAO_Address', 'Saint Helier St', 'id', 'street_address',
78 'Database check, Address created successfully.'
81 $this->contactDelete($contactId);
86 * create various elements of location block
87 * without civicrm_loc_block entry
89 public function testCreateWithoutLocBlock() {
90 $contactId = $this->individualCreate();
92 //create various element of location block
93 //like address, phone, email, openid, im.
97 'street_address' => 'Saint Helier St',
98 'supplemental_address_1' => 'Hallmark Ct',
99 'supplemental_address_2' => 'Jersey Village',
100 'supplemental_address_3' => 'My Town',
102 'postal_code' => '01903',
103 'country_id' => 1228,
104 'state_province_id' => 1029,
105 'geo_code_1' => '18.219023',
106 'geo_code_2' => '-105.00973',
108 'location_type_id' => 1,
113 'email' => 'john.smith@example.org',
115 'location_type_id' => 1,
120 'phone_type_id' => 1,
121 'phone' => '303443689',
123 'location_type_id' => 1,
126 'phone_type_id' => 2,
127 'phone' => '9833910234',
128 'location_type_id' => 1,
133 'openid' => 'http://civicrm.org/',
134 'location_type_id' => 1,
140 'name' => 'jane.doe',
142 'location_type_id' => 1,
148 $params['contact_id'] = $contactId;
150 $location = CRM_Core_BAO_Location
::create($params);
152 $locBlockId = CRM_Utils_Array
::value('id', $location);
154 //Now check DB for contact
155 $searchParams = array(
156 'contact_id' => $contactId,
157 'location_type_id' => 1,
160 $compareParams = array(
161 'street_address' => 'Saint Helier St',
162 'supplemental_address_1' => 'Hallmark Ct',
163 'supplemental_address_2' => 'Jersey Village',
164 'supplemental_address_3' => 'My Town',
166 'postal_code' => '01903',
167 'country_id' => 1228,
168 'state_province_id' => 1029,
169 'geo_code_1' => '18.219023',
170 'geo_code_2' => '-105.00973',
172 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
174 $compareParams = array('email' => 'john.smith@example.org');
175 $this->assertDBCompareValues('CRM_Core_DAO_Email', $searchParams, $compareParams);
177 $compareParams = array('openid' => 'http://civicrm.org/');
178 $this->assertDBCompareValues('CRM_Core_DAO_OpenID', $searchParams, $compareParams);
180 $compareParams = array(
181 'name' => 'jane.doe',
184 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
186 $searchParams = array(
187 'contact_id' => $contactId,
188 'location_type_id' => 1,
190 'phone_type_id' => 1,
192 $compareParams = array('phone' => '303443689');
193 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
195 $searchParams = array(
196 'contact_id' => $contactId,
197 'location_type_id' => 1,
198 'phone_type_id' => 2,
200 $compareParams = array('phone' => '9833910234');
201 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
203 //delete the location block
204 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
205 $this->contactDelete($contactId);
210 * create various elements of location block
211 * with civicrm_loc_block
213 public function testCreateWithLocBlock() {
214 $this->_contactId
= $this->individualCreate();
215 $event = $this->eventCreate();
219 'street_address' => 'Saint Helier St',
220 'supplemental_address_1' => 'Hallmark Ct',
221 'supplemental_address_2' => 'Jersey Village',
222 'supplemental_address_3' => 'My Town',
224 'postal_code' => '01903',
225 'country_id' => 1228,
226 'state_province_id' => 1029,
227 'geo_code_1' => '18.219023',
228 'geo_code_2' => '-105.00973',
230 'location_type_id' => 1,
235 'email' => 'john.smith@example.org',
237 'location_type_id' => 1,
242 'phone_type_id' => 1,
243 'phone' => '303443689',
245 'location_type_id' => 1,
248 'phone_type_id' => 2,
249 'phone' => '9833910234',
250 'location_type_id' => 1,
255 'name' => 'jane.doe',
257 'location_type_id' => 1,
263 $params['entity_id'] = $event['id'];
264 $params['entity_table'] = 'civicrm_event';
266 //create location block.
267 //with various element of location block
268 //like address, phone, email, im.
269 $location = CRM_Core_BAO_Location
::create($params, NULL, TRUE);
270 $locBlockId = CRM_Utils_Array
::value('id', $location);
272 //update event record with location block id
273 $eventParams = array(
274 'id' => $event['id'],
275 'loc_block_id' => $locBlockId,
278 CRM_Event_BAO_Event
::add($eventParams);
280 //Now check DB for location block
282 $this->assertDBCompareValue('CRM_Event_DAO_Event',
287 'Checking database for the record.'
289 $locElementIds = array();
290 $locParams = array('id' => $locBlockId);
291 CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_LocBlock',
296 //Now check DB for location elements.
297 $searchParams = array(
298 'id' => CRM_Utils_Array
::value('address_id', $locElementIds),
299 'location_type_id' => 1,
302 $compareParams = array(
303 'street_address' => 'Saint Helier St',
304 'supplemental_address_1' => 'Hallmark Ct',
305 'supplemental_address_2' => 'Jersey Village',
306 'supplemental_address_3' => 'My Town',
308 'postal_code' => '01903',
309 'country_id' => 1228,
310 'state_province_id' => 1029,
311 'geo_code_1' => '18.219023',
312 'geo_code_2' => '-105.00973',
314 $this->assertDBCompareValues('CRM_Core_DAO_Address', $searchParams, $compareParams);
316 $searchParams = array(
317 '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);
324 $searchParams = array(
325 '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(
334 'id' => CRM_Utils_Array
::value('phone_2_id', $locElementIds),
335 'location_type_id' => 1,
336 'phone_type_id' => 2,
338 $compareParams = array('phone' => '9833910234');
339 $this->assertDBCompareValues('CRM_Core_DAO_Phone', $searchParams, $compareParams);
341 $searchParams = array(
342 'id' => CRM_Utils_Array
::value('im_id', $locElementIds),
343 'location_type_id' => 1,
346 $compareParams = array(
347 'name' => 'jane.doe',
350 $this->assertDBCompareValues('CRM_Core_DAO_IM', $searchParams, $compareParams);
353 CRM_Core_BAO_Location
::deleteLocBlock($locBlockId);
354 $this->eventDelete($event['id']);
355 $this->contactDelete($this->_contactId
);
359 * DeleteLocBlock() method
360 * delete the location block
361 * created with various elements.
363 public function testDeleteLocBlock() {
364 $this->_contactId
= $this->individualCreate();
365 //create test event record.
366 $event = $this->eventCreate();
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',
374 'supplemental_address_3' => 'My Town',
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'] = $event['id'];
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(
412 'id' => $event['id'],
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 $this->eventDelete($event['id']);
444 $this->contactDelete($this->_contactId
);
446 //Now check DB for Event
447 $this->assertDBNull('CRM_Event_DAO_Event', $event['id'], 'id', 'id',
448 'Database check, Event deleted successfully.'
454 * get the values of various location elements
456 public function testLocBlockgetValues() {
457 $contactId = $this->individualCreate();
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',
467 'supplemental_address_3' => 'My Town',
469 'postal_code' => '01903',
470 'country_id' => 1228,
471 'state_province_id' => 1029,
472 'geo_code_1' => '18.219023',
473 'geo_code_2' => '-105.00973',
475 'location_type_id' => 1,
480 'email' => 'john.smith@example.org',
482 'location_type_id' => 1,
487 'phone_type_id' => 1,
488 'phone' => '303443689',
490 'location_type_id' => 1,
493 'phone_type_id' => 2,
494 'phone' => '9833910234',
495 'location_type_id' => 1,
500 'openid' => 'http://civicrm.org/',
501 'location_type_id' => 1,
507 'name' => 'jane.doe',
509 'location_type_id' => 1,
515 $params['contact_id'] = $contactId;
517 //create location elements.
518 CRM_Core_BAO_Location
::create($params);
520 //get the values from DB
521 $values = CRM_Core_BAO_Location
::getValues($params);
523 //Now check values of address
524 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['address']),
525 CRM_Utils_Array
::value('1', $values['address'])
528 //Now check values of email
529 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['email']),
530 CRM_Utils_Array
::value('1', $values['email'])
533 //Now check values of phone
534 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['phone']),
535 CRM_Utils_Array
::value('1', $values['phone'])
538 //Now check values of mobile
539 $this->assertAttributesEquals(CRM_Utils_Array
::value('2', $params['phone']),
540 CRM_Utils_Array
::value('2', $values['phone'])
543 //Now check values of openid
544 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['openid']),
545 CRM_Utils_Array
::value('1', $values['openid'])
548 //Now check values of im
549 $this->assertAttributesEquals(CRM_Utils_Array
::value('1', $params['im']),
550 CRM_Utils_Array
::value('1', $values['im'])
552 $this->contactDelete($contactId);