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');
}
//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']);
$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']);
}
/**