CRM-20105 - Add merge function to case api
authorColeman Watts <coleman@civicrm.org>
Wed, 19 Apr 2017 16:15:27 +0000 (12:15 -0400)
committerColeman Watts <coleman@civicrm.org>
Wed, 19 Apr 2017 17:45:01 +0000 (13:45 -0400)
CRM/Core/Permission.php
api/v3/Case.php
tests/phpunit/api/v3/CaseTest.php

index 5152a7010b742f7bb03b96a05af0f93745c26ef1..e7190cc6ae8979852fb4d6cfe8b381f1da6a784c 100644 (file)
@@ -1019,6 +1019,9 @@ class CRM_Core_Permission {
       'restore' => array(
         'administer CiviCase',
       ),
+      'merge' => 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'),
index 9d2edfd7541b0a794f26584319667a5e2363f7f8..df6332a3eb258491d183aa39cba3c053bf60e271 100644 (file)
@@ -398,6 +398,42 @@ function _civicrm_api3_case_addtimeline_spec(&$params) {
   );
 }
 
+/**
+ * Merge 2 cases.
+ *
+ * @param array $params
+ *
+ * @throws API_Exception
+ * @return array
+ */
+function civicrm_api3_case_merge($params) {
+  $clients1 = CRM_Case_BAO_Case::getCaseClients($params['case_id_1']);
+  $clients2 = CRM_Case_BAO_Case::getCaseClients($params['case_id_2']);
+  CRM_Case_BAO_Case::mergeCases($clients1[0], $params['case_id_1'], $clients2[0], $params['case_id_2']);
+  return civicrm_api3_create_success();
+}
+
+/**
+ * Adjust Metadata for merge action.
+ *
+ * @param array $params
+ *   Array of parameters determined by getfields.
+ */
+function _civicrm_api3_case_merge_spec(&$params) {
+  $params['case_id_1'] = array(
+    'title' => 'Case ID 1',
+    'description' => 'Id of main case',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => 1,
+  );
+  $params['case_id_2'] = array(
+    'title' => 'Case ID 2',
+    'description' => 'Id of second case',
+    'type' => CRM_Utils_Type::T_INT,
+    'api.required' => 1,
+  );
+}
+
 /**
  * Declare deprecated api functions.
  *
index 712f706b76207e9c204519c8da46a823f3ab90b8..c4ce84537b896d93a4849fdffb18cd30a27abbb5 100644 (file)
@@ -704,4 +704,33 @@ class api_v3_CaseTest extends CiviCaseTestCase {
     $this->assertEquals('Follow up', $result['values'][1]['activity_type_id.name']);
   }
 
+
+  /**
+   * Test the case merge function.
+   *
+   * 2 cases should be mergeable into 1
+   *
+   * @throws \Exception
+   */
+  public function testCaseMerge() {
+    $contact1 = $this->individualCreate(array(), 1);
+    $case1 = $this->callAPISuccess('Case', 'create', array(
+      'contact_id' => $contact1,
+      'subject' => "Test case 1",
+      'case_type_id' => $this->caseTypeId,
+    ));
+    $case2 = $this->callAPISuccess('Case', 'create', array(
+      'contact_id' => $contact1,
+      'subject' => "Test case 2",
+      'case_type_id' => $this->caseTypeId,
+    ));
+    $result = $this->callAPISuccess('Case', 'getcount', array('contact_id' => $contact1));
+    $this->assertEquals(2, $result);
+
+    $this->callAPISuccess('Case', 'merge', array('case_id_1' => $case1['id'], 'case_id_2' => $case2['id']));
+
+    $result = $this->callAPISuccess('Case', 'getsingle', array('id' => $case2['id']));
+    $this->assertEquals(1, $result['is_deleted']);
+  }
+
 }