Add test cover for tax details in Dummy payment online contribution receipt
authorEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 14 Nov 2023 22:52:28 +0000 (11:52 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 14 Nov 2023 22:52:28 +0000 (11:52 +1300)
Civi/Test/ContributionPageTestTrait.php
tests/phpunit/CRM/Contribute/Form/Contribution/ConfirmTest.php
tests/phpunit/api/v3/ContributionPageTest.php

index 44a531fb60532a035103ed1c8789ece7a319ddba..9911e0b0fadf32bbe34fdce44fb60a4dde3b2386 100644 (file)
@@ -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,
+    ];
+  }
+
 }
index fd203cd653135b24297f366d1523bae9122b5109..dd6ff5dfaec85ef910b6c587339b28e225ea52c5 100644 (file)
@@ -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',
+    ]);
+
+  }
+
 }
index 5297027ff5fa13f15e7d19d9e71a37b133e6dc30..1d6c1c7667f7f844940965cec9babf164064642b 100644 (file)
@@ -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.
    *