From bdb24928de2d00a6a38f181bccd28e4fb1132fb4 Mon Sep 17 00:00:00 2001 From: Jon Goldberg Date: Fri, 12 Mar 2021 12:07:51 -0500 Subject: [PATCH] Fix custom fields changed from multiple-choice data type to Text --- CRM/Core/BAO/CustomField.php | 5 ++++ .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index b27bad0c39..f45707ca96 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -1994,6 +1994,11 @@ WHERE id IN ( %1, %2 ) } } + // Remove option group IDs from fields changed to Text html_type. + if ($htmlType == 'Text') { + $params['option_group_id'] = ''; + } + // check for orphan option groups if (!empty($params['option_group_id'])) { if (!empty($params['id'])) { diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 92742dc796..70678998fd 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -50,6 +50,34 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->customGroupDelete($customGroup['id']); } + /** + * Test changing a data type from multiple-choice to Text. + */ + public function testChangeDataType() { + $customGroup = $this->createCustomField(); + $fields = [ + 'label' => 'Radio to Text', + 'is_active' => 1, + 'data_type' => 'String', + 'html_type' => 'Radio', + 'custom_group_id' => $customGroup['id'], + 'option_type' => 1, + 'option_label' => ["One", "Two"], + 'option_value' => [1, 2], + 'option_weight' => [1, 2], + 'option_status' => [1, 1], + ]; + $customField = CRM_Core_BAO_CustomField::create($fields); + $this->assertNotNull($customField->option_group_id); + $fieldsNew = [ + 'id' => $customField->id, + 'html_type' => 'Text', + 'custom_group_id' => $customGroup['id'], + ]; + $customFieldModified = CRM_Core_BAO_CustomField::create($fieldsNew); + $this->assertFalse($customFieldModified->option_group_id ?? FALSE); + } + /** * Test custom field create accepts passed column name. */ -- 2.25.1