From c87bbced1f1c87615233443f1ff52fc840ed2ca3 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 19 Mar 2013 11:36:43 +1300 Subject: [PATCH] CRM-12133 option value create value & weight CRM-12133 fix default handling to be in BAO layer, also add tests on component_id & add to xml --- CRM/Core/BAO/OptionValue.php | 52 ++++++++++++- api/v3/OptionValue.php | 48 ++---------- tests/phpunit/api/v3/OptionValueTest.php | 95 +++++++++++++++++++++++- xml/schema/Core/OptionValue.xml | 8 +- 4 files changed, 156 insertions(+), 47 deletions(-) diff --git a/CRM/Core/BAO/OptionValue.php b/CRM/Core/BAO/OptionValue.php index 0340feabbc..c058f44008 100644 --- a/CRM/Core/BAO/OptionValue.php +++ b/CRM/Core/BAO/OptionValue.php @@ -40,7 +40,57 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue { function __construct() { parent::__construct(); } - + /* + * Create email address - note that the create function calls 'add' but + * has more business logic + * Note that this is the right place to add pre & post hooks if we want them + * ? any reason not to? + * + * @param array $params input parameters + */ + static function create($params) { + if (empty($params['id'])){ + self::setDefaults($params); + } + $ids = array(); + if (CRM_Utils_Array::value('id', $params)) { + $ids = array('optionValue' => $params['id']); + } + return CRM_Core_BAO_OptionValue::add($params, $ids); + ; + } + /** + * Set default Parameters + * This functions sets default parameters if not set: + * - name & label are set to each other as required (it might make more sense for one + * to be required but this would mean a change to the api level) + * - ditto weight & value - but they both default to next weight + * NB am not sure that weight should be set to value as higher priority to + * setting it to the next weight - although this is existing logic + * + * @param unknown_type $params + */ + static function setDefaults(&$params){ + if(empty($params['label'])){ + $params['label'] = $params['name']; + } + if(empty($params['name'])){ + $params['name'] = $params['label']; + } + if(empty($params['weight'])){ + //@todo consider this logic - see block comments + $params['weight'] = CRM_Utils_Array::value('value', $params, + (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', + array('option_group_id' => $params['option_group_id'])) + ); + } + if(empty($params['value'])){ + $params['value'] = CRM_Utils_Array::value('weight', $params, + (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', + array('option_group_id' => $params['option_group_id'])) + ); + } + } /** * Takes a bunch of params that are needed to match certain criteria and * retrieves the relevant objects. Typically the valid params are only diff --git a/api/v3/OptionValue.php b/api/v3/OptionValue.php index 30b7e26be1..de9c613949 100644 --- a/api/v3/OptionValue.php +++ b/api/v3/OptionValue.php @@ -40,47 +40,9 @@ function civicrm_api3_option_value_get($params) { */ function civicrm_api3_option_value_create($params) { - // CRM-10921: do not fill-in defaults if this is an update - if (!CRM_Utils_Array::value('id', $params)) { - if (!CRM_Utils_Array::value('label', $params) && CRM_Utils_Array::value('name', $params)) { - // 'label' defaults to 'name' - $params['label'] = $params['name']; - } - if (!CRM_Utils_Array::value('value', $params) && CRM_Utils_Array::value('option_group_id', $params)) { - // 'value' defaults to next weight in option_group - $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', - array('option_group_id' => $params['option_group_id']) - ); - } - if (!CRM_Utils_Array::value('weight', $params) && CRM_Utils_Array::value('value', $params)) { - // 'weight' defaults to 'value' - $params['weight'] = $params['value']; - } elseif (CRM_Utils_Array::value('weight', $params) && $params['weight'] == 'next' && CRM_Utils_Array::value('option_group_id', $params)) { - // weight is numeric, so it's safe-ish to treat symbol 'next' as magical value - $params['weight'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue', - array('option_group_id' => $params['option_group_id']) - ); - } - } - - if (isset($params['component'])) {// allow a component to be reset to '' - // convert 'component' to 'component_id' - if (empty($params['component'])) { - $params['component_id'] = ''; - } else { - $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant::component()); - } - unset($params['component']); - } - - if (CRM_Utils_Array::value('id', $params)) { - $ids = array('optionValue' => $params['id']); - } - $optionValueBAO = CRM_Core_BAO_OptionValue::add($params, $ids); + $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'])); - $values = array(); - _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id]); - return civicrm_api3_create_success($values, $params); + return $result; } /** @@ -91,10 +53,10 @@ function civicrm_api3_option_value_create($params) { */ function _civicrm_api3_option_value_create_spec(&$params) { $params['is_active']['api.default'] = 1; - $params['component']['type'] = CRM_Utils_Type::T_STRING; - $params['component']['options'] = array_values(CRM_Core_PseudoConstant::component()); + //continue to support component + $params['component_id']['api.aliases'] = array('component'); $params['name']['api.aliases'] = array('label'); - // $params['component_id']['pseudoconstant'] = 'component'; + $params['option_group_id']['api.required'] = TRUE; } /** diff --git a/tests/phpunit/api/v3/OptionValueTest.php b/tests/phpunit/api/v3/OptionValueTest.php index e06679afc2..4e771d4b1e 100644 --- a/tests/phpunit/api/v3/OptionValueTest.php +++ b/tests/phpunit/api/v3/OptionValueTest.php @@ -146,7 +146,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { 'name' => 'from_email_address', 'sequential' => 1, 'version' => $this->_apiversion, - 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com') + 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'), )); $this->assertAPISuccess($result); $optionValueId = $result['values'][0]['api.option_value.create']['id']; @@ -156,7 +156,99 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { 'return' => 'domain_id', )); $this->assertEquals(2, $domain_id); + } + /* + * Check that component_id is honoured + */ + public function testCreateOptionSpecifyComponentID() { + $result = civicrm_api('option_group', 'get', array( + 'name' => 'from_email_address', + 'sequential' => 1, + 'version' => $this->_apiversion, + 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'), + )); + $this->assertAPISuccess($result); + $optionValueId = $result['values'][0]['api.option_value.create']['id']; + $component_id = civicrm_api('option_value', 'getvalue', array( + 'id' => $optionValueId, + 'version' => $this->_apiversion, + 'return' => 'component_id', + )); + $this->assertEquals(2, $component_id); + } + /* + * Check that component continues to be honoured + */ + public function testCreateOptionSpecifyComponent() { + $result = civicrm_api('option_group', 'get', array( + 'name' => 'from_email_address', + 'sequential' => 1, + 'version' => $this->_apiversion, + 'api.option_value.create' => array( + 'component_id' => 'CiviContribute', + 'name' => 'my@y.com' + ), + )); + $this->assertAPISuccess($result); + $optionValueId = $result['values'][0]['api.option_value.create']['id']; + $component_id = civicrm_api('option_value', 'getvalue', array( + 'id' => $optionValueId, + 'version' => $this->_apiversion, + 'return' => 'component_id', + )); + $this->assertEquals(2, $component_id); + } + /* + * Check that component string is honoured + */ + public function testCreateOptionSpecifyComponentString() { + $result = civicrm_api('option_group', 'get', array( + 'name' => 'from_email_address', + 'sequential' => 1, + 'version' => $this->_apiversion, + 'api.option_value.create' => array( + 'component_id' => 'CiviContribute', + 'name' => 'my@y.com'), + + )); + $this->assertAPISuccess($result); + $optionValueId = $result['values'][0]['api.option_value.create']['id']; + $component_id = civicrm_api('option_value', 'getvalue', array( + 'id' => $optionValueId, + 'version' => $this->_apiversion, + 'return' => 'component_id', + )); + $this->assertEquals(2, $component_id); + } + /* + * Check that domain_id is honoured + */ + public function testCRM12133CreateOptionWeightNoValue() { + $optionGroup = civicrm_api( + 'option_group', 'get', array( + 'name' => 'gender', + 'sequential' => 1, + 'version' => $this->_apiversion, + )); + $this->assertAPISuccess($optionGroup); + $params = array( + 'option_group_id' => $optionGroup['id'], + 'label' => 'my@y.com', + 'version' => $this->_apiversion, + 'weight' => 3, + ); + $optionValue = civicrm_api('option_value', 'create', $params); + $this->assertAPISuccess($optionValue); + $params['weight'] = 4; + $optionValue2 = civicrm_api('option_value', 'create', $params ); + $this->assertAPISuccess($optionValue2); + $options = civicrm_api('option_value', 'get', array('version' => 3, 'option_group_id' => $optionGroup['id'])); + $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']); + + //cleanup + civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue['id'])); + civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue2['id'])); } /* @@ -206,6 +298,5 @@ class api_v3_OptionValueTest extends CiviUnitTestCase { ); $this->assertFalse(in_array('newest', $fields['values'])); } - } diff --git a/xml/schema/Core/OptionValue.xml b/xml/schema/Core/OptionValue.xml index 2dd7d8a344..a417fc3b53 100644 --- a/xml/schema/Core/OptionValue.xml +++ b/xml/schema/Core/OptionValue.xml @@ -123,7 +123,13 @@ component_id int unsigned Component that this option value belongs/caters to. - 2.0 + 2.0 + + component + civicrm_component
+ id + name +
component_id -- 2.25.1