From 5f44f69876768a037cf95f16dcb2ac5f53a94cca 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 7580405072..f531443290 100644 --- a/api/v3/Order.php +++ b/api/v3/Order.php @@ -126,6 +126,28 @@ function civicrm_api3_order_create(&$params) { return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create'); } +/** + * 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 Create action. * @@ -153,3 +175,20 @@ function _civicrm_api3_order_create_spec(&$params) { 'api.required' => TRUE, ); } + +/** + * 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 f00e7e0809..ed18a1914b 100644 --- a/tests/phpunit/api/v3/OrderTest.php +++ b/tests/phpunit/api/v3/OrderTest.php @@ -433,4 +433,27 @@ class api_v3_OrderTest extends CiviUnitTestCase { )); } + /** + * 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']); + } + } + } -- 2.25.1