From 11ecf680a103506a698f40b1a484d8d9d6e9c2b1 Mon Sep 17 00:00:00 2001 From: Igor Pavlov Date: Tue, 14 Aug 2018 16:46:27 +0100 Subject: [PATCH] PCHR-4110: Fix calendar feeds leave requests inclusion logic --- .../Service/LeaveRequestCalendarFeedData.php | 4 +- .../LeaveRequestCalendarFeedDataTest.php | 86 +++++++++++-------- 2 files changed, 52 insertions(+), 38 deletions(-) diff --git a/uk.co.compucorp.civicrm.hrleaveandabsences/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedData.php b/uk.co.compucorp.civicrm.hrleaveandabsences/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedData.php index ec0d3cc3382..95babf79675 100644 --- a/uk.co.compucorp.civicrm.hrleaveandabsences/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedData.php +++ b/uk.co.compucorp.civicrm.hrleaveandabsences/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedData.php @@ -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], ]; diff --git a/uk.co.compucorp.civicrm.hrleaveandabsences/tests/phpunit/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedDataTest.php b/uk.co.compucorp.civicrm.hrleaveandabsences/tests/phpunit/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedDataTest.php index e9571cef77c..330a377bddb 100644 --- a/uk.co.compucorp.civicrm.hrleaveandabsences/tests/phpunit/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedDataTest.php +++ b/uk.co.compucorp.civicrm.hrleaveandabsences/tests/phpunit/CRM/HRLeaveAndAbsences/Service/LeaveRequestCalendarFeedDataTest.php @@ -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) {