From: Eileen McNaughton Date: Tue, 14 Nov 2023 19:27:48 +0000 (+1300) Subject: Check tax is present in online contribution page receipt X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1b694a208e40dc7e00b56693d2e4855bc272554c;p=civicrm-core.git Check tax is present in online contribution page receipt --- diff --git a/Civi/Test/ContributionPageTestTrait.php b/Civi/Test/ContributionPageTestTrait.php index c1003e74b6..44a531fb60 100644 --- a/Civi/Test/ContributionPageTestTrait.php +++ b/Civi/Test/ContributionPageTestTrait.php @@ -11,6 +11,10 @@ namespace Civi\Test; +use Civi\Api4\UFField; +use Civi\Api4\UFGroup; +use Civi\Api4\UFJoin; + /** * Helper for event tests. * @@ -52,7 +56,9 @@ trait ContributionPageTestTrait { 'pay_later_text' => 'Send Money Now', ]; $contributionPageValues += $contributionPageDefaults; - return $this->createTestEntity('ContributionPage', $contributionPageValues, $identifier); + $return = $this->createTestEntity('ContributionPage', $contributionPageValues, $identifier); + $this->addProfilesToContributionPage(); + return $return; } /** @@ -254,4 +260,70 @@ trait ContributionPageTestTrait { ], 'check_box'); } + /** + * Add profiles to the event. + * + * This function is designed to reflect the + * normal use case where events do have profiles. + * + * Note if any classes do not want profiles, or want something different, + * the thinking is they should override this. Once that arises we can review + * making it protected rather than private & checking we are happy with the + * signature. + * + * @param string $identifier + */ + private function addProfilesToContributionPage(string $identifier = 'ContributionPage'): void { + $profiles = [ + ['name' => '_pre', 'title' => 'Page Pre Profile', 'weight' => 1, 'fields' => ['email']], + ['name' => '_post', 'title' => 'Page Post Profile', 'weight' => 2, 'fields' => ['first_name', 'last_name']], + ]; + foreach ($profiles as $profile) { + $this->createContributionPageProfile($profile, $identifier); + } + } + + /** + * Create a profile attached to an event. + * + * @param array $profile + * @param string $identifier + */ + private function createContributionPageProfile(array $profile, string $identifier): void { + $profileName = $identifier . $profile['name']; + $profileIdentifier = $profileName; + try { + $this->setTestEntity('UFGroup', UFGroup::create(FALSE)->setValues([ + 'group_type' => 'Individual,Contact', + 'name' => $profileName, + 'title' => $profile['title'], + 'frontend_title' => 'Public ' . $profile['title'], + ])->execute()->first(), + $profileIdentifier); + } + catch (\CRM_Core_Exception $e) { + $this->fail('UF group creation failed for ' . $profileName . ' with error ' . $e->getMessage()); + } + foreach ($profile['fields'] as $field) { + $this->setTestEntity('UFField', UFField::create(FALSE) + ->setValues([ + 'uf_group_id:name' => $profileName, + 'field_name' => $field, + 'label' => $field, + ]) + ->execute() + ->first(), $field . '_' . $profileIdentifier); + } + try { + $this->setTestEntity('UFJoin', UFJoin::create(FALSE)->setValues([ + 'module' => 'CiviContribute', + 'uf_group_id:name' => $profileName, + 'entity_id' => $this->getContributionPageID($identifier), + ])->execute()->first(), $profileIdentifier); + } + catch (\CRM_Core_Exception $e) { + $this->fail('UF join creation failed for UF Group ' . $profileName . ' with error ' . $e->getMessage()); + } + } + } diff --git a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php index 9244ec4cca..fd203cd653 100644 --- a/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php +++ b/tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php @@ -355,7 +355,7 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { 'id' => $this->getContributionPageID(), 'first_name' => 'J', 'last_name' => 'T', - 'email' => 'JT@ohcanada.ca', + 'email-5' => 'JT@ohcanada.ca', 'receive_date' => date('Y-m-d H:i:s'), 'payment_processor_id' => 0, 'priceSetId' => $this->getPriceSetID('ContributionPage'), @@ -385,6 +385,13 @@ class CRM_Contribute_Form_Contribution_ConfirmTest extends CiviUnitTestCase { $lineItems = LineItem::get()->addWhere('contribution_id', '=', $contribution['id'])->execute(); $this->assertEquals($lineItems[0]['line_total'] + $lineItems[1]['line_total'] + $lineItems[2]['line_total'], round($totalAmount, 2), 'Line Item Total is incorrect.'); $this->assertEquals(round($lineItems[0]['tax_amount'] + $lineItems[1]['tax_amount'] + $lineItems[2]['tax_amount'], 2), round($taxAmount, 2), 'Wrong Sales Tax Amount is calculated and stored.'); + $mailUtil = new CiviMailUtils($this); + $this->callAPISuccess('Payment', 'create', [ + 'contribution_id' => $contribution['id'], + 'total_amount' => round($totalAmount + $taxAmount, 2), + 'payment_instrument_id' => 'Check', + ]); + $mailUtil->checkMailLog([\Civi::format()->money(337.55), 'Tax Rate', 'Subtotal']); } } diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 8e6e5f8e16..ff638cdafb 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -3389,7 +3389,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { foreach ($items as $item) { $itemTotal += $item['amount']; } - $this->assertEquals($payment['total_amount'], $itemTotal); + $this->assertEquals(round((float) $payment['total_amount'], 2), round($itemTotal, 2)); } }