Add unit test cover for test offline mail receipt
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 27 Mar 2023 04:45:40 +0000 (17:45 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 27 Mar 2023 06:41:14 +0000 (19:41 +1300)
CRM/Member/Form/Membership.php
tests/phpunit/CRM/Member/Form/MembershipTest.php
xml/templates/message_templates/membership_offline_receipt_html.tpl

index abfa5a71d0d719fedba9485b3401606247d35767..e0e362a8508f78991558a31148ff8a639b157177 100644 (file)
@@ -1336,32 +1336,23 @@ DESC limit 1");
     $receiptSend = FALSE;
     $contributionId = $this->ids['Contribution'] ?? CRM_Member_BAO_Membership::getMembershipContributionId($this->getMembershipID());
     $membershipIds = $this->_membershipIDs;
-    if ($contributionId && !empty($membershipIds)) {
+    if ($this->getSubmittedValue('send_receipt') && $contributionId && !empty($membershipIds)) {
       $contributionDetails = CRM_Contribute_BAO_Contribution::getContributionDetails(
         CRM_Export_Form_Select::MEMBER_EXPORT, $this->_membershipIDs);
       if ($contributionDetails[$this->getMembershipID()]['contribution_status'] === 'Completed') {
-        $receiptSend = TRUE;
+        $formValues['contact_id'] = $this->_contactID;
+        $formValues['contribution_id'] = $contributionId;
+        // receipt_text_signup is no longer used in receipts from 5.47
+        // but may linger in some sites that have not updated their
+        // templates.
+        $formValues['receipt_text_signup'] = $this->getSubmittedValue('receipt_text');
+        // send email receipt
+        $this->assignBillingName();
+        $this->emailMembershipReceipt($formValues);
+        $this->addStatusMessage(ts('A membership confirmation and receipt has been sent to %1.', [1 => $this->_contributorEmail]));
       }
     }
 
-    $receiptSent = FALSE;
-    if ($this->getSubmittedValue('send_receipt') && $receiptSend) {
-      $formValues['contact_id'] = $this->_contactID;
-      $formValues['contribution_id'] = $contributionId;
-      // receipt_text_signup is no longer used in receipts from 5.47
-      // but may linger in some sites that have not updated their
-      // templates.
-      $formValues['receipt_text_signup'] = $formValues['receipt_text'];
-      // send email receipt
-      $this->assignBillingName();
-      $mailSend = $this->emailMembershipReceipt($formValues);
-      $receiptSent = TRUE;
-    }
-
-    if ($receiptSent && $mailSend) {
-      $this->addStatusMessage(ts('A membership confirmation and receipt has been sent to %1.', [1 => $this->_contributorEmail]));
-    }
-
     CRM_Core_Session::setStatus($this->getStatusMessage(), ts('Complete'), 'success');
     $this->setStatusMessage();
 
index 341764ba25ad607363b8410471e7a1d8560e235e..5fd4eaf96324c73ef6df8dad0d74bbd1685d767c 100644 (file)
@@ -19,6 +19,7 @@
 
 use Civi\Api4\FinancialType;
 use Civi\Api4\MembershipType;
+use Civi\Api4\PriceFieldValue;
 
 /**
  *  Test CRM_Member_Form_Membership functions.
@@ -222,6 +223,8 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    *  Test CRM_Member_Form_Membership::formRule() with a parameter
    *  that has an end date before the start date and a rolling
    *  membership type
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testFormRuleRollingEarlyEnd(): void {
     $unixYesterday = time() - (24 * 60 * 60);
@@ -1428,21 +1431,15 @@ Expires: ',
    * @throws \CRM_Core_Exception
    */
   public function testLineItemAmountOnSalesTax(): void {
+    $mailUtil = new CiviMailUtils($this, TRUE);
     $this->enableTaxAndInvoicing();
     $this->addTaxAccountToFinancialType(2);
-    $form = $this->getForm();
-    $form->preProcess();
-    $this->mut = new CiviMailUtils($this, TRUE);
-    $this->createLoggedInUser();
     $priceSet = $this->callAPISuccess('PriceSet', 'Get', ['extends' => 'CiviMember']);
-    $form->set('priceSetId', $priceSet['id']);
     // we are simulating the creation of a Price Set in Administer -> CiviContribute -> Manage Price Sets so set is_quick_config = 0
     $this->callAPISuccess('PriceSet', 'Create', ['id' => $priceSet['id'], 'is_quick_config' => 0]);
+    $fieldOption = PriceFieldValue::get()->addWhere('amount', '=', 50)->addSelect('id', 'price_field_id')->execute()->indexBy('id')->first();
     // clean the price options static variable to repopulate the options, in order to fetch tax information
     \Civi::$statics['CRM_Price_BAO_PriceField']['priceOptions'] = NULL;
-    CRM_Price_BAO_PriceSet::buildPriceSet($form);
-    // rebuild the price set form variable to include the tax information against each price options
-    $form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSet['id']));
     $params = [
       'cid' => $this->_individualId,
       'join_date' => date('Y-m-d'),
@@ -1451,6 +1448,7 @@ Expires: ',
       // This format reflects the first number being the organisation & the second being the type.
       'membership_type_id' => [$this->ids['contact']['organization'], $this->ids['membership_type']['AnnualFixed']],
       'record_contribution' => 1,
+      'price_' . $fieldOption['price_field_id'] => $fieldOption['id'],
       'total_amount' => 55,
       'receive_date' => date('Y-m-d') . ' 20:36:00',
       'payment_instrument_id' => array_search('Check', $this->paymentInstruments, TRUE),
@@ -1458,9 +1456,17 @@ Expires: ',
       //Member dues, see data.xml
       'financial_type_id' => 2,
       'payment_processor_id' => $this->_paymentProcessorID,
+      'send_receipt' => 1,
+      'from_email_address' => 'bob@example.com',
     ];
-    $form->_contactID = $this->_individualId;
-    $form->testSubmit($params);
+
+    $form = $this->getForm($params);
+    $this->createLoggedInUser();
+    $form->postProcess();
+    $email = preg_replace('/\s+/', ' ', $mailUtil->getMostRecentEmail());
+    foreach ($this->getExpectedEmailStrings() as $string) {
+      $this->assertStringContainsString($string, $email);
+    }
 
     $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
     $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership']);
@@ -1506,6 +1512,28 @@ Expires: ',
     $this->assertEquals($contribution['total_amount'], $financialItems_sum);
   }
 
