From 329629009434c8d8d5b21b747af2210e181a232a Mon Sep 17 00:00:00 2001 From: Anna Larch Date: Mon, 12 Aug 2024 16:50:08 +0200 Subject: [PATCH] fix(userstatus): trigger absence start job even for absences starting in the past Signed-off-by: Anna Larch --- apps/dav/lib/Service/AbsenceService.php | 43 +++++++++++-------- .../tests/unit/Service/AbsenceServiceTest.php | 34 ++++++++++----- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/apps/dav/lib/Service/AbsenceService.php b/apps/dav/lib/Service/AbsenceService.php index 7cbc0386d4387..6e4161023dc51 100644 --- a/apps/dav/lib/Service/AbsenceService.php +++ b/apps/dav/lib/Service/AbsenceService.php @@ -81,27 +81,32 @@ public function createOrUpdateAbsence( } $now = $this->timeFactory->getTime(); - if ($eventData->getStartDate() > $now) { - $this->jobList->scheduleAfter( - OutOfOfficeEventDispatcherJob::class, - $eventData->getStartDate(), - [ - 'id' => $absence->getId(), - 'event' => OutOfOfficeEventDispatcherJob::EVENT_START, - ], - ); - } - if ($eventData->getEndDate() > $now) { - $this->jobList->scheduleAfter( - OutOfOfficeEventDispatcherJob::class, - $eventData->getEndDate(), - [ - 'id' => $absence->getId(), - 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, - ], - ); + if($eventData->getEndDate() < $now) { + // absence is in the past + return $absence; } + // If the absence is for today the timestamp will be smaller than $now + // so run the job now if that is the case + $runJobAt = ($eventData->getStartDate() < $now) ? $now : $eventData->getStartDate(); + $this->jobList->scheduleAfter( + OutOfOfficeEventDispatcherJob::class, + $runJobAt, + [ + 'id' => $absence->getId(), + 'event' => OutOfOfficeEventDispatcherJob::EVENT_START, + ], + ); + + $this->jobList->scheduleAfter( + OutOfOfficeEventDispatcherJob::class, + $eventData->getEndDate(), + [ + 'id' => $absence->getId(), + 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, + ], + ); + return $absence; } diff --git a/apps/dav/tests/unit/Service/AbsenceServiceTest.php b/apps/dav/tests/unit/Service/AbsenceServiceTest.php index 1bc5f53f18ccd..a7f8c8490de4b 100644 --- a/apps/dav/tests/unit/Service/AbsenceServiceTest.php +++ b/apps/dav/tests/unit/Service/AbsenceServiceTest.php @@ -252,14 +252,21 @@ public function testCreateAbsenceSchedulesOnlyEndJob() { ->method('getUserTimezone') ->with('user') ->willReturn($tz->getName()); + $time = (new DateTimeImmutable('2023-01-07', $tz))->getTimestamp(); $this->timeFactory->expects(self::once()) ->method('getTime') - ->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp()); - $this->jobList->expects(self::once()) + ->willReturn($time); + $this->jobList->expects(self::exactly(2)) ->method('scheduleAfter') - ->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [ - 'id' => '1', - 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, + ->willReturnMap([ + [OutOfOfficeEventDispatcherJob::class, $time, [ + 'id' => '1', + 'event' => OutOfOfficeEventDispatcherJob::EVENT_START, + ]], + [OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [ + 'id' => '1', + 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, + ]], ]); $this->absenceService->createOrUpdateAbsence( @@ -391,14 +398,21 @@ public function testUpdateSchedulesOnlyEndJob() { ->method('getUserTimezone') ->with('user') ->willReturn($tz->getName()); + $time = (new DateTimeImmutable('2023-01-07', $tz))->getTimestamp(); $this->timeFactory->expects(self::once()) ->method('getTime') - ->willReturn((new DateTimeImmutable('2023-01-07', $tz))->getTimestamp()); - $this->jobList->expects(self::once()) + ->willReturn($time); + $this->jobList->expects(self::exactly(2)) ->method('scheduleAfter') - ->with(OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 23 * 3600 + 59 * 60, [ - 'id' => '1', - 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, + ->willReturnMap([ + [OutOfOfficeEventDispatcherJob::class, $time, [ + 'id' => '1', + 'event' => OutOfOfficeEventDispatcherJob::EVENT_START, + ]], + [OutOfOfficeEventDispatcherJob::class, $endDate->getTimestamp() + 3600 * 23 + 59 * 60, [ + 'id' => '1', + 'event' => OutOfOfficeEventDispatcherJob::EVENT_END, + ]], ]); $this->absenceService->createOrUpdateAbsence(