(NFC) Membership Tests - Update assertions to match behavior circa leap-day
authorTim Otten <totten@civicrm.org>
Tue, 1 Mar 2022 06:00:03 +0000 (22:00 -0800)
committerTim Otten <totten@civicrm.org>
Wed, 2 Mar 2022 04:24:10 +0000 (20:24 -0800)
If you run the tests on Mar 2, 2022, then some of the generated memberships
land on the leap-day in 2024.  The actual behavior is have the end-date on the
last day of the month (29 Feb 2024), but the test incorrectly asserts that
it comes before (28 Feb 2024).

tests/phpunit/CiviTest/CiviUnitTestCase.php
tests/phpunit/api/v3/ContributionPageTest.php

index b680aac7adebffce831df3d2c29c6c255caa9706..82c7f0a9095775e28f74567b457cc3b1e557d1e8 100644 (file)
@@ -744,19 +744,19 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase {
   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);
+    // 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'));
     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;
     }
+    $renewedMembershipEndDate->sub(new DateInterval('P1D'));
     return $renewedMembershipEndDate->format('Y-m-d');
   }
 
index 2f755d531a8799d8f2dc8118b2dd96f618fdbd94..74ca273263ca04a6cea0257f5b3041c97e99fcc8 100644 (file)
@@ -943,7 +943,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     //renew it with processor setting completed - should extend membership
     $renewContribution = $this->submitSecondContribution((int) $contribution['contact_id'], $submitParams, (int) $contribution['id']);
     $renewedMembership = $this->validateContributionWithContributionAndMembershipLineItems($renewContribution['id'], $preExistingMembershipID);
-    $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
+    $expectedEndDate = $this->membershipRenewalDate('year', $membership['end_date']);
+    $this->assertEquals($expectedEndDate, $renewedMembership['end_date']);
     $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', ['id' => $contribution['contribution_recur_id']]);
     $this->assertEquals($processor['payment_instrument_id'], $recurringContribution['payment_instrument_id']);
     $this->assertEquals(5, $recurringContribution['contribution_status_id']);
@@ -1014,7 +1015,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase {
     $this->validateTripleLines($renewContribution['id'], $preExistingMembershipID);
 
     $renewedMembership = $this->callAPISuccessGetSingle('membership', ['id' => $preExistingMembershipID + 1]);
-    $this->assertEquals(date('Y-m-d', strtotime('+ 1 ' . $this->params['recur_frequency_unit'], strtotime($membership['end_date']))), $renewedMembership['end_date']);
+    $expectEndDate = $this->membershipRenewalDate($this->params['recur_frequency_unit'], $membership['end_date']);
+    $this->assertEquals($expectEndDate, $renewedMembership['end_date']);
   }
 
   /**