3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
29 * Class CRM_Core_BAO_AddressTest
32 class CRM_Core_BAO_AddressTest
extends CiviUnitTestCase
{
33 public function setUp() {
36 $this->quickCleanup(array('civicrm_contact', 'civicrm_address'));
40 * Create() method (create and update modes)
42 public function testCreate() {
43 $contactId = $this->individualCreate();
46 $params['address']['1'] = array(
47 'street_address' => 'Oberoi Garden',
48 'supplemental_address_1' => 'Attn: Accounting',
49 'supplemental_address_2' => 'Powai',
50 'supplemental_address_3' => 'Somewhere',
52 'postal_code' => '01903',
53 'state_province_id' => '1000',
54 'country_id' => '1228',
55 'geo_code_1' => '18.219023',
56 'geo_code_2' => '-105.00973',
57 'location_type_id' => '1',
62 $params['contact_id'] = $contactId;
66 CRM_Core_BAO_Address
::create($params, $fixAddress, $entity = NULL);
67 $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', 'Oberoi Garden', 'id', 'street_address',
68 'Database check for created address.'
71 // Now call add() to modify an existing address
74 $params['address']['1'] = array(
76 'street_address' => '120 Terminal Road',
77 'supplemental_address_1' => 'A-wing:3037',
78 'supplemental_address_2' => 'Bandra',
79 'supplemental_address_3' => 'Somewhere',
81 'postal_code' => '01903',
82 'state_province_id' => '1000',
83 'country_id' => '1228',
84 'geo_code_1' => '18.219023',
85 'geo_code_2' => '-105.00973',
86 'location_type_id' => '1',
90 $params['contact_id'] = $contactId;
92 $block = CRM_Core_BAO_Address
::create($params, $fixAddress, $entity = NULL);
94 $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
95 'Database check for updated address by contactId.'
97 $this->assertDBNotNull('CRM_Core_DAO_Address', '120 Terminal Road', 'id', 'street_address',
98 'Database check for updated address by street_name.'
100 $this->contactDelete($contactId);
106 public function testAdd() {
107 $contactId = $this->individualCreate();
110 'street_address' => 'E 906N Pine Pl W',
111 'supplemental_address_1' => 'Editorial Dept',
112 'supplemental_address_2' => '',
113 'supplemental_address_3' => '',
115 'postal_code' => '88575',
116 'postal_code_suffix' => '',
117 'state_province_id' => '1001',
118 'country_id' => '1228',
119 'geo_code_1' => '31.694842',
120 'geo_code_2' => '-106.29998',
121 'location_type_id' => '1',
124 'contact_id' => $contactId,
127 $addAddress = CRM_Core_BAO_Address
::add($fixParams, $fixAddress = TRUE);
129 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
130 'Database check for created contact address.'
133 $this->assertEquals($addAddress->street_address
, 'E 906N Pine Pl W', 'In line' . __LINE__
);
134 $this->assertEquals($addAddress->supplemental_address_1
, 'Editorial Dept', 'In line' . __LINE__
);
135 $this->assertEquals($addAddress->city
, 'El Paso', 'In line' . __LINE__
);
136 $this->assertEquals($addAddress->postal_code
, '88575', 'In line' . __LINE__
);
137 $this->assertEquals($addAddress->geo_code_1
, '31.694842', 'In line' . __LINE__
);
138 $this->assertEquals($addAddress->geo_code_2
, '-106.29998', 'In line' . __LINE__
);
139 $this->assertEquals($addAddress->country_id
, '1228', 'In line' . __LINE__
);
140 $this->contactDelete($contactId);
144 * AllAddress() method ( )
146 public function testallAddress() {
147 $contactId = $this->individualCreate();
150 'street_address' => 'E 906N Pine Pl W',
151 'supplemental_address_1' => 'Editorial Dept',
152 'supplemental_address_2' => '',
153 'supplemental_address_3' => '',
155 'postal_code' => '88575',
156 'postal_code_suffix' => '',
157 'state_province_id' => '1001',
158 'country_id' => '1228',
159 'geo_code_1' => '31.694842',
160 'geo_code_2' => '-106.29998',
161 'location_type_id' => '1',
164 'contact_id' => $contactId,
167 CRM_Core_BAO_Address
::add($fixParams, $fixAddress = TRUE);
169 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
170 'Database check for created contact address.'
173 'street_address' => 'SW 719B Beech Dr NW',
174 'supplemental_address_1' => 'C/o OPDC',
175 'supplemental_address_2' => '',
176 'supplemental_address_3' => '',
177 'city' => 'Neillsville',
178 'postal_code' => '54456',
179 'postal_code_suffix' => '',
180 'state_province_id' => '1001',
181 'country_id' => '1228',
182 'geo_code_1' => '44.553719',
183 'geo_code_2' => '-90.61457',
184 'location_type_id' => '2',
187 'contact_id' => $contactId,
190 CRM_Core_BAO_Address
::add($fixParams, $fixAddress = TRUE);
192 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
193 'Database check for created contact address.'
196 $allAddress = CRM_Core_BAO_Address
::allAddress($contactId);
198 $this->assertEquals(count($allAddress), 2, 'Checking number of returned addresses.');
200 $this->contactDelete($contactId);
204 * AllAddress() method ( ) with null value
206 public function testnullallAddress() {
207 $contactId = $this->individualCreate();
210 'street_address' => 'E 906N Pine Pl W',
211 'supplemental_address_1' => 'Editorial Dept',
212 'supplemental_address_2' => '',
213 'supplemental_address_3' => '',
215 'postal_code' => '88575',
216 'postal_code_suffix' => '',
217 'state_province_id' => '1001',
218 'country_id' => '1228',
219 'geo_code_1' => '31.694842',
220 'geo_code_2' => '-106.29998',
221 'location_type_id' => '1',
224 'contact_id' => $contactId,
227 CRM_Core_BAO_Address
::add($fixParams, $fixAddress = TRUE);
229 $addParams = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
230 'Database check for created contact address.'
235 $allAddress = CRM_Core_BAO_Address
::allAddress($contact_Id);
237 $this->assertEquals($allAddress, NULL, 'Checking null for returned addresses.');
239 $this->contactDelete($contactId);
243 * GetValues() method (get Address fields)
245 public function testGetValues() {
246 $contactId = $this->individualCreate();
249 $params['address']['1'] = array(
250 'street_address' => 'Oberoi Garden',
251 'supplemental_address_1' => 'Attn: Accounting',
252 'supplemental_address_2' => 'Powai',
253 'supplemental_address_3' => 'Somewhere',
255 'postal_code' => '01903',
256 'state_province_id' => '1000',
257 'country_id' => '1228',
258 'geo_code_1' => '18.219023',
259 'geo_code_2' => '-105.00973',
260 'location_type_id' => '1',
265 $params['contact_id'] = $contactId;
269 CRM_Core_BAO_Address
::create($params, $fixAddress, $entity = NULL);
271 $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
272 'Database check for created address.'
275 $entityBlock = array('contact_id' => $contactId);
276 $address = CRM_Core_BAO_Address
::getValues($entityBlock);
277 $this->assertEquals($address[1]['id'], $addressId);
278 $this->assertEquals($address[1]['contact_id'], $contactId);
279 $this->assertEquals($address[1]['street_address'], 'Oberoi Garden');
280 $this->contactDelete($contactId);
283 public function setStreetAddressParsing($status) {
284 $address_options = CRM_Core_BAO_Setting
::valueOptions(
285 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
295 $address_options['street_address_parsing'] = $value;
296 CRM_Core_BAO_Setting
::setValueOption(
297 CRM_Core_BAO_Setting
::SYSTEM_PREFERENCES_NAME
,
304 * ParseStreetAddress if enabled, otherwise, don't.
306 public function testParseStreetAddressIfEnabled() {
307 // Turn off address standardization. Parsing should work without it.
308 Civi
::settings()->set('address_standardization_provider', NULL);
310 // Ensure street parsing happens if enabled.
311 $this->setStreetAddressParsing(TRUE);
313 $contactId = $this->individualCreate();
314 $street_address = "54 Excelsior Ave.";
316 'contact_id' => $contactId,
317 'street_address' => $street_address,
318 'location_type_id' => 1,
321 $result = civicrm_api3('Address', 'create', $params);
322 $value = array_pop($result['values']);
323 $street_number = CRM_Utils_Array
::value('street_number', $value);
324 $this->assertEquals($street_number, '54');
326 // Ensure street parsing does not happen if disabled.
327 $this->setStreetAddressParsing(FALSE);
328 $result = civicrm_api3('Address', 'create', $params);
329 $value = array_pop($result['values']);
330 $street_number = CRM_Utils_Array
::value('street_number', $value);
331 $this->assertEmpty($street_number);
336 * ParseStreetAddress() method (get street address parsed)
338 public function testParseStreetAddress() {
340 // valid Street address to be parsed ( without locale )
341 $street_address = "54A Excelsior Ave. Apt 1C";
342 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address);
343 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
344 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
345 $this->assertEquals($parsedStreetAddress['street_number'], '54');
346 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
348 // Out-of-range street number to be parsed.
349 $street_address = "505050505050 Main St";
350 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address);
351 $this->assertEquals($parsedStreetAddress['street_name'], '');
352 $this->assertEquals($parsedStreetAddress['street_unit'], '');
353 $this->assertEquals($parsedStreetAddress['street_number'], '');
354 $this->assertEquals($parsedStreetAddress['street_number_suffix'], '');
356 // valid Street address to be parsed ( $locale = 'en_US' )
357 $street_address = "54A Excelsior Ave. Apt 1C";
359 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
360 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
361 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
362 $this->assertEquals($parsedStreetAddress['street_number'], '54');
363 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
365 // invalid Street address ( $locale = 'en_US' )
366 $street_address = "West St. Apt 1";
368 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
369 $this->assertEquals($parsedStreetAddress['street_name'], 'West St.');
370 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1');
371 $this->assertNotContains('street_number', $parsedStreetAddress);
372 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
374 // valid Street address to be parsed ( $locale = 'fr_CA' )
375 $street_address = "2-123CA Main St";
377 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
378 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
379 $this->assertEquals($parsedStreetAddress['street_unit'], '2');
380 $this->assertEquals($parsedStreetAddress['street_number'], '123');
381 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'CA');
383 // invalid Street address ( $locale = 'fr_CA' )
384 $street_address = "123 Main St";
386 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
387 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
388 $this->assertEquals($parsedStreetAddress['street_number'], '123');
389 $this->assertNotContains('street_unit', $parsedStreetAddress);
390 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
394 * @dataProvider supportedAddressParsingLocales
396 public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) {
397 $isSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale($locale);
398 $this->assertTrue($isSupported);
402 * @dataProvider supportedAddressParsingLocales
404 public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) {
405 CRM_Core_Config
::singleton()->lcMessages
= $locale;
406 $isSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale();
407 $this->assertTrue($isSupported);
411 public function supportedAddressParsingLocales() {
420 * @dataProvider sampleOFUnsupportedAddressParsingLocales
422 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) {
423 $isNotSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale($locale);
424 $this->assertFalse($isNotSupported);
428 * @dataProvider sampleOFUnsupportedAddressParsingLocales
430 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) {
431 CRM_Core_Config
::singleton()->lcMessages
= $locale;
432 $isNotSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale();
433 $this->assertFalse($isNotSupported);
436 public function sampleOFUnsupportedAddressParsingLocales() {
445 * CRM-21214 - Ensure all child addresses are updated correctly - 1.
446 * 1. First, create three contacts: A, B, and C
447 * 2. Create an address for contact A
448 * 3. Use contact A's address for contact B
449 * 4. Use contact B's address for contact C
450 * 5. Change contact A's address
451 * Address of Contact C should reflect contact A's address change
452 * Also, Contact C's address' master_id should be Contact A's address id.
454 public function testSharedAddressChaining1() {
455 $contactIdA = $this->individualCreate(array(), 0);
456 $contactIdB = $this->individualCreate(array(), 1);
457 $contactIdC = $this->individualCreate(array(), 2);
459 $addressParamsA = array(
460 'street_address' => '123 Fake St.',
461 'location_type_id' => '1',
463 'contact_id' => $contactIdA,
465 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
467 $addressParamsB = array(
468 'street_address' => '123 Fake St.',
469 'location_type_id' => '1',
471 'master_id' => $addAddressA->id
,
472 'contact_id' => $contactIdB,
474 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB, FALSE);
476 $addressParamsC = array(
477 'street_address' => '123 Fake St.',
478 'location_type_id' => '1',
480 'master_id' => $addAddressB->id
,
481 'contact_id' => $contactIdC,
483 $addAddressC = CRM_Core_BAO_Address
::add($addressParamsC, FALSE);
485 $updatedAddressParamsA = array(
486 'id' => $addAddressA->id
,
487 'street_address' => '1313 New Address Lane',
488 'location_type_id' => '1',
490 'contact_id' => $contactIdA,
492 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
494 // CRM-21214 - Has Address C been updated with Address A's new values?
495 $newAddressC = new CRM_Core_DAO_Address();
496 $newAddressC->id
= $addAddressC->id
;
497 $newAddressC->find(TRUE);
498 $newAddressC->fetch(TRUE);
500 $this->assertEquals($updatedAddressA->street_address
, $newAddressC->street_address
);
501 $this->assertEquals($updatedAddressA->id
, $newAddressC->master_id
);
505 * CRM-21214 - Ensure all child addresses are updated correctly - 2.
506 * 1. First, create three contacts: A, B, and C
507 * 2. Create an address for contact A and B
508 * 3. Use contact A's address for contact C
509 * 4. Use contact B's address for contact A
510 * 5. Change contact B's address
511 * Address of Contact C should reflect contact B's address change
512 * Also, Contact C's address' master_id should be Contact B's address id.
514 public function testSharedAddressChaining2() {
515 $contactIdA = $this->individualCreate(array(), 0);
516 $contactIdB = $this->individualCreate(array(), 1);
517 $contactIdC = $this->individualCreate(array(), 2);
519 $addressParamsA = array(
520 'street_address' => '123 Fake St.',
521 'location_type_id' => '1',
523 'contact_id' => $contactIdA,
525 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
527 $addressParamsB = array(
528 'street_address' => '123 Fake St.',
529 'location_type_id' => '1',
531 'contact_id' => $contactIdB,
533 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB, FALSE);
535 $addressParamsC = array(
536 'street_address' => '123 Fake St.',
537 'location_type_id' => '1',
539 'master_id' => $addAddressA->id
,
540 'contact_id' => $contactIdC,
542 $addAddressC = CRM_Core_BAO_Address
::add($addressParamsC, FALSE);
544 $updatedAddressParamsA = array(
545 'id' => $addAddressA->id
,
546 'street_address' => '123 Fake St.',
547 'location_type_id' => '1',
549 'master_id' => $addAddressB->id
,
550 'contact_id' => $contactIdA,
552 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
554 $updatedAddressParamsB = array(
555 'id' => $addAddressB->id
,
556 'street_address' => '1313 New Address Lane',
557 'location_type_id' => '1',
559 'contact_id' => $contactIdB,
561 $updatedAddressB = CRM_Core_BAO_Address
::add($updatedAddressParamsB, FALSE);
563 // CRM-21214 - Has Address C been updated with Address B's new values?
564 $newAddressC = new CRM_Core_DAO_Address();
565 $newAddressC->id
= $addAddressC->id
;
566 $newAddressC->find(TRUE);
567 $newAddressC->fetch(TRUE);
569 $this->assertEquals($updatedAddressB->street_address
, $newAddressC->street_address
);
570 $this->assertEquals($updatedAddressB->id
, $newAddressC->master_id
);
574 * CRM-21214 - Ensure all child addresses are updated correctly - 3.
575 * 1. First, create a contact: A
576 * 2. Create an address for contact A
577 * 3. Use contact A's address for contact A's address
578 * An error should be given, and master_id should remain the same.
580 public function testSharedAddressChaining3() {
581 $contactIdA = $this->individualCreate(array(), 0);
583 $addressParamsA = array(
584 'street_address' => '123 Fake St.',
585 'location_type_id' => '1',
587 'contact_id' => $contactIdA,
589 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
591 $updatedAddressParamsA = array(
592 'id' => $addAddressA->id
,
593 'street_address' => '123 Fake St.',
594 'location_type_id' => '1',
596 'master_id' => $addAddressA->id
,
597 'contact_id' => $contactIdA,
599 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
601 // CRM-21214 - AdressA shouldn't be master of itself.
602 $this->assertEmpty($updatedAddressA->master_id
);