From 4033343a117d33066f40861cd82f42067b988ef9 Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 1 Jul 2013 14:10:03 +1200 Subject: [PATCH] CRM-12988 - OptionGroup.create doesn't update --- CRM/Core/BAO/OptionGroup.php | 7 ++- api/v3/OptionGroup.php | 36 ++++++++------- api/v3/examples/OptionGroupCreate.php | 13 ++++++ api/v3/examples/OptionGroupDelete.php | 57 ++++++++++++++++++++++++ api/v3/examples/OptionGroupGet.php | 8 ++++ tests/phpunit/api/v3/OptionGroupTest.php | 56 ++++++++++++++--------- 6 files changed, 140 insertions(+), 37 deletions(-) create mode 100644 api/v3/examples/OptionGroupDelete.php diff --git a/CRM/Core/BAO/OptionGroup.php b/CRM/Core/BAO/OptionGroup.php index 315fbf06f1..8d53f1f94b 100644 --- a/CRM/Core/BAO/OptionGroup.php +++ b/CRM/Core/BAO/OptionGroup.php @@ -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; } diff --git a/api/v3/OptionGroup.php b/api/v3/OptionGroup.php index 8d0706d6ab..bfe9590ee9 100644 --- a/api/v3/OptionGroup.php +++ b/api/v3/OptionGroup.php @@ -1,7 +1,6 @@ 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); +} diff --git a/api/v3/examples/OptionGroupCreate.php b/api/v3/examples/OptionGroupCreate.php index 3b77df3919..597c4d2b35 100644 --- a/api/v3/examples/OptionGroupCreate.php +++ b/api/v3/examples/OptionGroupCreate.php @@ -31,6 +31,14 @@ function option_group_create_expectedresult(){ $expectedResult = array( 'is_error' => 0, + 'undefined_fields' => array( + '0' => 'name', + '1' => 'is_reserved', + '2' => 'is_active', + '3' => 'api.OptionValue.create', + '4' => 'id', + '5' => 'is_default', + ), 'version' => 3, 'count' => 1, 'id' => 84, @@ -45,6 +53,11 @@ function option_group_create_expectedresult(){ 'api.OptionValue.create' => 720, ), ), + 'xdebug' => array( + 'peakMemory' => 35887080, + 'memory' => 32901496, + 'timeIndex' => '50.986313819885', + ), ); return $expectedResult ; diff --git a/api/v3/examples/OptionGroupDelete.php b/api/v3/examples/OptionGroupDelete.php new file mode 100644 index 0000000000..d186b6ac4f --- /dev/null +++ b/api/v3/examples/OptionGroupDelete.php @@ -0,0 +1,57 @@ + 89, +); + + $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, + 'xdebug' => array( + 'peakMemory' => 35887080, + 'memory' => 33229704, + 'timeIndex' => '51.428074836731', + ), +); + + 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 diff --git a/api/v3/examples/OptionGroupGet.php b/api/v3/examples/OptionGroupGet.php index b4fc0ceca6..553a7bc576 100644 --- a/api/v3/examples/OptionGroupGet.php +++ b/api/v3/examples/OptionGroupGet.php @@ -21,6 +21,9 @@ function option_group_get_expectedresult(){ $expectedResult = array( 'is_error' => 0, + 'undefined_fields' => array( + '0' => 'name', + ), 'version' => 3, 'count' => 1, 'id' => 1, @@ -33,6 +36,11 @@ function option_group_get_expectedresult(){ 'is_active' => '1', ), ), + 'xdebug' => array( + 'peakMemory' => 35887080, + 'memory' => 30302600, + 'timeIndex' => '50.701730966568', + ), ); return $expectedResult ; diff --git a/tests/phpunit/api/v3/OptionGroupTest.php b/tests/phpunit/api/v3/OptionGroupTest.php index b998870b52..5d95381271 100644 --- a/tests/phpunit/api/v3/OptionGroupTest.php +++ b/tests/phpunit/api/v3/OptionGroupTest.php @@ -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__); + } } -- 2.25.1