From cefed6df1125189fe47700e5d7992e2dcc39c048 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Thu, 16 Feb 2017 11:51:42 +1100 Subject: [PATCH] CRM-19657 Fix issue where domain address wasn't being used for recurring contribution emails but logged in was. --- CRM/Contribute/BAO/Contribution.php | 41 ++++++-- tests/phpunit/api/v3/ContributionTest.php | 110 ++++++++++++++++++++++ 2 files changed, 144 insertions(+), 7 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index 361ad3a378..8a611f35ed 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -4715,14 +4715,8 @@ LIMIT 1;"; $contribution->loadRelatedObjects($input, $ids, TRUE); // set receipt from e-mail and name in value if (!$returnMessageText) { - $userID = CRM_Core_Session::singleton()->getLoggedInContactID(); - if (!empty($userID)) { - list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); - $values['receipt_from_email'] = CRM_Utils_Array::value('receipt_from_email', $input, $userEmail); - $values['receipt_from_name'] = CRM_Utils_Array::value('receipt_from_name', $input, $userName); - } + list($values['receipt_from_name'], $values['receipt_from_email']) = self::generateFromEmailAndName($input, $contribution); } - $return = $contribution->composeMessageArray($input, $ids, $values, $recur, $returnMessageText); // Contribution ID should really always be set. But ? if (!$returnMessageText && (!isset($input['receipt_update']) || $input['receipt_update']) && empty($contribution->receipt_date)) { @@ -4731,6 +4725,39 @@ LIMIT 1;"; return $return; } + /** + * Generate From email and from name in an array values + */ + public static function generateFromEmailAndName($input, $contribution) { + // Use input valuse if supplied. + if (!empty($input['receipt_from_email'])) { + return array(CRM_Utils_array::value('receipt_from_name', $input, ''), $input['receipt_from_email']); + } + // if we are still empty see if we can use anything from a contribution page. + $pageValues = array(); + if (!empty($contribution->contribution_page_id)) { + $pageValues = civicrm_api3('ContributionPage', 'getsingle', array('id' => $contribution->contribution_page_id)); + } + // if we are still empty see if we can use anything from a contribution page. + if (!empty($pageValues['receipt_from_email'])) { + return array($pageValues['receipt_from_name'], $pageValues['receipt_from_email']); + } + // If we are still empty fall back to the domain. + $domain = civicrm_api3('domain', 'getsingle', array('id' => CRM_Core_Config::domainID())); + if (!empty($domain['from_email'])) { + return array($domain['from_name'], $domain['from_email']); + } + $userID = CRM_Core_Session::singleton()->getLoggedInContactID(); + $userName = ''; + $userEmail = ''; + if (!empty($userID)) { + list($userName, $userEmail) = CRM_Contact_BAO_Contact_Location::getEmailDetails($userID); + } + // If still empty fall back to the logged in user details. + // return empty values no matter what. + return array($userName, $userEmail); + } + /** * Generate credit note id with next avaible number * diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index 7b383de845..c6b6627250 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -3353,4 +3353,114 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->quickCleanUpFinancialEntities(); } + /** + * Test sending a mail via the API. + */ + public function testSendMailWithAPISetFromDetails() { + $mut = new CiviMailUtils($this, TRUE); + $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); + $this->callAPISuccess('contribution', 'sendconfirmation', array( + 'id' => $contribution['id'], + 'receipt_from_email' => 'api@civicrm.org', + 'receipt_from_name' => 'CiviCRM LLC', + )); + $mut->checkMailLog(array( + 'From: CiviCRM LLC ', + 'Contribution Information', + 'Please print this confirmation for your records', + ), array( + 'Event', + ) + ); + $mut->stop(); + } + + /** + * Test sending a mail via the API. + */ + public function testSendMailWithNoFromSetFallToDomain() { + $this->createLoggedInUser(); + $mut = new CiviMailUtils($this, TRUE); + $contribution = $this->callAPISuccess('contribution', 'create', $this->_params); + $this->callAPISuccess('contribution', 'sendconfirmation', array( + 'id' => $contribution['id'], + )); + $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1)); + $mut->checkMailLog(array( + 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>', + 'Contribution Information', + 'Please print this confirmation for your records', + ), array( + 'Event', + ) + ); + $mut->stop(); + } + + /** + * Test sending a mail via the API. + */ + public function testSendMailWithRepeatTransactionAPIFalltoDomain() { + $this->createLoggedInUser(); + $mut = new CiviMailUtils($this, TRUE); + $contribution = $this->setUpRepeatTransaction(array(), 'single'); + $this->callAPISuccess('contribution', 'repeattransaction', array( + 'contribution_status_id' => 'Completed', + 'trxn_id' => uniqid(), + 'original_contribution_id' => $contribution, + )); + $domain = $this->callAPISuccess('domain', 'getsingle', array('id' => 1)); + $mut->checkMailLog(array( + 'From: ' . $domain['from_name'] . ' <' . $domain['from_email'] . '>', + 'Contribution Information', + 'Please print this confirmation for your records', + ), array( + 'Event', + ) + ); + $mut->stop(); + } + + /** + * Test sending a mail via the API. + */ + public function testSendMailWithRepeatTransactionAPIFalltoContributionPage() { + $mut = new CiviMailUtils($this, TRUE); + $contributionPage = $this->contributionPageCreate(array('receipt_from_name' => 'CiviCRM LLC', 'receipt_from_email' => 'contributionpage@civicrm.org', 'is_email_receipt' => 1)); + $params['contribution_page_id'] = $contributionPage['id']; + $paymentProcessorID = $this->paymentProcessorCreate(); + $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', array( + 'contact_id' => $this->_individualId, + 'installments' => '12', + 'frequency_interval' => '1', + 'amount' => '500', + 'contribution_status_id' => 1, + 'start_date' => '2012-01-01 00:00:00', + 'currency' => 'USD', + 'frequency_unit' => 'month', + 'payment_processor_id' => $paymentProcessorID, + )); + $originalContribution = $this->callAPISuccess('contribution', 'create', array_merge( + $this->_params, + array( + 'contribution_recur_id' => $contributionRecur['id'], + 'contribution_page_id' => $contributionPage['id'])) + ); + $this->callAPISuccess('contribution', 'repeattransaction', array( + 'contribution_status_id' => 'Completed', + 'trxn_id' => uniqid(), + 'original_contribution_id' => $originalContribution, + ) + ); + $mut->checkMailLog(array( + 'From: CiviCRM LLC ', + 'Contribution Information', + 'Please print this confirmation for your records', + ), array( + 'Event', + ) + ); + $mut->stop(); + } + } -- 2.25.1