[REF] Fix Automatic renewal membership from front end contribution forms
authorSeamus Lee <seamuslee001@gmail.com>
Tue, 14 Jun 2022 23:49:26 +0000 (09:49 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Wed, 15 Jun 2022 00:50:00 +0000 (10:50 +1000)
Properly fix regression and update unit tests to match what the form does

CRM/Contribute/Form/ContributionBase.php
tests/phpunit/CRM/Contribute/Form/Contribution/MainTest.php

index e44a816bae8fdade5e4d58a50502bff6e0bd3879..a0ad0372f45e5a7a4d00feb612c8bee72a66ec15 100644 (file)
@@ -1151,10 +1151,13 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       return;
     }
 
+    // Check if membership the selected membership is automatically opted into auto renew or give user the option.
+    // In the 2nd case we check that the user has in deed opted in (auto renew as at June 22 is the field name for the membership auto renew checkbox)
+    // Also check that the payment Processor used can support recurring contributions.
     $membershipTypes = CRM_Price_BAO_PriceSet::getMembershipTypesFromPriceSet($this->_priceSetId);
     if (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required'])
       || (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']) &&
-        !empty($this->_params['is_recur']))
+        !empty($this->_params['auto_renew']))
         && !empty($this->_paymentProcessor['is_recur'])
     ) {
       $this->_params['auto_renew'] = TRUE;
@@ -1166,6 +1169,12 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form {
       $this->_params['frequency_interval'] = $this->_params['frequency_interval'] ?? $this->_values['fee'][$priceFieldId]['options'][$priceFieldValue]['membership_num_terms'];
       $this->_params['frequency_unit'] = $this->_params['frequency_unit'] ?? $membershipTypeDetails['duration_unit'];
     }
+    elseif (!$this->_separateMembershipPayment && (in_array($selectedMembershipTypeID, $membershipTypes['autorenew_required'])
+      || in_array($selectedMembershipTypeID, $membershipTypes['autorenew_optional']))) {
+      // otherwise check if we have a separate membership payment setting as that will allow people to independently opt into recurring contributions and memberships
+      // If we don't have that and the membership type is auto recur or opt into recur set is_recur to 0.
+      $this->_params['is_recur'] = $this->_values['is_recur'] = 0;
+    }
   }
 
   /**
index aa5d4bdd7c3255bdc1ea3a18eb6f8717a7ea7ad3..628b7c926c88dce7662f6383f76c2675b77b0ac8 100644 (file)
@@ -93,7 +93,6 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
     $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
     $form->testSubmit(array_merge($this->getSubmitParams(), [
       'price_' . $this->priceSetId => $priceFieldValueId,
-      'is_recur' => 1,
     ]));
     $this->assertEquals(1, $form->_params['is_recur']);
   }
@@ -107,7 +106,7 @@ class CRM_Contribute_Form_Contribution_MainTest extends CiviUnitTestCase {
     $priceFieldValueId = $this->getPriceFieldValue($membershipTypeID);
     $form->testSubmit(array_merge($this->getSubmitParams(), [
       'price_' . $this->priceSetId => $priceFieldValueId,
-      'is_recur' => 0,
+      'auto_renew' => 0,
     ]));
     $this->assertEquals(0, $form->_params['is_recur']);
   }