Skip to content
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
64 changes: 59 additions & 5 deletions Presenters/Admin/ManageSchedulesPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,15 @@ public function GetResources(): array
return $resources;
}

/**
* @param int $scheduleId
* @return Schedule
*/
public function GetSchedule($scheduleId)
{
return $this->scheduleRepository->LoadById($scheduleId);
}

/**
* @param int $scheduleId
* @return Schedule
Expand Down Expand Up @@ -625,10 +634,28 @@ public function ChangeAvailability()
$scheduleId = $this->page->GetScheduleId();
$timezone = ServiceLocator::GetServer()->GetUserSession()->Timezone;

if ($availableAllYear || empty($start) || empty($end)) {
// A blank date clears the availability, as the "all year" checkbox does.
$clearAvailability = $availableAllYear || $start === '' || $end === '';

if ($clearAvailability) {
$schedule = $this->manageSchedulesService->DeleteAvailability($scheduleId);
} else {
$schedule = $this->manageSchedulesService->UpdateAvailability($scheduleId, Date::Parse($start, $timezone), Date::Parse($end, $timezone));
$startDate = self::ParseSubmittedDate(value: $start, timezone: $timezone);
$endDate = self::ParseSubmittedDate(value: $end, timezone: $timezone);

if ($startDate === null || $endDate === null) {
// A date that is missing or was sent as a list must not clear
// the availability. The form replaces the displayed
// availability with the response, so show what is stored.
Log::Debug('Not changing schedule availability, a date is missing or malformed. Schedule %s', $scheduleId);
$schedule = $this->manageSchedulesService->GetSchedule(scheduleId: $scheduleId);
} else {
$schedule = $this->manageSchedulesService->UpdateAvailability(
scheduleId: $scheduleId,
start: $startDate,
end: $endDate,
);
}
}

$this->page->DisplayAvailability($schedule, $timezone);
Expand Down Expand Up @@ -681,15 +708,42 @@ public function AddLayoutSlot()
public function UpdateLayoutSlot()
{
$timezone = ServiceLocator::GetServer()->GetUserSession()->Timezone;
$start = Date::Parse($this->page->GetSlotStart(), $timezone);
$end = Date::Parse($this->page->GetSlotEnd(), $timezone);
$originalStart = Date::Parse($this->page->GetSlotId(), $timezone);
$start = self::ParseSubmittedDate(value: $this->page->GetSlotStart(), timezone: $timezone);
$end = self::ParseSubmittedDate(value: $this->page->GetSlotEnd(), timezone: $timezone);
$originalStart = self::ParseSubmittedDate(value: $this->page->GetSlotId(), timezone: $timezone);
$scheduleId = $this->page->GetScheduleId();

if ($start === null || $end === null || $originalStart === null) {
// The update deletes the original slot before adding its
// replacement, so it must not start without all three dates.
Log::Debug('Not updating custom layout slot, a date is missing or malformed. Schedule %s', $scheduleId);

return;
}

Log::Debug('Updating custom layout slot. Start %s, End %s, Schedule %s', $start, $end, $scheduleId);
$this->manageSchedulesService->UpdateCustomLayoutPeriod($scheduleId, $start, $end, $originalStart);
}

/**
* Parses a submitted date field.
*
* @param mixed $value the submitted field
* @return Date|null null when the field holds no date: it is missing,
* sent as a list (name[]=x), or a value Date::Parse()
* reads as no date, such as a blank string or '0'
*/
private static function ParseSubmittedDate(mixed $value, ?string $timezone): ?Date
{
if (!is_string($value)) {
return null;
}

$date = Date::Parse(dateString: $value, timezone: $timezone);

return $date instanceof NullDate ? null : $date;
}

public function DeleteLayoutSlot()
{
$timezone = ServiceLocator::GetServer()->GetUserSession()->Timezone;
Expand Down
154 changes: 154 additions & 0 deletions tests/Presenters/Admin/ManageSchedulesPresenterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,158 @@ public function testDeletesSchedule()

$presenter->Delete();
}

public function testChangeAvailabilitySetsAvailabilityWindow(): void
{
$schedule = new FakeSchedule();
$schedule->SetAvailableAllYear();

$this->page->method('GetScheduleId')->willReturn(1);
$this->page->method('GetAvailableAllYear')->willReturn(false);
$this->page->method('GetAvailabilityBegin')->willReturn('2026-03-01');
$this->page->method('GetAvailabilityEnd')->willReturn('2026-04-01');

$this->scheduleRepo->method('LoadById')->willReturn($schedule);
$this->scheduleRepo->expects($this->once())
->method('Update')
->with($this->identicalTo($schedule));

$presenter = new ManageSchedulesPresenter($this->page, $this->service, $this->groupRepo);
$presenter->ChangeAvailability();

$this->assertTrue($schedule->HasAvailability());
}

