Files
unsupervised-scheduler/tests/Unit/Auth/RegistrationMailerTest.php
T
thatguygriffandClaude Opus 4.8 7370755951
CI / Tests (PHP 8.1) (pull_request) Successful in 1m18s
CI / Tests (PHP 8.2) (pull_request) Successful in 1m18s
CI / No Debug Code (pull_request) Successful in 2s
CI / PHPStan (pull_request) Successful in 3m20s
CI / Coding Standards (pull_request) Successful in 3m25s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m33s
CI / Build Plugin Zip (pull_request) Skipped
Add open student registration with email confirmation and approval
Students could previously join by invite only. Add an optional
self-approval mode, toggled from Studio Settings → Registration: anyone
may sign up on the existing [us_student_register] page, confirm their
email via a tokenised link, and then be approved by a studio admin
before the account is usable.

- Enabling the toggle mirrors WordPress's own membership settings
  (users_can_register + default_role = us_student) and snapshots their
  previous values so disabling restores them.
- WordPress's native registration form is blocked while open
  registration is on (login_init redirect + registration_errors
  fail-safe + register_url) so it cannot bypass signup policy acceptance.
- Pending accounts: unconfirmed email cannot log in; confirmed but
  unapproved can log in but the booking capability is withheld and the
  booking page shows an "awaiting approval" screen.
- Approve/reject from Students → Pending Students; reject hard-deletes
  the account so the email is freed to re-apply.
- Invite registration is unchanged; both modes coexist.

Account lifecycle lives in user meta (RegistrationStatus); no new tables.

Closes #63

Co-Authored-By: Claude Opus 4.8 <[email protected]>
2026-07-18 10:50:21 -03:00

85 lines
2.7 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Mockery;
use Unsupervised\Schedular\Auth\RegistrationMailer;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class RegistrationMailerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Functions\when('get_bloginfo')->justReturn('Test Studio');
Functions\when('wp_login_url')->justReturn('http://example.test/login');
}
private function user(string $email): \WP_User
{
$user = Mockery::mock(\WP_User::class);
$user->user_email = $email;
$user->display_name = 'Ada';
return $user;
}
public function testSendConfirmationEmailsTheStudent(): void
{
Functions\expect('wp_mail')
->once()
->with(
'[email protected]',
Mockery::type('string'),
Mockery::on(static fn (string $body): bool => str_contains($body, 'http://confirm.test'))
)
->andReturn(true);
self::assertTrue((new RegistrationMailer())->sendConfirmation($this->user('[email protected]'), 'http://confirm.test'));
}
public function testSendConfirmationReturnsFalseWithoutRecipient(): void
{
self::assertFalse((new RegistrationMailer())->sendConfirmation($this->user(''), 'http://confirm.test'));
}
public function testNotifyAdminsPendingUsesAdminEmail(): void
{
Functions\when('get_option')->alias(static fn (string $name) => $name === 'admin_email' ? '[email protected]' : '');
Functions\expect('wp_mail')
->once()
->with('[email protected]', Mockery::type('string'), Mockery::type('string'))
->andReturn(true);
self::assertTrue((new RegistrationMailer())->notifyAdminsPending($this->user('[email protected]')));
}
public function testSendApprovedEmailsTheStudent(): void
{
Functions\expect('wp_mail')
->once()
->with('[email protected]', Mockery::type('string'), Mockery::type('string'))
->andReturn(true);
self::assertTrue((new RegistrationMailer())->sendApproved($this->user('[email protected]')));
}
public function testSendRejectedEmailsTheAddress(): void
{
Functions\expect('wp_mail')
->once()
->with('[email protected]', Mockery::type('string'), Mockery::type('string'))
->andReturn(true);
self::assertTrue((new RegistrationMailer())->sendRejected('[email protected]'));
}
public function testSendRejectedReturnsFalseWithoutRecipient(): void
{
self::assertFalse((new RegistrationMailer())->sendRejected(''));
}
}