Skip to content

Commit

Permalink
Merge pull request #9320 from colemanw/CRM-19543
Browse files Browse the repository at this point in the history
CRM-19543 - Fix integer 0 matching for api pseudoconstants
  • Loading branch information
colemanw authored Oct 26, 2016
2 parents 6a47b6a + 415d9ab commit c7ff046
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
16 changes: 11 additions & 5 deletions api/v3/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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']) {
Expand All @@ -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) {
Expand Down Expand Up @@ -2390,7 +2395,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;
Expand Down
23 changes: 23 additions & 0 deletions tests/phpunit/api/v3/GrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit c7ff046

Please sign in to comment.