Skip to content

Commit

Permalink
Merge pull request #2823 from compucorp/PCHR-4110-fix-calendar-feeds-…
Browse files Browse the repository at this point in the history
…lr-inclusion

PCHR-4110: Fix calendar feeds leave requests inclusion logic
  • Loading branch information
igorpavlov authored Aug 15, 2018
2 parents 3f9b648 + 11ecf68 commit bdd8389
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 38 deletions.
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

0 comments on commit bdd8389

Please sign in to comment.