CI / Tests (PHP 8.1) (pull_request) Successful in 1m0s
CI / Tests (PHP 8.2) (pull_request) Successful in 1m0s
CI / No Debug Code (pull_request) Successful in 3s
CI / Coding Standards (pull_request) Successful in 3m8s
CI / Build Plugin Zip (pull_request) Skipped
CI / PHPStan (pull_request) Successful in 2m49s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m44s
Five items from the latest demo pass: - A policy's title can be edited from the Policies screen. Only the title moves; the slug is what the gates resolve policies by, so a rename can never detach a policy from acceptances already recorded against it. - Signup is one page again. The studio's registration questions move from a second step behind "Next" onto the main form, in an "About you" panel above the students being added, and that panel also asks an adult student for their birth year (the same us_birth_year meta a child's uses). register.js disables and hides the whole panel for a pure guardian, since the questions describe a student. - The password is re-scored on submit, not only as it is typed. zxcvbn's dictionary arrives after page load, so a password typed straight away was never scored at all and the first the student heard of it was the server rejecting the whole form. - Group-class sessions appear alongside lessons wherever upcoming lessons are listed: the [us_scheduler] panel (students and instructors) and the admin student detail page. GroupClass\SessionSchedule derives them from Offering::sessionWindows(), the same derivation the billing scan uses. They carry kind = 'group_class' and no Cancel action - a session is one date in a term, not a booked slot. - Deleting a user releases what the account was holding: each upcoming lesson is cancelled, its slot freed for rebooking, its pending payment voided, and active class enrolments cancelled. Past lessons and paid history are left alone. Tests: composer test (851), composer lint, composer cs all pass. Co-Authored-By: Claude Opus 5 <[email protected]>
325 lines
13 KiB
PHP
325 lines
13 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Tests\Unit\Policy;
|
|
|
|
use Brain\Monkey\Functions;
|
|
use Mockery;
|
|
use Unsupervised\Schedular\Policy\Policy;
|
|
use Unsupervised\Schedular\Policy\PolicyController;
|
|
use Unsupervised\Schedular\Policy\PolicyRepository;
|
|
use Unsupervised\Schedular\Policy\PolicyService;
|
|
use Unsupervised\Schedular\Policy\PolicyVersion;
|
|
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
|
use Unsupervised\Schedular\Tests\Unit\TestCase;
|
|
|
|
class PolicyControllerTest extends TestCase
|
|
{
|
|
private PolicyRepository&Mockery\MockInterface $policies;
|
|
private PolicyVersionRepository&Mockery\MockInterface $versions;
|
|
private PolicyService&Mockery\MockInterface $service;
|
|
private PolicyController $controller;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->policies = Mockery::mock(PolicyRepository::class);
|
|
$this->versions = Mockery::mock(PolicyVersionRepository::class);
|
|
$this->service = Mockery::mock(PolicyService::class);
|
|
$this->controller = new PolicyController($this->policies, $this->versions, $this->service);
|
|
|
|
$_POST = [];
|
|
$_GET = [];
|
|
|
|
Functions\when('current_user_can')->justReturn(true);
|
|
Functions\when('check_admin_referer')->justReturn(true);
|
|
Functions\when('wp_unslash')->returnArg();
|
|
Functions\when('sanitize_text_field')->returnArg();
|
|
Functions\when('sanitize_title')->returnArg();
|
|
Functions\when('wp_kses_post')->returnArg();
|
|
$this->stubAutop();
|
|
Functions\when('sanitize_key')->alias(
|
|
static fn ($key) => strtolower((string) preg_replace('/[^a-zA-Z0-9_\-]/', '', (string) $key))
|
|
);
|
|
Functions\when('absint')->alias(static fn ($value) => abs((int) $value));
|
|
Functions\when('selected')->justReturn('');
|
|
Functions\when('wp_nonce_field')->justReturn('');
|
|
Functions\when('admin_url')->justReturn('admin.php');
|
|
Functions\when('add_query_arg')->alias(
|
|
static fn (array $args, string $url) => $url . '?' . http_build_query($args)
|
|
);
|
|
Functions\when('submit_button')->alias(static function (string $text = ''): void {
|
|
echo $text; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- test stub
|
|
});
|
|
}
|
|
|
|
public function testViewingAVersionRendersItsBodyAndAnEditForm(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', currentVersionId: 7, id: 4);
|
|
$version = new PolicyVersion(
|
|
policyId: 4,
|
|
versionNumber: 2,
|
|
body: '<p>Cancel 24 hours ahead.</p>',
|
|
status: PolicyVersion::STATUS_PUBLISHED,
|
|
publishedAt: '2026-07-01 09:00:00',
|
|
id: 7,
|
|
);
|
|
|
|
$_GET = [
|
|
'policy_id' => '4',
|
|
'version_id' => '7',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([$version]);
|
|
$this->versions->shouldReceive('findById')->with(7)->andReturn($version);
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringContainsString('Cancel 24 hours ahead.', $html);
|
|
self::assertStringContainsString('value="edit_version"', $html);
|
|
self::assertStringContainsString('Save as New Draft', $html);
|
|
}
|
|
|
|
public function testVersionListLinksToTheViewer(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', currentVersionId: 7, id: 4);
|
|
$version = new PolicyVersion(policyId: 4, versionNumber: 2, body: 'text', id: 7);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([$version]);
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringContainsString('page=us-policies&policy_id=4&version_id=7', $html);
|
|
}
|
|
|
|
public function testAVersionBelongingToAnotherPolicyIsNotShown(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', id: 4);
|
|
$other = new PolicyVersion(policyId: 9, versionNumber: 1, body: 'Someone else\'s policy', id: 7);
|
|
|
|
$_GET = [
|
|
'policy_id' => '4',
|
|
'version_id' => '7',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([]);
|
|
$this->versions->shouldReceive('findById')->with(7)->andReturn($other);
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringNotContainsString('Someone else\'s policy', $html);
|
|
self::assertStringNotContainsString('value="edit_version"', $html);
|
|
}
|
|
|
|
public function testEditingAPublishedVersionCreatesANewDraftAndLeavesTheSourceUntouched(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', currentVersionId: 7, id: 4);
|
|
$published = new PolicyVersion(
|
|
policyId: 4,
|
|
versionNumber: 2,
|
|
body: '<p>Old text.</p>',
|
|
status: PolicyVersion::STATUS_PUBLISHED,
|
|
id: 7,
|
|
);
|
|
$draft = new PolicyVersion(policyId: 4, versionNumber: 3, body: '<p>New text.</p>', id: 11);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'edit_version',
|
|
'policy_id' => '4',
|
|
'version_id' => '7',
|
|
'body' => '<p>New text.</p>',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([$draft, $published]);
|
|
$this->versions->shouldReceive('findById')->with(7)->andReturn($published);
|
|
$this->versions->shouldReceive('findById')->with(11)->andReturn($draft);
|
|
|
|
// A new draft is written; the edited version itself is never updated.
|
|
$this->service->shouldReceive('addDraftVersion')->once()->with(4, '<p>New text.</p>')->andReturn(11);
|
|
$this->versions->shouldNotReceive('updateBody');
|
|
|
|
$html = $this->render();
|
|
|
|
// The viewer follows through to the draft that was just created.
|
|
self::assertStringContainsString('New text.', $html);
|
|
self::assertStringContainsString('saved as a new draft version', $html);
|
|
}
|
|
|
|
public function testEditingADraftVersionUpdatesItInPlace(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', id: 4);
|
|
$draft = new PolicyVersion(
|
|
policyId: 4,
|
|
versionNumber: 3,
|
|
body: '<p>Old draft text.</p>',
|
|
status: PolicyVersion::STATUS_DRAFT,
|
|
id: 11,
|
|
);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'edit_version',
|
|
'policy_id' => '4',
|
|
'version_id' => '11',
|
|
'body' => '<p>Revised draft text.</p>',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([$draft]);
|
|
$this->versions->shouldReceive('findById')->with(11)->andReturn($draft);
|
|
|
|
// An unpublished draft is rewritten rather than branched.
|
|
$this->versions->shouldReceive('updateBody')->once()->with(11, '<p>Revised draft text.</p>')->andReturn(true);
|
|
$this->service->shouldNotReceive('addDraftVersion');
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringContainsString('Draft version 3 was updated', $html);
|
|
}
|
|
|
|
public function testEditingAnArchivedVersionCreatesANewDraft(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', currentVersionId: 9, id: 4);
|
|
$archived = new PolicyVersion(
|
|
policyId: 4,
|
|
versionNumber: 1,
|
|
body: '<p>Superseded text.</p>',
|
|
status: PolicyVersion::STATUS_ARCHIVED,
|
|
id: 7,
|
|
);
|
|
$draft = new PolicyVersion(policyId: 4, versionNumber: 4, body: '<p>Reinstated text.</p>', id: 12);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'edit_version',
|
|
'policy_id' => '4',
|
|
'version_id' => '7',
|
|
'body' => '<p>Reinstated text.</p>',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([$draft, $archived]);
|
|
$this->versions->shouldReceive('findById')->with(7)->andReturn($archived);
|
|
$this->versions->shouldReceive('findById')->with(12)->andReturn($draft);
|
|
|
|
$this->service->shouldReceive('addDraftVersion')->once()->with(4, '<p>Reinstated text.</p>')->andReturn(12);
|
|
$this->versions->shouldNotReceive('updateBody');
|
|
|
|
$this->render();
|
|
}
|
|
|
|
public function testEditingAVersionOfAnotherPolicyIsRejected(): void
|
|
{
|
|
$policy = new Policy('Cancellation', 'cancellation', id: 4);
|
|
$other = new PolicyVersion(policyId: 9, versionNumber: 1, body: 'Other', id: 7);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'edit_version',
|
|
'policy_id' => '4',
|
|
'version_id' => '7',
|
|
'body' => 'Injected text',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([]);
|
|
$this->versions->shouldReceive('findById')->with(7)->andReturn($other);
|
|
|
|
$this->service->shouldNotReceive('addDraftVersion');
|
|
$this->versions->shouldNotReceive('updateBody');
|
|
|
|
$this->render();
|
|
}
|
|
|
|
public function testRenamingAPolicyUpdatesTheTitleAndLeavesTheSlugAlone(): void
|
|
{
|
|
$policy = new Policy('Studio Policy', 'studio-policy', id: 4);
|
|
$renamed = new Policy('Terms of Enrolment', 'studio-policy', id: 4);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'rename_policy',
|
|
'policy_id' => '4',
|
|
'title' => 'Terms of Enrolment',
|
|
];
|
|
|
|
// The lookups that guard the action see the old title; the page is
|
|
// rendered from a fresh read, so it shows the new one.
|
|
$this->policies->shouldReceive('findById')->with(4)->once()->andReturn($policy);
|
|
$this->policies->shouldReceive('updateTitle')->once()->with(4, 'Terms of Enrolment')->andReturn(true);
|
|
$this->policies->shouldReceive('findAll')->andReturn([$renamed]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($renamed);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([]);
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringContainsString('Policy renamed to "Terms of Enrolment"', $html);
|
|
self::assertStringContainsString('Versions of "Terms of Enrolment"', $html);
|
|
}
|
|
|
|
public function testRenamingAPolicyToNothingIsRejected(): void
|
|
{
|
|
$policy = new Policy('Studio Policy', 'studio-policy', id: 4);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'rename_policy',
|
|
'policy_id' => '4',
|
|
'title' => ' ',
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([]);
|
|
|
|
$this->policies->shouldNotReceive('updateTitle');
|
|
|
|
$html = $this->render();
|
|
|
|
self::assertStringContainsString('Versions of "Studio Policy"', $html);
|
|
}
|
|
|
|
public function testRenamingAPolicyBeyondTheColumnLengthIsRejected(): void
|
|
{
|
|
$policy = new Policy('Studio Policy', 'studio-policy', id: 4);
|
|
|
|
$_GET = ['policy_id' => '4'];
|
|
$_POST = [
|
|
'usc_action' => 'rename_policy',
|
|
'policy_id' => '4',
|
|
'title' => str_repeat('a', Policy::MAX_TITLE_LENGTH + 1),
|
|
];
|
|
|
|
$this->policies->shouldReceive('findAll')->andReturn([$policy]);
|
|
$this->policies->shouldReceive('findById')->with(4)->andReturn($policy);
|
|
$this->versions->shouldReceive('findByPolicy')->with(4)->andReturn([]);
|
|
|
|
$this->policies->shouldNotReceive('updateTitle');
|
|
|
|
$this->render();
|
|
}
|
|
|
|
private function render(): string
|
|
{
|
|
ob_start();
|
|
$this->controller->renderPage();
|
|
|
|
return (string) ob_get_clean();
|
|
}
|
|
}
|