dev/core#3084 Bug fix on line item financial types
authorEileen McNaughton <emcnaughton@wikimedia.org>
Sun, 24 Apr 2022 21:12:48 +0000 (09:12 +1200)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Tue, 26 Apr 2022 09:17:12 +0000 (21:17 +1200)
This initially caused test fails on test written for
https://lab.civicrm.org/dev/core/-/issues/860

However, I think the issue is flaws in the test, which
I have adjusted to have more accurate set up

CRM/Financial/BAO/Order.php
tests/phpunit/CRM/Member/Form/MembershipTest.php
tests/phpunit/CRMTraits/Financial/PriceSetTrait.php

index 935403d1638d37da2ac39c9068160222a3fc2e45..63476d8749accb41a324fcaca39c2b589f5beb56 100644 (file)
@@ -124,7 +124,7 @@ class CRM_Financial_BAO_Order {
    *
    * @var bool
    */
-  protected $isPermitOverrideFinancialTypeForMultipleLines = TRUE;
+  protected $isPermitOverrideFinancialTypeForMultipleLines = FALSE;
 
   /**
    * @return bool
index 5b78ae844dc340dea0967216e4eb159930c649e2..a9fe4f84a57b0f47f9eb6ba666e4a20aadd5fd16 100644 (file)
@@ -866,14 +866,17 @@ class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
    * @throws \API_Exception
    */
   public function testSubmitRecurTwoRows(): void {
-    $pfvIDs = $this->createMembershipPriceSet();
+    $this->createMembershipPriceSet();
     MembershipType::update()
       ->addWhere('id', '=', $this->ids['membership_type']['AnnualRollingOrg2'])
       ->setValues(['frequency_interval' => 1, 'frequency_unit' => 'month', 'auto_renew' => 1])->execute();
     $form = $this->getForm();
     $form->_mode = 'live';
     $priceParams = [
-      'price_' . $this->getPriceFieldID() => $pfvIDs,
+      'price_' . $this->getPriceFieldID() => [
+        $this->ids['PriceFieldValue']['AnnualRollingOrg2'] => 1,
+        $this->ids['PriceFieldValue']['AnnualRolling'] => 1,
+      ],
       'price_set_id' => $this->getPriceSetID(),
       'membership_type_id' => NULL,
       // Set financial type id to null to check it is retrieved from the price set.
@@ -1160,8 +1163,6 @@ Expires: ',
    * @throws \CiviCRM_API3_Exception
    */
   public function testTwoInheritedMembershipsViaPriceSetInBackend(): void {
-    // @todo figure out why financial validation fails with this test.
-    $this->isValidateFinancialsOnPostAssert = FALSE;
     // Create an organization and give it a "Member of" relationship to $this->_individualId.
     $orgID = $this->organizationCreate();
     $relationship = $this->callAPISuccess('Relationship', 'create', [
@@ -1172,7 +1173,7 @@ Expires: ',
     ]);
 
     // Create two memberships for the organization, via a price set in the back end.
-    $this->createTwoMembershipsViaPriceSetInBackEnd($orgID);
+    $this->createTwoMembershipsViaPriceSetInBackEnd($orgID, FALSE);
 
     // Check the primary memberships on the organization.
     $orgMembershipResult = $this->callAPISuccess('membership', 'get', [
@@ -1242,8 +1243,6 @@ Expires: ',
    * @throws \CiviCRM_API3_Exception
    */
   public function testTwoMembershipsViaPriceSetInBackendWithDiscount(): void {
-    // @todo figure out how to fix to pass valid financials.
-    $this->isValidateFinancialsOnPostAssert = FALSE;
     // Register buildAmount hook to apply discount.
     $this->hookClass->setHook('civicrm_buildAmount', [$this, 'buildAmountMembershipDiscount']);
     $this->enableTaxAndInvoicing();
@@ -1364,15 +1363,19 @@ Expires: ',
    * create two memberships for the same individual, via a price set in the back end.
    *
    * @param int $contactId Id of contact on which the memberships will be created.
+   * @param bool $isTaxEnabled
+   *   Is tax enabled for the Member Dues financial type.
+   *   Note that currently this ALSO assumes a discount has been
+   *   applied - this overloading would ideally be cleaned up.
    *
    * @throws \CRM_Core_Exception
    * @throws \CiviCRM_API3_Exception
    */
-  protected function createTwoMembershipsViaPriceSetInBackEnd($contactId): void {
+  protected function createTwoMembershipsViaPriceSetInBackEnd(int $contactId, $isTaxEnabled = TRUE): void {
     $form = $this->getForm();
     $form->preProcess();
     $this->createLoggedInUser();
-    $pfvIDs = $this->createMembershipPriceSet();
+    $this->createMembershipPriceSet();
 
     // register for both of these memberships via backoffice membership form submission
     $params = [
@@ -1381,14 +1384,19 @@ Expires: ',
       'join_date' => date('Y-m-d'),
       'start_date' => '',
       'end_date' => '',
-      "price_" . $this->getPriceFieldID() => $pfvIDs,
+      'price_' . $this->getPriceFieldID() => [
+        $this->ids['PriceFieldValue']['AnnualRollingOrg2'] => 1,
+        $this->ids['PriceFieldValue']['AnnualRolling'] => 1,
+      ],
+      // $15 for discounted goat + $259 = $274 + $27.4 in tax.
+      // or without tax (and without discount) $279.
+      'total_amount' => $isTaxEnabled ? 301.4 : 279,
       'price_set_id' => $this->getPriceSetID(),
       'membership_type_id' => [1 => 0],
       'auto_renew' => '0',
       'max_related' => '',
       'num_terms' => '2',
       'source' => '',
-      'total_amount' => '30.00',
       //Member dues, see data.xml
       'financial_type_id' => '2',
       'soft_credit_type_id' => '',
index cd92b0a45a7f93ffcf0b54e6c8d03d8d4d982993..8e3670d5ee00238d605fab6a3fd6fbef734a127d 100644 (file)
@@ -82,10 +82,8 @@ trait CRMTraits_Financial_PriceSetTrait {
    *
    * The price field is of type checkbox and each price-option
    * corresponds to a membership type
-   *
-   * @return array
    */
-  protected function createMembershipPriceSet(): array {
+  protected function createMembershipPriceSet(): void {
     $this->ids['PriceSet']['membership'] = (int) $this->callAPISuccess('PriceSet', 'create', [
       'is_quick_config' => 0,
       'extends' => 'CiviMember',
@@ -109,15 +107,11 @@ trait CRMTraits_Financial_PriceSetTrait {
         'price_field_id' => $this->ids['PriceField']['membership'],
         'label' => array_shift($labels),
         'amount' => array_shift($amounts),
-        'financial_type_id' => 'Donation',
+        'financial_type_id' => 'Member Dues',
         'membership_type_id' => $membershipTypeID,
         'membership_num_terms' => array_shift($membershipNumTerms),
       ])['id'];
     }
-    return [
-      $this->ids['PriceFieldValue']['AnnualRollingOrg2'] => 1,
-      $this->ids['PriceFieldValue']['AnnualRolling'] => 1,
-    ];
   }
 
   /**