From 6e4339c4b549f9d957503214ad35a1de7d614ff9 Mon Sep 17 00:00:00 2001 From: Saurabh Batra Date: Mon, 1 Aug 2016 13:17:40 +0530 Subject: [PATCH] add tests and cleanup --- api/v3/Generic/Validate.php | 1 + api/v3/utils.php | 33 ++++++++----- tests/phpunit/api/v3/ValidateTest.php | 69 +++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 tests/phpunit/api/v3/ValidateTest.php diff --git a/api/v3/Generic/Validate.php b/api/v3/Generic/Validate.php index 48ce2c477a..7b09ff3588 100644 --- a/api/v3/Generic/Validate.php +++ b/api/v3/Generic/Validate.php @@ -36,6 +36,7 @@ */ function _civicrm_api3_generic_validate_spec(&$params) { $params['action']['api.required'] = TRUE; + $params['action']['title'] = ts('API Action'); } /** diff --git a/api/v3/utils.php b/api/v3/utils.php index 68f98e569c..b11f4f2a55 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -1551,24 +1551,36 @@ function _civicrm_api3_validate($entity, $action, $params) { $fields = $fields['values']; // Check for required fields. - foreach ($fields as $field => $values) { - if (!empty($values['api.required'] && empty($params[$field]))) { - $errors[$values['name']] = "Mandatory key(s) missing from params array: " . $values['name']; + foreach ($fields as $values) { + if (!empty($values['api.required']) && empty($params[$values['name']])) { + $errors[$values['name']] = array( + 'message' => "Mandatory key(s) missing from params array: " . $values['name'], + 'code' => "mandatory_missing", + ); } } // Select only the fields which have been input as a param. - $fields = array_intersect_key($fields, $params); + $finalfields = array(); + foreach ($fields as $values) { + if (array_key_exists($values['name'], $params)) { + $finalfields[] = $values; + } + } // This derives heavily from the function "_civicrm_api3_validate_fields". // However, the difference is that try-catch blocks are nested in the loop, making it // possible for us to get all errors in one go. - foreach ($fields as $fieldInfo) { + foreach ($finalfields as $fieldInfo) { + $fieldName = $fieldInfo['name']; try { - _civicrm_api3_validate_switch_cases($fieldInfo, $entity, $params); + _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params); } catch (Exception $e) { - $errors[$fieldName] = $e->getMessage(); + $errors[$fieldName] = array( + 'message' => $e->getMessage(), + 'code' => 'incorrect_value', + ); } } @@ -1582,8 +1594,7 @@ function _civicrm_api3_validate($entity, $action, $params) { * * @throws Exception */ -function _civicrm_api3_validate_switch_cases($fieldInfo, $entity, $params) { - $feildName = $fieldInfo['name']; +function _civicrm_api3_validate_switch_cases($fieldName, $fieldInfo, $entity, $params) { switch (CRM_Utils_Array::value('type', $fieldInfo)) { case CRM_Utils_Type::T_INT: _civicrm_api3_validate_integer($params, $fieldName, $fieldInfo, $entity); @@ -1606,9 +1617,7 @@ function _civicrm_api3_validate_switch_cases($fieldInfo, $entity, $params) { case CRM_Utils_Type::T_MONEY: list($fieldValue, $op) = _civicrm_api3_field_value_check($params, $fieldName); - if (strpos($op, 'NULL') !== FALSE || strpos($op, 'EMPTY') !== FALSE) { - break; - } + foreach ((array) $fieldValue as $fieldvalue) { if (!CRM_Utils_Rule::money($fieldvalue) && !empty($fieldvalue)) { throw new Exception($fieldName . " is not a valid amount: " . $params[$fieldName]); diff --git a/tests/phpunit/api/v3/ValidateTest.php b/tests/phpunit/api/v3/ValidateTest.php new file mode 100644 index 0000000000..62db6f9b41 --- /dev/null +++ b/tests/phpunit/api/v3/ValidateTest.php @@ -0,0 +1,69 @@ +callAPISuccess('Contact', 'validate', array('action' => "create")); + $expectedOut = array( + 'contact_type' => array( + 'message' => "Mandatory key(s) missing from params array: contact_type", + 'code' => "mandatory_missing", + ), + ); + $this->assertEquals($validation['values'][0], $expectedOut); + } + + public function testContributionValidate() { + $validation = $this->callAPISuccess('Contribution', 'validate', array('action' => "create", 'total_amount' => "100w")); + $totalAmountErrors = array( + 'message' => "total_amount is not a valid amount: 100w", + 'code' => "incorrect_value", + ); + + $contactIdErrors = array( + 'message' => "Mandatory key(s) missing from params array: contact_id", + 'code' => "mandatory_missing", + ); + + $this->assertEquals($validation['values'][0]['total_amount'], $totalAmountErrors); + $this->assertEquals($validation['values'][0]['contact_id'], $contactIdErrors); + } + +} -- 2.25.1