Commit 2d3fdfe5 authored by colemanw's avatar colemanw Committed by GitHub

Merge pull request #11089 from seamuslee001/CRM-19346

CRM-19346 Ensure Option Values do not share same value
parents 9a265785 8326c4b5
......@@ -50,6 +50,12 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
*/
protected $_gLabel;
/**
* Is this Option Group Domain Specific
* @var bool
*/
protected $_domainSpecific = FALSE;
/**
* Pre-process
*/
......@@ -74,6 +80,7 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
'name'
);
$this->_gLabel = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup', $this->_gid, 'title');
$this->_domainSpecific = in_array($this->_gName, CRM_Core_OptionGroup::$_domainIDGroups);
$url = "civicrm/admin/options/{$this->_gName}";
$params = "reset=1";
......@@ -172,6 +179,11 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
CRM_Core_DAO::getAttribute('CRM_Core_DAO_OptionValue', 'value'),
TRUE
);
$this->addRule('value',
ts('This Value already exists in the database for this option group. Please select a different Value.'),
'optionExists',
array('CRM_Core_DAO_OptionValue', $this->_id, $this->_gid, 'value', $this->_domainSpecific)
);
}
else {
$this->add('text', 'icon', ts('Icon'), array('class' => 'crm-icon-picker', 'title' => ts('Choose Icon'), 'allowClear' => TRUE));
......@@ -187,12 +199,10 @@ class CRM_Admin_Form_Options extends CRM_Admin_Form {
'addressee',
)) && !$isReserved
) {
$domainSpecificOptionGroups = array('from_email_address');
$domainSpecific = in_array($this->_gName, $domainSpecificOptionGroups) ? TRUE : FALSE;
$this->addRule('label',
ts('This Label already exists in the database for this option group. Please select a different Value.'),
ts('This Label already exists in the database for this option group. Please select a different Label.'),
'optionExists',
array('CRM_Core_DAO_OptionValue', $this->_id, $this->_gid, 'label', $domainSpecific)
array('CRM_Core_DAO_OptionValue', $this->_id, $this->_gid, 'label', $this->_domainSpecific)
);
}
......
......@@ -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