Commit e5720c45 authored by Seamus Lee's avatar Seamus Lee

CRM-19346 Patch API also to ensure that same value cannot be used as well

Add in fixes to unit tests

Switch to using CRM_Core_OptionGroup:: rather than hard codeded array
parent 82f3aedc
......@@ -170,8 +170,8 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'label'),
TRUE
);
$domainSpecificOptionGroups = array('from_email_address');
$this->_domainSpecific = in_array($this->_gName, $domainSpecificOptionGroups) ? TRUE : FALSE;
$this->_domainSpecific = in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups) ? TRUE : FALSE;
if ($this->_gName != 'activity_type') {
$this->add('text',
'value',
......
......@@ -219,6 +219,15 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
$groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
$groupId, 'name', 'id'
);
if (empty($ids['optionValue']) && empty($params['id']) && !empty($params['value'])) {
$domainSpecifc = in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups) ? TRUE : FALSE;
$dao = new CRM_Core_DAO_OptionValue();
$dao->value = $params['value'];
$dao->option_group_id = $groupId;
if ($dao->find(TRUE)) {
throw new CRM_Core_Exception('Value already exists in the database');
}
}
if (in_array($groupName, CRM_Core_OptionGroup::$_domainIDGroups)) {
$optionValue->domain_id = CRM_Utils_Array::value('domain_id', $params, CRM_Core_Config::domainID());
}
......
......@@ -3003,6 +3003,12 @@ AND ( TABLE_NAME LIKE 'civicrm_value_%' )
CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
$optionGroupID = $this->callAPISuccessGetValue('option_group', array('return' => 'id', 'name' => 'acl_role'));
$ov = new CRM_Core_DAO_OptionValue();
$ov->option_group_id = $optionGroupID;
$ov->value = 55;
if ($ov->find(TRUE)) {
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_option_value WHERE id = {$ov->id}");
}
$optionValue = $this->callAPISuccess('option_value', 'create', array(
'option_group_id' => $optionGroupID,
'label' => 'pick me',
......
......@@ -159,7 +159,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
$result = $this->callAPISuccess('option_group', 'get', array(
'name' => 'from_email_address',
'sequential' => 1,
'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com', 'value' => '10'),
));
$optionValueId = $result['values'][0]['api.option_value.create']['id'];
......@@ -168,6 +168,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
'return' => 'domain_id',
));
$this->assertEquals(2, $domain_id);
$this->callAPISuccess('option_value', 'delete', array('id' => $optionValueId));
}
/**
......@@ -186,6 +187,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
'return' => 'component_id',
));
$this->assertEquals(2, $component_id);
$this->callAPISuccess('option_value', 'delete', array('id' => $optionValueId));
}
/**
......@@ -207,6 +209,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
'return' => 'component_id',
));
$this->assertEquals(2, $component_id);
$this->callAPISuccess('option_value', 'delete', array('id' => $optionValueId));
}
/**
......@@ -374,4 +377,41 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
$this->assertEquals($val, 0, "update with group id is not proper " . __LINE__);
}
/**
* CRM-19346 Ensur that Option Values cannot share same value in the same option value group
*/
public function testCreateOptionValueWithSameValue() {
$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'
$ov2 = $this->callAPIFailure('option_value', 'create',
array('option_group_id' => $og['id'], 'label' => 'Test 2nd option value', 'value' => $ov['values'][$ov['id']]['value'])
);
}
public function testCreateOptionValueWithSameValueDiffOptionGroup() {
$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')
);
$og2 = $this->callAPISuccess('option_group', 'create', array(
'name' => 'our test Option Group for with group id 2',
'is_active' => 1,
));
// update option value without 'option_group_id'
$ov2 = $this->callAPISuccess('option_value', 'create',
array('option_group_id' => $og2['id'], 'label' => 'Test 2nd option value', 'value' => $ov['values'][$ov['id']]['value'])
);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment