From 0d7e780ef0248d61e087a9fe1190af7b65991fd1 Mon Sep 17 00:00:00 2001 From: Tim Otten <totten@civicrm.org> Date: Mon, 28 Feb 2022 22:00:03 -0800 Subject: [PATCH] (NFC) Membership Tests - Update assertions to match behavior circa leap-day If you run the tests on Mar 2, 2022, then some of the generated memberships land on the leap-day in 2024. The actual behavior is have the end-date on the last day of the month (29 Feb 2024), but the test incorrectly asserts that it comes before (28 Feb 2024). --- tests/phpunit/CiviTest/CiviUnitTestCase.php | 8 ++++---- tests/phpunit/api/v3/ContributionPageTest.php | 6 ++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index b680aac7ad..82c7f0a909 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -744,19 +744,19 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { 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'); } diff --git a/tests/phpunit/api/v3/ContributionPageTest.php b/tests/phpunit/api/v3/ContributionPageTest.php index 2f755d531a..74ca273263 100644 --- a/tests/phpunit/api/v3/ContributionPageTest.php +++ b/tests/phpunit/api/v3/ContributionPageTest.php @@ -943,7 +943,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { //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']); @@ -1014,7 +1015,8 @@ class api_v3_ContributionPageTest extends CiviUnitTestCase { $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']); } /** -- 2.25.1