Merge branch 'CRM-12133' into master-CRM-12133
authorCiviCRM <info@civicrm.org>
Wed, 20 Mar 2013 12:45:48 +0000 (08:45 -0400)
committerCiviCRM <info@civicrm.org>
Wed, 20 Mar 2013 12:45:48 +0000 (08:45 -0400)
CRM/Core/BAO/OptionValue.php
api/v3/CustomSearch.php
api/v3/OptionValue.php
api/v3/ReportTemplate.php
tests/phpunit/api/v3/CustomSearchTest.php
tests/phpunit/api/v3/OptionValueTest.php
tests/phpunit/api/v3/ReportTemplateTest.php
xml/schema/Core/OptionValue.xml

index 0340feabbc4e08febb5f4fc88f27e0acaa6f78db..edc771bf333f87a8757eb3ec0dc8b9ffa6bba20e 100644 (file)
@@ -40,7 +40,81 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
   function __construct() {
     parent::__construct();
   }
+  /**
+   * Create option value - note that the create function calls 'add' but
+  * has more business logic
+  *
+  * @param array $params input parameters
+  */
+  static function create($params) {
+    if (empty($params['id'])){
+      self::setDefaults($params);
+    }
+    $ids = array();
+    if (CRM_Utils_Array::value('id', $params)) {
+      $ids = array('optionValue' => $params['id']);
+    }
+    return  CRM_Core_BAO_OptionValue::add($params, $ids);
+    ;
+  }
+  /**
+   * Set default Parameters
+   * This functions sets default parameters if not set:
+   * - name & label are set to each other as required (it might make more sense for one
+   * to be required but this would mean a change to the api level)
+   * - weight & value will be set to their respective option groups next values
+   * if nothing is passed in.
+   *
+   * Note this function does not check for presence of $params['id'] so should only be called
+   * if 'id' is not present
+   *
+   * @param array $params
+   */
+  static function setDefaults(&$params){
+    if(CRM_Utils_Array::value('label', $params, NULL) === NULL){
+      $params['label'] = $params['name'];
+    }
+    if(CRM_Utils_Array::value('name', $params, NULL) === NULL){
+      $params['name'] = $params['label'];
+    }
+    if(CRM_Utils_Array::value('weight', $params, NULL) === NULL){
+      $params['weight'] = self::getDefaultWeight($params);
+    }
+    if (CRM_Utils_Array::value('value', $params, NULL) === NULL){
+      $params['value'] = self::getDefaultValue($params);
+    }
+  }
+  /**
+   * Get next available value
+   * We will take the highest numeric value (or 0 if no numeric values exist)
+   * and add one. The calling function is responsible for any
+   * more complex decision making
+   * @param array $params
+   */
+  static function getDefaultWeight($params){
+    return (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
+          array('option_group_id' => $params['option_group_id']));
+  }
 
