From 9e828bf2f56736a1ab79be096ff25178c7080f3d Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 27 Mar 2023 17:45:40 +1300 Subject: [PATCH] Add unit test cover for test offline mail receipt --- CRM/Member/Form/Membership.php | 31 +++++------- .../CRM/Member/Form/MembershipTest.php | 48 +++++++++++++++---- .../membership_offline_receipt_html.tpl | 2 +- 3 files changed, 50 insertions(+), 31 deletions(-) diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index abfa5a71d0..e0e362a850 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -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(); diff --git a/tests/phpunit/CRM/Member/Form/MembershipTest.php b/tests/phpunit/CRM/Member/Form/MembershipTest.php index 341764ba25..5fd4eaf963 100644 --- a/tests/phpunit/CRM/Member/Form/MembershipTest.php +++ b/tests/phpunit/CRM/Member/Form/MembershipTest.php @@ -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 [ + '', + '
', + '', + '', + '', + 'Total Tax Amount ', + 'Amount ', + 'Paid By ', + ]; + } + /** * Test that membership end_date is correct for multiple terms for pending contribution * diff --git a/xml/templates/message_templates/membership_offline_receipt_html.tpl b/xml/templates/message_templates/membership_offline_receipt_html.tpl index 65de42bd1a..0d00a94e11 100644 --- a/xml/templates/message_templates/membership_offline_receipt_html.tpl +++ b/xml/templates/message_templates/membership_offline_receipt_html.tpl @@ -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} -
Item Fee SubTotal Tax Rate Tax Amount Total Membership Start Date Membership Expiration Date
Membership Amount - AnnualFixed $50.00 $50.00 10.00% $5.00 $55.00 ', + 'Amount Before Tax: $50.00  Sales Tax 10.00%  $5.00 $5.00 $55.00 Check
-- 2.25.1