From ecddfe70946c316ec909fda5694e74c5cf8dae29 Mon Sep 17 00:00:00 2001 From: eileen Date: Tue, 15 Jan 2019 18:44:19 +1300 Subject: [PATCH] Add unit test for payment email from AdditionalPayment form, rationalise some parameters to do this. Note that the goal is to move this function off this class to somewhere api accessible so breaking the connections with the form makes sense & in fact some like the fromEmails handling are really just badly copied & pasted from elsewhere (ie. the fromEmailIds doesn't exist & is created on the fly to house the email being passed in --- CRM/Contribute/Form/AdditionalPayment.php | 26 ++++++++----------- .../Contribute/Form/AdditionalPaymentTest.php | 21 ++++++++++++--- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/CRM/Contribute/Form/AdditionalPayment.php b/CRM/Contribute/Form/AdditionalPayment.php index 6fc1277577..16c6ba562b 100644 --- a/CRM/Contribute/Form/AdditionalPayment.php +++ b/CRM/Contribute/Form/AdditionalPayment.php @@ -386,8 +386,7 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract if (!empty($result) && !empty($this->_params['is_email_receipt'])) { $this->_params['contact_id'] = $this->_contactId; $this->_params['contribution_id'] = $this->_contributionId; - // to get 'from email id' for send receipt - $this->fromEmailId = $this->_params['from_email_address']; + $sendReceipt = $this->emailReceipt($this->_params); if ($sendReceipt) { $statusMsg .= ' ' . ts('A receipt has been emailed to the contributor.'); @@ -509,6 +508,11 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract public function emailReceipt(&$params) { $templateEngine = CRM_Core_Smarty::singleton(); // email receipt sending + list($contributorDisplayName, $contributorEmail, $doNotMail) = CRM_Contact_BAO_Contact::getContactDetails($params['contact_id']); + if (!$contributorEmail || $doNotMail) { + return FALSE; + } + $templateEngine->assign('contactDisplayName', $contributorDisplayName); // send message template if ($this->_component == 'event') { @@ -550,7 +554,6 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract $templateEngine->assign('paymentAmount', $params['total_amount']); $templateEngine->assign('paymentsComplete', $paymentsComplete); } - $templateEngine->assign('contactDisplayName', $this->_contributorDisplayName); // assign trxn details $templateEngine->assign('trxn_id', CRM_Utils_Array::value('trxn_id', $params)); @@ -565,22 +568,15 @@ class CRM_Contribute_Form_AdditionalPayment extends CRM_Contribute_Form_Abstract $sendTemplateParams = array( 'groupName' => 'msg_tpl_workflow_contribution', 'valueName' => 'payment_or_refund_notification', - 'contactId' => $this->_contactId, + 'contactId' => $params['contact_id'], 'PDFFilename' => ts('notification') . '.pdf', ); - // try to send emails only if email id is present - // and the do-not-email option is not checked for that contact - if ($this->_contributorEmail && !$this->_toDoNotEmail) { - if (array_key_exists($params['from_email_address'], $this->_fromEmails['from_email_id'])) { - $receiptFrom = $params['from_email_address']; - } + $sendTemplateParams['from'] = $params['from_email_address']; + $sendTemplateParams['toName'] = $contributorDisplayName; + $sendTemplateParams['toEmail'] = $contributorEmail; - $sendTemplateParams['from'] = $receiptFrom; - $sendTemplateParams['toName'] = $this->_contributorDisplayName; - $sendTemplateParams['toEmail'] = $this->_contributorEmail; - } - list($mailSent, $subject, $message, $html) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); + list($mailSent) = CRM_Core_BAO_MessageTemplate::sendTemplate($sendTemplateParams); return $mailSent; } diff --git a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php index fa20f943e8..8bf35e2609 100644 --- a/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php +++ b/tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php @@ -129,11 +129,22 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { * Test the submit function that completes the partially paid Contribution using Credit Card. */ public function testAddPaymentUsingCreditCardForPartialyPaidContribution() { + $mut = new CiviMailUtils($this, TRUE); $this->createContribution('Partially paid'); // pay additional amount by using Credit Card - $this->submitPayment(70, 'live'); + $this->submitPayment(70, 'live', TRUE); $this->checkResults(array(30, 70), 2); + $mut->assertSubjects(['Payment Receipt -']); + $mut->checkMailLog([ + 'Dear Anthony Anderson', + 'Payment Details', + 'Total Fees: $ 100.00', + 'This Payment Amount: $ 70.00', + 'Balance Owed: $ 0.00 ', + ]); + + $mut->stop(); } /** @@ -332,9 +343,9 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { * Payment Amount * @param string $mode * Mode of Payment - * + * @param bool $isEmailReceipt */ - public function submitPayment($amount, $mode = NULL) { + public function submitPayment($amount, $mode = NULL, $isEmailReceipt = FALSE) { $form = new CRM_Contribute_Form_AdditionalPayment(); $submitParams = array( @@ -347,10 +358,12 @@ class CRM_Contribute_Form_AdditionalPaymentTest extends CiviUnitTestCase { 'receive_date_time' => '11:27PM', 'trxn_date' => '2017-04-11 13:05:11', 'payment_processor_id' => 0, + 'is_email_receipt' => $isEmailReceipt, + 'from_email_address' => 'site@something.com', ); if ($mode) { $submitParams += array( - 'payment_instrument_id' => array_search('Credit card', $this->paymentInstruments), + 'payment_instrument_id' => array_search('Credit Card', $this->paymentInstruments), 'payment_processor_id' => $this->paymentProcessorID, 'credit_card_exp_date' => array('M' => 5, 'Y' => 2025), 'credit_card_number' => '411111111111111', -- 2.25.1