Files
unsupervised-scheduler/tests/Unit/Policy/PolicyRepositoryTest.php
T
thatguygriffandClaude Opus 5 cb347ffca0
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
Demo follow-ups: editable policy name, one-page signup, group classes in upcoming lessons, deletion cleanup
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]>
2026-07-30 11:45:04 -03:00

135 lines
4.4 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\PolicyRepository;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class PolicyRepositoryTest extends TestCase
{
private \wpdb $db;
private PolicyRepository $repo;
protected function setUp(): void
{
parent::setUp();
$this->db = Mockery::mock(\wpdb::class);
$this->db->prefix = 'wp_';
$this->repo = new PolicyRepository($this->db);
}
public function testInsertReturnsId(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-06-01 12:00:00');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_policies',
Mockery::on(static fn (array $d): bool => $d['title'] === 'Cancellation' && $d['slug'] === 'cancellation' && $d['current_version_id'] === null && $d['acceptance_scope'] === Policy::SCOPE_BOOKING),
['%s', '%s', '%d', '%s', '%s']
);
$this->db->insert_id = 7;
self::assertSame(7, $this->repo->insert(new Policy('Cancellation', 'cancellation')));
}
public function testFindForScopeMatchesScopeOrBoth(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(
Mockery::pattern('/acceptance_scope = %s OR acceptance_scope = %s/'),
'wp_us_policies',
Policy::SCOPE_SIGNUP,
Policy::SCOPE_BOTH
)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([
(object) ['id' => '1', 'title' => 'Terms', 'slug' => 'terms', 'current_version_id' => '5', 'acceptance_scope' => Policy::SCOPE_SIGNUP],
]);
$found = $this->repo->findForScope(Policy::SCOPE_SIGNUP);
self::assertCount(1, $found);
self::assertSame(Policy::SCOPE_SIGNUP, $found[0]->acceptanceScope);
}
public function testUpdateTitleWritesOnlyTheTitle(): void
{
$this->db->shouldReceive('update')
->once()
->with('wp_us_policies', ['title' => 'Terms of Enrolment'], ['id' => 7], ['%s'], ['%d'])
->andReturn(1);
self::assertTrue($this->repo->updateTitle(7, 'Terms of Enrolment'));
}
public function testUpdateTitleReportsFailure(): void
{
$this->db->shouldReceive('update')->once()->andReturn(false);
self::assertFalse($this->repo->updateTitle(7, 'Terms of Enrolment'));
}
public function testUpdateCurrentVersion(): void
{
$this->db->shouldReceive('update')
->once()
->with('wp_us_policies', ['current_version_id' => 9], ['id' => 7], ['%d'], ['%d'])
->andReturn(1);
self::assertTrue($this->repo->updateCurrentVersion(7, 9));
}
public function testFindBySlugReturnsPolicy(): void
{
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn((object) [
'id' => '7',
'title' => 'Cancellation',
'slug' => 'cancellation',
'current_version_id' => null,
'acceptance_scope' => Policy::SCOPE_BOOKING,
]);
$policy = $this->repo->findBySlug('cancellation');
self::assertInstanceOf(Policy::class, $policy);
self::assertSame('cancellation', $policy->slug);
}
public function testFindBySlugReturnsNullWhenMissing(): void
{
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn(null);
self::assertNull($this->repo->findBySlug('nope'));
}
public function testFindAllMapsRows(): void
{
$this->db->shouldReceive('get_results')->andReturn([
(object) ['id' => '1', 'title' => 'A', 'slug' => 'a', 'current_version_id' => null, 'acceptance_scope' => Policy::SCOPE_BOOKING],
]);
$all = $this->repo->findAll();
self::assertCount(1, $all);
self::assertInstanceOf(Policy::class, $all[0]);
}
public function testDeleteCallsWpdb(): void
{
$this->db->shouldReceive('delete')->once()->with('wp_us_policies', ['id' => 3], ['%d'])->andReturn(1);
self::assertTrue($this->repo->delete(3));
}
}