168 lines
5.4 KiB
PHP
168 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\MailTest;
|
|
|
|
use App\Domains\Attachable\Enums\AttachmentType;
|
|
use App\Domains\Attachable\Models\Attachment;
|
|
use App\Domains\MailTest\Mailables\TestMail;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Tests\TestCase;
|
|
|
|
class MailTestControllerTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_it_sends_a_test_email(): void
|
|
{
|
|
Mail::fake();
|
|
$tenant = $this->createTenant();
|
|
|
|
$response = $this->postJson('/api/acme/mail-test/send', [
|
|
'to' => 'recipient@example.com',
|
|
'subject' => 'SMTP test',
|
|
'message' => 'Test message',
|
|
]);
|
|
|
|
$response->assertOk()
|
|
->assertJsonPath('message', 'Correo de prueba enviado correctamente.')
|
|
->assertJsonPath('recipient', 'recipient@example.com')
|
|
->assertJsonPath('tenant_code', 'acme')
|
|
->assertJsonPath('mailer', 'array')
|
|
->assertJsonStructure(['sent_at']);
|
|
|
|
Mail::assertSent(TestMail::class, function (TestMail $mail) use ($tenant): bool {
|
|
return $mail->hasTo('recipient@example.com')
|
|
&& $mail->mailSubject === 'SMTP test'
|
|
&& $mail->mailMessage === 'Test message'
|
|
&& $mail->tenant->is($tenant);
|
|
});
|
|
}
|
|
|
|
public function test_it_uses_default_content_when_optional_fields_are_omitted(): void
|
|
{
|
|
Mail::fake();
|
|
$this->createTenant();
|
|
|
|
$this->postJson('/api/acme/mail-test/send', [
|
|
'to' => 'recipient@example.com',
|
|
])->assertOk();
|
|
|
|
Mail::assertSent(TestMail::class, function (TestMail $mail): bool {
|
|
return $mail->mailSubject === 'Prueba de correo de Shopit'
|
|
&& $mail->mailMessage === 'Este es un correo de prueba enviado desde Shopit.';
|
|
});
|
|
}
|
|
|
|
public function test_it_validates_the_recipient(): void
|
|
{
|
|
Mail::fake();
|
|
$this->createTenant();
|
|
|
|
$this->postJson('/api/acme/mail-test/send', [
|
|
'to' => 'invalid-email',
|
|
])->assertUnprocessable()
|
|
->assertJsonValidationErrors(['to']);
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
public function test_the_mail_template_uses_the_tenant_branding(): void
|
|
{
|
|
$tenant = new Tenant([
|
|
'codigo' => 'tenant-store',
|
|
'nombre' => 'Tenant Store',
|
|
'primary_color' => '#778899',
|
|
'header_bg_color' => '#112233',
|
|
'footer_bg_color' => '#445566',
|
|
]);
|
|
|
|
$tenant->setRelation('headerLogo', new class extends Attachment
|
|
{
|
|
public function getTemporaryUrl(int $expiresInMinutes = 10): string
|
|
{
|
|
return 'https://example.com/header-logo.png';
|
|
}
|
|
});
|
|
$tenant->setRelation('footerLogo', new class extends Attachment
|
|
{
|
|
public function getTemporaryUrl(int $expiresInMinutes = 10): string
|
|
{
|
|
return 'https://example.com/footer-logo.png';
|
|
}
|
|
});
|
|
|
|
$mail = new TestMail(
|
|
'Branded email',
|
|
'Tenant message',
|
|
$tenant,
|
|
);
|
|
|
|
$html = $mail->render();
|
|
|
|
$this->assertStringContainsString('https://example.com/header-logo.png', $html);
|
|
$this->assertStringContainsString('https://example.com/footer-logo.png', $html);
|
|
$this->assertStringContainsString('background-color: #112233', $html);
|
|
$this->assertStringContainsString('background-color: #445566', $html);
|
|
$this->assertStringContainsString('color: #778899', $html);
|
|
$this->assertStringContainsString('Tenant Store', $html);
|
|
$this->assertStringContainsString('Tenant message', $html);
|
|
}
|
|
|
|
public function test_it_returns_not_found_for_an_unknown_tenant(): void
|
|
{
|
|
Mail::fake();
|
|
|
|
$this->postJson('/api/unknown/mail-test/send', [
|
|
'to' => 'recipient@example.com',
|
|
])->assertNotFound();
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
public function test_it_does_not_resolve_the_tenant_by_id(): void
|
|
{
|
|
Mail::fake();
|
|
$tenant = $this->createTenant();
|
|
|
|
$this->postJson("/api/{$tenant->id}/mail-test/send", [
|
|
'to' => 'recipient@example.com',
|
|
])->assertNotFound();
|
|
|
|
Mail::assertNothingSent();
|
|
}
|
|
|
|
private function createTenant(): Tenant
|
|
{
|
|
$headerLogo = Attachment::create([
|
|
'path' => 'tenants/header.png',
|
|
'filename' => 'header.png',
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
'extension' => 'png',
|
|
]);
|
|
$footerLogo = Attachment::create([
|
|
'path' => 'tenants/footer.png',
|
|
'filename' => 'footer.png',
|
|
'type' => AttachmentType::Image,
|
|
'mime_type' => 'image/png',
|
|
'extension' => 'png',
|
|
]);
|
|
|
|
return Tenant::create([
|
|
'codigo' => 'acme',
|
|
'nombre' => 'Acme Store',
|
|
'dominio' => 'acme.example.com',
|
|
'primary_color' => '#778899',
|
|
'secondary_color' => '#64748b',
|
|
'danger_color' => '#dc2626',
|
|
'success_color' => '#16a34a',
|
|
'header_bg_color' => '#112233',
|
|
'footer_bg_color' => '#445566',
|
|
'header_logo_id' => $headerLogo->id,
|
|
'footer_logo_id' => $footerLogo->id,
|
|
]);
|
|
}
|
|
}
|