APIv4 - Fix getFields returning default value of integer fields
authorcolemanw <coleman@civicrm.org>
Sun, 1 Oct 2023 22:17:12 +0000 (18:17 -0400)
committercolemanw <coleman@civicrm.org>
Tue, 3 Oct 2023 01:20:40 +0000 (21:20 -0400)
CRM/Utils/Type.php
Civi/Api4/Service/Spec/SpecFormatter.php
tests/phpunit/api/v4/Action/GetFieldsTest.php

index 510718060bc59599b1b1a2e8b11fbb46b21266ce..ea610939de198efc46cbb6b102542ea2de67c940 100644 (file)
@@ -70,7 +70,7 @@ class CRM_Utils_Type {
    * @return string
    *   String identifying the data type, e.g. 'Int' or 'String'.
    */
-  public static function typeToString($type) {
+  public static function typeToString($type): string {
     // @todo Use constants in the case statements, e.g. "case T_INT:".
     // @todo return directly, instead of assigning a value.
     // @todo Use a lookup array, as a property or as a local variable.
@@ -134,7 +134,7 @@ class CRM_Utils_Type {
         break;
     }
 
-    return (isset($string)) ? $string : "";
+    return $string ?? '';
   }
 
   /**
index d2c699192ab2de7ae1af0a7a89be99f2b8ea63e7..622a11178bbae6aa610ea76807c6019aeaa708e5 100644 (file)
@@ -130,14 +130,15 @@ class SpecFormatter {
    * @return string
    */
   private static function getDataType(array $data) {
-    if (isset($data['data_type'])) {
-      return !empty($data['time_format']) ? 'Timestamp' : $data['data_type'];
+    $dataType = $data['data_type'] ?? $data['dataType'] ?? NULL;
+    if (isset($dataType)) {
+      return !empty($data['time_format']) ? 'Timestamp' : $dataType;
     }
 
     $dataTypeInt = $data['type'] ?? NULL;
     $dataTypeName = \CRM_Utils_Type::typeToString($dataTypeInt);
 
-    return $dataTypeName;
+    return $dataTypeName === 'Int' ? 'Integer' : $dataTypeName;
   }
 
   /**
@@ -303,10 +304,10 @@ class SpecFormatter {
       self::setLegacyDateFormat($inputAttrs);
     }
     // Number input for numeric fields
-    if ($inputType === 'Text' && in_array($dataTypeName, ['Int', 'Float'], TRUE)) {
+    if ($inputType === 'Text' && in_array($dataTypeName, ['Integer', 'Float'], TRUE)) {
       $inputType = 'Number';
       // Todo: make 'step' configurable for the custom field
-      $inputAttrs['step'] = $dataTypeName === 'Int' ? 1 : .01;
+      $inputAttrs['step'] = $dataTypeName === 'Integer' ? 1 : .01;
     }
     // Date/time settings from custom fields
     if ($inputType == 'Date' && !empty($data['custom_group_id'])) {
index b12e26e068821ec5dbd468e48af6a8f7dc23e495..9d148e8a8cc46729679c2dc76264c4939a4b9248 100644 (file)
@@ -29,6 +29,7 @@ use Civi\Api4\Contribution;
 use Civi\Api4\CustomGroup;
 use Civi\Api4\Email;
 use Civi\Api4\EntityTag;
+use Civi\Api4\OptionValue;
 use Civi\Api4\UserJob;
 use Civi\Api4\Utils\CoreUtil;
 use Civi\Test\TransactionalInterface;
@@ -176,6 +177,12 @@ class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
     $this->assertTrue($aclFields['is_active']['default_value']);
     $this->assertFalse($aclFields['is_active']['nullable']);
     $this->assertFalse($aclFields['is_active']['required']);
+
+    $optionValueFields = OptionValue::getFields(FALSE)
+      ->setAction('create')
+      ->execute()->indexBy('name');
+    $this->assertIsInt($optionValueFields['filter']['default_value']);
+    $this->assertEquals(0, $optionValueFields['filter']['default_value']);
   }
 
   public function testGetSuffixes(): void {