CRM-16188, added order api for delete action and test
authorPradeep Nayak <pradpnayak@gmail.com>
Wed, 27 Jan 2016 21:08:05 +0000 (02:38 +0530)
committerPradeep Nayak <pradpnayak@gmail.com>
Mon, 8 Feb 2016 09:54:28 +0000 (15:24 +0530)
----------------------------------------
* CRM-16188: Create an order API
  https://issues.civicrm.org/jira/browse/CRM-16188

api/v3/Order.php
tests/phpunit/api/v3/OrderTest.php

index 7580405072b9dfe51216d6acd5584c2c9cbbb7c4..f53144329037e774f73c7c8cdb2891ede117865a 100644 (file)
@@ -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');
+}
index f00e7e08093bb25a49da53215b1e88eb639c7211..ed18a1914bf36259359b824e0215786f45631424 100644 (file)
@@ -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']);
+    }
+  }
+
 }