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 // valid Street address to be parsed ( $locale = 'en_US' )
349 $street_address = "54A Excelsior Ave. Apt 1C";
351 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
352 $this->assertEquals($parsedStreetAddress['street_name'], 'Excelsior Ave.');
353 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1C');
354 $this->assertEquals($parsedStreetAddress['street_number'], '54');
355 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'A');
357 // invalid Street address ( $locale = 'en_US' )
358 $street_address = "West St. Apt 1";
360 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
361 $this->assertEquals($parsedStreetAddress['street_name'], 'West St.');
362 $this->assertEquals($parsedStreetAddress['street_unit'], 'Apt 1');
363 $this->assertNotContains('street_number', $parsedStreetAddress);
364 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
366 // valid Street address to be parsed ( $locale = 'fr_CA' )
367 $street_address = "2-123CA Main St";
369 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
370 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
371 $this->assertEquals($parsedStreetAddress['street_unit'], '2');
372 $this->assertEquals($parsedStreetAddress['street_number'], '123');
373 $this->assertEquals($parsedStreetAddress['street_number_suffix'], 'CA');
375 // invalid Street address ( $locale = 'fr_CA' )
376 $street_address = "123 Main St";
378 $parsedStreetAddress = CRM_Core_BAO_Address
::parseStreetAddress($street_address, $locale);
379 $this->assertEquals($parsedStreetAddress['street_name'], 'Main St');
380 $this->assertEquals($parsedStreetAddress['street_number'], '123');
381 $this->assertNotContains('street_unit', $parsedStreetAddress);
382 $this->assertNotContains('street_number_suffix', $parsedStreetAddress);
386 * @dataProvider supportedAddressParsingLocales
388 public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) {
389 $isSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale($locale);
390 $this->assertTrue($isSupported);
394 * @dataProvider supportedAddressParsingLocales
396 public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) {
397 CRM_Core_Config
::singleton()->lcMessages
= $locale;
398 $isSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale();
399 $this->assertTrue($isSupported);
403 public function supportedAddressParsingLocales() {
412 * @dataProvider sampleOFUnsupportedAddressParsingLocales
414 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) {
415 $isNotSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale($locale);
416 $this->assertFalse($isNotSupported);
420 * @dataProvider sampleOFUnsupportedAddressParsingLocales
422 public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) {
423 CRM_Core_Config
::singleton()->lcMessages
= $locale;
424 $isNotSupported = CRM_Core_BAO_Address
::isSupportedParsingLocale();
425 $this->assertFalse($isNotSupported);
428 public function sampleOFUnsupportedAddressParsingLocales() {
437 * CRM-21214 - Ensure all child addresses are updated correctly - 1.
438 * 1. First, create three contacts: A, B, and C
439 * 2. Create an address for contact A
440 * 3. Use contact A's address for contact B
441 * 4. Use contact B's address for contact C
442 * 5. Change contact A's address
443 * Address of Contact C should reflect contact A's address change
444 * Also, Contact C's address' master_id should be Contact A's address id.
446 public function testSharedAddressChaining1() {
447 $contactIdA = $this->individualCreate(array(), 0);
448 $contactIdB = $this->individualCreate(array(), 1);
449 $contactIdC = $this->individualCreate(array(), 2);
451 $addressParamsA = array(
452 'street_address' => '123 Fake St.',
453 'location_type_id' => '1',
455 'contact_id' => $contactIdA,
457 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
459 $addressParamsB = array(
460 'street_address' => '123 Fake St.',
461 'location_type_id' => '1',
463 'master_id' => $addAddressA->id
,
464 'contact_id' => $contactIdB,
466 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB, FALSE);
468 $addressParamsC = array(
469 'street_address' => '123 Fake St.',
470 'location_type_id' => '1',
472 'master_id' => $addAddressB->id
,
473 'contact_id' => $contactIdC,
475 $addAddressC = CRM_Core_BAO_Address
::add($addressParamsC, FALSE);
477 $updatedAddressParamsA = array(
478 'id' => $addAddressA->id
,
479 'street_address' => '1313 New Address Lane',
480 'location_type_id' => '1',
482 'contact_id' => $contactIdA,
484 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
486 // CRM-21214 - Has Address C been updated with Address A's new values?
487 $newAddressC = new CRM_Core_DAO_Address();
488 $newAddressC->id
= $addAddressC->id
;
489 $newAddressC->find(TRUE);
490 $newAddressC->fetch(TRUE);
492 $this->assertEquals($updatedAddressA->street_address
, $newAddressC->street_address
);
493 $this->assertEquals($updatedAddressA->id
, $newAddressC->master_id
);
497 * CRM-21214 - Ensure all child addresses are updated correctly - 2.
498 * 1. First, create three contacts: A, B, and C
499 * 2. Create an address for contact A and B
500 * 3. Use contact A's address for contact C
501 * 4. Use contact B's address for contact A
502 * 5. Change contact B's address
503 * Address of Contact C should reflect contact B's address change
504 * Also, Contact C's address' master_id should be Contact B's address id.
506 public function testSharedAddressChaining2() {
507 $contactIdA = $this->individualCreate(array(), 0);
508 $contactIdB = $this->individualCreate(array(), 1);
509 $contactIdC = $this->individualCreate(array(), 2);
511 $addressParamsA = array(
512 'street_address' => '123 Fake St.',
513 'location_type_id' => '1',
515 'contact_id' => $contactIdA,
517 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
519 $addressParamsB = array(
520 'street_address' => '123 Fake St.',
521 'location_type_id' => '1',
523 'contact_id' => $contactIdB,
525 $addAddressB = CRM_Core_BAO_Address
::add($addressParamsB, FALSE);
527 $addressParamsC = array(
528 'street_address' => '123 Fake St.',
529 'location_type_id' => '1',
531 'master_id' => $addAddressA->id
,
532 'contact_id' => $contactIdC,
534 $addAddressC = CRM_Core_BAO_Address
::add($addressParamsC, FALSE);
536 $updatedAddressParamsA = array(
537 'id' => $addAddressA->id
,
538 'street_address' => '123 Fake St.',
539 'location_type_id' => '1',
541 'master_id' => $addAddressB->id
,
542 'contact_id' => $contactIdA,
544 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
546 $updatedAddressParamsB = array(
547 'id' => $addAddressB->id
,
548 'street_address' => '1313 New Address Lane',
549 'location_type_id' => '1',
551 'contact_id' => $contactIdB,
553 $updatedAddressB = CRM_Core_BAO_Address
::add($updatedAddressParamsB, FALSE);
555 // CRM-21214 - Has Address C been updated with Address B's new values?
556 $newAddressC = new CRM_Core_DAO_Address();
557 $newAddressC->id
= $addAddressC->id
;
558 $newAddressC->find(TRUE);
559 $newAddressC->fetch(TRUE);
561 $this->assertEquals($updatedAddressB->street_address
, $newAddressC->street_address
);
562 $this->assertEquals($updatedAddressB->id
, $newAddressC->master_id
);
566 * CRM-21214 - Ensure all child addresses are updated correctly - 3.
567 * 1. First, create a contact: A
568 * 2. Create an address for contact A
569 * 3. Use contact A's address for contact A's address
570 * An error should be given, and master_id should remain the same.
572 public function testSharedAddressChaining3() {
573 $contactIdA = $this->individualCreate(array(), 0);
575 $addressParamsA = array(
576 'street_address' => '123 Fake St.',
577 'location_type_id' => '1',
579 'contact_id' => $contactIdA,
581 $addAddressA = CRM_Core_BAO_Address
::add($addressParamsA, FALSE);
583 $updatedAddressParamsA = array(
584 'id' => $addAddressA->id
,
585 'street_address' => '123 Fake St.',
586 'location_type_id' => '1',
588 'master_id' => $addAddressA->id
,
589 'contact_id' => $contactIdA,
591 $updatedAddressA = CRM_Core_BAO_Address
::add($updatedAddressParamsA, FALSE);
593 // CRM-21214 - AdressA shouldn't be master of itself.
594 $this->assertEmpty($updatedAddressA->master_id
);