Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Test framework] re-re-fix test and add test for test #18013

Merged
merged 1 commit into from
Jul 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions tests/phpunit/CRM/Contribute/BAO/ContributionRecurTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,14 +372,14 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {
$this->validateAllCounts($membershipId1, 4);
$this->validateAllCounts($membershipId2, 4);

$expectedMonth = date('m') + 4;
$expectedDate = $this->getYearAndMonthFromOffset(4);
// check membership end date.
foreach ([$membershipId1, $membershipId2] as $mId) {
$endDate = $this->callAPISuccessGetValue('Membership', [
'id' => $mId,
'return' => 'end_date',
]);
$this->assertEquals(date('Y-' . $expectedMonth . '-27'), $endDate, ts('End date incorrect.'));
$this->assertEquals("{$expectedDate['year']}-{$expectedDate['month']}-27", $endDate, ts('End date incorrect.'));
}

// At this moment Contact 2 is deceased, but we wait until payment is recorded in civi before marking the contact deceased.
Expand Down Expand Up @@ -446,7 +446,8 @@ public function testAutoRenewalWhenOneMemberIsDeceased() {
'id' => $membershipId1,
'return' => 'end_date',
]);
$this->assertEquals(date('Y-m', strtotime('+6 months')) . '-27', $endDate, ts('End date incorrect.'));
$expectedDate = $this->getYearAndMonthFromOffset(6);
$this->assertEquals("{$expectedDate['year']}-{$expectedDate['month']}-27", $endDate, ts('End date incorrect.'));
// check line item and membership payment count.
$this->validateAllCounts($membershipId1, 6);
$this->validateAllCounts($membershipId2, 4);
Expand Down Expand Up @@ -479,4 +480,80 @@ public function validateAllCounts($membershipId, $count) {
$this->callAPISuccessGetCount('MembershipPayment', $memPayParams, $count);
}

/**
* Given a number of months offset, get the year and month.
* Note the way php arithmetic works, using strtotime('+x months') doesn't
* work because it will roll over the day accounting for different number
* of days in the month, but we want the same day of the month, x months
* from now.
* e.g. July 31 + 4 months will return Dec 1 if using php functions, but
* we want Nov 31.
*
* @param int $offset
* @param int $year Optional input year to start
* @param int $month Optional input month to start
*
* @return array
* ['year' => int, 'month' => int]
*/
private function getYearAndMonthFromOffset(int $offset, int $year = NULL, int $month = NULL) {
$dateInfo = [
'year' => $year ?? date('Y'),
'month' => ($month ?? date('m')) + $offset,
];
if ($dateInfo['month'] > 12) {
$dateInfo['year']++;
$dateInfo['month'] -= 12;
}
if ($dateInfo['month'] < 10) {
$dateInfo['month'] = "0{$dateInfo['month']}";
}

return $dateInfo;
}

/**
* Test getYearAndMonthFromOffset
* @dataProvider yearMonthProvider
*
* @param array $input
* @param array $expected
*/
public function testGetYearAndMonthFromOffset($input, $expected) {
$this->assertEquals($expected, $this->getYearAndMonthFromOffset($input[0], $input[1], $input[2]));
}

/**
* data provider for testGetYearAndMonthFromOffset
*/
public function yearMonthProvider() {
return [
// input = offset, year, current month
['input' => [4, 2020, 1], 'output' => ['year' => '2020', 'month' => '05']],
['input' => [6, 2020, 1], 'output' => ['year' => '2020', 'month' => '07']],
['input' => [4, 2020, 2], 'output' => ['year' => '2020', 'month' => '06']],
['input' => [6, 2020, 2], 'output' => ['year' => '2020', 'month' => '08']],
['input' => [4, 2020, 3], 'output' => ['year' => '2020', 'month' => '07']],
['input' => [6, 2020, 3], 'output' => ['year' => '2020', 'month' => '09']],
['input' => [4, 2020, 4], 'output' => ['year' => '2020', 'month' => '08']],
['input' => [6, 2020, 4], 'output' => ['year' => '2020', 'month' => '10']],
['input' => [4, 2020, 5], 'output' => ['year' => '2020', 'month' => '09']],
['input' => [6, 2020, 5], 'output' => ['year' => '2020', 'month' => '11']],
['input' => [4, 2020, 6], 'output' => ['year' => '2020', 'month' => '10']],
['input' => [6, 2020, 6], 'output' => ['year' => '2020', 'month' => '12']],
['input' => [4, 2020, 7], 'output' => ['year' => '2020', 'month' => '11']],
['input' => [6, 2020, 7], 'output' => ['year' => '2021', 'month' => '01']],
['input' => [4, 2020, 8], 'output' => ['year' => '2020', 'month' => '12']],
['input' => [6, 2020, 8], 'output' => ['year' => '2021', 'month' => '02']],
['input' => [4, 2020, 9], 'output' => ['year' => '2021', 'month' => '01']],
['input' => [6, 2020, 9], 'output' => ['year' => '2021', 'month' => '03']],
['input' => [4, 2020, 10], 'output' => ['year' => '2021', 'month' => '02']],
['input' => [6, 2020, 10], 'output' => ['year' => '2021', 'month' => '04']],
['input' => [4, 2020, 11], 'output' => ['year' => '2021', 'month' => '03']],
['input' => [6, 2020, 11], 'output' => ['year' => '2021', 'month' => '05']],
['input' => [4, 2020, 12], 'output' => ['year' => '2021', 'month' => '04']],
['input' => [6, 2020, 12], 'output' => ['year' => '2021', 'month' => '06']],
];
}

}