Files
unsupervised-scheduler/tests/Unit/Payment/StudioSettingsTest.php
T
thatguygriffandClaude Opus 4.8 8b90b8d78d
CI / Tests (PHP 8.1) (pull_request) Successful in 41s
CI / Tests (PHP 8.2) (pull_request) Successful in 53s
CI / No Debug Code (pull_request) Successful in 2s
CI / PHPStan (pull_request) Successful in 2m51s
CI / Coding Standards (pull_request) Successful in 2m54s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m38s
CI / Build Plugin Zip (pull_request) Skipped
Add cancellation cutoff limiting how close to a lesson a student can cancel
Students can no longer cancel their own lesson online once it starts within a
configured window; instructors and studio admins can always cancel.

- Studio default `us_cancellation_cutoff_hours` (stored/computed in hours,
  entered and displayed in days under Studio Settings → Cancellations).
- Optional per-offering override `cancellation_cutoff_hours` (entered in hours);
  blank inherits the studio default, 0 allows anytime cancellation.
- `Booking\CancellationPolicy` resolves the effective window and decides;
  `BookingEndpoint::cancel()` returns a 403 `cancellation_closed` when too late.
  The instructor status endpoint and studio-admin student actions bypass it.

Closes #93

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-23 11:57:18 -03:00

112 lines
4.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Payment;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\RoleManager;
use Unsupervised\Schedular\Payment\StudioSettings;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class StudioSettingsTest extends TestCase
{
public function testRegistrationModeDefaultsToInvite(): void
{
Functions\when('get_option')->alias(static fn (string $name, $default) => $default);
$settings = new StudioSettings();
self::assertSame(StudioSettings::MODE_INVITE, $settings->registrationMode());
self::assertFalse($settings->openRegistrationEnabled());
}
public function testCancellationCutoffDefaultsToOneDay(): void
{
Functions\when('get_option')->alias(static fn (string $name, $default) => $default);
self::assertSame(24, (new StudioSettings())->cancellationCutoffHours());
}
public function testCancellationCutoffReadsStoredHours(): void
{
Functions\when('get_option')->alias(static fn (string $name) =>
$name === StudioSettings::OPT_CANCELLATION_CUTOFF_HOURS ? '72' : '');
self::assertSame(72, (new StudioSettings())->cancellationCutoffHours());
}
public function testCancellationCutoffClampsNegativeToZero(): void
{
Functions\when('get_option')->alias(static fn (string $name) =>
$name === StudioSettings::OPT_CANCELLATION_CUTOFF_HOURS ? '-5' : '');
self::assertSame(0, (new StudioSettings())->cancellationCutoffHours());
}
public function testOpenRegistrationEnabledWhenStored(): void
{
Functions\when('get_option')->alias(static fn (string $name) =>
$name === StudioSettings::OPT_REGISTRATION_MODE ? StudioSettings::MODE_SELF_APPROVAL : '');
self::assertTrue((new StudioSettings())->openRegistrationEnabled());
}
public function testEnablingMirrorsCoreOptionsAndSnapshots(): void
{
Functions\when('get_option')->alias(static function (string $name, $default = false) {
return match ($name) {
StudioSettings::OPT_REGISTRATION_MODE => StudioSettings::MODE_INVITE, // currently closed
'users_can_register' => false, // snapshot as '0'
'default_role' => 'contributor',
default => $default,
};
});
Functions\expect('update_option')->once()->with(StudioSettings::OPT_PREV_USERS_CAN_REGISTER, '0');
Functions\expect('update_option')->once()->with(StudioSettings::OPT_PREV_DEFAULT_ROLE, 'contributor');
Functions\expect('update_option')->once()->with('users_can_register', '1');
Functions\expect('update_option')->once()->with('default_role', RoleManager::STUDENT);
Functions\expect('update_option')->once()->with(StudioSettings::OPT_REGISTRATION_MODE, StudioSettings::MODE_SELF_APPROVAL);
$this->applyMode(true);
}
public function testDisablingRestoresSnapshot(): void
{
Functions\when('get_option')->alias(static function (string $name, $default = false) {
return match ($name) {
StudioSettings::OPT_REGISTRATION_MODE => StudioSettings::MODE_SELF_APPROVAL, // currently open
StudioSettings::OPT_PREV_USERS_CAN_REGISTER => '1',
StudioSettings::OPT_PREV_DEFAULT_ROLE => 'contributor',
default => $default,
};
});
Functions\expect('update_option')->once()->with('users_can_register', '1');
Functions\expect('update_option')->once()->with('default_role', 'contributor');
Functions\expect('delete_option')->once()->with(StudioSettings::OPT_PREV_USERS_CAN_REGISTER);
Functions\expect('delete_option')->once()->with(StudioSettings::OPT_PREV_DEFAULT_ROLE);
Functions\expect('update_option')->once()->with(StudioSettings::OPT_REGISTRATION_MODE, StudioSettings::MODE_INVITE);
$this->applyMode(false);
}
public function testNoTransitionLeavesCoreOptionsUntouched(): void
{
// Already open, asked to enable again: nothing should be written.
Functions\when('get_option')->alias(static fn (string $name, $default = false) =>
$name === StudioSettings::OPT_REGISTRATION_MODE ? StudioSettings::MODE_SELF_APPROVAL : $default);
Functions\expect('update_option')->never();
Functions\expect('delete_option')->never();
$this->applyMode(true);
}
private function applyMode(bool $enable): void
{
$method = new \ReflectionMethod(StudioSettings::class, 'applyRegistrationMode');
$method->invoke(new StudioSettings(), $enable);
}
}