From: Eileen McNaughton Date: Wed, 19 Mar 2014 09:12:48 +0000 (+1300) Subject: CRM-14355 fix test-exposed bugs in contribute, participant, pledge get X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=82f7d8b23a89f75b50ab374fabf075c1274f8a0c;p=civicrm-core.git CRM-14355 fix test-exposed bugs in contribute, participant, pledge get ---------------------------------------- * CRM-14355: make api treat limit = 0 as an unlimited request http://issues.civicrm.org/jira/browse/CRM-14355 --- diff --git a/api/v3/Contribution.php b/api/v3/Contribution.php index 63322f70e6..19047e9c3b 100644 --- a/api/v3/Contribution.php +++ b/api/v3/Contribution.php @@ -175,30 +175,9 @@ function _civicrm_api3_contribution_delete_spec(&$params) { */ function civicrm_api3_contribution_get($params) { - $options = _civicrm_api3_get_options_from_params($params, TRUE,'contribution','get'); - $sort = CRM_Utils_Array::value('sort', $options, NULL); - $offset = CRM_Utils_Array::value('offset', $options); - $rowCount = CRM_Utils_Array::value('limit', $options); - $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params); - $inputParams = CRM_Utils_Array::value('input_params', $options, array()); - $returnProperties = CRM_Utils_Array::value('return', $options, NULL); - if (empty($returnProperties)) { - $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE); - } - - $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); - $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL, - FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE - ); - list($select, $from, $where, $having) = $query->query(); - - $sql = "$select $from $where $having"; - - if (!empty($sort)) { - $sql .= " ORDER BY $sort "; - } - $sql .= " LIMIT $offset, $rowCount "; - $dao = CRM_Core_DAO::executeQuery($sql); + $mode = CRM_Contact_BAO_Query::MODE_CONTRIBUTE; + $entity = 'contribution'; + list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity); $contribution = array(); while ($dao->fetch()) { diff --git a/api/v3/Participant.php b/api/v3/Participant.php index cf39df191f..c53339f7e3 100644 --- a/api/v3/Participant.php +++ b/api/v3/Participant.php @@ -138,35 +138,15 @@ function _civicrm_api3_participant_create_spec(&$params) { * @access public */ function civicrm_api3_participant_get($params) { + $mode = CRM_Contact_BAO_Query::MODE_EVENT; + $entity = 'participant'; - $options = _civicrm_api3_get_options_from_params($params, TRUE,'participant','get'); - $sort = CRM_Utils_Array::value('sort', $options, NULL); - $offset = CRM_Utils_Array::value('offset', $options); - $rowCount = CRM_Utils_Array::value('limit', $options); - $smartGroupCache = CRM_Utils_Array::value('smartGroupCache', $params); - $inputParams = CRM_Utils_Array::value('input_params', $options, array()); - $returnProperties = CRM_Utils_Array::value('return', $options, NULL); - - if (empty($returnProperties)) { - $returnProperties = CRM_Event_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_EVENT); - } - $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); - $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL, - FALSE, FALSE, CRM_Contact_BAO_Query::MODE_EVENT - ); - list($select, $from, $where, $having) = $query->query(); - - $sql = "$select $from $where $having"; - - if (!empty($sort)) { - $sql .= " ORDER BY $sort "; - } - $sql .= " LIMIT $offset, $rowCount "; - $dao = CRM_Core_DAO::executeQuery($sql); + list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity); $participant = array(); while ($dao->fetch()) { $participant[$dao->participant_id] = $query->store($dao); + //@todo - is this required - contribution & pledge use the same query but don't self-retrieve custom data _civicrm_api3_custom_data_get($participant[$dao->participant_id], 'Participant', $dao->participant_id, NULL); } diff --git a/api/v3/Pledge.php b/api/v3/Pledge.php index 291f9b7304..6b5dfeaa59 100644 --- a/api/v3/Pledge.php +++ b/api/v3/Pledge.php @@ -129,25 +129,11 @@ function _civicrm_api3_pledge_create_spec(&$params) { * @access public */ function civicrm_api3_pledge_get($params) { + $mode = CRM_Contact_BAO_Query::MODE_PLEDGE; + $entity = 'pledge'; + + list($dao, $query) = _civicrm_api3_get_query_object($params, $mode, $entity); - $options = _civicrm_api3_get_options_from_params($params, TRUE, 'pledge','get'); - if (empty($options['return'])) { - $options['return'] = CRM_Pledge_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_PLEDGE); - } - else { - $options['return']['pledge_id'] = 1; - } - $newParams = CRM_Contact_BAO_Query::convertFormValues($options['input_params']); - $query = new CRM_Contact_BAO_Query($newParams, $options['return'], NULL, - FALSE, FALSE, CRM_Contact_BAO_Query::MODE_PLEDGE - ); - list($select, $from, $where) = $query->query(); - $sql = "$select $from $where"; - if (!empty($options['sort'])) { - $sql .= " ORDER BY " . $options['sort']; - } - $sql .= " LIMIT " . $options['offset'] . " , " . $options['limit']; - $dao = CRM_Core_DAO::executeQuery($sql); $pledge = array(); while ($dao->fetch()) { $pledge[$dao->pledge_id] = $query->store($dao); diff --git a/api/v3/utils.php b/api/v3/utils.php index f1dd90988e..2312c043aa 100644 --- a/api/v3/utils.php +++ b/api/v3/utils.php @@ -390,6 +390,8 @@ function _civicrm_api3_store_values(&$fields, &$params, &$values) { * others that use the query object. Note that this function passes permission information in. * The others don't * + * * Ideally this would be merged with _civicrm_get_query_object but we need to resolve differences in what the + * 2 variants call * @param $entity * @param array $params as passed into api get or getcount function * @param array $additional_options @@ -480,6 +482,45 @@ function _civicrm_api3_get_using_query_object($entity, $params, $additional_opti return $entities; } +/** + * get dao query object based on input params + * Ideally this would be merged with _civicrm_get_using_query_object but we need to resolve differences in what the + * 2 variants call + * + * @param array $params + * @param string $mode + * @param string $entity + * @return CRM_Core_DAO query object + */ +function _civicrm_api3_get_query_object($params, $mode, $entity) { + $options = _civicrm_api3_get_options_from_params($params, TRUE, $entity, 'get'); + $sort = CRM_Utils_Array::value('sort', $options, NULL); + $offset = CRM_Utils_Array::value('offset', $options); + $rowCount = CRM_Utils_Array::value('limit', $options); + $inputParams = CRM_Utils_Array::value('input_params', $options, array()); + $returnProperties = CRM_Utils_Array::value('return', $options, NULL); + if (empty($returnProperties)) { + $returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties($mode); + } + + $newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams); + $query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL, + FALSE, FALSE, $mode + ); + list($select, $from, $where, $having) = $query->query(); + + $sql = "$select $from $where $having"; + + if (!empty($sort)) { + $sql .= " ORDER BY $sort "; + } + if(!empty($rowCount)) { + $sql .= " LIMIT $offset, $rowCount "; + } + $dao = CRM_Core_DAO::executeQuery($sql); + return array($dao, $query); +} + /** * Function transfers the filters being passed into the DAO onto the params object */ diff --git a/tests/phpunit/api/v3/SyntaxConformanceTest.php b/tests/phpunit/api/v3/SyntaxConformanceTest.php index 596f48b6a0..7527b27ec4 100644 --- a/tests/phpunit/api/v3/SyntaxConformanceTest.php +++ b/tests/phpunit/api/v3/SyntaxConformanceTest.php @@ -268,16 +268,12 @@ class api_v3_SyntaxConformanceTest extends CiviUnitTestCase { public static function toBeSkipped_getlimit() { $entitiesWithout = array( 'Case',//case api has non-std mandatory fields one of (case_id, contact_id, activity_id, contact_id) - 'Contribution', //existing behaviour = fatal if 0 limit applied as offset of null is put in the query 'EntityTag', // non-standard api - has inappropriate mandatory fields & doesn't implement limit 'Event', // failed 'check that a 5 limit returns 5' - probably is_template field is wrong or something, or could be limit doesn't work right 'Extension', // can't handle creating 25 'MailingGroup', // no get call on MailingGroup 'Note', // fails on 5 limit - probably a set up problem - 'Participant', //existing behaviour = fatal if 0 limit applied as null offset in sql - 'Pledge', //existing behaviour = fatal if 0 limit applied as null offset in sql 'Setting', //a bit of a pseudoapi - keys by domain - ); return $entitiesWithout; }