CRM-21446 - Allow case id as well as hash in inbound email processing to autofile...
authorJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 24 Nov 2017 12:31:00 +0000 (18:01 +0530)
committerJitendra Purohit <jitendra@fuzion.co.nz>
Fri, 24 Nov 2017 12:42:07 +0000 (18:12 +0530)
CRM/Activity/BAO/Activity.php
tests/phpunit/api/v3/ActivityCaseTest.php

index a591d350f760803e05134412bc7cf9d5cb65dffe..787a1c955d08b153f27a7d941e5c1a922e580e92 100644 (file)
@@ -605,10 +605,16 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
 
     // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916)
     $matches = array();
-    if (preg_match('/\[case #([0-9a-h]{7})\]/', CRM_Utils_Array::value('subject', $params), $matches)) {
+    $subjectToMatch = CRM_Utils_Array::value('subject', $params);
+    if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) {
       $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY);
       $hash = $matches[1];
-      $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '$hash'";
+      $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '" . CRM_Core_DAO::escapeString($hash) . "'";
+    }
+    elseif (preg_match('/\[case #(\d+)\]/', $subjectToMatch, $matches)) {
+      $query = "SELECT id FROM civicrm_case WHERE id = '" . CRM_Core_DAO::escapeString($matches[1]) . "'";
+    }
+    if (!empty($matches)) {
       $caseParams = array(
         'activity_id' => $activity->id,
         'case_id' => CRM_Core_DAO::singleValueQuery($query),
@@ -617,7 +623,7 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity {
         CRM_Case_BAO_Case::processCaseActivity($caseParams);
       }
       else {
-        self::logActivityAction($activity, "unknown case hash encountered: $hash");
+        self::logActivityAction($activity, "Case details for {$matches[1]} not found while recording an activity on case.");
       }
     }
 
index 2a8187b0026abe575fe3c3ba2366f3d9f6a62b47..b2de06cdd8c096e85ad9f844fba55ff6fccb2de0 100644 (file)
@@ -49,6 +49,28 @@ class api_v3_ActivityCaseTest extends CiviCaseTestCase {
     ));
   }
 
+  /**
+   * Test activity creation on case based
+   * on id or hash present in case subject.
+   */
+  public function testActivityCreateOnCase() {
+    $hash = substr(sha1(CIVICRM_SITE_KEY . $this->_case['id']), 0, 7);
+    $subjectArr = array(
+      "[case #{$this->_case['id']}] test activity recording under case with id",
+      "[case #{$hash}] test activity recording under case with id",
+    );
+    foreach ($subjectArr as $subject) {
+      $activity = $this->callAPISuccess('Activity', 'create', array(
+        'source_contact_id' => $this->_cid,
+        'activity_type_id' => 'Phone Call',
+        'subject' => $subject,
+      ));
+      $case = $this->callAPISuccessGetSingle('Activity', array('return' => array("case_id"), 'id' => $activity['id']));
+      //Check if case id is present for the activity.
+      $this->assertEquals($this->_case['id'], $case['case_id'][0]);
+    }
+  }
+
   public function testGet() {
     $this->assertTrue(is_numeric($this->_case['id']));
     $this->assertTrue(is_numeric($this->_otherActivity['id']));