69 lines
2.3 KiB
PHP
69 lines
2.3 KiB
PHP
<?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);
|
|
});
|