Files
termanager2/tests/Feature/ConfermaEmailTest.php
Francesco Picone fc8623f7db Posta configurabile dall'app, contatti dei proclamatori, conferma email
- Server di posta in Impostazioni (password cifrata, email di prova): se
  compilato prende il posto del .env; non si esporta e l'import XML non lo
  cancella. MAIL_SCHEME=tls nel .env.example non era valido per Laravel 11.
- Proclamatori con email e telefono facoltativi, cifrati, nell'XML e
  cancellati dall'anonimizzazione; il numero apre WhatsApp.
- Dopo l'assegnazione un riquadro in evidenza con il link del PDF,
  "Invia su WhatsApp" con il messaggio pronto e la scelta "inviato /
  non ancora", che resta finché non si sceglie.
- Conferma dell'email obbligatoria per tutti gli utenti, anche esistenti:
  email in italiano, link valido senza login, nuova conferma a ogni cambio
  di indirizzo, "Reinvia conferma" in Utenti. Senza conferma restano
  raggiungibili profilo, cambio password e, per l'admin, il server di
  posta; comando utenti:conferma-email per l'emergenza.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-12 17:12:31 +02:00

175 lines
5.9 KiB
PHP

<?php
namespace Tests\Feature;
use App\Livewire\Profile;
use App\Livewire\Settings\UsersIndex;
use App\Models\Setting;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\URL;
use Livewire\Livewire;
use Tests\TestCase;
/**
* Un account funziona solo con l'indirizzo email confermato.
*/
class ConfermaEmailTest extends TestCase
{
use RefreshDatabase;
protected bool $confermaUtentiCreati = false;
protected function setUp(): void
{
parent::setUp();
$this->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_non_confermato_puo_configurare_la_posta(): void
{
$admin = $this->utente('amministratore');
$this->actingAs($admin)->get(route('settings.edit'))->assertRedirect(route('verification.notice'));
$this->actingAs($admin)->get(route('posta.edit'))->assertOk()->assertSee('Server di posta');
}
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());
}
}