add tests and cleanup
authorSaurabh Batra <saurabh.batra96@gmail.com>
Mon, 1 Aug 2016 07:47:40 +0000 (13:17 +0530)
committerSaurabh Batra <saurabh.batra96@gmail.com>
Wed, 3 Aug 2016 06:47:58 +0000 (12:17 +0530)
api/v3/Generic/Validate.php
api/v3/utils.php
tests/phpunit/api/v3/ValidateTest.php [new file with mode: 0644]

index 48ce2c477a21cfd78a637de89eb7cc88499a34e1..7b09ff3588338fa3ba1d7112aa85e216c781cdfa 100644 (file)
@@ -36,6 +36,7 @@
  */
 function _civicrm_api3_generic_validate_spec(&$params) {
   $params['action']['api.required'] = TRUE;
+  $params['action']['title'] = ts('API Action');
 }
 
 /**
index 68f98e569ceb59e892dbee54ef2f0c3eda7916e8..b11f4f2a55ac71006113961beeb32514570fd553 100644 (file)
@@ -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 (file)
index 0000000..62db6f9
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+/*
+ +--------------------------------------------------------------------+
+ | CiviCRM version 4.7                                                |
+ +--------------------------------------------------------------------+
+ | Copyright CiviCRM LLC (c) 2004-2016                                |
+ +--------------------------------------------------------------------+
+ | This file is a part of CiviCRM.                                    |
+ |                                                                    |
+ | CiviCRM is free software; you can copy, modify, and distribute it  |
+ | under the terms of the GNU Affero General Public License           |
+ | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
+ |                                                                    |
+ | CiviCRM is distributed in the hope that it will be useful, but     |
+ | WITHOUT ANY WARRANTY; without even the implied warranty of         |
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
+ | See the GNU Affero General Public License for more details.        |
+ |                                                                    |
+ | You should have received a copy of the GNU Affero General Public   |
+ | License and the CiviCRM Licensing Exception along                  |
+ | with this program; if not, contact CiviCRM LLC                     |
+ | at info[AT]civicrm[DOT]org. If you have questions about the        |
+ | GNU Affero General Public License or the licensing of CiviCRM,     |
+ | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
+ +--------------------------------------------------------------------+
+ */
+
+/**
+ *  Tests for the generic validate API action.
+ *
+ * @package CiviCRM_APIv3
+ * @group headless
+ */
+class api_v3_ValidateTest extends CiviUnitTestCase {
+  /**
+   * This method is called before a test is executed.
+   */
+  protected function setUp() {
+    parent::setUp();
+  }
+
+  public function testEmptyContactValidate() {
+    $validation = $this->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);
+  }
+
+}