From fbb5ba18ac150e5077bb1fc01d1cce8840113dd1 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Fri, 21 Apr 2023 17:23:29 -0400 Subject: [PATCH] empty pseudoconstant string on create causes an error --- Civi/Api4/Generic/Traits/DAOActionTrait.php | 10 +++---- .../api/v4/Custom/BasicCustomFieldTest.php | 30 +++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index 9ef9c08bb4..55785c8af0 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -240,16 +240,16 @@ trait DAOActionTrait { continue; } - // Null and empty string are interchangeable as far as the custom bao understands - if (NULL === $value) { - $value = ''; - } - if ($field['suffix']) { $options = FormattingUtil::getPseudoconstantList($field, $name, $params, $this->getActionName()); $value = FormattingUtil::replacePseudoconstant($options, $value, TRUE); } + // Null and empty string are interchangeable as far as the custom bao understands + if (NULL === $value) { + $value = ''; + } + if ($field['html_type'] === 'CheckBox') { // this function should be part of a class formatCheckBoxField($value, 'custom_' . $field['id'], $this->getEntityName()); diff --git a/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php b/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php index d086558d27..503923fb5b 100644 --- a/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php +++ b/tests/phpunit/api/v4/Custom/BasicCustomFieldTest.php @@ -419,6 +419,36 @@ class BasicCustomFieldTest extends CustomTestBase { $this->assertEquals($optionGroupCount, OptionGroup::get(FALSE)->selectRowCount()->execute()->count()); } + /** + * Pseudoconstant lookups that are passed an empty string return NULL, not an empty string. + * @throws \CRM_Core_Exception + */ + public function testPseudoConstantCreate(): void { + $optionGroupId = $this->createTestRecord('OptionGroup')['id']; + $this->createTestRecord('OptionValue', ['option_group_id' => $optionGroupId]); + + $customGroup = CustomGroup::create(FALSE) + ->addValue('title', 'MyIndividualFields') + ->addValue('extends', 'Individual') + ->execute() + ->first(); + + CustomField::create(FALSE) + ->addValue('label', 'FavMovie') + ->addValue('custom_group_id', $customGroup['id']) + ->addValue('html_type', 'Select') + ->addValue('data_type', 'String') + ->addValue('option_group_id', $optionGroupId) + ->execute(); + + Contact::create(FALSE) + ->addValue('first_name', 'Johann') + ->addValue('last_name', 'Tester') + ->addValue('contact_type', 'Individual') + ->addValue('MyIndividualFields.FavMovie:label', '') + ->execute(); + } + public function testUpdateWeights() { $getValues = function($groupName) { return CustomField::get(FALSE) -- 2.25.1