enable us to disable registration

This commit is contained in:
Mark Gregory
2026-11-16 09:45:47 +00:00
parent e2fe2c7c05
commit ff2ef836c6
3 changed files with 72 additions and 1 deletions
@@ -0,0 +1,68 @@
<?php
use App\Models\User;
use Illuminate\Foundation\Application;
trait BootsWithPublicRegistrationDisabled
{
public function createApplication(): Application
{
$previous = $_SERVER['PUBLIC_REGISTRATION_ENABLED'] ?? null;
$_SERVER['PUBLIC_REGISTRATION_ENABLED'] = 'false';
try {
return parent::createApplication();
} finally {
if ($previous === null) {
unset($_SERVER['PUBLIC_REGISTRATION_ENABLED']);
} else {
$_SERVER['PUBLIC_REGISTRATION_ENABLED'] = $previous;
}
}
}
}
uses(BootsWithPublicRegistrationDisabled::class);
test('guests cannot open the public registration form', function () {
$this->get('/register')->assertNotFound();
});
test('public registration rejects form and JSON submissions without creating a user', function (bool $json) {
$credentials = [
'name' => 'Public Signup',
'email' => 'signup@example.com',
'password' => 'password',
'password_confirmation' => 'password',
];
$response = $json
? $this->postJson('/register', $credentials)
: $this->post('/register', $credentials);
$response->assertNotFound();
$this->assertDatabaseMissing('users', ['email' => $credentials['email']]);
$this->assertGuest();
})->with(['form' => [false], 'JSON' => [true]]);
test('login and welcome views hide their registration links', function () {
$registrationLink = 'href="'.url('/register').'"';
$this->get('/login')->assertOk()->assertDontSee($registrationLink, false);
$this->view('stylist-theme-frontend::welcome')->assertDontSee($registrationLink, false);
});
test('administrators can create users while public registration is disabled', function () {
$admin = User::factory()->create(['helpdesk_admin' => true]);
$this->actingAs($admin)->post(route('user-management.store'), [
'name' => 'Provisioned User',
'email' => 'provisioned@example.com',
'password' => 'password',
'password_confirmation' => 'password',
])->assertSessionHasNoErrors()->assertRedirect();
$user = User::query()->where('email', 'provisioned@example.com')->sole();
expect($user->hasVerifiedEmail())->toBeTrue();
$this->assertAuthenticatedAs($admin);
});