Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-16188, added order api for delete action and test #7684

Merged
merged 2 commits into from
Feb 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions api/v3/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note here - one way to make this shorter would be

if (!civicrm_api3('Contribution', 'getvalue', array('return' => 'is_test', 'id' => $params['id')) {
throw new ....
}

Note you can be sure $params['id'] will be set due to your aliasing - you don't need to check contribution_id.

Also - I suggest you edit the exception to say only test orders can be deleted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had tried with

$result = civicrm_api3('Contribution', 'getvalue', array(
'sequential' => 1,
'return' => array("is_test"),
'id' => 22,
));

I was getting exception error with 'field Array unset or not existing'.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah I think that might be a bug in the contribution api - no accepting that as a return value

'return' => array('is_test'),
'id' => $params['id'],
));
if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
$result = civicrm_api3('Contribution', 'delete', $params);
}
else {
throw new API_Exception('Only test orders can be deleted.');
}
return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
}

/**
* Adjust Metadata for Create action.
*
Expand Down Expand Up @@ -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');
}
23 changes: 23 additions & 0 deletions tests/phpunit/api/v3/OrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,27 @@ public function testAddOrderWithLineItems() {
));
}

/**
* Test delete order api
*/
public function testDeleteOrder() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice test!

$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']);
}
}

}