From 18e4774cafd82452ca91f05de055c9453db9097b Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Thu, 28 Jan 2016 02:38:05 +0530 Subject: [PATCH] CRM-16188, added order api for delete action and test ---------------------------------------- * CRM-16188: Create an order API https://issues.civicrm.org/jira/browse/CRM-16188 --- api/v3/Order.php | 39 ++++++++++++++++++++++++++++++ tests/phpunit/api/v3/OrderTest.php | 23 ++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/api/v3/Order.php b/api/v3/Order.php index 4ef1b32b250a..f825ef0bf7f7 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -59,3 +59,42 @@ function civicrm_api3_order_get($params) { $params['sequential'] = $isSequential; return civicrm_api3_create_success($contributions, $params, 'Order', 'get'); } + +/** + * Delete a Order. + * + * @param array $params + * Input parameters. + * + * @return array + */ +function civicrm_api3_order_delete($params) { + $contribution = civicrm_api3('Contribution', 'get', array( + 'return' => array('is_test'), + 'id' => CRM_Utils_Array::value('contribution_id', $params, $params['id']), + )); + if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { + $result = civicrm_api3('Contribution', 'delete', $params); + } + else { + throw new API_Exception('Could not delete Order.'); + } + return civicrm_api3_create_success(CRM_Utils_Array::value('values', $result), $params, 'Order', 'delete'); +} + +/** + * Adjust Metadata for Delete action. + * + * The metadata is used for setting defaults, documentation & validation. + * + * @param array $params + * Array of parameters determined by getfields. + */ +function _civicrm_api3_order_delete_spec(&$params) { + $params['contribution_id'] = array( + 'api.required' => TRUE, + 'title' => 'Contribution ID', + 'type' => CRM_Utils_Type::T_INT, + ); + $params['id']['api.aliases'] = array('contribution_id'); +} diff --git a/tests/phpunit/api/v3/OrderTest.php b/tests/phpunit/api/v3/OrderTest.php index 4585cdd8745f..3c63dd24c1bc 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -171,4 +171,27 @@ public function addOrder($isPriceSet, $amount = 300, $extraParams = array()) { return $this->callAPISuccess('Contribution', 'create', $p); } + /** + * Test delete order api + */ + public function testDeleteOrder() { + $order = $this->addOrder(FALSE, 100); + $params = array( + 'contribution_id' => $order['id'], + ); + try { + $this->callAPISuccess('order', 'delete', $params); + $this->fail("Missed expected exception"); + } + catch (Exception $expected) { + $this->callAPISuccess('contribution', 'create', array( + 'contribution_id' => $order['id'], + 'is_test' => TRUE, + )); + $this->callAPISuccess('order', 'delete', $params); + $order = $this->callAPISuccess('order', 'get', $params); + $this->assertEquals(0, $order['count']); + } + } + }