Skip to content

Commit

Permalink
Merge pull request #27198 from larssandergreen/Fix-membership-renewal…
Browse files Browse the repository at this point in the history
…-leap-year-bug

dev/core#4541 Fix membership leap year bug that causes test failure
  • Loading branch information
eileenmcnaughton authored Aug 29, 2023
2 parents bb49a5d + 6f1164c commit 7dfd3a0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 11 additions & 7 deletions CRM/Member/BAO/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,19 +521,19 @@ public static function getRenewalDatesForMembershipType($membershipId, $changeTo
$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],
(double) ($date[2] + 1),
(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
Expand All @@ -548,6 +548,10 @@ public static function getRenewalDatesForMembershipType($membershipId, $changeTo
break;

case 'month':
$date = explode('-', $logStartDate);
$year = $date[0];
$month = $date[1];
$day = $date[2] - 1;
$month = $month + ($numRenewTerms * $membershipTypeDetails['duration_interval']);
break;

Expand All @@ -561,7 +565,7 @@ public static function getRenewalDatesForMembershipType($membershipId, $changeTo
else {
$endDate = date('Y-m-d', mktime(0, 0, 0,
$month,
$day - 1,
$day,
$year
));
}
Expand Down
4 changes: 0 additions & 4 deletions tests/phpunit/CRM/Member/BAO/MembershipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,10 +466,6 @@ public function testDeleteRelatedMemberships(): void {
/**
* 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 {
Expand Down

0 comments on commit 7dfd3a0

Please sign in to comment.