From 80452d9607756bb17bd6ec89e3049ef2b953eaeb Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Oct 2016 15:20:49 -0400 Subject: [PATCH 1/2] CRM-19543 - Fix integer 0 matching for api pseudoconstants --- api/v3/utils.php | 5 +++-- tests/phpunit/api/v3/GrantTest.php | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index ba7393aebab3..38071aa2c435 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2172,7 +2172,7 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent return; } - if (!empty($fieldValue)) { + if (!empty($fieldValue) || $fieldValue === '0' || $fieldValue === 0) { // if value = 'user_contact_id' (or similar), replace value with contact id if (!is_numeric($fieldValue) && is_scalar($fieldValue)) { $realContactId = _civicrm_api3_resolve_contactID($fieldValue); @@ -2390,7 +2390,8 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN } // Translate value into key - $newValue = array_search($value, $options); + // Cast $value to string to avoid a bug in array_search + $newValue = array_search((string) $value, $options); if ($newValue !== FALSE) { $value = $newValue; return; diff --git a/tests/phpunit/api/v3/GrantTest.php b/tests/phpunit/api/v3/GrantTest.php index 2154d3b42162..605916db5f80 100644 --- a/tests/phpunit/api/v3/GrantTest.php +++ b/tests/phpunit/api/v3/GrantTest.php @@ -143,4 +143,27 @@ public function testDeleteGrant() { $this->assertEquals(0, $checkDeleted['count']); } + /** + * Test Grant status with `0` value. + */ + public function testGrantWithZeroStatus() { + $params = array( + 'action' => 'create', + 'grant_type_id' => "Emergency", + 'amount_total' => 100, + 'contact_id' => "1", + 'status_id' => 0, + 'id' => 1, + ); + $validation = $this->callAPISuccess('Grant', 'validate', $params); + + $expectedOut = array( + 'status_id' => array( + 'message' => "'0' is not a valid option for field status_id", + 'code' => "incorrect_value", + ), + ); + $this->assertEquals($validation['values'][0], $expectedOut); + } + } From 415d9abb858a1b24e1d82abdeaee748c192da87d Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Oct 2016 17:14:07 -0400 Subject: [PATCH 2/2] CRM-19543 - Don't match pseudoconstants when using comparison operators --- api/v3/utils.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index 38071aa2c435..76952c0cd9c5 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -2184,7 +2184,7 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent } } if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { - _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo); + _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op); } // After swapping options, ensure we have an integer(s) @@ -2305,7 +2305,7 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti } } if (!empty($fieldInfo['pseudoconstant']) || !empty($fieldInfo['options'])) { - _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo); + _civicrm_api3_api_match_pseudoconstant($fieldValue, $entity, $fieldName, $fieldInfo, $op); } // Check our field length elseif (is_string($fieldValue) && !empty($fieldInfo['maxlength']) && strlen(utf8_decode($fieldValue)) > $fieldInfo['maxlength']) { @@ -2329,10 +2329,15 @@ function _civicrm_api3_validate_string(&$params, &$fieldName, &$fieldInfo, $enti * @param string $entity : api entity name * @param string $fieldName : field name used in api call (not necessarily the canonical name) * @param array $fieldInfo : getfields meta-data + * @param string $op * * @throws \API_Exception */ -function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo) { +function _civicrm_api3_api_match_pseudoconstant(&$fieldValue, $entity, $fieldName, $fieldInfo, $op = '=') { + if (in_array($op, array('>', '<', '>=', '<=', 'LIKE', 'NOT LIKE'))) { + return; + } + $options = CRM_Utils_Array::value('options', $fieldInfo); if (!$options) {