Stop the availability form failing in silence
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
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
This commit is contained in:
@@ -49,6 +49,101 @@ class AvailabilityRepositoryTest extends TestCase
|
||||
self::assertSame(42, $result);
|
||||
}
|
||||
|
||||
public function testInsertReturnsZeroWhenTheWriteFails(): void
|
||||
{
|
||||
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
|
||||
|
||||
// wpdb::insert returns false on error, but insert_id still holds the
|
||||
// previous statement's id — returning it made a failed write look like a
|
||||
// successful one.
|
||||
$this->db->shouldReceive('insert')->once()->andReturn(false);
|
||||
$this->db->insert_id = 42;
|
||||
|
||||
$slot = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 30);
|
||||
|
||||
self::assertSame(0, $this->repo->insert($slot));
|
||||
}
|
||||
|
||||
public function testCreateFromWindowOmitsChunksThatFailedToInsert(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-04-01 12:00:00');
|
||||
|
||||
// Three chunks; the middle write fails.
|
||||
$results = [null, false, null];
|
||||
$ids = [11, 13];
|
||||
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(3)
|
||||
->andReturnUsing(function () use (&$results, &$ids) {
|
||||
$outcome = array_shift($results);
|
||||
|
||||
if (false !== $outcome) {
|
||||
$this->db->insert_id = array_shift($ids);
|
||||
}
|
||||
|
||||
return $outcome;
|
||||
});
|
||||
|
||||
$window = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:30:00', 30);
|
||||
|
||||
self::assertSame([11, 13], $this->repo->createFromWindow($window));
|
||||
}
|
||||
|
||||
public function testWeeklySeriesIsClampedToTheMaximum(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-04-01 12:00:00');
|
||||
|
||||
// The form's max is advisory; a hand-crafted POST could ask for any
|
||||
// number, so the ceiling is enforced here.
|
||||
$next = 1;
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(AvailabilitySlot::MAX_WEEKLY_OCCURRENCES)
|
||||
->andReturnUsing(function () use (&$next) {
|
||||
$this->db->insert_id = $next++;
|
||||
|
||||
return null;
|
||||
});
|
||||
$this->db->shouldReceive('update')->once();
|
||||
|
||||
$first = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 60);
|
||||
|
||||
self::assertCount(
|
||||
AvailabilitySlot::MAX_WEEKLY_OCCURRENCES,
|
||||
$this->repo->createWeeklySeries($first, 10000)
|
||||
);
|
||||
}
|
||||
|
||||
public function testWeeklySeriesGroupsOnTheFirstRowThatActuallyWrote(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-04-01 12:00:00');
|
||||
|
||||
// The first insert fails. A failed row must not become the recurrence
|
||||
// group (its id is 0), which would orphan every later occurrence.
|
||||
$results = [false, null, null];
|
||||
$ids = [21, 22];
|
||||
|
||||
$this->db->shouldReceive('insert')
|
||||
->times(3)
|
||||
->andReturnUsing(function () use (&$results, &$ids) {
|
||||
$outcome = array_shift($results);
|
||||
|
||||
if (false !== $outcome) {
|
||||
$this->db->insert_id = array_shift($ids);
|
||||
}
|
||||
|
||||
return $outcome;
|
||||
});
|
||||
|
||||
// The group is set from row 21 — the first that survived.
|
||||
$this->db->shouldReceive('update')
|
||||
->once()
|
||||
->with('wp_us_availability', ['recurrence_group' => 21], ['id' => 21], ['%d'], ['%d']);
|
||||
|
||||
$first = new AvailabilitySlot(5, '2026-04-01 09:00:00', '2026-04-01 10:00:00', 60);
|
||||
|
||||
self::assertSame([21, 22], $this->repo->createWeeklySeries($first, 3));
|
||||
}
|
||||
|
||||
public function testCreateWeeklySeriesInsertsWeeklyAndSharesGroup(): void
|
||||
{
|
||||
Functions\when('current_time')->justReturn('2026-04-07 12:00:00');
|
||||
|
||||
Reference in New Issue
Block a user