Files
unsupervised-scheduler/tests/Unit/Booking/BookingRepositoryTest.php
T
thatguygriffandClaude Opus 5 8c21a3fa9d
CI / Tests (PHP 8.1) (pull_request) Successful in 6m39s
CI / Tests (PHP 8.2) (pull_request) Successful in 57s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m59s
CI / Tests (PHP 8.5) (pull_request) Successful in 3m31s
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards & Static Analysis (pull_request) Successful in 3m28s
CI / Build Plugin Zip (pull_request) Skipped
Let the studio book lessons and record intake collected elsewhere
Two related gaps, closed together because the second is created by the first.

A private lesson could only be booked by the student or their guardian, so a
booking taken over the phone had no way in — where group classes have had "Add
students directly" all along. "Book a lesson for a student" is now a panel on
Scheduler and My Lessons: student, open time, lesson type, with weekly term
reservations and a no-charge option for make-up lessons. The booking core is
extracted to Booking\LessonBooker and shared with POST /bookings, so the two
paths cannot drift on offering rules, slot claiming, or billing.

That leaves a registration with no intake answers and no policy acceptances,
because nobody was at a keyboard to give them — already true of every directly
added group-class student. Ticking the boxes on a student's behalf would be an
audit trail that says something untrue, so instead the answers are collected
another way and recorded afterwards, from a lesson's or an enrolment's detail
page. Every recording must say how it was collected, which is stamped on each
row along with who typed it and shown in a new "How it was given" column: a
policy ticked online and one transcribed from paper must never look alike.

Only staff-made registrations qualify (us_lessons.booked_by,
us_group_enrollments.enrolled_by) — one the student made already holds their
own answers. Only what is still missing can be recorded, re-checked at write
time, so a stale or double-posted form cannot duplicate or overwrite. No IP is
stored for a transcription, and accepted_by stays the student while recorded_by
names the staff member.

Intake is now generic over Registration\IntakeSubject, which Lesson and
Enrollment both implement; LessonDetail became Registration\IntakeAudit and is
shared by both detail views rather than duplicated.

Closes #182

Co-Authored-By: Claude Opus 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01QfHt6CyJHz6KkA4RuaS7WK
2026-08-24 14:06:16 -03:00

