From 3f49e80a6a229f3e6f6218a9d12b79be0557ff81 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 1 Oct 2023 18:17:12 -0400 Subject: [PATCH] APIv4 - Fix getFields returning default value of integer fields --- CRM/Utils/Type.php | 4 ++-- Civi/Api4/Service/Spec/SpecFormatter.php | 11 ++++++----- tests/phpunit/api/v4/Action/GetFieldsTest.php | 7 +++++++ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/CRM/Utils/Type.php b/CRM/Utils/Type.php index 510718060b..ea610939de 100644 --- a/CRM/Utils/Type.php +++ b/CRM/Utils/Type.php @@ -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 ?? ''; } /** diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index d2c699192a..622a11178b 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -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'])) { diff --git a/tests/phpunit/api/v4/Action/GetFieldsTest.php b/tests/phpunit/api/v4/Action/GetFieldsTest.php index b12e26e068..9d148e8a8c 100644 --- a/tests/phpunit/api/v4/Action/GetFieldsTest.php +++ b/tests/phpunit/api/v4/Action/GetFieldsTest.php @@ -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 { -- 2.25.1