From a4b1b46da9c0a6479763e35418a50a51636936d8 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 15 Nov 2023 11:52:28 +1300 Subject: [PATCH] Add test cover for tax details in Dummy payment online contribution receipt --- Civi/Test/ContributionPageTestTrait.php | 22 +++++++ .../Form/Contribution/ConfirmTest.php | 59 +++++++++++++++++-- tests/phpunit/api/v3/ContributionPageTest.php | 38 ------------ 3 files changed, 77 insertions(+), 42 deletions(-) diff --git a/Civi/Test/ContributionPageTestTrait.php b/Civi/Test/ContributionPageTestTrait.php index 44a531fb60..9911e0b0fa 100644 --- a/Civi/Test/ContributionPageTestTrait.php +++ b/Civi/Test/ContributionPageTestTrait.php @@ -326,4 +326,26 @@ trait ContributionPageTestTrait { } } + /** + * Get suitable values for submitting the contribution form with a billing block. + * + * @param string $processorIdentifier + * + * @return array + */ + protected function getBillingSubmitValues(string $processorIdentifier = 'dummy'): array { + // @todo determine the fields from the processor. + return [ + 'billing_first_name' => 'Dave', + 'billing_middle_name' => 'Joseph', + 'billing_last_name' => 'Wong', + 'email' => 'dave@example.com', + 'payment_processor_id' => $this->ids['PaymentProcessor'][$processorIdentifier], + 'credit_card_number' => '4111111111111111', + 'credit_card_type' => 'Visa', + 'credit_card_exp_date' => ['M' => 9, 'Y' => 2040], + 'cvv2' => 123, + ]; + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php index fd203cd653..dd6ff5dfae 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Contribution; use Civi\Api4\LineItem; use Civi\Api4\PriceSetEntity; use Civi\Test\ContributionPageTestTrait; @@ -242,7 +243,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { * @noinspection PhpDocMissingThrowsInspection * @noinspection PhpUnhandledExceptionInspection */ - protected function createContributionPage(array $params, $isDefaultContributionPriceSet = TRUE): int { + protected function createContributionPage(array $params, bool $isDefaultContributionPriceSet = TRUE): int { $contributionPageID = (int) $this->callAPISuccess('ContributionPage', 'create', array_merge([ 'title' => 'Test Contribution Page', 'financial_type_id' => 'Campaign Contribution', @@ -320,8 +321,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { 'year' => 2021, 'month' => 2, ]; - $form = $this->submitOnlineContributionForm($submittedValues, $contributionPageID); - return $form; + return $this->submitOnlineContributionForm($submittedValues, $contributionPageID); } /** @@ -345,7 +345,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { * * @dataProvider getThousandSeparators */ - public function testSubmitContributionPageWithPriceSetQuantity(string $thousandSeparator): void { + public function testSubmitContributionComplexPriceSetPayLater(string $thousandSeparator): void { $this->setCurrencySeparators($thousandSeparator); $this->enableTaxAndInvoicing(); $this->contributionPageWithPriceSetCreate([], ['is_quick_config' => FALSE]); @@ -394,4 +394,55 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { $mailUtil->checkMailLog([\Civi::format()->money(337.55), 'Tax Rate', 'Subtotal']); } + /** + * Test form submission with multiple option price set. + * + * @param string $thousandSeparator + * punctuation used to refer to thousands. + * + * @dataProvider getThousandSeparators + * + * @throws \CRM_Core_Exception + */ + public function testSubmitContributionPageWithPriceSetTaxEnabled(string $thousandSeparator): void { + $this->setCurrencySeparators($thousandSeparator); + $this->enableTaxAndInvoicing(); + $this->contributionPageWithPriceSetCreate([], ['is_quick_config' => FALSE]); + // This function sets the Tax Rate at 10% - it currently has no way to pass Tax Rate into it - so let's work with 10% + $this->addTaxAccountToFinancialType($this->ids['FinancialType']['second']); + $this->submitOnlineContributionForm([ + 'id' => $this->getContributionPageID(), + 'first_name' => 'Billy', + 'last_name' => 'Gruff', + 'email-5' => 'billy@goat.gruff', + 'receive_date' => date('Y-m-d H:i:s'), + 'payment_processor_id' => 0, + 'priceSetId' => $this->getPriceSetID('ContributionPage'), + // qty = 1 * unit_price = $10.00 = 10. No sales tax. + 'price_' . $this->ids['PriceField']['radio_field'] => $this->ids['PriceFieldValue']['10_dollars'], + // qty = 2 * unit_price = $16.95 = 33.90. Tax = $3.39. + 'price_' . $this->ids['PriceField']['text_field_16.95'] => 2, + ] + $this->getBillingSubmitValues(), + $this->getContributionPageID() + ); + + $contribution = Contribution::get()->addWhere('contribution_page_id', '=', $this->getContributionPageID())->execute()->first(); + $this->assertEquals(47.29, $contribution['total_amount']); + $lineItems = $this->callAPISuccess('LineItem', 'get', [ + 'contribution_id' => $contribution['id'], + ]); + $this->assertEquals(2, $lineItems['count']); + $totalLineAmount = 0; + foreach ($lineItems['values'] as $lineItem) { + $totalLineAmount += $lineItem['line_total']; + } + $this->assertEquals(43.90, $totalLineAmount); + $this->assertMailSentContainingStrings([ + \Civi::format()->money(3.39), + 'Tax Rate', + 'Subtotal', + ]); + + } + } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 5297027ff5..1d6c1c7667 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -1541,44 +1541,6 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $this->assertEquals($contribution['id'], $pledgePayment[2]['contribution_id']); } - /** - * Test form submission with multiple option price set. - * - * @param string $thousandSeparator - * punctuation used to refer to thousands. - * - * @dataProvider getThousandSeparators - */ - public function testSubmitContributionPageWithPriceSet(string $thousandSeparator): void { - $this->setCurrencySeparators($thousandSeparator); - $this->setUpContributionPage([], ['is_quick_config' => FALSE]); - $submitParams = [ - 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']), - 'id' => $this->getContributionPageID(), - 'first_name' => 'Billy', - 'last_name' => 'Gruff', - 'email' => 'billy@goat.gruff', - 'is_pay_later' => TRUE, - ]; - $this->addPriceFields($submitParams); - - $this->callAPISuccess('ContributionPage', 'submit', $submitParams); - $contribution = $this->callAPISuccessGetSingle('contribution', [ - 'contribution_page_id' => $this->getContributionPageID(), - 'contribution_status_id' => 'Pending', - ]); - $this->assertEquals(80, $contribution['total_amount']); - $lineItems = $this->callAPISuccess('LineItem', 'get', [ - 'contribution_id' => $contribution['id'], - ]); - $this->assertEquals(3, $lineItems['count']); - $totalLineAmount = 0; - foreach ($lineItems['values'] as $lineItem) { - $totalLineAmount += $lineItem['line_total']; - } - $this->assertEquals(80, $totalLineAmount); - } - /** * Function to add additional price fields to price set. * -- 2.25.1