76 lines
2.4 KiB
PHP
76 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Mail\ExpiryService;
|
|
use App\Mail\GenericTemplateMail;
|
|
use App\Models\Setting;
|
|
use App\Support\AutoEmailGate;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\Mail;
|
|
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]);
|
|
}
|
|
}
|