From eaf39b47cbe8672a080b94ea7824188933b750c4 Mon Sep 17 00:00:00 2001 From: Mark Hanna Date: Thu, 14 Feb 2019 12:35:38 -0600 Subject: [PATCH] CiviCRM API, lookup state_province_id options based on country parameter if present, or default country, with test --- CRM/Core/BAO/Address.php | 2 +- api/v3/utils.php | 18 ++++++++++++++---- tests/phpunit/api/v3/AddressTest.php | 20 ++++++++++++++++++++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 4ada8f0bdf..b2c594ad9c 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -1320,7 +1320,7 @@ SELECT is_primary, $props['country_id'] = $config->defaultContactCountry; } } - if (!empty($props['country_id']) && $context !== 'validate') { + if (!empty($props['country_id'])) { $params['condition'] = 'country_id IN (' . implode(',', (array) $props['country_id']) . ')'; } break; diff --git a/api/v3/utils.php b/api/v3/utils.php index e21de97898..52ffaedd01 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2069,7 +2069,11 @@ function _civicrm_api3_validate_integer(&$params, $fieldName, &$fieldInfo, $enti } } if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { - _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op); + $additional_lookup_params = array(); + if (strtolower($entity) == 'address' && $fieldName == 'state_province_id' && !empty($params['country_id'])) { + $additional_lookup_params = ['country_id' => $params['country_id']]; + } + _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op, $additional_lookup_params); } // After swapping options, ensure we have an integer(s) @@ -2213,10 +2217,11 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti * @param string $fieldName : field name used in api call (not necessarily the canonical name) * @param array $fieldInfo : getfields meta-data * @param string $op + * @param array $additional_lookup_params * * @throws \API_Exception */ -function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo, $op = '=') { +function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo, $op = '=', $additional_lookup_params = array()) { if (in_array($op, array('>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'))) { return; } @@ -2228,11 +2233,16 @@ function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldNam // We need to get the options from the entity the field relates to. $entity = $fieldInfo['entity']; } - $options = civicrm_api($entity, 'getoptions', array( + $options_lookup_params = [ 'version' => 3, 'field' => $fieldInfo['name'], 'context' => 'validate', - )); + ]; + if (!empty($additional_lookup_params)) { + $options_lookup_params = array_merge($additional_lookup_params, $options_lookup_params); + } + $options = civicrm_api($entity, 'getoptions', $options_lookup_params); + $options = CRM_Utils_Array::value('values', $options, array()); } diff --git a/tests/phpunit/api/v3/AddressTest.php b/tests/phpunit/api/v3/AddressTest.php index a496071e77..ad1c1c401e 100644 --- a/tests/phpunit/api/v3/AddressTest.php +++ b/tests/phpunit/api/v3/AddressTest.php @@ -390,4 +390,24 @@ class api_v3_AddressTest extends CiviUnitTestCase { $this->assertEquals('Individual', $result['contact_id.contact_type']); } + /** + * Test Address create with a state name that at least two countries have, e.g. Maryland, United States vs. Maryland, Liberia + * + * @see https://lab.civicrm.org/dev/core/issues/725 + */ + public function testCreateAddressStateProvinceIDCorrectForCountry() { + $params = $this->_params; + $params['sequential'] = 1; + $params['country_id'] = '1228'; // United States country id + $params['state_province_id'] = 'Maryland'; + $params['city'] = 'Baltimore'; + $params['street_address'] = '600 N Charles St.'; + $params['postal_code'] = '21201'; + unset($params['street_name']); + unset($params['street_number']); + $address1 = $this->callAPISuccess('address', 'create', $params); + // should find state_province_id of 1019, Maryland, United States ... NOT 3497, Maryland, Liberia + $this->assertEquals('1019', $address1['values'][0]['state_province_id']); + } + } -- 2.25.1