Skip to content

Commit

Permalink
Move membershipRenewalDate to own function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Mar 16, 2018
1 parent 0e83e4d commit 2084ec3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
7 changes: 5 additions & 2 deletions tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function testIPNPaymentRecurSuccess() {
* 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();
Expand All @@ -116,7 +117,9 @@ public function testIPNPaymentMembershipRecurSuccess() {
$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,
Expand Down
19 changes: 19 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,25 @@ public function membershipStatusDelete($membershipStatusID) {
$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
*
Expand Down
17 changes: 2 additions & 15 deletions tests/phpunit/api/v3/ContributionPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,21 +920,8 @@ public function doSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPay
$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']);
Expand Down

0 comments on commit 2084ec3

Please sign in to comment.