Add open student registration with email confirmation and approval
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

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]>
This commit is contained in:
2026-07-18 10:50:21 -03:00
co-authored by Claude Opus 4.8
parent e7d8257973
commit 7370755951
23 changed files with 1713 additions and 88 deletions
@@ -0,0 +1,105 @@
<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Tests\Unit\Auth;
use Brain\Monkey\Functions;
use Unsupervised\Schedular\Auth\EmailConfirmationHandler;
use Unsupervised\Schedular\Auth\RegistrationController;
use Unsupervised\Schedular\Auth\RegistrationMailer;
use Unsupervised\Schedular\Payment\StudioSettings;
use Unsupervised\Schedular\Tests\Unit\TestCase;
class EmailConfirmationHandlerTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
Functions\when('wp_unslash')->alias(static fn ($v) => $v);
Functions\when('sanitize_key')->alias(static fn ($v) => $v);
}
protected function tearDown(): void
{
unset($_REQUEST['action']);
parent::tearDown();
}
private function handler(): EmailConfirmationHandler
{
return new EmailConfirmationHandler(new StudioSettings(), new RegistrationMailer());
}
private function stubMode(string $mode): void
{
Functions\when('get_option')->alias(static function (string $name, $default = false) use ($mode) {
if ($name === StudioSettings::OPT_REGISTRATION_MODE) {
return $mode;
}
if ($name === RegistrationController::OPTION_PAGE) {
return 5;
}
return $default;
});
}
public function testRegisterUrlPassesThroughWhenClosed(): void
{
$this->stubMode(StudioSettings::MODE_INVITE);
self::assertSame('http://wp/register', $this->handler()->registerUrl('http://wp/register'));
}
public function testRegisterUrlPointsAtRegistrationPageWhenOpen(): void
{
$this->stubMode(StudioSettings::MODE_SELF_APPROVAL);
Functions\when('get_permalink')->justReturn('http://studio.test/register');
self::assertSame('http://studio.test/register', $this->handler()->registerUrl('http://wp/register'));
}
public function testBlockNativeRegistrationIsNoOpWhenClosed(): void
{
$this->stubMode(StudioSettings::MODE_INVITE);
$_REQUEST['action'] = 'register';
// Must not redirect/exit when open registration is off.
Functions\expect('wp_safe_redirect')->never();
$this->handler()->blockNativeRegistration();
unset($_REQUEST['action']);
self::assertTrue(true);
}
public function testBlockNativeRegistrationIgnoresOtherActions(): void
{
$this->stubMode(StudioSettings::MODE_SELF_APPROVAL);
$_REQUEST['action'] = 'lostpassword';
Functions\expect('wp_safe_redirect')->never();
$this->handler()->blockNativeRegistration();
unset($_REQUEST['action']);
self::assertTrue(true);
}
public function testBlockRegistrationErrorsRejectsWhenOpen(): void
{
$this->stubMode(StudioSettings::MODE_SELF_APPROVAL);
$errors = $this->handler()->blockRegistrationErrors(new \WP_Error());
self::assertSame('us_registration_redirect', $errors->get_error_code());
}
public function testBlockRegistrationErrorsPassesThroughWhenClosed(): void
{
$this->stubMode(StudioSettings::MODE_INVITE);
$errors = $this->handler()->blockRegistrationErrors(new \WP_Error());
self::assertSame('', $errors->get_error_code());
}
}