262 lines
8.9 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Booking;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Booking\BookingRepository;
use Unsupervised\Schedular\Booking\Lesson;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class BookingRepositoryTest extends TestCase
{
private \wpdb $db;
private BookingRepository $repo;
protected function setUp(): void
{
parent::setUp();
$this->db = Mockery::mock(\wpdb::class);
$this->db->prefix = 'wp_';
$this->repo = new BookingRepository($this->db);
}
public function testInsertCallsWpdbInsertAndReturnsId(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-04-01 12:00:00');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_lessons',
Mockery::on(static function (array $data): bool {
return $data['slot_id'] === 10
&& $data['student_id'] === 5
&& $data['offering_id'] === 7
&& $data['recurrence'] === Lesson::RECURRENCE_SINGLE
&& $data['status'] === Lesson::STATUS_PENDING
// Booked through the student-facing flow: no staff booker.
&& $data['booked_by'] === 0;
}),
['%d', '%d', '%d', '%d', '%s', '%d', '%s', '%d', '%s', '%d', '%s']
);
$this->db->insert_id = 77;
$lesson = new Lesson(slotId: 10, studentId: 5, instructorId: 3, offeringId: 7);
$result = $this->repo->insert($lesson);
self::assertSame(77, $result);
}
public function testInsertSeriesSharesSeriesIdAcrossSlots(): void
{
Functions\when('current_time')->justReturn('2026-04-01 12:00:00');
$ids = [40, 41, 42];
$this->db->shouldReceive('insert')
->times(3)
->andReturnUsing(function () use (&$ids): void {
$this->db->insert_id = array_shift($ids);
});
// The first lesson is back-filled with its own id as the series id.
$this->db->shouldReceive('update')
->once()
->with('wp_us_lessons', ['series_id' => 40], ['id' => 40], ['%d'], ['%d']);
$template = new Lesson(slotId: 0, studentId: 5, instructorId: 3, offeringId: 7);
$result = $this->repo->insertSeries($template, [100, 101, 102]);
self::assertSame([40, 41, 42], $result);
}
public function testFindByIdReturnsNullWhenNotFound(): void
{
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn(null);
self::assertNull($this->repo->findById(99));
}
public function testFindByIdReturnsLesson(): void
{
$row = (object) [
'id' => '15',
'slot_id' => '10',
'offering_id' => null,
'student_id' => '5',
'instructor_id' => '3',
'recurrence' => Lesson::RECURRENCE_SINGLE,
'series_id' => null,
'status' => 'pending',
'payment_id' => null,
'notes' => null,
];
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn($row);
$lesson = $this->repo->findById(15);
self::assertInstanceOf(Lesson::class, $lesson);
self::assertSame(15, $lesson->id);
}
public function testUpdateStatusReturnsFalseForInvalidStatus(): void
{
$result = $this->repo->updateStatus(1, 'invalid');
self::assertFalse($result);
}
public function testUpdateStatusCallsWpdbUpdate(): void
{
$this->db->shouldReceive('update')
->once()
->with(
'wp_us_lessons',
['status' => Lesson::STATUS_CONFIRMED],
['id' => 1],
['%s'],
['%d']
)
->andReturn(1);
self::assertTrue($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
}
public function testUpdateStatusReturnsFalseWhenDbFails(): void
{
$this->db->shouldReceive('update')->andReturn(0);
self::assertFalse($this->repo->updateStatus(1, Lesson::STATUS_CONFIRMED));
}
public function testUpdateStatusForSeriesReturnsFalseForInvalidStatus(): void
{
self::assertFalse($this->repo->updateStatusForSeries(12, 'invalid'));
}
public function testUpdateStatusForSeriesUpdatesNonCancelledRows(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::on(static fn (string $sql): bool =>
str_contains($sql, 'series_id = %d') && str_contains($sql, 'status != %s')),
'wp_us_lessons',
Lesson::STATUS_CONFIRMED,
12,
Lesson::STATUS_CANCELLED
)
->andReturn('UPDATE ...');
$this->db->shouldReceive('query')->once()->with('UPDATE ...')->andReturn(3);
self::assertTrue($this->repo->updateStatusForSeries(12, Lesson::STATUS_CONFIRMED));
}
public function testFindUpcomingForStudentJoinsSlotAndExcludesCancelled(): void
{
Functions\when('current_time')->justReturn('2026-06-08 12:00:00');
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/SELECT l\.\*.*l.student_id = %d.*l.status != %s.*a.start_dt >= %s.*ORDER BY a.start_dt ASC/s'), 'wp_us_lessons', 'wp_us_availability', 5, Lesson::STATUS_CANCELLED, '2026-06-08 12:00:00')
->andReturn('SELECT ...');
$row = (object) [
'id' => '15',
'slot_id' => '10',
'offering_id' => null,
'student_id' => '5',
'instructor_id' => '3',
'recurrence' => Lesson::RECURRENCE_SINGLE,
'series_id' => null,
'status' => 'confirmed',
'payment_id' => null,
'notes' => null,
];
$this->db->shouldReceive('get_results')->andReturn([$row]);
$lessons = $this->repo->findUpcomingForStudent(5);
self::assertCount(1, $lessons);
self::assertSame(15, $lessons[0]->id);
}
public function testFindUnbilledScheduledLessonsJoinsOfferingAndFiltersUnbilled(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::pattern('/l.status != %s.*l.payment_id IS NULL.*o.billing_mode IN \( %s, %s \)/s'),
'wp_us_lessons',
'wp_us_availability',
'wp_us_offerings',
Lesson::STATUS_CANCELLED,
'weekly',
'monthly'
)
->andReturn('SELECT ...');
$row = (object) [
'id' => '15',
'student_id' => '5',
'instructor_id' => '3',
'offering_id' => '9',
'start_dt' => '2026-07-15 18:00:00',
'billing_mode' => 'weekly',
'title' => 'Piano',
'price' => '35.00',
'currency' => 'CAD',
'etransfer_email' => null,
];
$this->db->shouldReceive('get_results')->andReturn([$row]);
$rows = $this->repo->findUnbilledScheduledLessons();
self::assertCount(1, $rows);
self::assertSame('15', $rows[0]->id);
}
public function testCountUpcomingForStudent(): void
{
Functions\when('current_time')->justReturn('2026-06-08 12:00:00');
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/COUNT\(\*\).*l.student_id = %d.*a.start_dt >= %s/s'), 'wp_us_lessons', 'wp_us_availability', 5, Lesson::STATUS_CANCELLED, '2026-06-08 12:00:00')
->andReturn('SELECT ...');
$this->db->shouldReceive('get_var')->andReturn('3');
self::assertSame(3, $this->repo->countUpcomingForStudent(5));
}
public function testFindByStudentReturnsLessons(): void
{
$row = (object) [
'id' => '1',
'slot_id' => '2',
'offering_id' => null,
'student_id' => '5',
'instructor_id' => '3',
'recurrence' => Lesson::RECURRENCE_SINGLE,
'series_id' => null,
'status' => 'pending',
'payment_id' => null,
'notes' => null,
];
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$row]);
$lessons = $this->repo->findByStudent(5);
self::assertCount(1, $lessons);
self::assertInstanceOf(Lesson::class, $lessons[0]);
}
}