From 76ac5fa2df409a8ce246ca529154fb246918f4d1 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 20 Nov 2023 11:32:02 +1300 Subject: [PATCH] Move Zero amount test --- Civi/Test/FormTrait.php | 32 ++++++++++++++++ .../Form/Contribution/ConfirmTest.php | 37 +++++++++++++++++++ tests/phpunit/api/v3/ContributionPageTest.php | 36 ------------------ 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/Civi/Test/FormTrait.php b/Civi/Test/FormTrait.php index 56944e05c7..47c602c9c7 100644 --- a/Civi/Test/FormTrait.php +++ b/Civi/Test/FormTrait.php @@ -57,6 +57,18 @@ trait FormTrait { } } + /** + * Assert that the sent mail included the supplied strings. + * + * @param array $strings + * @param int $mailIndex + */ + protected function assertMailSentNotContainStrings(array $strings, int $mailIndex = 0): void { + foreach ($strings as $string) { + $this->assertMailSentNotContainingString($string, $mailIndex); + } + } + /** * Assert that the sent mail included the supplied string. * @@ -68,6 +80,17 @@ trait FormTrait { $this->assertStringContainsString(preg_replace('/\s+/', '', $string), preg_replace('/\s+/', '', $mail['body'])); } + /** + * Assert that the sent mail included the supplied string. + * + * @param string $string + * @param int $mailIndex + */ + protected function assertMailSentNotContainingString(string $string, int $mailIndex = 0): void { + $mail = $this->form->getMail()[$mailIndex]; + $this->assertStringNotContainsString(preg_replace('/\s+/', '', $string), preg_replace('/\s+/', '', $mail['body'])); + } + /** * Assert that the sent mail included the supplied string. * @@ -79,6 +102,15 @@ trait FormTrait { $this->assertStringContainsString($string, $mail['headers']); } + /** + * Assert the right number of mails were sent. + * + * @param int $count + */ + protected function assertMailSentCount(int $count): void { + $this->assertCount($count, $this->form->getMail()); + } + /** * Assert that the sent mail included the supplied strings. * diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php index 2d1f0f9f26..97c3aca606 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php @@ -529,4 +529,41 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { $this->assertMailSentContainingHeaderString('Test Frontend title'); } + /** + * Test a zero dollar membership (quick config, not separate payment). + * + * @throws \CRM_Core_Exception + */ + public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail(): void { + $this->createTestEntity('MembershipType', [ + 'min_amount' => 0, + 'name' => 'Free', + 'duration_unit' => 'year', + 'duration_interval' => 1, + 'period_type' => 'rolling', + 'member_of_contact_id' => CRM_Core_BAO_Domain::getDomain()->contact_id, + 'financial_type_id:name' => 'Member Dues', + 'is_active' => 1, + 'sequential' => 1, + 'visibility' => 'Public', + ], 'free'); + $this->contributionPageQuickConfigCreate([], [], FALSE, TRUE, TRUE, TRUE); + $this->submitOnlineContributionForm([ + 'payment_processor_id' => $this->ids['PaymentProcessor']['dummy'], + 'price_' . $this->ids['PriceField']['contribution_amount'] => -1, + 'price_' . $this->ids['PriceField']['membership_amount'] => $this->ids['PriceFieldValue']['membership_free'], + 'id' => $this->getContributionPageID(), + ] + $this->getBillingSubmitValues(), + $this->getContributionPageID()); + + $contribution = $this->callAPISuccessGetSingle('Contribution', ['contribution_page_id' => $this->getContributionPageID()]); + $this->callAPISuccessGetSingle('MembershipPayment', ['contribution_id' => $contribution['id']]); + //Assert only one mail is being sent. + $this->assertMailSentCount(1); + $this->assertMailSentContainingStrings([ + 'Free', + ]); + $this->assertMailSentNotContainingString('Amount'); + } + } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index ae883719e4..ba7adb6385 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -306,42 +306,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $this->assertEquals(date('Y-m-d', strtotime('+ 2 year - 1 day')), $membership['end_date']); } - /** - * Test submit with a membership block in place. - */ - public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail(): void { - $mut = new CiviMailUtils($this, TRUE); - $this->setUpMembershipContributionPage(FALSE, FALSE, ['minimum_fee' => 0]); - $this->addProfile('supporter_profile', $this->getContributionPageID()); - - $submitParams = [ - $this->getPriceFieldLabel('membership') => $this->getPriceFieldValue('general'), - 'id' => $this->getContributionPageID(), - 'billing_first_name' => 'Billy', - 'billing_middle_name' => 'Goat', - 'billing_last_name' => 'Gruffier', - 'email-Primary' => 'billy-goat@the-new-bridge.net', - 'payment_processor_id' => $this->params['payment_processor_id'], - ]; - - $this->callAPISuccess('ContributionPage', 'submit', $submitParams); - $contribution = $this->callAPISuccess('contribution', 'getsingle', ['contribution_page_id' => $this->getContributionPageID()]); - $this->callAPISuccess('membership_payment', 'getsingle', ['contribution_id' => $contribution['id']]); - //Assert only one mail is being sent. - $msgs = $mut->getAllMessages(); - $this->assertCount(1, $msgs); - - $mut->checkMailLog([ - 'Membership Type', - 'General', - 'Gruffier', - ], [ - 'Amount', - ]); - $mut->stop(); - $mut->clearMessages(); - } - /** * Test submit with a pay later and check line item in mails. */ -- 2.25.1