This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Mail\ContactMessageReceivedMail;
|
||||
use Illuminate\Foundation\Testing\TestCase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
use RuntimeException;
|
||||
|
||||
// No database or external email: all transport calls are intercepted.
|
||||
class PortalContactTest extends TestCase
|
||||
{
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
config(['contact_messages.recipient' => 'tempo@example.invalid']);
|
||||
DB::shouldReceive('connection')->never();
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private function message(): array
|
||||
{
|
||||
return ['name' => 'Teste QA', 'email' => 'sender@example.invalid', 'subject' => 'Pedido de contacto',
|
||||
'message' => 'Mensagem de teste do adaptador local.'];
|
||||
}
|
||||
|
||||
public function test_the_original_fields_are_delivered_through_the_local_mail_transport(): void
|
||||
{
|
||||
Mail::fake();
|
||||
$this->postJson('/contacto/mensagem', [...$this->message(), 'platform' => 'Other'])
|
||||
->assertCreated()->assertJson(['success' => true]);
|
||||
Mail::assertSent(ContactMessageReceivedMail::class, fn (ContactMessageReceivedMail $mail) => $mail->hasTo('tempo@example.invalid') && $mail->contactMessage['platform'] === 'InvesteMoz'
|
||||
&& $mail->contactMessage['phone'] === null
|
||||
&& $mail->envelope()->replyTo[0]->address === 'sender@example.invalid'
|
||||
&& $mail->envelope()->subject === 'Pedido de contacto');
|
||||
Mail::assertSentCount(1);
|
||||
}
|
||||
|
||||
public function test_invalid_fields_are_rejected_before_sending(): void
|
||||
{
|
||||
Mail::fake();
|
||||
$this->postJson('/contacto/mensagem', ['name' => 'A', 'email' => 'invalid', 'subject' => 'AB', 'message' => 'short'])
|
||||
->assertUnprocessable()->assertJsonValidationErrors(['name', 'email', 'subject', 'message']);
|
||||
Mail::assertNothingSent();
|
||||
}
|
||||
|
||||
public function test_transport_failure_is_not_reported_as_success(): void
|
||||
{
|
||||
Mail::shouldReceive('to')->once()->with('tempo@example.invalid')->andReturnSelf();
|
||||
Mail::shouldReceive('send')->once()->andThrow(new RuntimeException('transport unavailable'));
|
||||
$this->postJson('/contacto/mensagem', $this->message())
|
||||
->assertStatus(500)->assertJson(['success' => false]);
|
||||
}
|
||||
|
||||
public function test_the_copied_email_template_escapes_user_content(): void
|
||||
{
|
||||
$mail = new ContactMessageReceivedMail([...$this->message(), 'phone' => null, 'platform' => 'InvesteMoz',
|
||||
'message' => '<script>alert(1)</script>']);
|
||||
$html = $mail->render();
|
||||
$this->assertStringContainsString('<script>', $html);
|
||||
$this->assertStringNotContainsString('<script>', $html);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user