From e6f202aeabecdf176b27333918672071ae5f9c42 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 23 Jan 2018 10:03:17 -0500 Subject: [PATCH] CRM-21701 - ensure street addresses are parsed Even if USPS street parsing is disabled. --- CRM/Core/BAO/Address.php | 49 ++++++++++---------- tests/phpunit/CRM/Core/BAO/AddressTest.php | 52 ++++++++++++++++++++++ 2 files changed, 76 insertions(+), 25 deletions(-) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index edf8d6d69562..a3ca4343e130 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -350,33 +350,32 @@ public static function fixAddress(&$params) { $params['country_id'] == 1228 ) { CRM_Utils_Address_USPS::checkAddress($params); + } + // do street parsing again if enabled, since street address might have changed + $parseStreetAddress = CRM_Utils_Array::value( + 'street_address_parsing', + CRM_Core_BAO_Setting::valueOptions( + CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'address_options' + ), + FALSE + ); - // do street parsing again if enabled, since street address might have changed - $parseStreetAddress = CRM_Utils_Array::value( - 'street_address_parsing', - CRM_Core_BAO_Setting::valueOptions( - CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - 'address_options' - ), - FALSE - ); - - if ($parseStreetAddress && !empty($params['street_address'])) { - foreach (array( - 'street_number', - 'street_name', - 'street_unit', - 'street_number_suffix', - ) as $fld) { - unset($params[$fld]); - } - // main parse string. - $parseString = CRM_Utils_Array::value('street_address', $params); - $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($parseString); - - // merge parse address in to main address block. - $params = array_merge($params, $parsedFields); + if ($parseStreetAddress && !empty($params['street_address'])) { + foreach (array( + 'street_number', + 'street_name', + 'street_unit', + 'street_number_suffix', + ) as $fld) { + unset($params[$fld]); } + // main parse string. + $parseString = CRM_Utils_Array::value('street_address', $params); + $parsedFields = CRM_Core_BAO_Address::parseStreetAddress($parseString); + + // merge parse address in to main address block. + $params = array_merge($params, $parsedFields); } // check if geocode should be skipped (can be forced with an optional parameter through the api) diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index b20a6f45a734..c6204889e946 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -280,6 +280,58 @@ public function testGetValues() { $this->contactDelete($contactId); } + public function setStreetAddressParsing($status) { + $address_options = CRM_Core_BAO_Setting::valueOptions( + CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'address_options', + TRUE, NULL, TRUE + ); + if ($status) { + $value = 1; + } + else { + $value = 0; + } + $address_options['street_address_parsing'] = $value; + CRM_Core_BAO_Setting::setValueOption( + CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, + 'address_options', + $address_options + ); + } + + /** + * ParseStreetAddress if enabled, otherwise, don't. + */ + public function testParseStreetAddressIfEnabled() { + // Turn off address standardization. Parsing should work without it. + Civi::settings()->set('address_standardization_provider', NULL); + + // Ensure street parsing happens if enabled. + $this->setStreetAddressParsing(TRUE); + + $contactId = $this->individualCreate(); + $street_address = "54 Excelsior Ave."; + $params = array( + 'contact_id' => $contactId, + 'street_address' => $street_address, + 'location_type_id' => 1, + ); + + $result = civicrm_api3('Address', 'create', $params); + $value = array_pop($result['values']); + $street_number = CRM_Utils_Array::value('street_number', $value); + $this->assertEquals($street_number, '54'); + + // Ensure street parsing does not happen if disabled. + $this->setStreetAddressParsing(FALSE); + $result = civicrm_api3('Address', 'create', $params); + $value = array_pop($result['values']); + $street_number = CRM_Utils_Array::value('street_number', $value); + $this->assertEmpty($street_number); + + } + /** * ParseStreetAddress() method (get street address parsed) */