CRM-12988 - OptionGroup.create doesn't update
authoreileen <eileen@fuzion.co.nz>
Mon, 1 Jul 2013 02:10:03 +0000 (14:10 +1200)
committereileen <eileen@fuzion.co.nz>
Mon, 1 Jul 2013 02:11:19 +0000 (14:11 +1200)
CRM/Core/BAO/OptionGroup.php
api/v3/OptionGroup.php
api/v3/examples/OptionGroupCreate.php
api/v3/examples/OptionGroupDelete.php [new file with mode: 0644]
api/v3/examples/OptionGroupGet.php
tests/phpunit/api/v3/OptionGroupTest.php

index 315fbf06f107b99e1c375bdadbb23012db4bad15..8d53f1f94bf6cc45cb51fdb8f052b4d9c6b298e9 100644 (file)
@@ -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;
   }
index 8d0706d6abcdd7acfca0755ff3fc68a4a6a5e3e4..bfe9590ee9b25be6ba4d4b9f29be3b05961ea152 100644 (file)
@@ -1,7 +1,6 @@
 <?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);
+}
index 3b77df39197913e0555cce484fa5f53690e8197c..597c4d2b3579399e2b124daf65c45ea450956417 100644 (file)
@@ -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 (file)
index 0000000..d186b6a
--- /dev/null
@@ -0,0 +1,57 @@
+<?php
+
+/*
+ */
+function option_group_delete_example(){
+$params = array(
+  'id' => 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
index b4fc0ceca66d591e7424143e699820ab82483e2d..553a7bc576d96dd50723936526cf8da5e11e3823 100644 (file)
@@ -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  ;
index b998870b52be11707d463d26a7c50a25901ef552..5d953812713363c0045c874c7ec610256d456705 100644 (file)
@@ -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__);
+  }
 }