From 7cbde1aa1e9e992980e0998b9c7cb70c001f2866 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 12 Aug 2018 19:16:12 +1200 Subject: [PATCH] Fix option group caching issue. Per https://lab.civicrm.org/dev/core/issues/304 in some cases the api cache has already been built so adding a new option_group is not picked up causing a crash --- api/v3/OptionGroup.php | 4 +- .../phpunit/CRM/Core/BAO/OptionGroupTest.php | 66 +++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/CRM/Core/BAO/OptionGroupTest.php diff --git a/api/v3/OptionGroup.php b/api/v3/OptionGroup.php index 24822c8520..f86c3a21bb 100644 --- a/api/v3/OptionGroup.php +++ b/api/v3/OptionGroup.php @@ -53,7 +53,9 @@ function civicrm_api3_option_group_get($params) { * @return array */ function civicrm_api3_option_group_create($params) { - return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionGroup'); + $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'OptionGroup'); + civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1)); + return $result; } /** diff --git a/tests/phpunit/CRM/Core/BAO/OptionGroupTest.php b/tests/phpunit/CRM/Core/BAO/OptionGroupTest.php new file mode 100644 index 0000000000..d4c9894b8d --- /dev/null +++ b/tests/phpunit/CRM/Core/BAO/OptionGroupTest.php @@ -0,0 +1,66 @@ +useTransaction(TRUE); + } + + /** + * Ensure only one option value exists after calling ensureOptionValueExists. + */ + public function testEnsureOptionGroupExistsExistingValue() { + CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array('name' => 'contribution_status')); + $this->callAPISuccessGetSingle('OptionGroup', array('name' => 'contribution_status')); + } + + /** + * Ensure only one option value exists adds a new value. + */ + public function testEnsureOptionGroupExistsNewValue() { + CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array('name' => 'Bombed')); + $optionGroups = $this->callAPISuccess('OptionValue', 'getoptions', array('field' => 'option_group_id'))['values']; + $this->assertTrue(in_array('Bombed', $optionGroups)); + + CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array('name' => 'Bombed Again')); + $optionGroups = $this->callAPISuccess('OptionValue', 'getoptions', array('field' => 'option_group_id'))['values']; + $this->assertTrue(in_array('Bombed Again', $optionGroups)); + } + +} -- 2.25.1