Skip to content

Commit

Permalink
Merge pull request #2876 from compucorp/PCHR-3039-admin-change-absenc…
Browse files Browse the repository at this point in the history
…e-type

PCHR-3039: Allow admins to change Absence Type of open Leave Requests
  • Loading branch information
davialexandre authored Oct 9, 2018
2 parents e05631f + b469ab6 commit b081293
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@ class CRM_HRLeaveAndAbsences_Service_LeaveRequestRights {
*/
private $leaveManagerService;

/**
* @var array|null
* Stores the list of option values for the LeaveRequest status_id field.
*/
private static $leaveStatuses;

/**
* CRM_HRLeaveAndAbsences_Service_LeaveRequestRights constructor.
*
Expand Down Expand Up @@ -57,13 +51,7 @@ public function canChangeDatesFor($contactID, $statusID, $requestType) {
return TRUE;
}

$leaveRequestStatuses = self::getLeaveRequestStatuses();
$openStatuses = [
$leaveRequestStatuses['awaiting_approval'],
$leaveRequestStatuses['more_information_required']
];

$isOpenLeaveRequest = in_array($statusID, $openStatuses);
$isOpenLeaveRequest = in_array($statusID, LeaveRequest::getOpenStatuses());
$currentUserIsLeaveContact = $this->currentUserIsLeaveContact($contactID);

if ($currentUserIsLeaveContact && $isOpenLeaveRequest) {
Expand All @@ -88,9 +76,10 @@ public function canChangeDatesFor($contactID, $statusID, $requestType) {
* @return bool
*/
public function canChangeAbsenceTypeFor($contactID, $statusID) {
$leaveRequestStatuses = self::getLeaveRequestStatuses();
return $this->currentUserIsLeaveContact($contactID) &&
in_array($statusID, [$leaveRequestStatuses['awaiting_approval'], $leaveRequestStatuses['more_information_required']]);
$isOpenLeaveRequest = in_array($statusID, LeaveRequest::getOpenStatuses());
$isLeaveContactOrAdmin = $this->currentUserIsLeaveContact($contactID) || $this->currentUserIsAdmin();

return $isLeaveContactOrAdmin && $isOpenLeaveRequest;
}

/**
Expand Down Expand Up @@ -161,19 +150,6 @@ private function currentUserIsLeaveContact($contactID) {
return CRM_Core_Session::getLoggedInContactID() == $contactID;
}

/**
* Returns the array of the option values for the LeaveRequest status_id field.
*
* @return array
*/
private static function getLeaveRequestStatuses() {
if (is_null(self::$leaveStatuses)) {
self::$leaveStatuses = array_flip(LeaveRequest::buildOptions('status_id', 'validate'));
}

return self::$leaveStatuses;
}

/**
* Checks whether the current user can cancel the TOIL Request with
* past dates or not.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,24 +228,45 @@ public function testCanChangeAbsenceTypeForReturnsFalseWhenCurrentUserIsLeaveCon
}

/**
* @dataProvider leaveRequestStatusesDataProvider
* @dataProvider openLeaveRequestStatusesDataProvider
*/
public function testCanChangeAbsenceTypeForReturnsFalseWhenCurrentUserNotLeaveContactIrrespectiveOfStatusPassed($status) {
public function testCanChangeAbsenceTypeForReturnsTrueWhenCurrentUserIsAdminAndTheLeaveRequestIsOpen($status) {
$contactID = 2;

$this->assertFalse(
$this->getLeaveRequestRightsForLeaveManagerAsCurrentUser()->canChangeAbsenceTypeFor(
$this->assertTrue(
$this->getLeaveRequestRightsForAdminAsCurrentUser()->canChangeAbsenceTypeFor(
$contactID,
$status
)
);
}

/**
* @dataProvider closedLeaveRequestStatusesDataProvider
*/
public function testCanChangeAbsenceTypeForReturnsFalseWhenCurrentUserIsAdminAndTheLeaveRequestIsClosed($status) {
$contactID = 2;

$this->assertFalse(
$this->getLeaveRequestRightsForAdminAsCurrentUser()->canChangeAbsenceTypeFor(
$contactID,
$status
)
);
}

/**
* @dataProvider leaveRequestStatusesDataProvider
*/
public function testCanChangeAbsenceTypeForReturnsFalseWhenCurrentUserNotLeaveContactIrrespectiveOfStatusPassed($status) {
$contactID = 2;

$this->assertFalse(
$this->getLeaveRequestRightsForLeaveManagerAsCurrentUser()->canChangeAbsenceTypeFor(
$contactID,
$status
)
);

$this->assertFalse(
$this->getLeaveRightsService()->canChangeAbsenceTypeFor(
Expand Down

0 comments on commit b081293

Please sign in to comment.