Files
unsupervised-scheduler/tests/Unit/Auth/InviteRepositoryTest.php
thatguygriffandClaude Opus 4.8 a281935811
CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 49s
CI / No Debug Code (pull_request) Successful in 2s
CI / PHPStan (pull_request) Successful in 2m49s
CI / Coding Standards (pull_request) Successful in 2m55s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m40s
CI / Build Plugin Zip (pull_request) Skipped
Add invite-only group classes
Group classes can now be marked invite-only (us_offerings.access_mode).
Invite-only classes are hidden from the public catalog and reachable only
when the instructor lets someone in via one of three paths, managed from
My Lessons -> My Group Classes:

- Add students directly: enrols them now with a pending payment.
- Make available: grants registered students access to self-enrol through
  the normal paid flow (multi-select, emailed a notice).
- Invite by email: tokenised registration invite tied to the class for a
  non-account address; after they register the class becomes enrollable.
  Reuses an existing pending invite instead of sending a second link.

New us_group_access table records grants; GET /offerings merges granted
invite-only classes for the caller; enrolment requires a grant
(403 invite_required) and flips it to enrolled on success.

composer test (487), composer lint, composer cs all pass.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-23 13:51:02 -03:00

174 lines
5.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Auth\Invite;
use Unsupervised\Schedular\Auth\InviteRepository;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class InviteRepositoryTest extends TestCase
{
private \wpdb $db;
private InviteRepository $repo;
protected function setUp(): void
{
parent::setUp();
$this->db = Mockery::mock(\wpdb::class);
$this->db->prefix = 'wp_';
$this->repo = new InviteRepository($this->db);
}
public function testInsertReturnsId(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-06-02 09:00:00');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_invites',
Mockery::on(static function (array $d): bool {
return $d['email'] === '[email protected]'
&& $d['token'] === 'tok123'
&& $d['kind'] === Invite::KIND_PERSONAL
&& $d['offering_id'] === null
&& $d['status'] === Invite::STATUS_PENDING
&& $d['invited_by'] === 2
&& $d['expires_at'] === null;
}),
['%s', '%s', '%s', '%s', '%d', '%s', '%d', '%d', '%s', '%s', '%s']
);
$this->db->insert_id = 5;
self::assertSame(5, $this->repo->insert(new Invite('[email protected]', 'tok123', invitedBy: 2)));
}
public function testInsertReturnsZeroWhenDbInsertFails(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-06-02 09:00:00');
$this->db->shouldReceive('insert')->once()->andReturn(false);
$this->db->insert_id = 99; // Stale id from an earlier insert must not leak out.
self::assertSame(0, $this->repo->insert(new Invite('[email protected]', 'tok123')));
}
public function testInsertPersistsGroupKindAndExpiry(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-06-02 09:00:00');
$this->db->shouldReceive('insert')
->once()
->with(
'wp_us_invites',
Mockery::on(static function (array $d): bool {
return $d['email'] === ''
&& $d['kind'] === Invite::KIND_GROUP
&& $d['expires_at'] === '2026-08-31 23:59:59';
}),
Mockery::type('array')
);
$this->db->insert_id = 6;
$invite = new Invite('', 'tok456', invitedBy: 2, kind: Invite::KIND_GROUP, expiresAt: '2026-08-31 23:59:59');
self::assertSame(6, $this->repo->insert($invite));
}
public function testFindByTokenReturnsInvite(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/token = %s/'), 'wp_us_invites', 'tok123')
->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn($this->row());
$invite = $this->repo->findByToken('tok123');
self::assertInstanceOf(Invite::class, $invite);
self::assertSame('[email protected]', $invite->email);
}
public function testFindByTokenReturnsNullWhenMissing(): void
{
$this->db->shouldReceive('prepare')->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn(null);
self::assertNull($this->repo->findByToken('nope'));
}
public function testFindPendingByEmailFiltersStatus(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/email = %s AND status = %s/'), 'wp_us_invites', '[email protected]', Invite::STATUS_PENDING)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_row')->andReturn($this->row());
self::assertInstanceOf(Invite::class, $this->repo->findPendingByEmail('[email protected]'));
}
public function testFindPendingMapsRows(): void
{
$this->db->shouldReceive('prepare')
->once()
->with(Mockery::pattern('/status = %s/'), 'wp_us_invites', Invite::STATUS_PENDING)
->andReturn('SELECT ...');
$this->db->shouldReceive('get_results')->andReturn([$this->row()]);
$pending = $this->repo->findPending();
self::assertCount(1, $pending);
self::assertInstanceOf(Invite::class, $pending[0]);
}
public function testMarkAcceptedUpdatesRow(): void
{
Functions\expect('current_time')->with('mysql')->andReturn('2026-06-02 10:00:00');
$this->db->shouldReceive('update')
->once()
->with(
'wp_us_invites',
Mockery::on(static fn (array $d): bool => $d['status'] === Invite::STATUS_ACCEPTED && $d['accepted_user_id'] === 9),
['id' => 5],
['%s', '%d', '%s'],
['%d']
)
->andReturn(1);
self::assertTrue($this->repo->markAccepted(5, 9));
}
public function testRevokeUpdatesStatus(): void
{
$this->db->shouldReceive('update')
->once()
->with('wp_us_invites', ['status' => Invite::STATUS_REVOKED], ['id' => 5], ['%s'], ['%d'])
->andReturn(1);
self::assertTrue($this->repo->revoke(5));
}
private function row(): object
{
return (object) [
'id' => '5',
'email' => '[email protected]',
'token' => 'tok123',
'role' => 'us_student',
'status' => Invite::STATUS_PENDING,
'invited_by' => '2',
'accepted_user_id' => null,
'accepted_at' => null,
];
}
}