190 lines
6.2 KiB
PHP
190 lines
6.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\PublicSite;
|
|
|
|
use App\Mail\ContactBriefing;
|
|
use App\Mail\ContactBriefingConfirmation;
|
|
use App\Models\SiteSetting;
|
|
use Illuminate\Foundation\Http\Middleware\PreventRequestForgery;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Routing\Middleware\ThrottleRequests;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Tests\TestCase;
|
|
|
|
class ContactBriefingTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->withoutMiddleware([ThrottleRequests::class, PreventRequestForgery::class]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $overrides
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function validPayload(array $overrides = []): array
|
|
{
|
|
return array_merge([
|
|
'nome' => 'Maria Silva',
|
|
'email' => 'maria@example.com',
|
|
'telefone' => '(11) 98888-7777',
|
|
'tipo_evento' => 'Casamento',
|
|
'data_periodo' => 'novembro de 2027',
|
|
'cidade' => 'São Paulo',
|
|
'convidados' => '120',
|
|
'servico_interesse' => 'Planejamento completo',
|
|
'mensagem' => 'Queremos um casamento ao ar livre para 120 convidados.',
|
|
'privacidade' => '1',
|
|
'empresa' => '',
|
|
], $overrides);
|
|
}
|
|
|
|
public function test_briefing_page_renders_event_form_with_all_fields(): void
|
|
{
|
|
SiteSetting::instance();
|
|
|
|
$response = $this->get(route('briefing'));
|
|
|
|
$response
|
|
->assertOk()
|
|
->assertSee('<form', false)
|
|
->assertSee('action="'.route('briefing.store').'"', false)
|
|
->assertSee('name="nome"', false)
|
|
->assertSee('name="email"', false)
|
|
->assertSee('name="telefone"', false)
|
|
->assertSee('name="tipo_evento"', false)
|
|
->assertSee('name="data_periodo"', false)
|
|
->assertSee('name="cidade"', false)
|
|
->assertSee('name="convidados"', false)
|
|
->assertSee('name="servico_interesse"', false)
|
|
->assertSee('name="mensagem"', false)
|
|
->assertSee('name="privacidade"', false)
|
|
->assertSee('name="empresa"', false)
|
|
->assertSee(route('privacy'), false);
|
|
}
|
|
|
|
public function test_valid_submission_sends_briefing_and_confirmation_emails(): void
|
|
{
|
|
$settings = SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$response = $this->post(route('briefing.store'), $this->validPayload());
|
|
|
|
$response
|
|
->assertRedirect(route('briefing'))
|
|
->assertSessionHas('status', 'briefing-sent');
|
|
|
|
Mail::assertQueued(ContactBriefing::class, function (ContactBriefing $mail) use ($settings): bool {
|
|
return $mail->hasTo($settings->email);
|
|
});
|
|
|
|
Mail::assertQueued(ContactBriefingConfirmation::class, function (ContactBriefingConfirmation $mail): bool {
|
|
return $mail->hasTo('maria@example.com');
|
|
});
|
|
}
|
|
|
|
public function test_submission_normalizes_email_and_phone_before_reaching_mailables(): void
|
|
{
|
|
$settings = SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$response = $this->post(route('briefing.store'), $this->validPayload([
|
|
'email' => ' Maria.Silva@EXAMPLE.com ',
|
|
'telefone' => '+55 (11) 98888-7777',
|
|
]));
|
|
|
|
$response
|
|
->assertRedirect(route('briefing'))
|
|
->assertSessionHas('status', 'briefing-sent');
|
|
|
|
Mail::assertQueued(ContactBriefing::class, function (ContactBriefing $mail) use ($settings): bool {
|
|
return $mail->hasTo($settings->email)
|
|
&& $mail->fields['E-mail'] === 'maria.silva@example.com'
|
|
&& $mail->fields['Telefone/WhatsApp'] === '(11) 98888-7777';
|
|
});
|
|
|
|
Mail::assertQueued(ContactBriefingConfirmation::class, function (ContactBriefingConfirmation $mail): bool {
|
|
return $mail->hasTo('maria.silva@example.com');
|
|
});
|
|
}
|
|
|
|
public function test_honeypot_submission_is_dropped_without_sending_emails(): void
|
|
{
|
|
SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$response = $this->post(route('briefing.store'), $this->validPayload([
|
|
'empresa' => 'http://spam.example',
|
|
]));
|
|
|
|
$response
|
|
->assertRedirect(route('briefing'))
|
|
->assertSessionHas('status', 'briefing-sent');
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
public function test_submission_requires_privacy_acceptance(): void
|
|
{
|
|
SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$response = $this->from(route('briefing'))->post(route('briefing.store'), $this->validPayload([
|
|
'privacidade' => '',
|
|
]));
|
|
|
|
$response->assertRedirect(route('briefing'));
|
|
$response->assertSessionHasErrors('privacidade');
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
public function test_invalid_submission_returns_validation_errors(): void
|
|
{
|
|
SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$response = $this->from(route('briefing'))->post(route('briefing.store'), []);
|
|
|
|
$response->assertRedirect(route('briefing'));
|
|
|
|
foreach (['nome', 'email', 'telefone', 'tipo_evento', 'cidade', 'mensagem', 'privacidade'] as $field) {
|
|
$response->assertSessionHasErrors($field);
|
|
}
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
public function test_duplicate_submission_is_not_sent_twice(): void
|
|
{
|
|
SiteSetting::instance();
|
|
Mail::fake();
|
|
|
|
$payload = $this->validPayload();
|
|
|
|
$this->post(route('briefing.store'), $payload)->assertRedirect(route('briefing'));
|
|
$this->post(route('briefing.store'), $payload)->assertRedirect(route('briefing'));
|
|
|
|
Mail::assertQueued(ContactBriefing::class, 1);
|
|
Mail::assertQueued(ContactBriefingConfirmation::class, 1);
|
|
}
|
|
|
|
public function test_email_failure_does_not_block_submission(): void
|
|
{
|
|
SiteSetting::instance();
|
|
config(['mail.default' => 'mailer-inexistente']);
|
|
|
|
$response = $this->post(route('briefing.store'), $this->validPayload());
|
|
|
|
$response
|
|
->assertRedirect(route('briefing'))
|
|
->assertSessionHas('status', 'briefing-sent');
|
|
}
|
|
}
|