CI / Tests (PHP 8.1) (pull_request) Successful in 56s
CI / Tests (PHP 8.2) (pull_request) Successful in 46s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 3m3s
CI / PHPStan (pull_request) Successful in 2m51s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m50s
CI / Build Plugin Zip (pull_request) Skipped
Adding availability for 5:30-6:00 PM with the lesson length left on its 60-minute default saved nothing and said nothing. A window is stored as consecutive lesson-length slots, so one that fits no lesson splits into none: splitByDuration() returned [], createFromWindow() inserted nothing, and addSlot() discarded the result and re-rendered the page unchanged. The REST endpoint already rejected that window with a 400. The admin form checked the same rules separately, and its copy was both laxer and mute — an unreadable date, an end before the start, and a two-day window were bare `return`s, and it never checked offering ownership at all, so a crafted POST could tie a slot to another instructor's offering and inherit their price and payment routing. Both callers now go through WindowValidator, which returns the window or a WP_Error explaining the refusal. The endpoint returns that error as is; the page renders its message as a notice. handleFormAction returns a [notice, error] pair so deletes report themselves too, and a successful add says how many slots it created. Two failures could also go unnoticed underneath: wpdb::insert's result was ignored, and insert_id still holds the previous statement's id after a failed write, so a failure looked like a success — and could become the recurrence group of a weekly series, orphaning every later occurrence. weeks was unbounded server-side despite the form's max=52. availability-admin.js narrows the lesson-length choices to those that fit the window and blocks submission when none do, which is what makes the original mistake hard to repeat. It is a convenience: the server validates regardless. Closes #130
117 lines
4.1 KiB
PHP
117 lines
4.1 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Availability;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Availability\AvailabilityEndpoint;
|
|
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
|
use Unsupervised\Schedular\Availability\AvailabilitySlot;
|
|
use Unsupervised\Schedular\Availability\WindowValidator;
|
|
use Unsupervised\Schedular\Offering\OfferingRepository;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class AvailabilityEndpointTest extends TestCase
|
|
{
|
|
private AvailabilityRepository $repository;
|
|
private OfferingRepository $offerings;
|
|
private AvailabilityEndpoint $endpoint;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Functions\when('absint')->alias(static fn ($v): int => abs((int) $v));
|
|
Functions\when('get_current_user_id')->justReturn(5);
|
|
|
|
$this->repository = Mockery::mock(AvailabilityRepository::class);
|
|
$this->offerings = Mockery::mock(OfferingRepository::class);
|
|
$this->endpoint = new AvailabilityEndpoint($this->repository, new WindowValidator($this->offerings));
|
|
}
|
|
|
|
public function testCreateRejectsWindowSpanningMultipleDays(): void
|
|
{
|
|
$this->repository->shouldNotReceive('createFromWindow');
|
|
|
|
$request = new \WP_REST_Request([
|
|
'start_dt' => '2026-06-01 19:52:00',
|
|
'end_dt' => '2026-06-30 19:52:00',
|
|
'duration_minutes' => 60,
|
|
]);
|
|
|
|
$result = $this->endpoint->create($request);
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('invalid_window', $result->get_error_code());
|
|
}
|
|
|
|
public function testCreateRejectsWindowShorterThanLessonLength(): void
|
|
{
|
|
$this->repository->shouldNotReceive('createFromWindow');
|
|
|
|
$request = new \WP_REST_Request([
|
|
'start_dt' => '2026-07-06 09:00:00',
|
|
'end_dt' => '2026-07-06 09:30:00',
|
|
'duration_minutes' => 60,
|
|
]);
|
|
|
|
$result = $this->endpoint->create($request);
|
|
|
|
self::assertInstanceOf(\WP_Error::class, $result);
|
|
self::assertSame('invalid_window', $result->get_error_code());
|
|
}
|
|
|
|
public function testCreateStoresWindowAsLessonLengthSlots(): void
|
|
{
|
|
$this->repository->shouldReceive('createFromWindow')
|
|
->once()
|
|
->with(
|
|
Mockery::on(static function (AvailabilitySlot $window): bool {
|
|
return $window->instructorId === 5
|
|
&& $window->startDt === '2026-07-06 09:00:00'
|
|
&& $window->endDt === '2026-07-06 16:00:00'
|
|
&& $window->durationMinutes === 60;
|
|
}),
|
|
false,
|
|
1
|
|
)
|
|
->andReturn([1, 2, 3, 4, 5, 6, 7]);
|
|
|
|
$request = new \WP_REST_Request([
|
|
'start_dt' => '2026-07-06 09:00:00',
|
|
'end_dt' => '2026-07-06 16:00:00',
|
|
'duration_minutes' => 60,
|
|
'recurrence' => 'single',
|
|
'weeks' => 1,
|
|
]);
|
|
|
|
$result = $this->endpoint->create($request);
|
|
|
|
self::assertInstanceOf(\WP_REST_Response::class, $result);
|
|
self::assertSame(201, $result->get_status());
|
|
self::assertSame(['ids' => [1, 2, 3, 4, 5, 6, 7]], $result->get_data());
|
|
}
|
|
|
|
public function testCreateWeeklyPassesRecurrenceThrough(): void
|
|
{
|
|
$this->repository->shouldReceive('createFromWindow')
|
|
->once()
|
|
->with(Mockery::type(AvailabilitySlot::class), true, 4)
|
|
->andReturn([1, 2, 3, 4]);
|
|
|
|
$request = new \WP_REST_Request([
|
|
'start_dt' => '2026-07-06 09:00:00',
|
|
'end_dt' => '2026-07-06 10:00:00',
|
|
'duration_minutes' => 60,
|
|
'recurrence' => 'weekly',
|
|
'weeks' => 4,
|
|
]);
|
|
|
|
$result = $this->endpoint->create($request);
|
|
|
|
self::assertInstanceOf(\WP_REST_Response::class, $result);
|
|
self::assertSame(['ids' => [1, 2, 3, 4]], $result->get_data());
|
|
}
|
|
}
|