Add unit test for getdisplayvalue api
authorJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 20 Sep 2019 06:09:55 +0000 (11:39 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 20 Sep 2019 06:20:00 +0000 (11:50 +0530)
api/v3/CustomValue.php
tests/phpunit/api/v3/CustomValueTest.php

index 48c3f93b858367cfae24c2a0eb2870ed11916841..be44a1b6b31955f061d36b88ad101fa7075935c7 100644 (file)
@@ -394,3 +394,50 @@ function civicrm_api3_custom_value_gettree($params) {
   }
   return civicrm_api3_create_success($result, $params, 'CustomValue', 'gettree');
 }
+
+/**
+ * CustomValue.getdisplayvalue API specification
+ *
+ * @param array $spec description of fields supported by this API call
+ */
+function _civicrm_api3_custom_value_getdisplayvalue_spec(&$spec) {
+  $spec['entity_id'] = [
+    'title' => 'Entity Id',
+    'description' => 'Id of entity',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => 1,
+  ];
+  $spec['custom_field_id'] = [
+    'title' => 'Custom Field ID',
+    'description' => 'Id of custom field',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => 1,
+  ];
+  $spec['custom_field_value'] = [
+    'title' => 'Custom Field value',
+    'description' => 'Specify the value of the custom field to return as displayed value',
+    'type' => CRM_Utils_Type::T_STRING,
+    'api.required' => 0,
+  ];
+}
+
+/**
+ * CustomValue.getdisplayvalue API
+ *
+ * @param array $params
+ *
+ * @return array API result
+ * @throws \CiviCRM_API3_Exception
+ */
+function civicrm_api3_custom_value_getdisplayvalue($params) {
+  if (empty($params['custom_field_value'])) {
+    $params['custom_field_value'] = civicrm_api3('CustomValue', 'getsingle', [
+      'return' => ["custom_{$params['custom_field_id']}"],
+      'entity_id' => $params['entity_id'],
+    ]);
+    $params['custom_field_value'] = $params['custom_field_value']['latest'];
+  }
+  $values[$params['custom_field_id']]['display'] = CRM_Core_BAO_CustomField::displayValue($params['custom_field_value'], $params['custom_field_id'], CRM_Utils_Array::value('entity_id', $params));
+  $values[$params['custom_field_id']]['raw'] = $params['custom_field_value'];
+  return civicrm_api3_create_success($values, $params, 'CustomValue', 'getdisplayvalue');
+}
index b037e91f4b2903eed76fe7c6de2cb8753ce4be8f..378150e185187b518598acf91f987841908a6ff5 100644 (file)
@@ -620,4 +620,35 @@ class api_v3_CustomValueTest extends CiviUnitTestCase {
     $this->assertEquals(count($customFieldValues), $result['count']);
   }
 
+  /**
+   * Test getdisplayvalue api and verify if it returns
+   * the custom text for display.
+   */
+  public function testGetDisplayValue() {
+    list($cid, $customFieldValues) = $this->_testGetCustomValueMultiple();
+    foreach ($customFieldValues as $field => $value) {
+      list(, $customFieldID) = explode("_", $field);
+      $result = $this->callAPISuccess('CustomValue', 'getdisplayvalue', [
+        'entity_id' => $cid,
+        'custom_field_id' => $customFieldID,
+      ]);
+      $expectedValue = [
+        'display' => $value,
+        'raw' => $value,
+      ];
+      $this->checkArrayEquals($result['values'][$customFieldID], $expectedValue);
+
+      $customDisplayValue = $this->callAPISuccess('CustomValue', 'getdisplayvalue', [
+        'entity_id' => $cid,
+        'custom_field_id' => $customFieldID,
+        'custom_field_value' => "Test Custom Display - {$value}",
+      ]);
+      $expectedValue = [
+        'display' => "Test Custom Display - {$value}",
+        'raw' => "Test Custom Display - {$value}",
+      ];
+      $this->checkArrayEquals($customDisplayValue['values'][$customFieldID], $expectedValue);
+    }
+  }
+
 }