From: eileen Date: Tue, 17 May 2016 01:05:23 +0000 (+1200) Subject: Add tests for Ensure OptionGroup exists fn & fix optionValueExists to accept ID X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=bf87703e8feafd825f623278f25844bddbcb6e05;p=civicrm-core.git Add tests for Ensure OptionGroup exists fn & fix optionValueExists to accept ID The option_group_id field accepts name or id so use that. --- diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 9eabbf380c..a529d1b18d 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -529,7 +529,7 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { */ public static function ensureOptionValueExists($params) { $existingValues = civicrm_api3('OptionValue', 'get', array( - 'option_group_name' => $params['option_group_id'], + 'option_group_id' => $params['option_group_id'], 'name' => $params['name'], )); if (!$existingValues['count']) { diff --git a/tests/phpunit/api/v3/OptionGroupTest.php b/tests/phpunit/api/v3/OptionGroupTest.php index da261662dd..1e65e3f2cc 100644 --- a/tests/phpunit/api/v3/OptionGroupTest.php +++ b/tests/phpunit/api/v3/OptionGroupTest.php @@ -169,4 +169,26 @@ class api_v3_OptionGroupTest extends CiviUnitTestCase { $this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__); } + /** + * Ensure only one option value exists after calling ensureOptionValueExists. + */ + public function testEnsureOptionGroupExistsExistingValue() { + CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array('name' => 'participant_role')); + $this->callAPISuccessGetSingle('OptionGroup', array('name' => 'participant_role')); + } + + /** + * Ensure only one option value exists adds a new value. + */ + public function testEnsureOptionGroupExistsNewValue() { + $optionGroupID = CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(array( + 'name' => 'Bombed', + 'title' => ts('Catastrophy'), + 'description' => ts('blah blah'), + 'is_reserved' => 1, + )); + $optionGroup = $this->callAPISuccessGetSingle('OptionGroup', array('name' => 'Bombed')); + $this->assertEquals($optionGroupID, $optionGroup['id']); + } + }