withoutVite(); $this->seed(RolesAndPermissionsSeeder::class); Setting::firstOrCreate([], ['congregazione_nome' => 'Congregazione di prova']); } private function utente(string $ruolo = 'operatore', bool $confermato = false): User { $utente = User::create([ 'name' => 'Mario Rossi', 'email' => $ruolo . '@example.test', 'password' => 'password-di-prova', ]); $utente->assignRole($ruolo); if ($confermato) { $utente->markEmailAsVerified(); } return $utente->fresh(); } private function linkDiConferma(User $utente, ?string $email = null): string { return URL::temporarySignedRoute('verification.verify', now()->addHour(), [ 'id' => $utente->id, 'hash' => sha1($email ?? $utente->email), ]); } public function test_senza_conferma_l_account_non_funziona(): void { $utente = $this->utente(); $this->actingAs($utente)->get(route('dashboard'))->assertRedirect(route('verification.notice')); $this->actingAs($utente)->get(route('territori.index'))->assertRedirect(route('verification.notice')); $this->actingAs($utente)->get(route('verification.notice'))->assertOk()->assertSee('Conferma il tuo indirizzo email'); $this->actingAs($utente)->get(route('profile'))->assertOk(); } public function test_un_amministratore_e_esente_dalla_conferma(): void { $admin = $this->utente('amministratore'); $this->assertNull($admin->email_verified_at); $this->assertTrue($admin->hasVerifiedEmail()); $this->actingAs($admin)->get(route('settings.edit'))->assertOk(); $this->actingAs($admin)->get(route('posta.edit'))->assertOk()->assertSee('Server di posta'); } public function test_chi_perde_il_ruolo_di_amministratore_torna_a_dover_confermare(): void { $utente = $this->utente('amministratore'); $utente->syncRoles(['operatore']); $this->assertFalse($utente->fresh()->hasVerifiedEmail()); $this->actingAs($utente->fresh())->get(route('dashboard'))->assertRedirect(route('verification.notice')); } public function test_all_amministratore_creato_non_parte_la_conferma(): void { Notification::fake(); $admin = $this->utente('amministratore'); Livewire::actingAs($admin)->test(UsersIndex::class) ->set('name', 'Luca Neri') ->set('email', 'luca@example.test') ->set('password', 'password-di-prova') ->set('password_confirmation', 'password-di-prova') ->set('selectedRole', 'amministratore') ->call('createUser') ->assertHasNoErrors(); $nuovo = User::where('email', 'luca@example.test')->sole(); $this->assertTrue($nuovo->hasVerifiedEmail()); Notification::assertNothingSentTo($nuovo); } public function test_l_amministratore_che_cambia_email_resta_dentro(): void { Notification::fake(); $admin = $this->utente('amministratore'); Livewire::actingAs($admin)->test(Profile::class) ->set('name', 'Mario Rossi') ->set('email', 'nuovo-admin@example.test') ->call('updateProfile') ->assertRedirect(route('profile')); $this->assertTrue($admin->fresh()->hasVerifiedEmail()); Notification::assertNothingSentTo($admin->fresh()); } public function test_il_link_conferma_anche_senza_essere_entrati(): void { $utente = $this->utente(); $this->get($this->linkDiConferma($utente))->assertRedirect(route('login')); $this->assertTrue($utente->fresh()->hasVerifiedEmail()); $this->actingAs($utente->fresh())->get(route('dashboard'))->assertOk(); } public function test_un_link_alterato_o_di_un_altro_indirizzo_non_conferma(): void { $utente = $this->utente(); $link = $this->linkDiConferma($utente); $this->get(str_replace('/' . $utente->id . '/', '/999/', $link))->assertForbidden(); $this->get($this->linkDiConferma($utente, 'vecchio@example.test'))->assertForbidden(); $this->assertFalse($utente->fresh()->hasVerifiedEmail()); } public function test_richiedere_l_email_la_manda(): void { Notification::fake(); $utente = $this->utente(); $this->actingAs($utente)->post(route('verification.send'))->assertSessionHas('success'); Notification::assertSentTo($utente, VerifyEmail::class); } public function test_l_amministratore_crea_un_utente_e_gli_parte_la_conferma(): void { Notification::fake(); $admin = $this->utente('amministratore', confermato: true); Livewire::actingAs($admin)->test(UsersIndex::class) ->set('name', 'Giulia Bianchi') ->set('email', 'giulia@example.test') ->set('password', 'password-di-prova') ->set('password_confirmation', 'password-di-prova') ->set('selectedRole', 'operatore') ->call('createUser') ->assertHasNoErrors() ->assertDispatched('avviso'); $nuovo = User::where('email', 'giulia@example.test')->sole(); $this->assertFalse($nuovo->hasVerifiedEmail()); Notification::assertSentTo($nuovo, VerifyEmail::class); } public function test_cambiare_email_la_rende_da_confermare(): void { Notification::fake(); $utente = $this->utente(confermato: true); Livewire::actingAs($utente)->test(Profile::class) ->set('name', 'Mario Rossi') ->set('email', 'nuovo@example.test') ->call('updateProfile') ->assertRedirect(route('verification.notice')); $this->assertFalse($utente->fresh()->hasVerifiedEmail()); Notification::assertSentTo($utente->fresh(), VerifyEmail::class); } public function test_cambiare_solo_il_nome_non_tocca_la_conferma(): void { $utente = $this->utente(confermato: true); Livewire::actingAs($utente)->test(Profile::class) ->set('name', 'Mario Verdi') ->set('email', $utente->email) ->call('updateProfile'); $this->assertTrue($utente->fresh()->hasVerifiedEmail()); } public function test_il_comando_di_emergenza_conferma_un_utente(): void { $utente = $this->utente(); $this->artisan('utenti:conferma-email', ['email' => $utente->email])->assertSuccessful(); $this->assertTrue($utente->fresh()->hasVerifiedEmail()); } public function test_il_primo_amministratore_da_riga_di_comando_nasce_confermato(): void { $this->artisan('app:create-initial-admin', [ '--name' => 'Admin', '--email' => 'primo@example.test', '--password' => 'password-di-prova', ])->assertSuccessful(); $this->assertTrue(User::where('email', 'primo@example.test')->sole()->hasVerifiedEmail()); } }