From aa4143bb3ef0cf2fe13228942568dc6c458aa059 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 9 Jul 2019 10:41:20 +1200 Subject: [PATCH] Fix obscure bug on updating custom fields (not necessarily hittable via UI) This fixes a bug which I think only applies to programatically created custom fields -basically if the field has an empty option_group_id and then you update it to have one it does a check to see if the existing option_group_id is in use and does not handle the fact the field is empty - leading to a mysql error. --- CRM/Core/BAO/CustomField.php | 2 +- tests/phpunit/api/v3/CustomFieldTest.php | 43 ++++++++++++++++++------ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/CRM/Core/BAO/CustomField.php b/CRM/Core/BAO/CustomField.php index 8d23280394..1dc707ecd6 100644 --- a/CRM/Core/BAO/CustomField.php +++ b/CRM/Core/BAO/CustomField.php @@ -2156,7 +2156,7 @@ INNER JOIN civicrm_custom_field f ON ( g.id = f.option_group_id ) ); // get the updated option group // if both are same return - if ($currentOptionGroupId == $optionGroupId) { + if (!$currentOptionGroupId || $currentOptionGroupId == $optionGroupId) { return; } diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index 6e822b1bf1..b05a439e11 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -33,21 +33,18 @@ */ class api_v3_CustomFieldTest extends CiviUnitTestCase { - protected $_apiversion; - - public function setUp() { - $this->_apiversion = 3; - parent::setUp(); - } - + /** + * Clean up after test. + * + * @throws \CRM_Core_Exception + */ public function tearDown() { - $tablesToTruncate = [ + $this->quickCleanup([ 'civicrm_contact', 'civicrm_file', 'civicrm_entity_file', - ]; - // true tells quickCleanup to drop custom_value tables that might have been created in the test - $this->quickCleanup($tablesToTruncate, TRUE); + ], TRUE); + parent::tearDown(); } /** @@ -300,6 +297,30 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { $this->assertEquals($optionGroupID, 3); } + /** + * Test adding an optionGroup to an existing field doesn't cause a fatal error. + * + * (this was happening due to a check running despite no existing option_group_id) + * + * @throws \CiviCRM_API3_Exception + */ + public function testUpdateCustomFieldAddOptionGroup() { + $customGroup = $this->customGroupCreate(['extends' => 'Organization', 'title' => 'test_group']); + $params = [ + 'custom_group_id' => $customGroup['id'], + 'label' => 'Organization Gender', + 'html_type' => 'Text', + 'data_type' => 'Int', + ]; + + $customField = $this->callAPISuccess('custom_field', 'create', $params); + $this->callAPISuccess('CustomField', 'create', [ + 'option_group_id' => civicrm_api3('OptionGroup', 'getvalue', ['options' => ['limit' => 1], 'return' => 'id']), + 'id' => $customField['id'], + 'html_type' => 'Select', + ]); + } + /** * Test custom field get works & return param works */ -- 2.25.1