Move membershipRenewalDate to own function
authorMatthew Wire <devel@mrwire.co.uk>
Fri, 2 Feb 2018 15:21:43 +0000 (15:21 +0000)
committerMatthew Wire <devel@mrwire.co.uk>
Wed, 21 Mar 2018 12:39:29 +0000 (12:39 +0000)
tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContributionPageTest.php

index be171636e559d2f867867135a1a9b2ef58690e1b..c1769393d71439a0ac21c8659caca2b8b55d2d96 100644 (file)
@@ -102,7 +102,8 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase {
    * Test IPN response updates contribution_recur & contribution for first & second contribution.
    */
   public function testIPNPaymentMembershipRecurSuccess() {
-    $this->setupMembershipRecurringPaymentProcessorTransaction(array('duration_unit' => 'year', 'frequency_unit' => 'year'));
+    $durationUnit = 'year';
+    $this->setupMembershipRecurringPaymentProcessorTransaction(array('duration_unit' => $durationUnit, 'frequency_unit' => $durationUnit));
     $this->callAPISuccessGetSingle('membership_payment', array());
     $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurTransaction());
     $paypalIPN->main();
@@ -116,7 +117,9 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase {
     $this->assertEquals(5, $contributionRecur['contribution_status_id']);
     $paypalIPN = new CRM_Core_Payment_PaypalProIPN($this->getPaypalProRecurSubsequentTransaction());
     $paypalIPN->main();
-    $this->assertEquals(strtotime('+ 1 year', strtotime($membershipEndDate)), strtotime($this->callAPISuccessGetValue('membership', array('return' => 'end_date'))));
+
+    $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membershipEndDate);
+    $this->assertEquals($renewedMembershipEndDate, $this->callAPISuccessGetValue('membership', array('return' => 'end_date')));
     $contribution = $this->callAPISuccess('contribution', 'get', array(
         'contribution_recur_id' => $this->_contributionRecurID,
         'sequential' => 1,
index 1580d8468cc0bf67418d52c008b683ffd23fc5b0..f9ca0428ada209e80daaad961d8acaea0a3f0a73 100644 (file)
@@ -1239,6 +1239,25 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase {
     $result = $this->callAPISuccess('MembershipStatus', 'Delete', array('id' => $membershipStatusID));
   }
 
+  public function membershipRenewalDate($durationUnit, $membershipEndDate) {
+    // We only have an end_date if frequency units match, otherwise membership won't be autorenewed and dates won't be calculated.
+    $renewedMembershipEndDate = new DateTime($membershipEndDate);
+    switch ($durationUnit) {
+      case 'year':
+        $renewedMembershipEndDate->add(new DateInterval('P1Y'));
+        break;
+
+      case 'month':
+        // We have to add 1 day first in case it's the end of the month, then subtract afterwards
+        // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28
+        $renewedMembershipEndDate->add(new DateInterval('P1D'));
+        $renewedMembershipEndDate->add(new DateInterval('P1M'));
+        $renewedMembershipEndDate->sub(new DateInterval('P1D'));
+        break;
+    }
+    return $renewedMembershipEndDate->format('Y-m-d');
+  }
+
   /**
    * @param array $params
    *
index 68c2153f6286d6b3fd1adc83de4e7c4e07586256..8ad64c4d8ba5547faf90c842f7945f926d31dcd4 100644 (file)
@@ -920,21 +920,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
     if ($durationUnit) {
       // We only have an end_date if frequency units match, otherwise membership won't be autorenewed and dates won't be calculated.
-      $renewedMembershipEndDate = new DateTime($membership['end_date']);
-      switch ($durationUnit) {
-        case 'year':
-          $renewedMembershipEndDate->add(new DateInterval('P1Y'));
-          break;
-
-        case 'month':
-          // We have to add 1 day first in case it's the end of the month, then subtract afterwards
-          // eg. 2018-02-28 should renew to 2018-03-31, if we just added 1 month we'd get 2018-03-28
-          $renewedMembershipEndDate->add(new DateInterval('P1D'));
-          $renewedMembershipEndDate->add(new DateInterval('P1M'));
-          $renewedMembershipEndDate->sub(new DateInterval('P1D'));
-          break;
-      }
-      $this->assertEquals($renewedMembershipEndDate->format('Y-m-d'), $renewedMembership['end_date']);
+      $renewedMembershipEndDate = $this->membershipRenewalDate($durationUnit, $membership['end_date']);
+      $this->assertEquals($renewedMembershipEndDate, $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']);