Files
portal-api/tests/Feature/MailEnabledToggleTest.php
T
baodev0andClaude Opus 5.5 94614d9cb3 feat: khởi tạo/huỷ domain Plesk qua job, gửi mail hoá đơn qua queue, đính kèm triển khai
- ProvisionDnsDomainJob/SuspendDnsDomainJob: tạo domain admin lên Plesk, huỷ = suspend (is_created=3)
- Khách xem zone DNS được gán ở /dich-vu/domain, quản lý bản ghi A/AAAA/CNAME/TXT/MX/SRV
- send-mail hoá đơn đẩy SendInvoiceMailJob, preview + sửa nội dung, ghi mail.order_id
- invoice_attachments.type (payment_proof/deployment)
- CacheService xoá theo SCAN pattern, Redis không persistent mặc định
- Backfill transactions.public_code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-11 09:56:18 +07:00

95 lines
3.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Jobs\Mail\SendTemplateEmailJob;
use App\Mail\ExpiryService;
use App\Mail\GenericTemplateMail;
use App\Models\Setting;
use App\Services\Email\EmailTemplateService;
use App\Support\AutoEmailGate;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Queue;
use Tests\TestCase;
class MailEnabledToggleTest extends TestCase
{
use DatabaseTransactions;
private function setMailEnabled(bool $enabled): void
{
$setting = Setting::firstOrNew(['key' => 'email_settings']);
$current = $setting->value ? json_decode($setting->value, true) : [];
$setting->value = json_encode(array_merge($current, ['mail_enabled' => $enabled]));
$setting->save();
}
private function disableAuto(array $scenarios): void
{
$setting = Setting::firstOrNew(['key' => 'email_settings']);
$current = $setting->value ? json_decode($setting->value, true) : [];
$setting->value = json_encode(array_merge($current, ['mail_enabled' => true, 'auto_disabled' => $scenarios]));
$setting->save();
}
private function send(?string $scenario = null): mixed
{
return Mail::to('user@example.com')->send(new GenericTemplateMail('Subject', '<p>Body</p>', [], $scenario));
}
public function test_mail_is_blocked_when_disabled(): void
{
$this->setMailEnabled(false);
$this->assertNull($this->send());
$this->assertCount(0, Mail::mailer()->getSymfonyTransport()->messages());
}
public function test_mail_is_sent_when_enabled(): void
{
$this->setMailEnabled(true);
$this->assertNotNull($this->send());
$this->assertCount(1, Mail::mailer()->getSymfonyTransport()->messages());
}
public function test_disabled_scenario_is_blocked_but_others_are_sent(): void
{
$this->disableAuto(['invoice_payment_reminder']);
$this->assertNull($this->send('invoice_payment_reminder'));
$this->assertNotNull($this->send('order_created'));
$this->assertNotNull($this->send());
}
public function test_manual_send_bypasses_disabled_scenario(): void
{
$this->disableAuto(['invoice_payment_reminder']);
AutoEmailGate::markManual();
$this->assertNotNull($this->send('invoice_payment_reminder'));
}
public function test_template_mailable_exposes_its_scenario(): void
{
$this->assertSame('service_expiry', (new ExpiryService(0, []))->buildViewData()[AutoEmailGate::SCENARIO_DATA_KEY]);
}
public function test_template_service_does_not_queue_when_mail_disabled_or_scenario_off(): void
{
Queue::fake();
$service = app(EmailTemplateService::class);
$this->setMailEnabled(false);
$service->send('order_created', 'user@example.com');
$this->disableAuto(['order_created']);
$service->send('order_created', 'user@example.com');
Queue::assertNothingPushed();
$service->send('domain_order_approved', 'user@example.com');
Queue::assertPushed(SendTemplateEmailJob::class, 1);
}
}