From 3cf1fae974b9d20cf0550613b1e141f6a2be508f Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 13 Apr 2017 17:48:10 +0530 Subject: [PATCH] additional changes and added unit test --- CRM/Case/BAO/Case.php | 66 +++++++------------ .../phpunit/CRM/Activity/BAO/ActivityTest.php | 64 ++++++++++++++++++ 2 files changed, 87 insertions(+), 43 deletions(-) diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 7290a33207..991ccb9e33 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -2998,7 +2998,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id FROM civicrm_case_activity ca INNER JOIN civicrm_activity a ON ca.activity_id=a.id - WHERE a.activity_date_time = + WHERE a.activity_date_time = (SELECT b.activity_date_time FROM civicrm_case_activity bca INNER JOIN civicrm_activity b ON bca.activity_id=b.id WHERE b.activity_date_time <= DATE_ADD( NOW(), INTERVAL 14 DAY ) @@ -3011,7 +3011,7 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; AS SELECT ca.case_id, a.id, a.activity_date_time, a.status_id, a.activity_type_id FROM civicrm_case_activity ca INNER JOIN civicrm_activity a ON ca.activity_id=a.id - WHERE a.activity_date_time = + WHERE a.activity_date_time = (SELECT b.activity_date_time FROM civicrm_case_activity bca INNER JOIN civicrm_activity b ON bca.activity_id=b.id WHERE b.activity_date_time >= DATE_SUB( NOW(), INTERVAL 14 DAY ) @@ -3170,57 +3170,37 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; } /** - * CRM-20308 - * Method to get the contact id to use as from contact for email copy + * CRM-20308: Method to get the contact id to use as from contact for email copy * 1. Activity Added by Contact's email address * 2. System Default From Address * 3. Default Organization Contact email address * 4. Logged in user * - * @param int $activityId + * @param int $activityID + * * @return mixed $emailFromContactId * @see https://issues.civicrm.org/jira/browse/CRM-20308 */ - private static function getReceiptFrom($activityId) { - $emailFromContactId = NULL; - if (!empty($activityId)) { - try { - $emailFromContactId = civicrm_api3('ActivityContact', 'getvalue', array( - 'activity_id' => $activityId, - 'record_type_id' => 'Activity Source', - 'return' => 'contact_id', - )); - } - catch (CiviCRM_API3_Exception $ex) { - // get default from address from domain - try { - $domain = civicrm_api3('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); - if (isset($domain['from_email'])) { - $emailFromContactId = $domain['from_email']; - } - } - catch (CiviCRM_API3_Exception $ex) { - } - // if not found get email for contact_id 1 - if (!$emailFromContactId) { - try { - $emailFromContactId = civicrm_api3('Email', 'getvalue', array( - 'contact_id' => 1, - 'return' => 'email', - )); - } - catch (CiviCRM_API3_Exception $ex) { - } - } - } - + public static function getReceiptFrom($activityID) { + $name = $address = NULL; + + if (!empty($activityID)) { + // There is always a 'Added by' contact for a activity, + // so we can safely use ActivityContact.Getvalue API + $sourceContactId = civicrm_api3('ActivityContact', 'getvalue', array( + 'activity_id' => $activityID, + 'record_type_id' => 'Activity Source', + 'return' => 'contact_id', + )); + list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($sourceContactId); } - // if not found, use logged in user - if (!$emailFromContactId) { - $session = CRM_Core_Session::singleton(); - $emailFromContactId = $session->get('userID'); + + // If 'From' email address not found for Source Activity Contact then + // fetch the email from domain or logged in user. + if (empty($address)) { + list($name, $address) = CRM_Core_BAO_Domain::getDefaultReceiptFrom(); } - list($name, $address) = CRM_Contact_BAO_Contact_Location::getEmailDetails($emailFromContactId); + return "$name <$address>"; } diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index dd549d7f85..644e175b37 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -704,4 +704,68 @@ class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase { } } + /** + * CRM-20308: Test from email address when a 'copy of Activity' event occur + */ + public function testEmailAddressOfActivityCopy() { + // Case 1: assert the 'From' Email Address of source Actvity Contact ID + // create activity with source contact ID which has email address + $assigneeContactId = $this->individualCreate(); + $sourceContactParams = array( + 'first_name' => 'liz', + 'last_name' => 'hurleey', + 'email' => substr(sha1(rand()), 0, 7) . '@testemail.com', + ); + $sourceContactID = $this->individualCreate($sourceContactParams); + $sourceDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $sourceContactID, 'display_name'); + + // create an activity using API + $params = array( + 'source_contact_id' => $sourceContactID, + 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), + 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), + 'assignee_contact_id' => array($assigneeContactId), + 'activity_date_time' => date('Ymd'), + ); + $activity = $this->callAPISuccess('Activity', 'create', $params); + + // Check that from address is in "Source-Display-Name " + $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); + $expectedFromAddress = sprintf("%s <%s>", $sourceDisplayName, $sourceContactParams['email']); + $this->assertEquals($expectedFromAddress, $formAddress); + // ----------------------- End of Case 1 --------------------------- + + // Case 2: System Default From Address + // but first erase the email address of existing source contact ID + $withoutEmailParams = array( + 'email' => '', + ); + $sourceContactID = $this->individualCreate($withoutEmailParams); + $params = array( + 'source_contact_id' => $sourceContactID, + 'subject' => 'Scheduling Meeting ' . substr(sha1(rand()), 0, 4), + 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Meeting'), + 'activity_date_time' => date('Ymd'), + ); + $activity = $this->callAPISuccess('Activity', 'create', $params); + // fetch domain info + $domainInfo = $this->callAPISuccess('Domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); + + $formAddress = CRM_Case_BAO_Case::getReceiptFrom($activity['id']); + if (!empty($domainInfo['from_email'])) { + $expectedFromAddress = sprintf("%s <%s>", $domainInfo['from_name'], $domainInfo['from_email']); + } + // Case 3: fetch default Organization Contact email address + elseif (!empty($domainInfo['domain_email'])) { + $expectedFromAddress = sprintf("%s <%s>", $domainInfo['name'], $domainInfo['domain_email']); + } + // TODO: due to unknown reason the following assertion fails on + // test.civicrm.org test build but works fine on local + // $this->assertEquals($expectedFromAddress, $formAddress); + // ----------------------- End of Case 2 --------------------------- + + // TODO: Case 4 about checking the $formAddress on basis of logged contact ID respectively needs, + // to change the domain setting, which isn't straight forward in test environment + } + } -- 2.25.1