Files
unsupervised-scheduler/tests/Unit/ShortcodeRegistrarTest.php
T
thatguygriffandClaude Opus 5 b772e1811e 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]>
2026-07-29 16:07:52 -03:00

170 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit;
use Brain\Monkey\Actions;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Auth\LoginPage;
use Unsupervised\Schedular\Auth\RegistrationPage;
use Unsupervised\Schedular\Booking\BookingPage;
use Unsupervised\Schedular\GroupClass\GroupClassPage;
use Unsupervised\Schedular\Guardian\FamilyPage;
use Unsupervised\Schedular\ShortcodeRegistrar;
class ShortcodeRegistrarTest extends TestCase
{
private BookingPage&Mockery\MockInterface $bookingPage;
private LoginPage&Mockery\MockInterface $loginPage;
private RegistrationPage&Mockery\MockInterface $registrationPage;
private GroupClassPage&Mockery\MockInterface $groupClassPage;
private FamilyPage&Mockery\MockInterface $familyPage;
private ShortcodeRegistrar $registrar;
/** @var array<string, callable> */
private array $shortcodes = [];
/** @var array<string, mixed> */
private array $localized = [];
protected function setUp(): void
{
parent::setUp();
$this->bookingPage = Mockery::mock(BookingPage::class);
$this->loginPage = Mockery::mock(LoginPage::class);
$this->registrationPage = Mockery::mock(RegistrationPage::class);
$this->groupClassPage = Mockery::mock(GroupClassPage::class);
$this->familyPage = Mockery::mock(FamilyPage::class);
$this->registrar = new ShortcodeRegistrar(
$this->bookingPage,
$this->loginPage,
$this->registrationPage,
$this->groupClassPage,
$this->familyPage,
);
$shortcodes = &$this->shortcodes;
Functions\when('add_shortcode')->alias(
static function (string $tag, callable $callback) use (&$shortcodes): void {
$shortcodes[$tag] = $callback;
}
);
}
public function testRegisterAddsAllShortcodesAndHooks(): void
{
Actions\expectAdded('template_redirect')
->once()
->with([$this->registrationPage, 'maybeRedirectToRegistrationPage']);
Actions\expectAdded('wp_enqueue_scripts')
->once()
->with([$this->registrar, 'enqueueAssets']);
$this->registrar->register();
self::assertSame(
['us_booking', 'us_student_login', 'us_student_register', 'us_group_classes', 'us_family'],
array_keys($this->shortcodes)
);
}
/**
* WordPress passes an empty string (not an array) to a shortcode callback
* when the shortcode is used without attributes, e.g. `[us_booking]` —
* the wrapper must normalize it before the typed render methods.
*/
public function testBareShortcodeUsageIsNormalizedToAnEmptyAttributeArray(): void
{
$this->registrar->register();
$this->bookingPage->shouldReceive('render')->once()->with([])->andReturn('booking');
$this->loginPage->shouldReceive('render')->once()->with([])->andReturn('login');
$this->registrationPage->shouldReceive('render')->once()->with([])->andReturn('register');
$this->groupClassPage->shouldReceive('render')->once()->with([])->andReturn('group');
self::assertSame('booking', $this->shortcodes['us_booking'](''));
self::assertSame('login', $this->shortcodes['us_student_login'](''));
self::assertSame('register', $this->shortcodes['us_student_register'](''));
self::assertSame('group', $this->shortcodes['us_group_classes'](''));
}
/**
* The booking and group-class scripts both read prices through the shared
* pricing helper, so it must be registered ahead of them (and behind the
* payment helper, which carries the localized config it reads).
*/
public function testPricingHelperIsRegisteredAheadOfTheBookingAndGroupScripts(): void
{
$scripts = $this->captureEnqueuedAssets();
self::assertSame(['us-scheduler-payment'], $scripts['us-scheduler-pricing']);
self::assertSame(['us-scheduler-pricing', 'us-scheduler-guardian'], $scripts['us-scheduler']);
self::assertSame(['us-scheduler-pricing', 'us-scheduler-guardian'], $scripts['us-scheduler-group']);
}
/**
* The studio HST rate reaches the front end so a price quoted on a booking
* form matches the total the student is actually billed.
*/
public function testStudioTaxRateIsLocalizedToTheFrontEnd(): void
{
$this->captureEnqueuedAssets();
self::assertSame(13.0, $this->localized['taxRate']);
}
/**
* @return array<string, array<int, string>> Registered script handle => dependencies.
*/
private function captureEnqueuedAssets(): array
{
$scripts = [];
$localized = &$this->localized;
Functions\when('wp_register_style')->justReturn(true);
Functions\when('rest_url')->justReturn('https://example.test/wp-json/us-scheduler/v1/');
Functions\when('wp_create_nonce')->justReturn('nonce');
Functions\when('get_option')->alias(
static fn (string $name, mixed $default = false): mixed => match ($name) {
'us_hst_rate' => '13',
'start_of_week' => 1,
default => $default,
}
);
Functions\when('wp_register_script')->alias(
static function (string $handle, string $src, array $deps = []) use (&$scripts): bool {
$scripts[$handle] = $deps;
return true;
}
);
Functions\when('wp_localize_script')->alias(
static function (string $handle, string $object, array $data) use (&$localized): bool {
$localized = $data;
return true;
}
);
$this->registrar->enqueueAssets();
return $scripts;
}
public function testShortcodeAttributesArePassedThroughUnchanged(): void
{
$this->registrar->register();
$this->bookingPage->shouldReceive('render')
->once()->with(['login_page_id' => '5'])->andReturn('booking');
$this->loginPage->shouldReceive('render')
->once()->with(['booking_page_id' => '9'])->andReturn('login');
self::assertSame('booking', $this->shortcodes['us_booking'](['login_page_id' => '5']));
self::assertSame('login', $this->shortcodes['us_student_login'](['booking_page_id' => '9']));
}
}