From 17e615c72a74b9ace0539c2ac3883cdcaa207c98 Mon Sep 17 00:00:00 2001 From: Matthew Wire Date: Fri, 2 Feb 2018 15:21:43 +0000 Subject: [PATCH] Move membershipRenewalDate to own function --- .../CRM/Core/Payment/PayPalProIPNTest.php | 7 +++++-- tests/phpunit/CiviTest/CiviUnitTestCase.php | 19 +++++++++++++++++++ tests/phpunit/api/v3/ContributionPageTest.php | 17 ++--------------- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php b/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php index be171636e5..c1769393d7 100644 --- a/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php +++ b/tests/phpunit/CRM/Core/Payment/PayPalProIPNTest.php @@ -102,7 +102,8 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { * 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(); @@ -116,7 +117,9 @@ class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase { $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, diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 1580d8468c..f9ca0428ad 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -1239,6 +1239,25 @@ class CiviUnitTestCase extends PHPUnit_Extensions_Database_TestCase { $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 * diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 68c2153f62..8ad64c4d8b 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -920,21 +920,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $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']); -- 2.25.1