dev/core#149 - Fatal Error on customvalue get api
authorJitendra Purohit <jitendra@fuzion.co.nz>
Tue, 29 May 2018 10:39:15 +0000 (16:09 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Thu, 31 May 2018 06:55:19 +0000 (12:25 +0530)
api/v3/CustomValue.php
tests/phpunit/api/v3/CustomValueTest.php

index e5371f8feaf6d080587a2fbeebb7bcc1061dbbd9..20aa18c51880299636d94f8bcec81b8bf973285e 100644 (file)
@@ -145,22 +145,27 @@ function civicrm_api3_custom_value_get($params) {
   unset($params['entity_id'], $params['entity_table']);
   foreach ($params as $id => $param) {
     if ($param && substr($id, 0, 6) == 'return') {
-      $id = substr($id, 7);
-      list($c, $i) = CRM_Utils_System::explode('_', $id, 2);
-      if ($c == 'custom' && is_numeric($i)) {
-        $names['custom_' . $i] = 'custom_' . $i;
-        $id = $i;
+      $returnVal = $param;
+      if (!empty(substr($id, 7))) {
+        $returnVal = substr($id, 7);
       }
-      else {
-        // Lookup names if ID was not supplied
-        list($group, $field) = CRM_Utils_System::explode(':', $id, 2);
-        $id = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
-        if (!$id) {
-          continue;
+      foreach ((array) $returnVal as $value) {
+        list($c, $i) = CRM_Utils_System::explode('_', $value, 2);
+        if ($c == 'custom' && is_numeric($i)) {
+          $names['custom_' . $i] = 'custom_' . $i;
+          $fldId = $i;
+        }
+        else {
+          // Lookup names if ID was not supplied
+          list($group, $field) = CRM_Utils_System::explode(':', $value, 2);
+          $fldId = CRM_Core_BAO_CustomField::getCustomFieldID($field, $group);
+          if (!$fldId) {
+            continue;
+          }
+          $names['custom_' . $fldId] = 'custom_' . $i;
         }
-        $names['custom_' . $id] = 'custom_' . $i;
+        $getParams['custom_' . $fldId] = 1;
       }
-      $getParams['custom_' . $id] = 1;
     }
   }
 
index 791eea0344ce105e80b9c8380bc3252d70b8d11e..9cf28c8325d8670c565c6fcd6f86ce7cb395a8d1 100644 (file)
@@ -230,6 +230,30 @@ class api_v3_CustomValueTest extends CiviUnitTestCase {
     $params = array('entity_id' => $contactId, 'custom_' . $customId => $selectedValue);
     $this->callAPISuccess('CustomValue', 'create', $params);
 
+    //Test for different return value syntax.
+    $returnValues = [
+      ['return' => "custom_{$customId}"],
+      ['return' => ["custom_{$customId}"]],
+      ["return.custom_{$customId}" => 1],
+    ];
+    foreach ($returnValues as $val) {
+      $params = array_merge($val, [
+        'sequential' => 1,
+        'entity_id' => $contactId,
+      ]);
+      $customValue = $this->callAPISuccess('CustomValue', 'get', $params);
+      if (is_array($selectedValue)) {
+        $expected = array_values($selectedValue);
+        $this->checkArrayEquals($expected, $customValue['values'][0]['latest']);
+      }
+      elseif ($type == 'date') {
+        $this->assertEquals($selectedValue, date('Ymd', strtotime(str_replace('.', '/', $customValue['values'][0]['latest']))));
+      }
+      else {
+        $this->assertEquals($selectedValue, $customValue['values'][0]['latest']);
+      }
+    }
+
     foreach ($sqlOps as $op) {
       $qillOp = CRM_Utils_Array::value($op, CRM_Core_SelectValues::getSearchBuilderOperators(), $op);
       switch ($op) {