From 6f408d71109896447f74b6a7184db2fc8a547bd9 Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 3 Feb 2016 22:54:28 +1300 Subject: [PATCH] CRM-17951 Add function to safe-add option values in upgrade --- CRM/Core/BAO/OptionValue.php | 20 ++++- .../phpunit/CRM/Core/BAO/OptionValueTest.php | 84 +++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/CRM/Core/BAO/OptionValueTest.php diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 04b0e38237..0e3bb21dc5 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -169,7 +169,7 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { * * @return CRM_Core_DAO_OptionValue */ - public static function add(&$params, &$ids) { + public static function add(&$params, $ids) { // CRM-10921: do not reset attributes to default if this is an update //@todo consider if defaults are being set in the right place. 'dumb' defaults like // these would be usefully set @ the api layer so they are visible to api users @@ -519,4 +519,22 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { return $options; } + /** + * Ensure an option value exists. + * + * This function is intended to be called from the upgrade script to ensure + * that an option value exists, without hitting an error if it already exists. + * + * This is sympathetic to sites who might pre-add it. + */ + public static function ensureOptionValueExists($params) { + $existingValues = civicrm_api3('OptionValue', 'get', array( + 'option_group_name' => $params['option_group_id'], + 'name' => $params['name'], + )); + if (!$existingValues['count']) { + civicrm_api3('OptionValue', 'create', $params); + } + } + } diff --git a/tests/phpunit/CRM/Core/BAO/OptionValueTest.php b/tests/phpunit/CRM/Core/BAO/OptionValueTest.php new file mode 100644 index 0000000000..fe385b74cd --- /dev/null +++ b/tests/phpunit/CRM/Core/BAO/OptionValueTest.php @@ -0,0 +1,84 @@ +useTransaction(TRUE); + } + + /** + * Ensure only one option value exists after calling ensureOptionValueExists. + */ + public function testEnsureOptionValueExistsExistingValue() { + CRM_Core_BAO_OptionValue::ensureOptionValueExists(array('name' => 'Completed', 'option_group_id' => 'contribution_status')); + $this->callAPISuccessGetSingle('OptionValue', array('name' => 'Completed', 'option_group_id' => 'contribution_status')); + } + + /** + * Ensure only one option value exists adds a new value. + */ + public function testEnsureOptionValueExistsNewValue() { + CRM_Core_BAO_OptionValue::ensureOptionValueExists(array('name' => 'Bombed', 'option_group_id' => 'contribution_status')); + $optionValues = $this->callAPISuccess('OptionValue', 'get', array('option_group_id' => 'contribution_status')); + foreach ($optionValues['values'] as $value) { + if ($value['name'] == 'Bombed') { + return; + } + } + $this->fail('Should not have gotten this far'); + } + + + /** + * Ensure only one option value copes with disabled. + * + * (Our expectation is no change - ie. currently we are respecting 'someone's + * decision to disable it & leaving it in that state. + */ + public function testEnsureOptionValueExistsDisabled() { + CRM_Core_BAO_OptionValue::ensureOptionValueExists(array('name' => 'Crashed', 'option_group_id' => 'contribution_status', 'is_active' => 0)); + $value = $this->callAPISuccessGetSingle('OptionValue', array('name' => 'Crashed', 'option_group_id' => 'contribution_status')); + $this->assertEquals(0, $value['is_active']); + CRM_Core_BAO_OptionValue::ensureOptionValueExists(array('name' => 'Crashed', 'option_group_id' => 'contribution_status')); + $value = $this->callAPISuccessGetSingle('OptionValue', array('name' => 'Crashed', 'option_group_id' => 'contribution_status')); + $this->assertEquals(0, $value['is_active']); + } + +} -- 2.25.1