Split availability windows into bookable lesson-length slots with weekly calendar views
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 45s
CI / PHPStan (pull_request) Successful in 2m48s
CI / Tests (PHP 8.1) (pull_request) Successful in 41s
CI / No Debug Code (pull_request) Failing after 2s
CI / Coding Standards (pull_request) Successful in 52s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m36s
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Tests (PHP 8.2) (pull_request) Successful in 45s
CI / PHPStan (pull_request) Successful in 2m48s
CI / Tests (PHP 8.1) (pull_request) Successful in 41s
CI / No Debug Code (pull_request) Failing after 2s
CI / Coding Standards (pull_request) Successful in 52s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m36s
Availability windows were stored and served as a single bookable row, so a 9:00 AM-4:00 PM window showed to students as one giant slot and booking it consumed the whole day; past and multi-day windows also leaked into the booking page as nonsense entries. - Split windows into consecutive lesson-length slots on save (REST and admin form); each chunk is independently bookable and weekly recurrence creates a series per chunk so "reserve this time weekly" holds the same hour each week - Reject windows spanning multiple days or shorter than the lesson length (400 invalid_window) - Never return slots whose start has passed from GET /availability - Migrate pre-split rows: Plugin::boot re-runs the Installer on version change and AvailabilityRepository::splitOversizedWindows() rewrites unbooked same-day oversized windows in place - Display all times in 12-hour AM/PM form (booking page, wp-admin lists, editor previews) - Add a List | Week view toggle to the student booking page and the instructor availability page, with previous/next-week navigation honouring the site's start_of_week option (new WeekCalendar helper) Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
<?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\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, $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());
|
||||
}
|
||||
}
|
||||
@@ -147,11 +147,16 @@ class AvailabilityRepositoryTest extends TestCase
|
||||
self::assertFalse($this->repo->delete(1));
|
||||
}
|
||||
|
||||
public function testFindAvailableWithNoFiltersPreparesTableOnly(): void
|
||||
public function testFindAvailableWithNoFiltersExcludesPastSlots(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$this->db->shouldReceive('prepare')
|
||||
->once()
|
||||
->with(Mockery::pattern('/WHERE is_booked = 0/'), ['wp_us_availability'])
|
||||
->with(
|
||||
Mockery::pattern('/WHERE is_booked = 0 AND start_dt >= %s/'),
|
||||
['wp_us_availability', '2026-07-05 12:00:00']
|
||||
)
|
||||
->andReturn('SELECT ...');
|
||||
|
||||
$this->db->shouldReceive('get_results')
|
||||
@@ -166,6 +171,8 @@ class AvailabilityRepositoryTest extends TestCase
|
||||
|
||||
public function testFindAvailableWithInstructorFilterPreparesQuery(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$this->db->shouldReceive('prepare')
|
||||
->once()
|
||||
->with(Mockery::pattern('/instructor_id = %d/'), Mockery::any())
|
||||
@@ -178,11 +185,13 @@ class AvailabilityRepositoryTest extends TestCase
|
||||
|
||||
public function testFindAvailableWithOfferingAndDurationFilters(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$this->db->shouldReceive('prepare')
|
||||
->once()
|
||||
->with(
|
||||
Mockery::pattern('/offering_id = %d AND duration_minutes = %d/'),
|
||||
Mockery::on(static fn (array $p): bool => $p === ['wp_us_availability', 8, 30])
|
||||
Mockery::on(static fn (array $p): bool => $p === ['wp_us_availability', '2026-07-05 12:00:00', 8, 30])
|
||||
)
|
||||
->andReturn('SELECT ...');
|
||||
|
||||
@@ -191,6 +200,118 @@ class AvailabilityRepositoryTest extends TestCase
|
||||
$this->repo->findAvailable(offeringId: 8, durationMinutes: 30);
|
||||
}
|
||||
|
||||
public function testCreateFromWindowInsertsOneRowPerLessonLengthChunk(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$captured = [];
|
||||
$ids = [21, 22, 23];
|
||||
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(3)
|
||||
->andReturnUsing(function (string $table, array $data) use (&$captured, &$ids): void {
|
||||
$captured[] = [$data['start_dt'], $data['end_dt']];
|
||||
$this->db->insert_id = array_shift($ids);
|
||||
});
|
||||
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 12:00:00', 60);
|
||||
$result = $this->repo->createFromWindow($window);
|
||||
|
||||
self::assertSame([21, 22, 23], $result);
|
||||
self::assertSame(
|
||||
[
|
||||
['2026-07-06 09:00:00', '2026-07-06 10:00:00'],
|
||||
['2026-07-06 10:00:00', '2026-07-06 11:00:00'],
|
||||
['2026-07-06 11:00:00', '2026-07-06 12:00:00'],
|
||||
],
|
||||
$captured
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreateFromWindowWeeklyCreatesASeriesPerChunk(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$captured = [];
|
||||
$ids = [30, 31, 40, 41];
|
||||
|
||||
// Two chunks × two weeks: each chunk becomes its own weekly series.
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(4)
|
||||
->andReturnUsing(function (string $table, array $data) use (&$captured, &$ids): void {
|
||||
$captured[] = $data['start_dt'];
|
||||
$this->db->insert_id = array_shift($ids);
|
||||
});
|
||||
|
||||
// Each series back-fills its first row with its own recurrence group.
|
||||
$this->db->shouldReceive('update')
|
||||
->once()
|
||||
->with('wp_us_availability', ['recurrence_group' => 30], ['id' => 30], ['%d'], ['%d']);
|
||||
$this->db->shouldReceive('update')
|
||||
->once()
|
||||
->with('wp_us_availability', ['recurrence_group' => 40], ['id' => 40], ['%d'], ['%d']);
|
||||
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 11:00:00', 60);
|
||||
$result = $this->repo->createFromWindow($window, weekly: true, weeks: 2);
|
||||
|
||||
self::assertSame([30, 31, 40, 41], $result);
|
||||
self::assertSame(
|
||||
[
|
||||
'2026-07-06 09:00:00',
|
||||
'2026-07-13 09:00:00',
|
||||
'2026-07-06 10:00:00',
|
||||
'2026-07-13 10:00:00',
|
||||
],
|
||||
$captured
|
||||
);
|
||||
}
|
||||
|
||||
public function testSplitOversizedWindowsTrimsRowAndInsertsRemainingChunks(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-07-05 12:00:00');
|
||||
|
||||
$row = (object) [
|
||||
'id' => '9',
|
||||
'instructor_id' => '5',
|
||||
'offering_id' => null,
|
||||
'start_dt' => '2026-07-06 09:00:00',
|
||||
'end_dt' => '2026-07-06 12:00:00',
|
||||
'duration_minutes' => '60',
|
||||
'is_booked' => '0',
|
||||
'recurrence_group' => null,
|
||||
];
|
||||
|
||||
$this->db->shouldReceive('prepare')
|
||||
->once()
|
||||
->with(Mockery::pattern('/TIMESTAMPDIFF\(MINUTE, start_dt, end_dt\) > duration_minutes/'), 'wp_us_availability')
|
||||
->andReturn('SELECT ...');
|
||||
$this->db->shouldReceive('get_results')->once()->with('SELECT ...')->andReturn([$row]);
|
||||
|
||||
// The original row is trimmed to the first lesson-length chunk.
|
||||
$this->db->shouldReceive('update')
|
||||
->once()
|
||||
->with('wp_us_availability', ['end_dt' => '2026-07-06 10:00:00'], ['id' => 9], ['%s'], ['%d']);
|
||||
|
||||
// The remaining two chunks become new rows.
|
||||
$inserted = [];
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(2)
|
||||
->andReturnUsing(function (string $table, array $data) use (&$inserted): void {
|
||||
$inserted[] = [$data['start_dt'], $data['end_dt']];
|
||||
$this->db->insert_id = 50;
|
||||
});
|
||||
|
||||
$this->repo->splitOversizedWindows();
|
||||
|
||||
self::assertSame(
|
||||
[
|
||||
['2026-07-06 10:00:00', '2026-07-06 11:00:00'],
|
||||
['2026-07-06 11:00:00', '2026-07-06 12:00:00'],
|
||||
],
|
||||
$inserted
|
||||
);
|
||||
}
|
||||
|
||||
public function testFindByInstructorReturnsSlots(): void
|
||||
{
|
||||
$row = (object) [
|
||||
|
||||
@@ -84,6 +84,51 @@ class AvailabilitySlotTest extends TestCase
|
||||
self::assertNull(AvailabilitySlot::normalizeDateTime("2026-04-01 09:00:00'); DROP TABLE x;--"));
|
||||
}
|
||||
|
||||
public function testSplitByDurationChunksWindowIntoLessonLengthSlots(): void
|
||||
{
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 12:00:00', 60, 8);
|
||||
|
||||
$slots = $window->splitByDuration();
|
||||
|
||||
self::assertCount(3, $slots);
|
||||
self::assertSame('2026-07-06 09:00:00', $slots[0]->startDt);
|
||||
self::assertSame('2026-07-06 10:00:00', $slots[0]->endDt);
|
||||
self::assertSame('2026-07-06 11:00:00', $slots[2]->startDt);
|
||||
self::assertSame('2026-07-06 12:00:00', $slots[2]->endDt);
|
||||
|
||||
// Each chunk keeps the window's instructor, duration, and offering.
|
||||
self::assertSame(5, $slots[1]->instructorId);
|
||||
self::assertSame(60, $slots[1]->durationMinutes);
|
||||
self::assertSame(8, $slots[1]->offeringId);
|
||||
}
|
||||
|
||||
public function testSplitByDurationDropsRemainderShorterThanALesson(): void
|
||||
{
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 16:30:00', 60);
|
||||
|
||||
$slots = $window->splitByDuration();
|
||||
|
||||
self::assertCount(7, $slots);
|
||||
self::assertSame('2026-07-06 16:00:00', $slots[6]->endDt);
|
||||
}
|
||||
|
||||
public function testSplitByDurationReturnsEmptyWhenWindowTooShort(): void
|
||||
{
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 09:45:00', 60);
|
||||
|
||||
self::assertSame([], $window->splitByDuration());
|
||||
}
|
||||
|
||||
public function testSplitByDurationHandlesThirtyMinuteLessons(): void
|
||||
{
|
||||
$window = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 10:30:00', 30);
|
||||
|
||||
$slots = $window->splitByDuration();
|
||||
|
||||
self::assertCount(3, $slots);
|
||||
self::assertSame('2026-07-06 09:30:00', $slots[0]->endDt);
|
||||
}
|
||||
|
||||
public function testToArrayContainsExpectedKeys(): void
|
||||
{
|
||||
$slot = new AvailabilitySlot(1, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 30, 8, false, null, 10);
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Tests\Unit\Availability;
|
||||
|
||||
use Unsupervised\Schedular\Availability\AvailabilitySlot;
|
||||
use Unsupervised\Schedular\Availability\WeekCalendar;
|
||||
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
||||
|
||||
class WeekCalendarTest extends TestCase
|
||||
{
|
||||
public function testWeekStartShiftsBackToConfiguredStartOfWeek(): void
|
||||
{
|
||||
// 2026-07-08 is a Wednesday; with Monday (1) starts, the week begins Jul 6.
|
||||
self::assertSame('2026-07-06', WeekCalendar::weekStart('2026-07-08', 1, '2026-01-01'));
|
||||
|
||||
// With Sunday (0) starts, the same Wednesday's week begins Jul 5.
|
||||
self::assertSame('2026-07-05', WeekCalendar::weekStart('2026-07-08', 0, '2026-01-01'));
|
||||
}
|
||||
|
||||
public function testWeekStartIsIdempotentWhenAnchorIsAlreadyTheStart(): void
|
||||
{
|
||||
self::assertSame('2026-07-06', WeekCalendar::weekStart('2026-07-06', 1, '2026-01-01'));
|
||||
}
|
||||
|
||||
public function testWeekStartFallsBackToTodayForInvalidInput(): void
|
||||
{
|
||||
// 2026-07-05 is a Sunday; with Monday starts, its week began Jun 29.
|
||||
self::assertSame('2026-06-29', WeekCalendar::weekStart('', 1, '2026-07-05'));
|
||||
self::assertSame('2026-06-29', WeekCalendar::weekStart('not-a-date', 1, '2026-07-05'));
|
||||
self::assertSame('2026-06-29', WeekCalendar::weekStart('2026-13-40', 1, '2026-07-05'));
|
||||
}
|
||||
|
||||
public function testDaysReturnsSevenBucketsWithSlotsOnTheirDates(): void
|
||||
{
|
||||
$monday = new AvailabilitySlot(5, '2026-07-06 09:00:00', '2026-07-06 10:00:00', 60, null, false, null, 1);
|
||||
$mondayTwo = new AvailabilitySlot(5, '2026-07-06 10:00:00', '2026-07-06 11:00:00', 60, null, false, null, 2);
|
||||
$thursday = new AvailabilitySlot(5, '2026-07-09 14:00:00', '2026-07-09 15:00:00', 60, null, false, null, 3);
|
||||
$nextWeek = new AvailabilitySlot(5, '2026-07-13 09:00:00', '2026-07-13 10:00:00', 60, null, false, null, 4);
|
||||
|
||||
$days = WeekCalendar::days('2026-07-06', [$monday, $mondayTwo, $thursday, $nextWeek]);
|
||||
|
||||
self::assertCount(7, $days);
|
||||
self::assertSame('2026-07-06', $days[0]['date']);
|
||||
self::assertSame('2026-07-12', $days[6]['date']);
|
||||
|
||||
self::assertCount(2, $days[0]['slots']);
|
||||
self::assertSame(3, $days[3]['slots'][0]->id);
|
||||
self::assertSame([], $days[1]['slots']);
|
||||
|
||||
// A slot outside the week is not bucketed anywhere.
|
||||
$ids = array_merge(...array_map(
|
||||
static fn (array $day): array => array_map(static fn (AvailabilitySlot $s): ?int => $s->id, $day['slots']),
|
||||
$days
|
||||
));
|
||||
self::assertNotContains(4, $ids);
|
||||
}
|
||||
|
||||
public function testDaysCrossesMonthBoundary(): void
|
||||
{
|
||||
$days = WeekCalendar::days('2026-06-29', []);
|
||||
|
||||
self::assertSame('2026-06-29', $days[0]['date']);
|
||||
self::assertSame('2026-07-05', $days[6]['date']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user