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-21446 - Allow case id as well as hash in inbound email processing… #11320

Merged
merged 1 commit into from
Nov 24, 2017
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
12 changes: 9 additions & 3 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,10 +605,16 @@ public static function create(&$params) {

// 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),
Expand All @@ -617,7 +623,7 @@ public static function create(&$params) {
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.");
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/api/v3/ActivityCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,28 @@ public function setUp() {
));
}

/**
* 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']));
Expand Down