From 283f988c7d9f8f0fda0dfe40b926566e02bea3fe Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Fri, 7 Jun 2013 14:47:34 -0700 Subject: [PATCH] More fixes for API option matching and validation CRM-12464 ---------------------------------------- * CRM-12464: Add PseudoConstants to Schema Metadata http://issues.civicrm.org/jira/browse/CRM-12464 --- api/v3/utils.php | 10 ++++++---- tests/phpunit/api/v3/ActivityTest.php | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/api/v3/utils.php b/api/v3/utils.php index fefb043a4a..54f0d378ec 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1452,9 +1452,11 @@ function _civicrm_api3_validate_integer(&$params, &$fieldName, &$fieldInfo, $ent _civicrm_api3_api_match_pseudoconstant($params, $entity, $fieldName, $fieldInfo); } - // After swapping options, ensure we have an integer - if (!is_numeric($params[$fieldName]) || (int) $params[$fieldName] != $params[$fieldName]) { - throw new API_Exception("$fieldname is not a valid integer", 2001, array('error_field' => $fieldname,"type" => "integer")); + // After swapping options, ensure we have an integer(s) + foreach ((array) ($params[$fieldName]) as $value) { + if (!is_numeric($value) || (int) $value != $value) { + throw new API_Exception("$fieldName is not a valid integer", 2001, array('error_field' => $fieldName, "type" => "integer")); + } } // Check our field length @@ -1573,7 +1575,7 @@ function _civicrm_api3_api_match_pseudoconstant_value(&$value, $options, $fieldN $options = array_map("strtolower", $options); $value = array_search($value, $options); if ($value === FALSE) { - throw new Exception($errorMsg); + throw new API_Exception($errorMsg, 2001, array('error_field' => $fieldName)); } } diff --git a/tests/phpunit/api/v3/ActivityTest.php b/tests/phpunit/api/v3/ActivityTest.php index e8b26cbe80..2648ed8250 100644 --- a/tests/phpunit/api/v3/ActivityTest.php +++ b/tests/phpunit/api/v3/ActivityTest.php @@ -310,7 +310,8 @@ class api_v3_ActivityTest extends CiviUnitTestCase { $result = civicrm_api('activity', 'create', $params); $this->assertEquals($result['is_error'], 1, "In line " . __LINE__); $this->assertEquals("'44' is not a valid option for field priority_id", $result['error_message']); - $this->assertEquals('priority_id',$result['error_field']); + $this->assertEquals(2001, $result['error_code']); + $this->assertEquals('priority_id', $result['error_field']); } function testActivityCreateWithValidStringPriority() { @@ -1094,7 +1095,6 @@ class api_v3_ActivityTest extends CiviUnitTestCase { $params['target_contact_id'] = array($contact2 => $contact2); $result = civicrm_api('Activity', 'Create', $params); - $result = civicrm_api('activity', 'create', $params); $activityId = $result['id']; $this->assertAPISuccess($result); $getParams = array( -- 2.25.1