+  /**
+   * Get the expected output from mail.
+   *
+   * @return string[]
+   */
+  private function getExpectedEmailStrings(): array {
+    return [
+      '<table id="crm-membership_receipt"',
+      'Membership Fee',
+      'Financial Type',
+      'Member Dues </td>',
+      '<tr> <td colspan="2" style="padding: 4px; border-bottom: 1px solid #999;"> <table> <tr> <th>Item</th> <th>Fee</th> <th>SubTotal</th> <th>Tax Rate</th> <th>Tax Amount</th> <th>Total</th> <th>Membership Start Date</th> <th>Membership Expiration Date</th> </tr> <tr> <td> Membership Amount - AnnualFixed </td>',
+      '<td> $50.00 </td> <td> $50.00 </td> <td> 10.00% </td> <td> $5.00 </td> <td> $55.00 </td> <td>',
+      'Amount Before Tax: </td>',
+      '<td style="padding: 4px; border-bottom: 1px solid #999;"> $50.00 </td>',
+      '<td>&nbsp;Sales Tax 10.00%</td> <td>&nbsp;$5.00</td>',
+      'Total Tax Amount </td> <td style="padding: 4px; border-bottom: 1px solid #999;"> $5.00 </td>',
+      'Amount </td> <td style="padding: 4px; border-bottom: 1px solid #999;"> $55.00 </td>',
+      'Paid By </td> <td style="padding: 4px; border-bottom: 1px solid #999;"> Check </td>',
+    ];
+  }
+
   /**
    * Test that membership end_date is correct for multiple terms for pending contribution
    *
index 65de42bd1ae8d86bc0dcbde04db271b3e6c51035..0d00a94e1177e7ef48023dd8fd5c79922025769f 100644 (file)
@@ -11,7 +11,7 @@
 {capture assign=labelStyle }style="padding: 4px; border-bottom: 1px solid #999; background-color: #f7f7f7;"{/capture}
 {capture assign=valueStyle }style="padding: 4px; border-bottom: 1px solid #999;"{/capture}
 
-  <table id="crm-event_receipt"
+  <table id="crm-membership_receipt"
          style="font-family: Arial, Verdana, sans-serif; text-align: left; width:100%; max-width:700px; padding:0; margin:0; border:0px;">
 
     <!-- BEGIN HEADER -->