Add unit test for payment email from AdditionalPayment form, rationalise some paramet...
authoreileen <emcnaughton@wikimedia.org>
Tue, 15 Jan 2019 05:44:19 +0000 (18:44 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 15 Jan 2019 05:48:40 +0000 (18:48 +1300)
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
tests/phpunit/CRM/Contribute/Form/AdditionalPaymentTest.php

index 6fc1277577236d24dc0c620c3ec3ebcbc41b6f46..16c6ba562b368961150c2394cad23bec18e774e3 100644 (file)
@@ -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;
   }
 
index fa20f943e80a243cb9e27821036091c245699f9e..8bf35e2609388d287c9b053bd9eeb770f61121e3 100644 (file)
@@ -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',