Files
unsupervised-scheduler/tests/Unit/Auth/RegistrationStatusTest.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

138 lines
5.1 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\RegistrationStatus;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class RegistrationStatusTest extends TestCase
{
public function testMarkPendingIssuesHashedTokenAndSetsFlags(): void
{
Functions\when('wp_generate_password')->justReturn('rawtoken');
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1');
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_CONFIRM_TOKEN, hash('sha256', 'rawtoken'));
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_CONFIRM_EXPIRES, \Mockery::type('string'));
self::assertSame('rawtoken', RegistrationStatus::markPending(7));
}
public function testConfirmEmailSetsFlagAndClearsToken(): void
{
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_EMAIL_CONFIRMED, '1');
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_CONFIRM_TOKEN);
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_CONFIRM_EXPIRES);
RegistrationStatus::confirmEmail(7);
}
public function testApproveClearsPendingFlags(): void
{
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_AWAITING_APPROVAL);
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_CONFIRM_TOKEN);
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_CONFIRM_EXPIRES);
Functions\expect('delete_user_meta')->once()->with(7, RegistrationStatus::META_AUTO_APPROVE);
RegistrationStatus::approve(7);
}
public function testMarkPendingWithAutoApproveSetsMarkerMeta(): void
{
Functions\when('wp_generate_password')->justReturn('rawtoken');
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_AWAITING_APPROVAL, '1');
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_CONFIRM_TOKEN, hash('sha256', 'rawtoken'));
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_CONFIRM_EXPIRES, \Mockery::type('string'));
Functions\expect('update_user_meta')
->once()
->with(7, RegistrationStatus::META_AUTO_APPROVE, '1');
self::assertSame('rawtoken', RegistrationStatus::markPending(7, true));
}
public function testIsAutoApproveReadsMeta(): void
{
Functions\when('get_user_meta')->alias(static function (int $id, string $key) {
return $key === RegistrationStatus::META_AUTO_APPROVE ? '1' : '';
});
self::assertTrue(RegistrationStatus::isAutoApprove(7));
self::assertFalse(RegistrationStatus::isAwaitingApproval(7));
}
public function testAwaitingApprovalAndEmailConfirmedReadMeta(): void
{
Functions\when('get_user_meta')->alias(static function (int $id, string $key) {
return $key === RegistrationStatus::META_AWAITING_APPROVAL ? '1' : '';
});
self::assertTrue(RegistrationStatus::isAwaitingApproval(7));
self::assertFalse(RegistrationStatus::emailConfirmed(7));
}
public function testUserIdForTokenLooksUpByHashOnly(): void
{
Functions\expect('get_users')
->once()
->with(\Mockery::on(static fn (array $args): bool =>
$args['meta_key'] === RegistrationStatus::META_CONFIRM_TOKEN
&& $args['meta_value'] === hash('sha256', 'rawtoken')))
->andReturn([9]);
self::assertSame(9, RegistrationStatus::userIdForToken('rawtoken'));
}
public function testUserIdForTokenReturnsNullWhenNoMatch(): void
{
Functions\when('get_users')->justReturn([]);
self::assertNull(RegistrationStatus::userIdForToken('rawtoken'));
}
public function testEmptyTokenShortCircuits(): void
{
// get_users must never be called for an empty token.
Functions\expect('get_users')->never();
self::assertNull(RegistrationStatus::userIdForToken(''));
}
public function testTokenExpiredWhenExpiryPassed(): void
{
Functions\when('get_user_meta')->justReturn('2020-01-01 00:00:00');
self::assertTrue(RegistrationStatus::isTokenExpired(7, '2020-01-02 00:00:00'));
}
public function testTokenNotExpiredWhenExpiryInFuture(): void
{
Functions\when('get_user_meta')->justReturn('2020-01-03 00:00:00');
self::assertFalse(RegistrationStatus::isTokenExpired(7, '2020-01-02 00:00:00'));
}
public function testMissingExpiryTreatedAsExpired(): void
{
Functions\when('get_user_meta')->justReturn('');
self::assertTrue(RegistrationStatus::isTokenExpired(7, '2020-01-02 00:00:00'));
}
}