From 1d148cb97e03ed7b4f89cab71979111520df744e Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Thu, 15 Feb 2018 13:04:36 +0000 Subject: [PATCH] CRM-21769: Show 'unsupported locale for parsing' warning only when enabling address parsing --- CRM/Admin/Form/Preferences/Address.php | 11 +++++ CRM/Core/BAO/Address.php | 52 ++++++++++++++------- tests/phpunit/CRM/Core/BAO/AddressTest.php | 53 ++++++++++++++++++++++ 3 files changed, 99 insertions(+), 17 deletions(-) diff --git a/CRM/Admin/Form/Preferences/Address.php b/CRM/Admin/Form/Preferences/Address.php index b00f7b6739..cfdbc19368 100644 --- a/CRM/Admin/Form/Preferences/Address.php +++ b/CRM/Admin/Form/Preferences/Address.php @@ -184,6 +184,17 @@ class CRM_Admin_Form_Preferences_Address extends CRM_Admin_Form_Preferences { } } } + + if ($title == ts('Street Address Parsing')) { + if (isset($this->_params['address_options']) && + !empty($this->_params['address_options'][$key]) + ) { + if (!CRM_Core_BAO_Address::isSupportedParsingLocale()) { + CRM_Core_Session::setStatus(ts('Unsupported default locale specified to parse Street Address. en_US locale will be used instead.'), ts('Unsupported Locale'), 'alert'); + } + + } + } } $this->postProcessCommon(); diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index b66f01a11b..a8c2ae4e8a 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -714,25 +714,11 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC, * parsed fields values. */ public static function parseStreetAddress($streetAddress, $locale = NULL) { - $config = CRM_Core_Config::singleton(); - - /* locales supported include: - * en_US - http://pe.usps.com/cpim/ftp/pubs/pub28/pub28.pdf - * en_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp - * fr_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-f.asp - * NB: common use of comma after street number also supported - * default is en_US - */ - - $supportedLocalesForParsing = array('en_US', 'en_CA', 'fr_CA'); - if (!$locale) { - $locale = $config->lcMessages; - } - // as different locale explicitly requested but is not available, display warning message and set $locale = 'en_US' - if (!in_array($locale, $supportedLocalesForParsing)) { - CRM_Core_Session::setStatus(ts('Unsupported locale specified to parseStreetAddress: %1. Proceeding with en_US locale.', array(1 => $locale)), ts('Unsupported Locale'), 'alert'); + // use 'en_US' for address parsing if the requested locale is not supported. + if (!self::isSupportedParsingLocale($locale)) { $locale = 'en_US'; } + $emptyParseFields = $parseFields = array( 'street_name' => '', 'street_unit' => '', @@ -876,6 +862,38 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC, return $parseFields; } + /** + * Determines if the specified locale is + * supported by address parsing. + * If no locale is specified then it + * will check the default configured locale. + * + * locales supported include: + * en_US - http://pe.usps.com/cpim/ftp/pubs/pub28/pub28.pdf + * en_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-e.asp + * fr_CA - http://www.canadapost.ca/tools/pg/manual/PGaddress-f.asp + * NB: common use of comma after street number also supported + * + * @param string $locale + * The locale to be checked + * + * @return boolean + */ + public static function isSupportedParsingLocale($locale = NULL) { + if (!$locale) { + $config = CRM_Core_Config::singleton(); + $locale = $config->lcMessages; + } + + $parsingSupportedLocales = array('en_US', 'en_CA', 'fr_CA'); + + if (in_array($locale, $parsingSupportedLocales)) { + return TRUE; + } + + return FALSE; + } + /** * Validate the address fields based on the address options enabled. * in the Address Settings diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index c6204889e9..42ac7a7729 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -382,6 +382,59 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase { $this->assertNotContains('street_number_suffix', $parsedStreetAddress); } + /** + * @dataProvider supportedAddressParsingLocales + */ + public function testIsSupportedByAddressParsingReturnTrueForSupportedLocales($locale) { + $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale); + $this->assertTrue($isSupported); + } + + /** + * @dataProvider supportedAddressParsingLocales + */ + public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLocales($locale) { + CRM_Core_Config::singleton()->lcMessages = $locale; + $isSupported = CRM_Core_BAO_Address::isSupportedParsingLocale(); + $this->assertTrue($isSupported); + + } + + public function supportedAddressParsingLocales() + { + return array( + 'en_US', + 'en_CA', + 'fr_CA', + ); + } + + /** + * @dataProvider sampleOFUnsupportedAddressParsingLocales + */ + public function testIsSupportedByAddressParsingReturnFalseForUnSupportedLocales($locale) { + $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale($locale); + $this->assertFalse($isNotSupported); + } + + /** + * @dataProvider sampleOFUnsupportedAddressParsingLocales + */ + public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultLocales($locale) { + CRM_Core_Config::singleton()->lcMessages = $locale; + $isNotSupported = CRM_Core_BAO_Address::isSupportedParsingLocale(); + $this->assertFalse($isNotSupported); + } + + public function sampleOFUnsupportedAddressParsingLocales() + { + return array( + 'en_GB', + 'af_ZA', + 'da_DK', + ); + } + /** * CRM-21214 - Ensure all child addresses are updated correctly - 1. * 1. First, create three contacts: A, B, and C -- 2.25.1