Skip to content

Commit cd07df5

Browse files
committed
fix(appointments): validate all config values
Signed-off-by: Kumar Saurabh <kumarsaurabh27d@gmail.com>
1 parent a7f7631 commit cd07df5

2 files changed

Lines changed: 61 additions & 5 deletions

File tree

lib/Controller/AppointmentConfigController.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,34 @@ private function validateAvailability(array $availability): void {
103103
/**
104104
* @throws InvalidArgumentException
105105
*/
106-
private function validateDurations(int $length, int $increment): void {
107-
if ($length <= 0 || $increment <= 0) {
108-
throw new InvalidArgumentException('Length and increment must be greater than zero');
106+
private function validateConfigValues(
107+
int $length,
108+
int $increment,
109+
int $preparationDuration,
110+
int $followupDuration,
111+
int $timeBeforeNextSlot,
112+
?int $dailyMax,
113+
?int $start,
114+
?int $end,
115+
?int $futureLimit,
116+
): void {
117+
if ($length <= 0) {
118+
throw new InvalidArgumentException('Length must be greater than zero');
119+
}
120+
if ($increment < 5 * 60) {
121+
throw new InvalidArgumentException('Increment must be at least 5 minutes');
122+
}
123+
if ($preparationDuration < 0 || $followupDuration < 0 || $timeBeforeNextSlot < 0) {
124+
throw new InvalidArgumentException('Durations must not be negative');
125+
}
126+
if ($dailyMax !== null && $dailyMax <= 0) {
127+
throw new InvalidArgumentException('Daily maximum must be greater than zero');
128+
}
129+
if ($futureLimit !== null && $futureLimit <= 0) {
130+
throw new InvalidArgumentException('Future limit must be greater than zero');
131+
}
132+
if ($start !== null && $end !== null && $start >= $end) {
133+
throw new InvalidArgumentException('Start must be before end');
109134
}
110135
}
111136

@@ -154,7 +179,7 @@ public function create(
154179
return JsonResponse::fail();
155180
}
156181
try {
157-
$this->validateDurations($length, $increment);
182+
$this->validateConfigValues($length, $increment, $preparationDuration, $followupDuration, $timeBeforeNextSlot, $dailyMax, $start, $end, $futureLimit);
158183
$this->validateAvailability($availability);
159184
} catch (InvalidArgumentException $e) {
160185
return JsonResponse::fail($e->getMessage(), Http::STATUS_UNPROCESSABLE_ENTITY);
@@ -233,7 +258,7 @@ public function update(
233258
return JsonResponse::fail(null, Http::STATUS_NOT_FOUND);
234259
}
235260
try {
236-
$this->validateDurations($length, $increment);
261+
$this->validateConfigValues($length, $increment, $preparationDuration, $followupDuration, $timeBeforeNextSlot, $dailyMax, $start, $end, $futureLimit);
237262
$this->validateAvailability($availability);
238263
} catch (InvalidArgumentException $e) {
239264
return JsonResponse::fail($e->getMessage(), Http::STATUS_UNPROCESSABLE_ENTITY);

tests/php/unit/Controller/AppointmentConfigControllerTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,37 @@ public function testCreateWithInvalidIncrement(): void {
163163
self::assertEquals(422, $response->getStatus());
164164
}
165165

166+
/**
167+
* @dataProvider invalidConfigValuesProvider
168+
*/
169+
public function testCreateWithInvalidConfigValues(array $values): void {
170+
$this->service->expects(self::never())
171+
->method('create');
172+
173+
$response = $this->controller->create(
174+
'Test',
175+
'Test',
176+
'Test',
177+
'PUBLIC',
178+
'test',
179+
$this->availability,
180+
...$values,
181+
);
182+
183+
self::assertEquals(422, $response->getStatus());
184+
}
185+
186+
public static function invalidConfigValuesProvider(): array {
187+
return [
188+
'negative preparation duration' => [[5 * 60, 5 * 60, -1, 0, 0, null, null, null, null]],
189+
'negative follow-up duration' => [[5 * 60, 5 * 60, 0, -1, 0, null, null, null, null]],
190+
'negative buffer' => [[5 * 60, 5 * 60, 0, 0, -1, null, null, null, null]],
191+
'zero daily maximum' => [[5 * 60, 5 * 60, 0, 0, 0, 0, null, null, null]],
192+
'zero future limit' => [[5 * 60, 5 * 60, 0, 0, 0, null, null, null, 0]],
193+
'end before start' => [[5 * 60, 5 * 60, 0, 0, 0, null, 2, 1, null]],
194+
];
195+
}
196+
166197
public function testCreate(): void {
167198
$appointment = new AppointmentConfig();
168199
$appointment->setName('Test');

0 commit comments

Comments
 (0)