+  /**
+   * Get next available value
+   * We will take the highest numeric value (or 0 if no numeric values exist)
+   * and add one. The calling function is responsible for any
+   * more complex decision making
+   * @param array $params
+   */
+  static function getDefaultValue($params){
+     $bao = new CRM_Core_BAO_OptionValue();
+     $bao->option_group_id = $params['option_group_id'];
+     if(isset($params['domain_id'])){
+       $bao->domain_id = $params['domain_id'];
+     }
+     $bao->selectAdd();
+     $bao->whereAdd("value REGEXP '^[0-9]+$'");
+     $bao->selectAdd('(ROUND(COALESCE(MAX(value),0)) +1) as nextvalue');
+     $bao->find(TRUE);
+     return $bao->nextvalue;
+  }
   /**
    * Takes a bunch of params that are needed to match certain criteria and
    * retrieves the relevant objects. Typically the valid params are only
@@ -91,6 +165,10 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
    */
   static function add(&$params, &$ids) {
     // CRM-10921: do not reset attributes to default if this is an update
+    //@todo consider if defaults are being set in the right place. 'dumb' defaults like
+    // these would be usefully set @ the api layer so they are visible to api users
+    // complex defaults like the domain id below would make sense in the setDefauls function
+    // but unclear what other ways this function is being used
     if (!CRM_Utils_Array::value('optionValue', $ids)) {
       $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
       $params['is_default'] = CRM_Utils_Array::value('is_default', $params, FALSE);
@@ -117,7 +195,6 @@ class CRM_Core_BAO_OptionValue extends CRM_Core_DAO_OptionValue {
       $p = array(1 => array($params['option_group_id'], 'Integer'));
       CRM_Core_DAO::executeQuery($query, $p);
     }
-
     $groupName = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_OptionGroup',
       $params['option_group_id'], 'name', 'id'
     );
index fe36d0126ce348f34f63ab3a0cc887ca59fbc76e..26458cb4e49b2b754effeb3c1860c89c578c621d 100644 (file)
@@ -54,7 +54,9 @@ function civicrm_api3_custom_search_create($params) {
 function _civicrm_api3_custom_search_create_spec(&$params) {
   require_once 'api/v3/OptionValue.php';
   _civicrm_api3_option_value_create_spec($params);
-  $params['weight']['api.default'] = 'next';
+  $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
+    'CRM_Core_DAO_OptionGroup', 'custom_search', 'id', 'name'
+  );
   $params['name']['api.aliases'] = array('class_name');
 }
 
index 30b7e26be114d16705fdd807f1a359ed73c5ed5c..de9c61394998603a33be2554c658ddd3652753ad 100644 (file)
@@ -40,47 +40,9 @@ function civicrm_api3_option_value_get($params) {
  */
 function civicrm_api3_option_value_create($params) {
 
-  // CRM-10921: do not fill-in defaults if this is an update
-  if (!CRM_Utils_Array::value('id', $params)) {
-    if (!CRM_Utils_Array::value('label', $params) && CRM_Utils_Array::value('name', $params)) {
-      // 'label' defaults to 'name'
-      $params['label'] = $params['name'];
-    }
-    if (!CRM_Utils_Array::value('value', $params) && CRM_Utils_Array::value('option_group_id', $params)) {
-      // 'value' defaults to next weight in option_group
-      $params['value'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
-        array('option_group_id' => $params['option_group_id'])
-      );
-    }
-    if (!CRM_Utils_Array::value('weight', $params) && CRM_Utils_Array::value('value', $params)) {
-      // 'weight' defaults to 'value'
-      $params['weight'] = $params['value'];
-    } elseif (CRM_Utils_Array::value('weight', $params) && $params['weight'] == 'next' && CRM_Utils_Array::value('option_group_id', $params)) {
-      // weight is numeric, so it's safe-ish to treat symbol 'next' as magical value
-      $params['weight'] = (int) CRM_Utils_Weight::getDefaultWeight('CRM_Core_DAO_OptionValue',
-        array('option_group_id' => $params['option_group_id'])
-      );
-    }
-  }
-
-  if (isset($params['component'])) {// allow a component to be reset to ''
-    // convert 'component' to 'component_id'
-    if (empty($params['component'])) {
-      $params['component_id'] = '';
-    } else {
-      $params['component_id'] = array_search($params['component'], CRM_Core_PseudoConstant::component());
-    }
-    unset($params['component']);
-  }
-
-  if (CRM_Utils_Array::value('id', $params)) {
-    $ids = array('optionValue' => $params['id']);
-  }
-  $optionValueBAO = CRM_Core_BAO_OptionValue::add($params, $ids);
+  $result = _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
   civicrm_api('option_value', 'getfields', array('version' => 3, 'cache_clear' => 1, 'option_group_id' => $params['option_group_id']));
-  $values = array();
-  _civicrm_api3_object_to_array($optionValueBAO, $values[$optionValueBAO->id]);
-  return civicrm_api3_create_success($values, $params);
+  return $result;
 }
 
 /**
@@ -91,10 +53,10 @@ function civicrm_api3_option_value_create($params) {
  */
 function _civicrm_api3_option_value_create_spec(&$params) {
   $params['is_active']['api.default'] = 1;
-  $params['component']['type'] = CRM_Utils_Type::T_STRING;
-  $params['component']['options'] = array_values(CRM_Core_PseudoConstant::component());
+  //continue to support component
+  $params['component_id']['api.aliases'] = array('component');
   $params['name']['api.aliases'] = array('label');
-  // $params['component_id']['pseudoconstant'] = 'component';
+  $params['option_group_id']['api.required'] = TRUE;
 }
 
 /**
index 86d748ccb3f77f6c9836f50582342b31f992cab1..84cb499a259e50e034b8c03f9c455c35cf890dbe 100644 (file)
@@ -38,6 +38,9 @@ function civicrm_api3_report_template_create($params) {
   $params['option_group_id'] = CRM_Core_DAO::getFieldValue(
     'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
   );
+  if (!empty($params['component_id']) && !is_numeric($params['component_id'])) {
+    $params['component_id'] = array_search($params['component_id'], CRM_Core_PseudoConstant::component());
+  }
   return civicrm_api3_option_value_create($params);
 }
 
@@ -50,9 +53,11 @@ function civicrm_api3_report_template_create($params) {
 function _civicrm_api3_report_template_create_spec(&$params) {
   require_once 'api/v3/OptionValue.php';
   _civicrm_api3_option_value_create_spec($params);
-  $params['weight']['api.default'] = 'next';
   $params['value']['api.aliases'] = array('report_url');
   $params['name']['api.aliases'] = array('class_name');
+  $params['option_group_id']['api.default'] = CRM_Core_DAO::getFieldValue(
+    'CRM_Core_DAO_OptionGroup', 'report_template', 'id', 'name'
+  );
   // $params['component']['api.required'] = TRUE;
 }
 
index 41783e4e705c9a39c22985cb216d8791a1cc324e..eda0c03519b1eb60c1f43cd81ccb1822ac9532da 100644 (file)
@@ -19,7 +19,7 @@ class api_v3_CustomSearchTest extends CiviUnitTestCase {
       'description' => 'Longish description of the example search form',
       'class_name' => 'CRM_Contact_Form_Search_Custom_Examplez',
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $entityId = $result['id'];
     $this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
@@ -36,7 +36,7 @@ class api_v3_CustomSearchTest extends CiviUnitTestCase {
       'id' => $entityId,
       'is_active' => 0,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
@@ -51,7 +51,7 @@ class api_v3_CustomSearchTest extends CiviUnitTestCase {
       'id' => $entityId,
       'is_active' => 1,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
@@ -64,7 +64,7 @@ class api_v3_CustomSearchTest extends CiviUnitTestCase {
       'version' => $this->_apiversion,
       'id' => $entityId,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Contact_Form_Search_Custom_Examplez"
index e06679afc22ed009d80d2b1939ebf1508faec271..4e771d4b1e932a6cf51cd288d9080f70445112fd 100644 (file)
@@ -146,7 +146,7 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
       'name' => 'from_email_address',
       'sequential' => 1,
       'version' => $this->_apiversion,
-      'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com')
+      'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
       ));
     $this->assertAPISuccess($result);
     $optionValueId = $result['values'][0]['api.option_value.create']['id'];
@@ -156,7 +156,99 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
       'return' => 'domain_id',
     ));
     $this->assertEquals(2, $domain_id);
+  }
+  /*
+   * Check that component_id is honoured
+  */
+  public function testCreateOptionSpecifyComponentID() {
+    $result = civicrm_api('option_group', 'get', array(
+      'name' => 'from_email_address',
+      'sequential' => 1,
+      'version' => $this->_apiversion,
+      'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
+    ));
+    $this->assertAPISuccess($result);
+    $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+    $component_id = civicrm_api('option_value', 'getvalue', array(
+      'id' => $optionValueId,
+      'version' => $this->_apiversion,
+      'return' => 'component_id',
+    ));
+    $this->assertEquals(2, $component_id);
+  }
+  /*
+   * Check that component  continues to be honoured
+  */
+  public function testCreateOptionSpecifyComponent() {
+    $result = civicrm_api('option_group', 'get', array(
+      'name' => 'from_email_address',
+      'sequential' => 1,
+      'version' => $this->_apiversion,
+      'api.option_value.create' => array(
+        'component_id' => 'CiviContribute',
+        'name' => 'my@y.com'
+       ),
 
+    ));
+    $this->assertAPISuccess($result);
+    $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+    $component_id = civicrm_api('option_value', 'getvalue', array(
+      'id' => $optionValueId,
+      'version' => $this->_apiversion,
+      'return' => 'component_id',
+    ));
+    $this->assertEquals(2, $component_id);
+  }
+  /*
+   * Check that component string is honoured
+  */
+  public function testCreateOptionSpecifyComponentString() {
+    $result = civicrm_api('option_group', 'get', array(
+      'name' => 'from_email_address',
+      'sequential' => 1,
+      'version' => $this->_apiversion,
+      'api.option_value.create' => array(
+        'component_id' => 'CiviContribute',
+        'name' => 'my@y.com'),
+
+    ));
+    $this->assertAPISuccess($result);
+    $optionValueId = $result['values'][0]['api.option_value.create']['id'];
+    $component_id = civicrm_api('option_value', 'getvalue', array(
+      'id' => $optionValueId,
+      'version' => $this->_apiversion,
+      'return' => 'component_id',
+    ));
+    $this->assertEquals(2, $component_id);
+  }
+  /*
+   * Check that domain_id is honoured
+  */
+  public function testCRM12133CreateOptionWeightNoValue() {
+    $optionGroup = civicrm_api(
+      'option_group', 'get', array(
+      'name' => 'gender',
+      'sequential' => 1,
+      'version' => $this->_apiversion,
+    ));
+    $this->assertAPISuccess($optionGroup);
+    $params = array(
+      'option_group_id' => $optionGroup['id'],
+      'label' => 'my@y.com',
+      'version' => $this->_apiversion,
+      'weight' => 3,
+    );
+    $optionValue = civicrm_api('option_value', 'create',  $params);
+    $this->assertAPISuccess($optionValue);
+    $params['weight'] = 4;
+    $optionValue2 = civicrm_api('option_value', 'create',  $params );
+    $this->assertAPISuccess($optionValue2);
+    $options = civicrm_api('option_value', 'get', array('version' => 3, 'option_group_id' => $optionGroup['id']));
+    $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
+
+  //cleanup
+    civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue['id']));
+    civicrm_api('option_value', 'delete', array('version' => 3, 'id' => $optionValue2['id']));
   }
 
   /*
@@ -206,6 +298,5 @@ class api_v3_OptionValueTest extends CiviUnitTestCase {
     );
     $this->assertFalse(in_array('newest', $fields['values']));
   }
-
 }
 
index d190ec1b13b8dd831b819af758b730ecf04b0be6..c6601e85f237243a6ab894cb0a014d2d695a661a 100644 (file)
@@ -54,7 +54,7 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
       'report_url' => 'example/path',
       'component' => 'CiviCase',
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $entityId = $result['id'];
     $this->assertTrue(is_numeric($entityId), 'In line ' . __LINE__);
@@ -71,7 +71,7 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
       'id' => $entityId,
       'component' => '',
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Report_Form_Examplez"
@@ -86,7 +86,7 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
       'id' => $entityId,
       'is_active' => 0,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Report_Form_Examplez"
@@ -100,7 +100,7 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
       'id' => $entityId,
       'is_active' => 1,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(1, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Report_Form_Examplez"
@@ -112,7 +112,7 @@ class api_v3_ReportTemplateTest extends CiviUnitTestCase {
       'version' => $this->_apiversion,
       'id' => $entityId,
     ));
-    $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
+    $this->assertAPISuccess($result);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
     $this->assertDBQuery(0, 'SELECT count(*) FROM civicrm_option_value
       WHERE name = "CRM_Report_Form_Examplez"
index 2dd7d8a3442046a1706ab17b69b3d02758b3b5a2..a417fc3b53e67ff29ff98151c5eafd3b407e04b6 100644 (file)
       <name>component_id</name>
       <type>int unsigned</type>
       <comment>Component that this option value belongs/caters to.</comment>
-       <add>2.0</add>
+      <add>2.0</add>
+      <pseudoconstant>
+        <name>component</name>
+        <table>civicrm_component</table>
+        <keyColumn>id</keyColumn>
+        <labelColumn>name</labelColumn>
+      </pseudoconstant>
   </field>
   <foreignKey>
        <name>component_id</name>