From 1d148cb97e03ed7b4f89cab71979111520df744e Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Thu, 15 Feb 2018 13:04:36 +0000 Subject: [PATCH 1/6] 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 b00f7b673953..cfdbc193685b 100644 --- a/CRM/Admin/Form/Preferences/Address.php +++ b/CRM/Admin/Form/Preferences/Address.php @@ -184,6 +184,17 @@ public function postProcess() { } } } + + 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 b66f01a11b05..a8c2ae4e8a68 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -714,25 +714,11 @@ public static function addressSequence() { * 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 @@ public static function parseStreetAddress($streetAddress, $locale = NULL) { 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 c6204889e946..42ac7a7729b9 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -382,6 +382,59 @@ public function testParseStreetAddress() { $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 From 9a208ac336b9019b256c0a921a56adf45e7879fe Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Thu, 15 Feb 2018 13:56:58 +0000 Subject: [PATCH 2/6] CRM-21769: Fixing code style erros --- CRM/Core/BAO/Address.php | 2 +- tests/phpunit/CRM/Core/BAO/AddressTest.php | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index a8c2ae4e8a68..ff706dfd3d8f 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -877,7 +877,7 @@ public static function parseStreetAddress($streetAddress, $locale = NULL) { * @param string $locale * The locale to be checked * - * @return boolean + * @return bool */ public static function isSupportedParsingLocale($locale = NULL) { if (!$locale) { diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index 42ac7a7729b9..05a7b3a39d63 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -400,8 +400,7 @@ public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLoca } - public function supportedAddressParsingLocales() - { + public function supportedAddressParsingLocales() { return array( 'en_US', 'en_CA', @@ -426,8 +425,7 @@ public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultL $this->assertFalse($isNotSupported); } - public function sampleOFUnsupportedAddressParsingLocales() - { + public function sampleOFUnsupportedAddressParsingLocales() { return array( 'en_GB', 'af_ZA', From 2ca38a629ee347e4daaafd70d0837d9a3b39f00b Mon Sep 17 00:00:00 2001 From: Omar abu hussein Date: Thu, 15 Feb 2018 23:11:29 +0200 Subject: [PATCH 3/6] CRM-217690: Fix tests data providers format --- tests/phpunit/CRM/Core/BAO/AddressTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index 05a7b3a39d63..4c4e86a43568 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -402,9 +402,9 @@ public function testIsSupportedByAddressParsingReturnTrueForSupportedDefaultLoca public function supportedAddressParsingLocales() { return array( - 'en_US', - 'en_CA', - 'fr_CA', + array('en_US'), + array('en_CA'), + array('fr_CA'), ); } @@ -427,9 +427,9 @@ public function testIsSupportedByAddressParsingReturnFalseForUnSupportedDefaultL public function sampleOFUnsupportedAddressParsingLocales() { return array( - 'en_GB', - 'af_ZA', - 'da_DK', + array('en_GB'), + array('af_ZA'), + array('da_DK'), ); } From b96fee81eebe96670591d90cb99183f833f40716 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 21 May 2018 20:46:32 +0100 Subject: [PATCH 4/6] refactor post process - remove unecessary foreach --- CRM/Admin/Form/Preferences/Address.php | 43 ++++++++++---------------- 1 file changed, 17 insertions(+), 26 deletions(-) diff --git a/CRM/Admin/Form/Preferences/Address.php b/CRM/Admin/Form/Preferences/Address.php index cfdbc193685b..838c6007c29f 100644 --- a/CRM/Admin/Form/Preferences/Address.php +++ b/CRM/Admin/Form/Preferences/Address.php @@ -165,36 +165,27 @@ public function postProcess() { } $this->_params = $this->controller->exportValues($this->_name); + $addressOptions = CRM_Core_OptionGroup::values('address_options', TRUE); // check if county option has been set - $options = CRM_Core_OptionGroup::values('address_options', FALSE, FALSE, TRUE); - foreach ($options as $key => $title) { - if ($title == ts('County')) { - // check if the $key is present in $this->_params - if (isset($this->_params['address_options']) && - !empty($this->_params['address_options'][$key]) - ) { - // print a status message to the user if county table seems small - $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county"); - if ($countyCount < 10) { - CRM_Core_Session::setStatus(ts('You have enabled the County option. Please ensure you populate the county table in your CiviCRM Database. You can find extensions to populate counties in the CiviCRM Extensions Directory.', array(1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', array('reset' => 1), TRUE, 'extensions-addnew') . '"')), - ts('Populate counties'), - "info" - ); - } - } + if (CRM_Utils_Array::value($addressOptions['County'], $this->_params['address_options'])) { + $countyCount = CRM_Core_DAO::singleValueQuery("SELECT count(*) FROM civicrm_county"); + if ($countyCount < 10) { + CRM_Core_Session::setStatus(ts('You have enabled the County option. Please ensure you populate the county table in your CiviCRM Database. You can find extensions to populate counties in the CiviCRM Extensions Directory.', array(1 => 'href="' . CRM_Utils_System::url('civicrm/admin/extensions', array('reset' => 1), TRUE, 'extensions-addnew') . '"')), + ts('Populate counties'), + "info" + ); } + } - 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'); - } - - } - } + // check that locale supports address parsing + if( + CRM_Utils_Array::value($addressOptions['Street Address Parsing'], $this->_params['address_options']) && + !CRM_Core_BAO_Address::isSupportedParsingLocale() + ) { + $config = CRM_Core_Config::singleton(); + $locale = $config->lcMessages; + CRM_Core_Session::setStatus(ts('Default locale (%1) does not support street parsing. en_US locale will be used instead.', [1 => $locale]), ts('Unsupported Locale'), 'alert'); } $this->postProcessCommon(); From 0eb2bbdf160755a461eba8248e7472dceba0e57a Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 21 May 2018 20:46:56 +0100 Subject: [PATCH 5/6] adding system check for address parsing --- CRM/Utils/Check/Component/AddressParsing.php | 67 ++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 CRM/Utils/Check/Component/AddressParsing.php diff --git a/CRM/Utils/Check/Component/AddressParsing.php b/CRM/Utils/Check/Component/AddressParsing.php new file mode 100644 index 000000000000..f79c11594c1f --- /dev/null +++ b/CRM/Utils/Check/Component/AddressParsing.php @@ -0,0 +1,67 @@ +Street address parsing is enabled but not supported by your locale (%1).', + [1 => $config->lcMessages] + ), + ts('Street address parsing'), + \Psr\Log\LogLevel::WARNING, + 'fa-address-card' + ); + } + } + + return $messages; + } + +} From 2bc9bf1fc0bedd0757cfc685279b890adfa88a36 Mon Sep 17 00:00:00 2001 From: Michael McAndrew Date: Mon, 21 May 2018 20:50:33 +0100 Subject: [PATCH 6/6] fixed civilint error --- CRM/Admin/Form/Preferences/Address.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CRM/Admin/Form/Preferences/Address.php b/CRM/Admin/Form/Preferences/Address.php index 838c6007c29f..27d200a6c774 100644 --- a/CRM/Admin/Form/Preferences/Address.php +++ b/CRM/Admin/Form/Preferences/Address.php @@ -179,7 +179,7 @@ public function postProcess() { } // check that locale supports address parsing - if( + if ( CRM_Utils_Array::value($addressOptions['Street Address Parsing'], $this->_params['address_options']) && !CRM_Core_BAO_Address::isSupportedParsingLocale() ) {