Files
unsupervised-scheduler/tests/Unit/ShortcodeRegistrarTest.php
T
thatguygriffandClaude Opus 5 9344ab7193
CI / Tests (PHP 8.2) (pull_request) Successful in 46s
CI / Tests (PHP 8.1) (pull_request) Successful in 56s
CI / No Debug Code (pull_request) Successful in 2s
CI / PHPStan (pull_request) Successful in 2m56s
CI / Coding Standards (pull_request) Successful in 2m59s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m41s
CI / Build Plugin Zip (pull_request) Skipped
Show price cadence and require a pay agreement at booking
Every price a student meets on the front end now carries the cadence it is
billed on — at booking, up front, weekly, monthly — so a bare amount can no
longer read as a one-off when it is a recurring charge.

Both registration forms then restate the price and require a second, separate
tick agreeing to pay it, distinct from the policy acceptances above it. The
agreed figure includes the studio HST so it matches Payment::total(), the amount
actually billed; the rate reaches the browser as a new localized `taxRate`.

A weekly reservation is charged per lesson for every week it claims, and a week
another student takes first is simply not claimed, so its total is quoted as a
ceiling ("up to 12 lessons") rather than a promise. Free offerings have nothing
to agree to and show no price block at all.

The formatting and the agreement live in one shared helper (`window.usPricing`,
registered as `us-scheduler-pricing`) so a price reads the same in the booking
form, the class catalogue and the editor preview.

Closes #124

Co-Authored-By: Claude Opus 5 <[email protected]>
2026-07-28 14:43:38 -03:00

166 lines
6.1 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\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 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->registrar = new ShortcodeRegistrar(
$this->bookingPage,
$this->loginPage,
$this->registrationPage,
$this->groupClassPage,
);
$shortcodes = &$this->shortcodes;
Functions\when('add_shortcode')->alias(
static function (string $tag, callable $callback) use (&$shortcodes): void {
$shortcodes[$tag] = $callback;
}
);
}
public function testRegisterAddsAllFourShortcodesAndHooks(): 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'],
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'], $scripts['us-scheduler']);
self::assertSame(['us-scheduler-pricing'], $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']));
}
}