Files
unsupervised-scheduler/tests/Unit/Auth/InviteRepositoryTest.php
T
thatguygriffandClaude Fable 5 356d9f984d
CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 46s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 2m46s
CI / PHPStan (pull_request) Successful in 2m51s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m38s
CI / Build Plugin Zip (pull_request) Skipped
Add multi-use group invite links with expiry and auto-approval on email confirmation
A studio admin can generate a shareable group invite link (e.g. for a
newsletter) from the Invites page, choosing a required expiry date. Anyone
with the link may register while it is valid, in any registration mode: the
form collects their own email, they must confirm it via the usual hashed
token, and confirming approves the account immediately — group signups never
enter the Pending Students queue.

- us_invites grows kind (personal/group) and expires_at; an explicit expiry
  wins over the personal 14-day window. Group links stay pending (multi-use)
  until revoked or expired.
- RegistrationPage: group signups create the account pending with the
  us_auto_approve marker and send the confirmation email; no auto-login.
- EmailConfirmationHandler: auto-approve accounts are approved on
  confirmation, emailed the approved notice, and redirected to a new
  us_confirmed=ready notice with a sign-in link.

Closes #77

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-07-22 10:44:16 -03:00

163 lines
5.3 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['status'] === Invite::STATUS_PENDING
&& $d['invited_by'] === 2
&& $d['expires_at'] === null;
}),
['%s', '%s', '%s', '%s', '%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 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,
];
}
}