Let parents register once and book for their children

A parent registers once and manages lessons for one or more children, who
need no login of their own. A child is a real wp_users row with the student
role but no usable login — so student_id keeps meaning "a WordPress user"
on every table, and booking, credits, policies and enrolments work unchanged.
A us_guardians link table maps guardian to child.

The signup form gains a parent/guardian tick that reveals a block per child,
with the account-signup questions asked per child rather than per guardian
— they describe the student, not the account holder. Signup policies are
recorded once per child with the guardian as the acceptor, which is the
record that actually means something. A family that half-creates is rolled
back entirely rather than leaving a guardian who cannot re-register.

The booking and enrolment forms gain a "Who is this for?" picker listing
children first, so the default selection is never the parent — booking for
the wrong child is correctable, quietly billing a parent for their kid's
lesson is not. POST /bookings and POST /enrollments take an optional
student_id honoured only for that child's guardian; anything else is a 403.
That check is the authorisation boundary of the feature.

Payments and credits gain a payer: the charge names the child it was for and
the guardian who owes it, so per-child reporting is unchanged while notices,
receipts and the payment step reach the parent. Credit is held by the payer,
so one child's cancellation can settle a sibling's charge, and the daily
billing scan sends a guardian one notice covering every child.

Closes #132

Co-Authored-By: Claude Opus 5 <[email protected]>
This commit is contained in:
2026-07-29 16:07:52 -03:00
co-authored by Claude Opus 5
parent c25260a367
commit b772e1811e
71 changed files with 4192 additions and 191 deletions
+122
View File
@@ -0,0 +1,122 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Guardian;
use Brain\Monkey\Filters;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Auth\RoleManager;
use Unsupervised\Schedular\Guardian\ChildLoginGate;
use Unsupervised\Schedular\Guardian\GuardianService;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class ChildLoginGateTest extends TestCase
{
private ChildLoginGate $gate;
protected function setUp(): void
{
parent::setUp();
$this->gate = new ChildLoginGate();
}
/**
* @param array<int, string> $children User IDs flagged as child accounts.
*/
private function stubChildren(array $children): void
{
Functions\when('get_user_meta')->alias(
static fn (int $userId, string $key, bool $single = false): string => in_array($userId, $children, true) ? '1' : ''
);
}
private function user(int $id): \WP_User
{
$user = Mockery::mock(\WP_User::class);
$user->ID = $id;
return $user;
}
public function testRegisterHooksBothFilters(): void
{
$this->gate->register();
self::assertNotFalse(Filters\has('wp_authenticate_user', [$this->gate, 'blockChildLogin']));
self::assertNotFalse(Filters\has('user_has_cap', [$this->gate, 'withholdBooking']));
}
public function testChildAccountCannotAuthenticate(): void
{
$this->stubChildren([42]);
$result = $this->gate->blockChildLogin($this->user(42));
self::assertInstanceOf(\WP_Error::class, $result);
self::assertSame('us_child_account', $result->get_error_code());
}
public function testOrdinaryStudentPassesThrough(): void
{
$this->stubChildren([42]);
$user = $this->user(9);
self::assertSame($user, $this->gate->blockChildLogin($user));
}
public function testAnEarlierAuthenticationErrorIsPassedThroughUntouched(): void
{
$this->stubChildren([42]);
$error = new \WP_Error('bad_password', 'Nope.');
self::assertSame($error, $this->gate->blockChildLogin($error));
}
public function testBookingCapabilityIsWithheldFromAChild(): void
{
$this->stubChildren([42]);
$caps = $this->gate->withholdBooking(
['read' => true, RoleManager::CAP_BOOK_LESSON => true],
[],
[],
$this->user(42)
);
self::assertArrayNotHasKey(RoleManager::CAP_BOOK_LESSON, $caps);
self::assertTrue($caps['read']);
}
public function testBookingCapabilityIsLeftAloneForAnOrdinaryStudent(): void
{
$this->stubChildren([42]);
$caps = $this->gate->withholdBooking(
[RoleManager::CAP_BOOK_LESSON => true],
[],
[],
$this->user(9)
);
self::assertTrue($caps[RoleManager::CAP_BOOK_LESSON]);
}
public function testNonUserSubjectIsIgnored(): void
{
$caps = [RoleManager::CAP_BOOK_LESSON => true];
self::assertSame($caps, $this->gate->withholdBooking($caps, [], [], null));
}
public function testGuardianServiceIsTheSingleSourceOfTheChildFlag(): void
{
$this->stubChildren([42]);
self::assertTrue(GuardianService::isChild(42));
self::assertFalse(GuardianService::isChild(9));
}
}