/**
* @return array<string, array{bool, mixed, mixed}>
*/
public static function clearedAvailabilityProvider(): array
{
return [
'all year checked' => [true, '2026-03-01', '2026-04-01'],
'all year checked without dates' => [true, null, null],
'blank start' => [false, '', '2026-04-01'],
'blank end' => [false, '2026-03-01', ''],
'blank start with malformed end' => [false, '', ['2026-04-01']],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('clearedAvailabilityProvider')]
public function testChangeAvailabilityClearsAvailabilityWindow(bool $allYear, mixed $start, mixed $end): void
{
$schedule = new FakeSchedule();

$this->page->method('GetScheduleId')->willReturn(1);
$this->page->method('GetAvailableAllYear')->willReturn($allYear);
$this->page->method('GetAvailabilityBegin')->willReturn($start);
$this->page->method('GetAvailabilityEnd')->willReturn($end);

$this->scheduleRepo->method('LoadById')->willReturn($schedule);
$this->scheduleRepo->expects($this->once())
->method('Update')
->with($this->identicalTo($schedule));

$presenter = new ManageSchedulesPresenter($this->page, $this->service, $this->groupRepo);
$presenter->ChangeAvailability();

$this->assertFalse($schedule->HasAvailability());
}

/**
* @return array<string, array{mixed, mixed}>
*/
public static function malformedAvailabilityProvider(): array
{
return [
// Sent as name[]=x.
'start as a list' => [['2026-03-01'], '2026-04-01'],
'end as a list' => ['2026-03-01', ['2026-04-01']],
'missing start' => [null, '2026-04-01'],
'missing end' => ['2026-03-01', null],
// Date::Parse() reads '0' as no date.
'zero start' => ['0', '2026-04-01'],
'zero end' => ['2026-03-01', '0'],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('malformedAvailabilityProvider')]
public function testChangeAvailabilityKeepsAvailabilityWindowWhenDateIsMalformed(mixed $start, mixed $end): void
{
$schedule = new FakeSchedule();

$this->page->method('GetScheduleId')->willReturn(1);
$this->page->method('GetAvailableAllYear')->willReturn(false);
$this->page->method('GetAvailabilityBegin')->willReturn($start);
$this->page->method('GetAvailabilityEnd')->willReturn($end);

$this->scheduleRepo->method('LoadById')->willReturn($schedule);
$this->scheduleRepo->expects($this->never())->method('Update');

// The form replaces the displayed availability with the response, so
// the stored availability must still be shown.
$this->page->expects($this->once())
->method('DisplayAvailability')
->with($this->identicalTo($schedule));
Comment thread
JohnVillalovos marked this conversation as resolved.
Resolved

$presenter = new ManageSchedulesPresenter($this->page, $this->service, $this->groupRepo);
$presenter->ChangeAvailability();

$this->assertTrue($schedule->HasAvailability());
}

public function testUpdateLayoutSlotReplacesSlot(): void
{
$this->page->method('GetScheduleId')->willReturn(1);
$this->page->method('GetSlotStart')->willReturn('2026-03-01 09:00');
$this->page->method('GetSlotEnd')->willReturn('2026-03-01 10:00');
$this->page->method('GetSlotId')->willReturn('2026-03-01 08:00');

$this->scheduleRepo->expects($this->once())->method('DeleteCustomLayoutPeriod');
$this->scheduleRepo->expects($this->once())->method('AddCustomLayoutPeriod');

$presenter = new ManageSchedulesPresenter($this->page, $this->service, $this->groupRepo);
$presenter->UpdateLayoutSlot();
}

/**
* @return array<string, array{mixed, mixed, mixed}>
*/
public static function malformedLayoutSlotProvider(): array
{
$start = '2026-03-01 09:00';
$end = '2026-03-01 10:00';
$slotId = '2026-03-01 08:00';

return [
// Sent as name[]=x.
'start as a list' => [[$start], $end, $slotId],
'end as a list' => [$start, [$end], $slotId],
'slot id as a list' => [$start, $end, [$slotId]],
'missing start' => [null, $end, $slotId],
'missing end' => [$start, null, $slotId],
'missing slot id' => [$start, $end, null],
'blank start' => ['', $end, $slotId],
'blank end' => [$start, '', $slotId],
'blank slot id' => [$start, $end, ''],
// Date::Parse() reads '0' as no date.
'zero start' => ['0', $end, $slotId],
'zero end' => [$start, '0', $slotId],
'zero slot id' => [$start, $end, '0'],
];
}

#[\PHPUnit\Framework\Attributes\DataProvider('malformedLayoutSlotProvider')]
public function testUpdateLayoutSlotKeepsSlotWhenDateIsMalformed(mixed $start, mixed $end, mixed $slotId): void
{
$this->page->method('GetScheduleId')->willReturn(1);
$this->page->method('GetSlotStart')->willReturn($start);
$this->page->method('GetSlotEnd')->willReturn($end);
$this->page->method('GetSlotId')->willReturn($slotId);

$this->scheduleRepo->expects($this->never())->method('DeleteCustomLayoutPeriod');
$this->scheduleRepo->expects($this->never())->method('AddCustomLayoutPeriod');

$presenter = new ManageSchedulesPresenter($this->page, $this->service, $this->groupRepo);
$presenter->UpdateLayoutSlot();
}
}