Skip to content
Snippets Groups Projects
Commit 25dbd8a4 authored by colemanw's avatar colemanw
Browse files

Merge pull request #1081 from eileenmcnaughton/CRM-12988

CRM-12988 - OptionGroup.create doesn't update
parents 802b2dc5 2cce72b0
Branches
Tags
No related merge requests found
......@@ -89,7 +89,11 @@ class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
*
* @return object
*/
static function add(&$params, &$ids) {
static function add(&$params, $ids = array()) {
if(empty($params['id'])){
$params['id'] = CRM_Utils_Array::value('optionGroup', $ids);
}
$params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
$params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
......@@ -102,7 +106,6 @@ class CRM_Core_BAO_OptionGroup extends CRM_Core_DAO_OptionGroup {
CRM_Core_DAO::executeQuery($query);
}
$optionGroup->id = CRM_Utils_Array::value('optionGroup', $ids);
$optionGroup->save();
return $optionGroup;
}
......
<?php
function civicrm_api3_option_group_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
......@@ -12,26 +11,15 @@ function civicrm_api3_option_group_get($params) {
* In case of updating existing survey, id of that particular survey must
* be in $params array.
*
* @param array $params (referance) Associative array of property
* name/value pairs to insert in new 'survey'
* @param array $params (reference) Associative array of property
* name/value pairs to insert in new 'survey'
*
* @return array survey array
*
* @access public
*/
function civicrm_api3_option_group_create($params) {
$ids = array();
$bao = CRM_Core_BAO_OptionGroup::add($params, $ids);
if (is_null($bao)) {
return civicrm_api3_create_error('Entity not created');
}
else {
$values = array();
_civicrm_api3_object_to_array($bao, $values[$bao->id]);
return civicrm_api3_create_success($values, $params, 'option_group', 'create', $bao);
}
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
/**
......@@ -43,3 +31,21 @@ function civicrm_api3_option_group_create($params) {
function _civicrm_api3_option_group_create_spec(&$params) {
$params['name']['api.unique'] = 1;
}
/**
* delete an existing Option Group
*
* This method is used to delete any existing Option Group. id of the group
* to be deleted is required field in $params array
*
* @param array $params (reference) array containing id of the group
* to be deleted
*
* @return array (reference) returns flag true if successfull, error
* message otherwise
* {@getfields OptionGroup_delete}
* @access public
*/
function civicrm_api3_option_group_delete($params) {
return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}
......@@ -17,6 +17,7 @@ $params = array(
'is_active' => 1,
'format.only_id' => 1,
),
'debug' => 0,
);
$result = civicrm_api( 'option_group','create',$params );
......
<?php
/*
*/
function option_group_delete_example(){
$params = array(
'id' => 89,
'version' => 3,
'debug' => 0,
);
$result = civicrm_api( 'option_group','delete',$params );
return $result;
}
/*
* Function returns array of result expected from previous function
*/
function option_group_delete_expectedresult(){
$expectedResult = array(
'is_error' => 0,
'version' => 3,
'count' => 1,
'values' => true,
);
return $expectedResult ;
}
/*
* This example has been generated from the API test suite. The test that created it is called
*
* testDeleteOptionGroup and can be found in
* http://svn.civicrm.org/civicrm/trunk/tests/phpunit/CiviTest/api/v3/OptionGroupTest.php
*
* You can see the outcome of the API tests at
* http://tests.dev.civicrm.org/trunk/results-api_v3
*
* To Learn about the API read
* http://book.civicrm.org/developer/current/techniques/api/
*
* and review the wiki at
* http://wiki.civicrm.org/confluence/display/CRMDOC/CiviCRM+Public+APIs
*
* Read more about testing here
* http://wiki.civicrm.org/confluence/display/CRM/Testing
*
* API Standards documentation:
* http://wiki.civicrm.org/confluence/display/CRM/API+Architecture+Standards
*/
\ No newline at end of file
......@@ -7,6 +7,7 @@ function option_group_get_example(){
$params = array(
'name' => 'preferred_communication_method',
'version' => 3,
'debug' => 0,
);
$result = civicrm_api( 'option_group','get',$params );
......
......@@ -30,10 +30,17 @@ require_once 'CiviTest/CiviUnitTestCase.php';
class api_v3_OptionGroupTest extends CiviUnitTestCase {
protected $_apiversion;
public $_eNoticeCompliant = TRUE;
protected $_entity = 'OptionGroup';
function setUp() {
$this->_apiversion = 3;
parent::setUp();
$this->_params = array(
'name' => 'our test Option Group',
'is_reserved' => 1,
'is_active' => 1,
);
}
function tearDown() {}
......@@ -42,51 +49,44 @@ class api_v3_OptionGroupTest extends CiviUnitTestCase {
*/
public function testGetOptionGroupGetFields() {
$result = civicrm_api('option_group', 'getfields', array('version' => 3));
$result = $this->callAPISuccess('option_group', 'getfields', array());
$this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
}
public function testGetOptionGroupGetFieldsCreateAction() {
$result = civicrm_api('option_group', 'getfields', array('action' => 'create', 'version' => 3));
$result = $this->callAPISuccess('option_group', 'getfields', array('action' => 'create', 'version' => 3));
$this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
$this->assertEquals($result['values']['name']['api.unique'], 1);
}
public function testGetOptionGroupByID() {
$result = civicrm_api('option_group', 'get', array('id' => 1, 'version' => 3));
$this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
$result = $this->callAPISuccess('option_group', 'get', array('id' => 1, 'version' => 3));
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
}
public function testGetOptionGroupByName() {
$params = array('name' => 'preferred_communication_method', 'version' => 3);
$result = civicrm_api('option_group', 'get', $params);
$this->documentMe($params, $result, __FUNCTION__, __FILE__);
$this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
$result = $this->callAPIAndDocument('option_group', 'get', $params, __FUNCTION__, __FILE__);
$this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
$this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
}
public function testGetOptionGroup() {
$result = civicrm_api('option_group', 'get', array('version' => 3));
$this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
$result = $this->callAPISuccess('option_group', 'get', array('version' => 3));
$this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
}
public function testGetOptionDoesNotExist() {
$result = civicrm_api('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB', 'version' => 3));
$this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
$result = $this->callAPISuccess('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB', 'version' => 3));
$this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
}
public function testGetOptionCreateSuccess() {
$params = array('version' => $this->_apiversion, 'sequential' => 1, 'name' => 'civicrm_event.amount.560', 'is_reserved' => 1, 'is_active' => 1, 'api.OptionValue.create' => array('label' => 'workshop', 'value' => 35, 'is_default' => 1, 'is_active' => 1, 'format.only_id' => 1));
$result = civicrm_api('OptionGroup', 'create', $params);
$this->documentMe($params, $result, __FUNCTION__, __FILE__);
$this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
$result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__, __FILE__);
$this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name'], 'In line ' . __LINE__);
$this->assertTrue(is_integer($result['values'][0]['api.OptionValue.create']));
$this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result['id']));
$this->callAPISuccess('OptionGroup', 'delete', array('version' => 3, 'id' => $result['id']));
}
/*
* Test the error message when a failure is due to a key duplication issue
......@@ -100,11 +100,9 @@ class api_v3_OptionGroupTest extends CiviUnitTestCase {
'is_reserved' => 1,
'is_active' => 1,
);
$result1 = civicrm_api('OptionGroup', 'create', $params);
$this->assertAPISuccess($result1);
$result = civicrm_api('OptionGroup', 'create', $params);
civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result1['id']));
$this->assertEquals("Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id'], $result['error_message']);
$result1 = $this->callAPISuccess('OptionGroup', 'create', $params);
$result = $this->callAPIFailure('OptionGroup', 'create', $params, "Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id']);
$this->callAPISuccess('OptionGroup', 'delete', array('id' => $result1['id']));
}
/*
......@@ -142,5 +140,23 @@ class api_v3_OptionGroupTest extends CiviUnitTestCase {
'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
);
}
/**
* success test for updating an existing Option Group
*/
public function testCreateUpdateOptionGroup() {
$result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
$params = array_merge($this->_params, array('id' => $result['id'], 'is_active' => 0));
$this->callAPISuccess($this->_entity, 'create', $params);
$this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
}
/**
* success test for deleting an existing Option Group
*/
public function testDeleteOptionGroup() {
$result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
$this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment