From 89ab56014f9cc892fa8a7c857547c0b4b88de241 Mon Sep 17 00:00:00 2001 From: Pratik Joshi Date: Fri, 22 Nov 2013 19:25:19 +0530 Subject: [PATCH] CRM-13814 --- CRM/Core/BAO/OptionValue.php | 13 ++++++- api/v3/OptionValue.php | 15 ++++++-- api/v3/examples/OptionValue/SortOption.php | 2 +- tests/phpunit/api/v3/OptionValueTest.php | 40 +++++++++++++++++++++- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index fab3a17efd..97da09c945 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -195,8 +195,19 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { $p = array(1 => array($params['option_group_id'], 'Integer')); CRM_Core_DAO::executeQuery($query, $p); } + + // CRM-13814 : evalute option group id + if (!array_key_exists('option_group_id', $params) && !empty($ids['optionValue'])) { + $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', + $ids['optionValue'], 'option_group_id', 'id' + ); + } + else { + $groupId = $params['option_group_id']; + } + $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', - $params['option_group_id'], 'name', 'id' + $groupId, 'name', 'id' ); if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) { $optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID()); diff --git a/api/v3/OptionValue.php b/api/v3/OptionValue.php index 772a0d5465..ad2bd40292 100644 --- a/api/v3/OptionValue.php +++ b/api/v3/OptionValue.php @@ -38,9 +38,20 @@ function civicrm_api3_option_value_get($params) { * @access public */ function civicrm_api3_option_value_create($params) { - $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params); - civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $params['option_group_id'])); + + // CRM-13814 : evalute option group id + // option group id would be passed in case of adding a new option value record + if (!empty($params['id']) && !array_key_exists('option_group_id', $params)) { + $groupId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionValue', + $params['id'], 'option_group_id', 'id' + ); + } + else { + $groupId = $params['option_group_id']; + } + + civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $groupId)); return $result; } diff --git a/api/v3/examples/OptionValue/SortOption.php b/api/v3/examples/OptionValue/SortOption.php index 367e3ce6b0..f93dbdd08d 100644 --- a/api/v3/examples/OptionValue/SortOption.php +++ b/api/v3/examples/OptionValue/SortOption.php @@ -67,4 +67,4 @@ function option_value_getsingle_expectedresult(){ * * API Standards documentation: * http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards -*/ \ No newline at end of file +*/ diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index 672def2d32..201c887975 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -278,5 +278,43 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id')); $this->assertFalse(in_array($newOption, $fields['values'])); } -} + + /* + * update option value with 'id' paramter and the value to update + * and not passing option group id + */ + public function testUpdateOptionValueNoGroupId() { + // create a option group + $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group', 'is_active' => 1)); + // create a option value + $ov = $this->callAPISuccess('option_value', 'create', + array('option_group_id' => $og['id'], 'label' => 'test option value') + ); + // update option value without 'option_group_id' + $res = $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'is_active' => 0)); + $val = $this->callAPISuccess('option_value', 'getvalue', array( + 'id' => $ov['id'], 'return' => 'is_active', + )); + $this->assertEquals($val, 0, "update with no group id is not proper" . __LINE__); + } + + /* + * update option value with 'id' paramter and the value to update + * and as well as option group id + */ + public function testUpdateOptionValueWithGroupId() { + // create a option group + $og = $this->callAPISuccess('option_group', 'create', array('name' => 'our test Option Group for with group id', 'is_active' => 1)); + // create a option value + $ov = $this->callAPISuccess('option_value', 'create', + array('option_group_id' => $og['id'], 'label' => 'test option value') + ); + // update option value without 'option_group_id' + $this->callAPISuccess('option_value', 'create', array('id' => $ov['id'], 'option_group_id' => $og['id'], 'is_active' => 0)); + $val = $this->callAPISuccess('option_value', 'getvalue', array( + 'id' => $ov['id'], 'return' => 'is_active', + )); + $this->assertEquals($val, 0, "update with group id is not proper " . __LINE__); + } +} \ No newline at end of file -- 2.25.1