From f274450a9b445fa8b97da34d80efb989fe94e0c5 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Apr 2017 20:09:13 -0400 Subject: [PATCH 1/4] CRM-19767 - Improve token category appearance --- CRM/Utils/Token.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index 4f8729229981..19cc7591c43d 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -1603,7 +1603,7 @@ public static function replaceEntityTokens($entity, $entityArray, $str, $knownTo /** * @param int $caseId - * @param int $str + * @param string $str * @param array $knownTokens * @param bool $escapeSmarty * @return string @@ -1875,7 +1875,7 @@ public static function formatTokensForDisplay($tokens) { else { $split = explode('.', trim($k, '{}')); if (isset($split[1])) { - $entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucfirst($split[0]); + $entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucwords(str_replace('_', ' ', $split[0])); } else { $entity = 'Contact'; From b69df99f293f0760ed180b07c0147e1e601c6992 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 25 Apr 2017 20:10:57 -0400 Subject: [PATCH 2/4] CRM-19767 - Add case tokens to email activities --- CRM/Activity/BAO/Activity.php | 10 +++++++++- CRM/Contact/Form/Task/EmailCommon.php | 3 ++- CRM/Contact/Form/Task/PDFLetterCommon.php | 17 ++++++++++------- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index a42019a40510..33a25cd597bb 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -1049,6 +1049,7 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add * The additional information of CC and BCC appended to the activity Details. * @param array $contributionIds * @param int $campaignId + * @param int $caseId * * @return array * ( sent, activityId) if any email is sent and activityId @@ -1069,7 +1070,8 @@ public static function sendEmail( $contactIds = NULL, $additionalDetails = NULL, $contributionIds = NULL, - $campaignId = NULL + $campaignId = NULL, + $caseId = NULL ) { // get the contact details of logged in contact, which we set as from email if ($userID == NULL) { @@ -1188,6 +1190,12 @@ public static function sendEmail( $tokenHtml = NULL; } + if ($caseId) { + $tokenSubject = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenSubject, $subjectToken, $escapeSmarty); + $tokenText = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenText, $messageToken, $escapeSmarty); + $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken, $escapeSmarty); + } + if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { // also add the contact tokens to the template $smarty->assign_by_ref('contact', $values); diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index 4591c8bf481a..d8e7af4c33a1 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -476,7 +476,8 @@ public static function submit(&$form, $formValues) { array_keys($form->_toContactDetails), $additionalDetails, $contributionIds, - CRM_Utils_Array::value('campaign_id', $formValues) + CRM_Utils_Array::value('campaign_id', $formValues), + $form->getVar('_caseId') ); $followupStatus = ''; diff --git a/CRM/Contact/Form/Task/PDFLetterCommon.php b/CRM/Contact/Form/Task/PDFLetterCommon.php index 61bf8f4519ff..ee7179f21086 100644 --- a/CRM/Contact/Form/Task/PDFLetterCommon.php +++ b/CRM/Contact/Form/Task/PDFLetterCommon.php @@ -104,8 +104,8 @@ public static function processMessageTemplate($formValues) { * Process the form after the input has been submitted and validated. * * @param CRM_Core_Form $form - * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ public static function postProcess(&$form) { $formValues = $form->controller->exportValues($form->getName()); @@ -127,6 +127,14 @@ public static function postProcess(&$form) { $caseId = NULL; $params = ['contact_id' => $contactId]; + $caseId = $form->getVar('_caseId'); + if (empty($caseId) && !empty($form->_caseIds[$item])) { + $caseId = $form->_caseIds[$item]; + } + if ($caseId) { + $params['case_id'] = $caseId; + } + list($contact) = CRM_Utils_Token::getTokenDetails($params, $returnProperties, $skipOnHold, @@ -142,12 +150,7 @@ public static function postProcess(&$form) { } $tokenHtml = CRM_Utils_Token::replaceContactTokens($html_message, $contact[$contactId], TRUE, $messageToken); - if (!empty($form->_caseId)) { - $caseId = $form->_caseId; - } - if (empty($caseId) && !empty($form->_caseIds[$item])) { - $caseId = $form->_caseIds[$item]; - } + if ($caseId) { $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseId, $tokenHtml, $messageToken); } From cb5108b8ff5dbdb1be49bba660c469554acce0ae Mon Sep 17 00:00:00 2001 From: varshith89 Date: Thu, 2 Aug 2018 16:21:45 +0000 Subject: [PATCH 3/4] CATL-994: Add Test Cases To Civicase Tokens (WIP) --- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 17f9497ba122..58bca4cd382c 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -1343,4 +1343,73 @@ public function hook_civicrm_aclWhereClause($type, &$tables, &$whereTables, &$co } } + public function testCaseTokens() { + $caseTest = new CiviCaseTestCase(); + $caseTest->setUp(); + // Create a contact and contactDetails array. + $contactId = $this->individualCreate(); + + // create a case for this user + $result = civicrm_api3('Case', 'create', array( + 'contact_id' => $contactId, + 'case_type_id' => '1', + 'subject' => "my case", + 'status_id' => "Open", + )); + + $caseId = $result['id']; + $html_message = "

This is a test case with id: {case.id} and subject: {case.subject}

"; + $html_message = CRM_Utils_Token::replaceCaseTokens($caseId, $html_message); + + $this->assertTrue(strpos($html_message, 'id: '.$caseId) !== 0); + $this->assertTrue(strpos($html_message, 'subject: my case') !== 0); + } + + public function testSendEmailWithCaseId() { + // Create a contact and contactDetails array. + $contactId = $this->individualCreate(); + + // create a logged in USER since the code references it for sendEmail user. + $this->createLoggedInUser(); + $session = CRM_Core_Session::singleton(); + $loggedInUser = $session->get('userID'); + + $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); + + // create a case for this user + $result = civicrm_api3('Case', 'create', array( + 'contact_id' => $contactId, + 'case_type_id' => 1, + 'subject' => "my case", + 'status_id' => "Open", + )); + + $caseId = $result['id']; + + $subject = __FUNCTION__ . ' subject'; + $html = __FUNCTION__ . ' html'; + $text = __FUNCTION__ . ' text'; + $userID = $loggedInUser; + + list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( + $contactDetails, + $subject, + $text, + $html, + $contact['email'], + $userID, + $from = __FUNCTION__ . '@example.com', + $attachments = NULL, + $cc = NULL, + $bcc = NULL, + $contactIds = NULL, + $additionalDetails = NULL, + NULL, + $campaign_id = NULL, + $caseId + ); + $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); + $this->assertEquals($activity['case_id'], $caseId, 'Activity case_id does not match.'); + } + } From f7f1cc3b179536bcf2b3e8d1c49a481c155fc933 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 14 Dec 2019 08:57:33 +1100 Subject: [PATCH 4/4] Ensure CaseID is stored with the email activity and fix unit tests and also switch the createEmailActivity to using API Add in check of mail log as well --- CRM/Activity/BAO/Activity.php | 13 +++-- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 47 ++++++++++--------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 33a25cd597bb..7941aadc1043 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -979,12 +979,13 @@ public static function getActivitiesCount($input) { * @param string $additionalDetails * @param int $campaignID * @param array $attachments + * @param int $caseID * * @return int * The created activity ID * @throws \CRM_Core_Exception */ - public static function createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignID, $attachments) { + public static function createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignID, $attachments, $caseID) { $activityTypeID = CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Email'); // CRM-6265: save both text and HTML parts in details (if present) @@ -1006,6 +1007,9 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'status_id', 'Completed'), 'campaign_id' => $campaignID, ]; + if (!empty($caseID)) { + $activityParams['case_id'] = $caseID; + } // CRM-5916: strip [case #…] before saving the activity (if present in subject) $activityParams['subject'] = preg_replace('/\[case #([0-9a-h]{7})\] /', '', $activityParams['subject']); @@ -1016,9 +1020,8 @@ public static function createEmailActivity($userID, $subject, $html, $text, $add $activityParams = array_merge($activityParams, $attachments); } - $activity = self::create($activityParams); - - return $activity->id; + $activity = civicrm_api3('Activity', 'create', $activityParams); + return $activity['id']; } /** @@ -1099,7 +1102,7 @@ public static function sendEmail( } //create the meta level record first ( email activity ) - $activityID = self::createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignId, $attachments); + $activityID = self::createEmailActivity($userID, $subject, $html, $text, $additionalDetails, $campaignId, $attachments, $caseId); $returnProperties = []; if (isset($messageToken['contact'])) { diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index 58bca4cd382c..890a2854a8dc 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -1350,66 +1350,71 @@ public function testCaseTokens() { $contactId = $this->individualCreate(); // create a case for this user - $result = civicrm_api3('Case', 'create', array( + $result = $this->callAPISuccess('Case', 'create', [ 'contact_id' => $contactId, 'case_type_id' => '1', 'subject' => "my case", 'status_id' => "Open", - )); + ]); $caseId = $result['id']; $html_message = "

This is a test case with id: {case.id} and subject: {case.subject}

"; $html_message = CRM_Utils_Token::replaceCaseTokens($caseId, $html_message); - $this->assertTrue(strpos($html_message, 'id: '.$caseId) !== 0); + $this->assertTrue(strpos($html_message, 'id: ' . $caseId) !== 0); $this->assertTrue(strpos($html_message, 'subject: my case') !== 0); + $caseTest->tearDown(); } public function testSendEmailWithCaseId() { + $caseTest = new CiviCaseTestCase(); + $caseTest->setUp(); // Create a contact and contactDetails array. $contactId = $this->individualCreate(); + $contact = $this->callAPISuccess('Contact', 'get', ['id' => $contactId]); // create a logged in USER since the code references it for sendEmail user. $this->createLoggedInUser(); + CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access CiviCRM', 'access all cases and activities', 'administer CiviCase']; $session = CRM_Core_Session::singleton(); $loggedInUser = $session->get('userID'); - $contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion)); - // create a case for this user - $result = civicrm_api3('Case', 'create', array( + $result = $this->callAPISuccess('Case', 'create', [ 'contact_id' => $contactId, 'case_type_id' => 1, 'subject' => "my case", 'status_id' => "Open", - )); + ]); $caseId = $result['id']; - $subject = __FUNCTION__ . ' subject'; - $html = __FUNCTION__ . ' html'; + $subject = __FUNCTION__ . ' subject {case.subject}'; + $html = __FUNCTION__ . ' html {case.subject}'; $text = __FUNCTION__ . ' text'; - $userID = $loggedInUser; + $mut = new CiviMailUtils($this, TRUE); list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail( - $contactDetails, + $contact['values'], $subject, $text, $html, - $contact['email'], - $userID, + $contact['values'][$contactId]['email'], + $loggedInUser, $from = __FUNCTION__ . '@example.com', - $attachments = NULL, - $cc = NULL, - $bcc = NULL, - $contactIds = NULL, - $additionalDetails = NULL, NULL, - $campaign_id = NULL, + NULL, + NULL, + [$contactId], + NULL, + NULL, + NULL, $caseId ); - $activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion)); - $this->assertEquals($activity['case_id'], $caseId, 'Activity case_id does not match.'); + $activity = $this->callAPISuccess('Activity', 'getsingle', ['id' => $activity_id, 'return' => ['case_id']]); + $this->assertEquals($caseId, $activity['case_id'][0], 'Activity case_id does not match.'); + $mut->checkMailLog(['subject my case']); + $mut->stop(); } }