From 8572e6dec13340e0f096b9b2793c1c2f72eac126 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 19 Apr 2017 00:31:52 -0400 Subject: [PATCH] CRM-20105 - Add restore function to case api --- CRM/Core/Permission.php | 3 +++ api/v3/Case.php | 30 ++++++++++++++++++++++++++++-- tests/phpunit/api/v3/CaseTest.php | 13 ++++++++++--- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/CRM/Core/Permission.php b/CRM/Core/Permission.php index 826d70a2847d..5152a7010b74 100644 --- a/CRM/Core/Permission.php +++ b/CRM/Core/Permission.php @@ -1016,6 +1016,9 @@ public static function getEntityActionPermissions() { 'access CiviCRM', 'delete in CiviCase', ), + 'restore' => array( + 'administer CiviCase', + ), 'default' => array( // At minimum the user needs one of the following. Finer-grained access is controlled by CRM_Case_BAO_Case::addSelectWhereClause array('access my cases and activities', 'access all cases and activities'), diff --git a/api/v3/Case.php b/api/v3/Case.php index ddf3aead95c2..9d2edfd7541b 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -492,8 +492,7 @@ function civicrm_api3_case_update($params) { * @endcode * * @throws API_Exception - * @return bool - * true if success, else false + * @return mixed */ function civicrm_api3_case_delete($params) { //check parameters @@ -507,6 +506,33 @@ function civicrm_api3_case_delete($params) { } } +/** + * Case.restore API specification + * + * @param array $spec description of fields supported by this API call + * @return void + */ +function _civicrm_api3_case_restore_spec(&$spec) { + $result = civicrm_api3('Case', 'getfields', array('api_action' => 'delete')); + $spec = array('id' => $result['values']['id']); +} + +/** + * Restore a specified case from the trash. + * + * @param array $params + * @throws API_Exception + * @return mixed + */ +function civicrm_api3_case_restore($params) { + if (CRM_Case_BAO_Case::restoreCase($params['id'])) { + return civicrm_api3_create_success($params, $params, 'Case', 'restore'); + } + else { + throw new API_Exception('Could not restore case.'); + } +} + /** * Augment case results with extra data. * diff --git a/tests/phpunit/api/v3/CaseTest.php b/tests/phpunit/api/v3/CaseTest.php index be0d687cc1f5..712f706b7620 100644 --- a/tests/phpunit/api/v3/CaseTest.php +++ b/tests/phpunit/api/v3/CaseTest.php @@ -177,14 +177,21 @@ public function testCaseDelete() { // Move Case to Trash $id = $result['id']; - $result = $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1)); + $this->callAPISuccess('case', 'delete', array('id' => $id, 'move_to_trash' => 1)); // Check result - also check that 'case_id' works as well as 'id' $result = $this->callAPISuccess('case', 'get', array('case_id' => $id)); $this->assertEquals(1, $result['values'][$id]['is_deleted']); - // Delete Case Permanently - also check that 'case_id' works as well as 'id' - $result = $this->callAPISuccess('case', 'delete', array('case_id' => $id)); + // Restore Case from Trash + $this->callAPISuccess('case', 'restore', array('id' => $id)); + + // Check result + $result = $this->callAPISuccess('case', 'get', array('case_id' => $id)); + $this->assertEquals(0, $result['values'][$id]['is_deleted']); + + // Delete Case Permanently + $this->callAPISuccess('case', 'delete', array('case_id' => $id)); // Check result - case should no longer exist $result = $this->callAPISuccess('case', 'get', array('id' => $id));