From 80452d9607756bb17bd6ec89e3049ef2b953eaeb Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Oct 2016 15:20:49 -0400 Subject: [PATCH] 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 ba7393aeba..38071aa2c4 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 2154d3b421..605916db5f 100644 --- a/tests/phpunit/api/v3/GrantTest.php +++ b/tests/phpunit/api/v3/GrantTest.php @@ -143,4 +143,27 @@ class api_v3_GrantTest extends CiviUnitTestCase { $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); + } + } -- 2.25.1