Fix membership leap year bug
authorlarssandergreen <lars@wildsight.ca>
Tue, 29 Aug 2023 18:33:59 +0000 (12:33 -0600)
committerlarssandergreen <lars@wildsight.ca>
Tue, 29 Aug 2023 18:33:59 +0000 (12:33 -0600)
CRM/Member/BAO/MembershipType.php
tests/phpunit/CRM/Member/BAO/MembershipTest.php

index 690279bd42954bcc670301010585d228efa28e39..0d686deacb7ad035be2a662dbe79f3d313611cdd 100644 (file)
@@ -521,7 +521,12 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem
         $date = $membershipDetails->end_date;
       }
       $date = explode('-', $date);
-      // We have to add 1 day first in case it's the end of the month, then subtract afterwards
+      $year = $date[0];
+      $month = $date[1];
+      $day = $date[2];
+
+      // $logStartDate is used for the membership log only, except if the membership is monthly
+      // then we 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
       $logStartDate = date('Y-m-d', mktime(0, 0, 0,
         (double) $date[1],
@@ -529,11 +534,6 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem
         (double) $date[0]
       ));
 
-      $date = explode('-', $logStartDate);
-      $year = $date[0];
-      $month = $date[1];
-      $day = $date[2];
-
       switch ($membershipTypeDetails['duration_unit']) {
         case 'year':
           //need to check if the upsell is from rolling to fixed and adjust accordingly
@@ -548,6 +548,10 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem
           break;
 
         case 'month':
+          $date = explode('-', $logStartDate);
+          $year = $date[0];
+          $month = $date[1];
+          $day = $date[2] - 1;
           $month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']);
           break;
 
@@ -561,7 +565,7 @@ class CRM_Member_BAO_MembershipType extends CRM_Member_DAO_MembershipType implem
       else {
         $endDate = date('Y-m-d', mktime(0, 0, 0,
           $month,
-          $day - 1,
+          $day,
           $year
         ));
       }
index 325dc033e990bd1e04474e26a0368e94e343fbdf..67cfe4388f1c4dad58d31c0fc2b0ddc2846142e1 100644 (file)
@@ -466,10 +466,6 @@ class CRM_Member_BAO_MembershipTest extends CiviUnitTestCase {
   /**
    * Renew membership with change in membership type.
    *
-   * @fixme Note that this test fails when today is August 29 2019 (and maybe
-   *   other years?): Verify correct end date is calculated after membership
-   *   renewal Failed asserting that two strings are equal.
-   *   Expected-'2021-03-01' Actual+'2021-02-28'
    * @throws \CRM_Core_Exception
    */
   public function testRenewMembership(): void {