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

PCHR-4110: Fix calendar feeds leave requests inclusion logic #2823

Merged
merged 1 commit into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,8 @@ private function getLeaveRequests() {
$params = [
'type_id' => ['IN' => $leaveTypes],
'status_id' => ['IN' => ['approved', 'admin_approved']],
'from_date' => ['>=' => $this->startDate->format('Y-m-d H:i:s')],
'to_date' => ['<=' => $this->endDate->format('Y-m-d H:i:s')],
'from_date' => ['<=' => $this->endDate->format('Y-m-d H:i:s')],
'to_date' => ['>=' => $this->startDate->format('Y-m-d H:i:s')],
'request_type' => ['!=' => LeaveRequest::REQUEST_TYPE_TOIL],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,59 +391,73 @@ public function testGetWillNotReturnToilRequestsData() {

public function testGetWillNotReturnDataForRequestsOutsideTheDateRange() {
$absenceType = AbsenceTypeFabricator::fabricate();
$contact1 = ContactFabricator::fabricate(['first_name' => 'Contact1', 'last_name' => 'LastContact1']);
$contact = ContactFabricator::fabricate(['first_name' => 'ContactName', 'last_name' => 'ContactLastName']);

HRJobContractFabricator::fabricate(
['contact_id' => $contact1['id']],
['contact_id' => $contact['id']],
['period_start_date' => CRM_Utils_Date::processDate('yesterday')]
);

//Leave is outside date range for feed data
$params[1] = [
'contact_id' => $contact1['id'],
'first_name' => $contact1['first_name'],
'last_name' => $contact1['last_name'],
'type_id' => $absenceType->id,
'from_date' => CRM_Utils_Date::processDate('yesterday'),
'to_date' => CRM_Utils_Date::processDate('yesterday'),
// These are test cases for different leave request dates.
// As a general rule, leave requests should only be included if at least
// one day appears in the period of [today - 3 months from today].
$testCases = [
[
'from_date' => CRM_Utils_Date::processDate('-3 days'),
'to_date' => CRM_Utils_Date::processDate('-2 days'),
'shouldBeIncluded' => false
],
[
'from_date' => CRM_Utils_Date::processDate('-1 days'),
'to_date' => CRM_Utils_Date::processDate('today'),
'shouldBeIncluded' => true
],
[
'from_date' => CRM_Utils_Date::processDate('+1 month +10 days'),
'to_date' => CRM_Utils_Date::processDate('+1 month +20 days'),
'shouldBeIncluded' => true
],
[
'from_date' => CRM_Utils_Date::processDate('+3 months'),
'to_date' => CRM_Utils_Date::processDate('+3 months +1 day'),
'shouldBeIncluded' => true
],
[
'from_date' => CRM_Utils_Date::processDate('+3 months +2 days'),
'to_date' => CRM_Utils_Date::processDate('+3 months +3 days'),
'shouldBeIncluded' => false
]
];

$params[2] = [
'contact_id' => $contact1['id'],
'first_name' => $contact1['first_name'],
'last_name' => $contact1['last_name'],
'type_id' => $absenceType->id,
'from_date' => CRM_Utils_Date::processDate('today'),
'to_date' => CRM_Utils_Date::processDate('+2 days'),
];
$expectedFeedData = [];

//Leave is outside date range for feed data
$params[2] = [
'contact_id' => $contact1['id'],
'first_name' => $contact1['first_name'],
'last_name' => $contact1['last_name'],
'type_id' => $absenceType->id,
'from_date' => CRM_Utils_Date::processDate('+4 months'),
'to_date' => CRM_Utils_Date::processDate('+4 months'),
];
foreach ($testCases as $testCase) {
$params = [
'contact_id' => $contact['id'],
'first_name' => $contact['first_name'],
'last_name' => $contact['last_name'],
'type_id' => $absenceType->id,
'from_date' => $testCase['from_date'],
'to_date' => $testCase['to_date'],
];

foreach ($params as &$param) {
$leaveRequest = LeaveRequestFabricator::fabricateWithoutValidation($param);
$param['id'] = $leaveRequest->id;
$leaveRequest = LeaveRequestFabricator::fabricateWithoutValidation($params);
$params['id'] = $leaveRequest->id;

$testCase['shouldBeIncluded'] && array_push($expectedFeedData, $params);
}

//feed config is for all contacts in any department/location
$feedConfig1 = LeaveCalendarFeedConfigFabricator::fabricate([
'title' => 'Feed 1',
// A sample feed config which is visible to everyone
$feedConfig = LeaveCalendarFeedConfigFabricator::fabricate([
'title' => 'Feed',
'composed_of' => [
'leave_type' => [$absenceType->id],
]
]);

$leaveFeedData = new LeaveRequestCalendarFeedData($feedConfig1->hash);
$leaveFeedData = new LeaveRequestCalendarFeedData($feedConfig->hash);

unset($params[1], $params[2]);
$this->assertEquals($this->getExpectedLeaveDataResult($params), $leaveFeedData->get());
$this->assertEquals($this->getExpectedLeaveDataResult($expectedFeedData), $leaveFeedData->get());
}

private function getExpectedLeaveDataResult($leaveData) {
Expand Down