withoutVite(); $this->seed(RolesAndPermissionsSeeder::class); Setting::firstOrCreate([], ['congregazione_nome' => 'Congregazione di prova']); $this->admin = User::create(['name' => 'Admin', 'email' => 'admin@example.test', 'password' => 'password-di-prova']); $this->admin->assignRole('amministratore'); } private function salva(array $valori = []): void { $componente = Livewire::actingAs($this->admin)->test(PostaElettronica::class); foreach ($valori + [ 'host' => 'smtp.esempio.it', 'porta' => '587', 'cifratura' => ConfigurazionePosta::CIFRATURA_AUTOMATICA, 'utente' => 'territori@esempio.it', 'password' => 'segreta-123', 'mittente' => 'territori@esempio.it', 'nomeMittente' => 'Territori', ] as $campo => $valore) { $componente->set($campo, $valore); } $componente->call('salva')->assertHasNoErrors(); } public function test_la_pagina_e_dell_amministratore(): void { $operatore = User::create(['name' => 'Op', 'email' => 'op@example.test', 'password' => 'password-di-prova']); $operatore->assignRole('operatore'); $this->actingAs($operatore)->get(route('posta.edit'))->assertForbidden(); $this->flushSession(); // due utenti nella stessa sessione: AuthenticateSession $this->actingAs($this->admin)->get(route('posta.edit'))->assertOk(); } public function test_la_configurazione_salvata_prende_il_posto_del_env(): void { config(['mail.default' => 'log']); $this->salva(); app(ConfigurazionePosta::class)->applica(); $this->assertSame('smtp', config('mail.default')); $this->assertSame('smtp.esempio.it', config('mail.mailers.smtp.host')); $this->assertSame(587, config('mail.mailers.smtp.port')); $this->assertSame('smtp', config('mail.mailers.smtp.scheme')); $this->assertSame('segreta-123', config('mail.mailers.smtp.password')); $this->assertSame('territori@esempio.it', config('mail.from.address')); } public function test_ssl_usa_smtps_e_la_porta_465(): void { $this->salva(['cifratura' => ConfigurazionePosta::CIFRATURA_SSL, 'porta' => '']); app(ConfigurazionePosta::class)->applica(); $this->assertSame('smtps', config('mail.mailers.smtp.scheme')); $this->assertSame(465, config('mail.mailers.smtp.port')); } public function test_la_password_e_cifrata_e_non_torna_al_browser(): void { $this->salva(); $grezza = DB::table('settings')->value('mail_password'); $this->assertNotSame('segreta-123', $grezza); $this->assertStringNotContainsString('segreta', $grezza); Livewire::actingAs($this->admin)->test(PostaElettronica::class) ->assertSet('password', '') ->assertDontSee('segreta-123'); } public function test_lasciare_vuota_la_password_tiene_quella_salvata(): void { $this->salva(); $this->salva(['password' => '', 'host' => 'smtp.altro.it']); Setting::flushCache(); $this->assertSame('segreta-123', Setting::instance()->mail_password); $this->assertSame('smtp.altro.it', Setting::instance()->mail_host); } public function test_l_email_di_prova_parte(): void { Mail::fake(); $this->salva(); Livewire::actingAs($this->admin)->test(PostaElettronica::class) ->call('inviaProva') ->assertDispatched('avviso', tipo: 'ok'); } public function test_l_export_xml_non_contiene_la_posta_e_l_import_non_la_cancella(): void { $this->salva(); $this->actingAs($this->admin); $xml = (new XmlExchange())->currentXmlBackup(); $this->assertStringNotContainsString('smtp.esempio.it', $xml); $this->assertStringNotContainsString('mail_host', $xml); $this->assertStringNotContainsString('segreta', $xml); $esito = (new XmlExchange())->importXmlFromContent($xml); $this->assertArrayNotHasKey('error', $esito); Setting::flushCache(); $this->assertSame('smtp.esempio.it', Setting::instance()->mail_host); $this->assertSame('segreta-123', Setting::instance()->mail_password); } public function test_rimuovere_torna_al_env(): void { $this->salva(); Livewire::actingAs($this->admin)->test(PostaElettronica::class)->call('rimuovi'); Setting::flushCache(); $this->assertNull(Setting::instance()->mail_host); $this->assertFalse(app(ConfigurazionePosta::class)->configurataNellApp()); } }