Update existing tests so that membership and recurring contribution durations match
authorMatthew Wire <devel@mrwire.co.uk>
Tue, 30 Jan 2018 10:09:44 +0000 (17:09 +0700)
committerMatthew Wire <devel@mrwire.co.uk>
Wed, 21 Mar 2018 11:20:33 +0000 (11:20 +0000)
tests/phpunit/api/v3/ContributionPageTest.php

index 11dad4a8a44858ba6d7ccd8a4d1f17b21d2a9d80..e9e9d87319d03a79c94a14b0254afc070aa2d4c3 100644 (file)
@@ -814,14 +814,34 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * - the first creates a new membership, completed contribution, in progress recurring. Check these
    * - create another - end date should be extended
    */
-  public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
+  public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentYear() {
+    $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'year'));
+  }
+
+  public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentMonth() {
+    $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'month', 'recur_frequency_unit' => 'month'));
+  }
+
+  public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPaymentDifferentFrequency() {
+    $this->doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment(array('duration_unit' => 'year', 'recur_frequency_unit' => 'month'));
+  }
+
+  public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment($params = array()) {
     $this->params['is_recur'] = 1;
-    $this->params['recur_frequency_unit'] = 'month';
-    $this->setUpMembershipContributionPage();
+    $this->params['recur_frequency_unit'] = $params['recur_frequency_unit'];
+    $membershipTypeParams['duration_unit'] = $params['duration_unit'];
+    $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
     $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
     $processor = $dummyPP->getPaymentProcessor();
 
+    if ($params['recur_frequency_unit'] === $params['duration_unit']) {
+      $expectedMembershipStatus = 1;
+    }
+    else {
+      $expectedMembershipStatus = 5;
+    }
+
     $submitParams = array(
       'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
       'id' => (int) $this->_ids['contribution_page'],
@@ -838,7 +858,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'cvv2' => 123,
       'is_recur' => 1,
       'frequency_interval' => 1,
-      'frequency_unit' => 'month',
+      'frequency_unit' => $this->params['recur_frequency_unit'],
     );
 
     $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
@@ -853,7 +873,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
     $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
     $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
-    $this->assertEquals(1, $membership['status_id']);
+    $this->assertEquals($expectedMembershipStatus, $membership['status_id']);
     $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
 
     $this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
@@ -867,7 +887,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'contribution_status_id' => 1,
     ));
     $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
-    $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
     $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
     $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
     $this->assertEquals(5, $recurringContribution['contribution_status_id']);
@@ -883,10 +903,10 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    */
   public function testSubmitMembershipComplexNonPriceSetPaymentPaymentProcessorRecurInstantPayment() {
     $this->params['is_recur'] = 1;
-    $this->params['recur_frequency_unit'] = 'month';
+    $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
     // Add a membership so membership & contribution are not both 1.
     $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
-    $this->setUpMembershipContributionPage();
+    $this->setUpMembershipContributionPage(FALSE, FALSE, $membershipTypeParams);
     $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
     $processor = $dummyPP->getPaymentProcessor();
@@ -908,7 +928,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'cvv2' => 123,
       'is_recur' => 1,
       'frequency_interval' => 1,
-      'frequency_unit' => 'month',
+      'frequency_unit' => $this->params['recur_frequency_unit'],
     );
 
     $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
@@ -951,7 +971,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
 
     $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
-    $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
     $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
     $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
     $this->assertEquals(5, $recurringContribution['contribution_status_id']);
@@ -967,7 +987,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    */
   public function testSubmitMembershipComplexPriceSetPaymentPaymentProcessorRecurInstantPayment() {
     $this->params['is_recur'] = 1;
-    $this->params['recur_frequency_unit'] = 'month';
+    $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
     // Add a membership so membership & contribution are not both 1.
     $preExistingMembershipID = $this->contactMembershipCreate(array('contact_id' => $this->contactIds[0]));
     $this->createPriceSetWithPage();
@@ -995,7 +1015,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
       'cvv2' => 123,
       'frequency_interval' => 1,
-      'frequency_unit' => 'month',
+      'frequency_unit' => $this->params['recur_frequency_unit'],
     );
 
     $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
@@ -1040,7 +1060,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->assertEquals($renewContribution['id'], $lines['values'][1]['entity_id']);
 
     $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $preExistingMembershipID + 1));
-    $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
+    $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
   }
 
   /**
@@ -1148,7 +1168,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    */
   public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
     $this->params['is_recur'] = 1;
-    $this->params['recur_frequency_unit'] = 'month';
+    $this->params['recur_frequency_unit'] = $membershipTypeParams['duration_unit'] = 'year';
     $this->setUpMembershipContributionPage();
     $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
     $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
@@ -1176,7 +1196,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
       'cvv2' => 123,
       'is_recur' => 1,
       'frequency_interval' => 1,
-      'frequency_unit' => 'month',
+      'frequency_unit' => $this->params['recur_frequency_unit'],
     );
 
     $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
@@ -1210,7 +1230,7 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
         'contact_id' => $contribution['contact_id'],
         'is_recur' => 1,
         'frequency_interval' => 1,
-        'frequency_unit' => 'month',
+        'frequency_unit' => $this->params['recur_frequency_unit'],
       )
     );
 
@@ -1274,9 +1294,10 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    * Set up membership contribution page.
    * @param bool $isSeparatePayment
    * @param bool $isRecur
+   * @param array $membershipTypeParams Parameters to pass to membershiptype.create API
    */
-  public function setUpMembershipContributionPage($isSeparatePayment = FALSE, $isRecur = FALSE) {
-    $this->setUpMembershipBlockPriceSet();
+  public function setUpMembershipContributionPage($isSeparatePayment = FALSE, $isRecur = FALSE, $membershipTypeParams = array()) {
+    $this->setUpMembershipBlockPriceSet($membershipTypeParams);
     $this->setupPaymentProcessor();
     $this->setUpContributionPage($isRecur);
 
@@ -1310,13 +1331,16 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
    *
    * This function ensures it exists & populates $this->_ids with it's data
    */
-  public function setUpMembershipBlockPriceSet() {
+  public function setUpMembershipBlockPriceSet($membershipTypeParams = array()) {
     $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
       'name' => 'default_membership_type_amount',
       'return' => 'id',
     ));
     if (empty($this->_ids['membership_type'])) {
-      $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
+      $membershipTypeParams = array_merge(array(
+        'minimum_fee' => 2,
+      ), $membershipTypeParams);
+      $this->_ids['membership_type'] = array($this->membershipTypeCreate($membershipTypeParams));
     }
     $priceField = $this->callAPISuccess('price_field', 'create', array(
       'price_set_id' => reset($this->_ids['price_set']),