81 lines
3.1 KiB
PHP
81 lines
3.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Mail;
|
|
|
|
use App\Http\Facade\SignRSA;
|
|
use App\Models\Setting;
|
|
use App\Support\MailSettings;
|
|
use App\Listeners\Mail\ApplySystemRecipients;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Mail\Events\MessageSending;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Symfony\Component\Mime\Email;
|
|
use Tests\TestCase;
|
|
|
|
class MailSettingsQueueTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function saveSmtp(string $host): void
|
|
{
|
|
Setting::updateOrCreate(['key' => 'setting'], ['value' => json_encode(['company' => 'Thế Giới Số'])]);
|
|
Setting::updateOrCreate(['key' => 'smtp'], ['value' => json_encode([
|
|
'host' => $host, 'port' => 465, 'user' => 'noreply@tgs.com.vn',
|
|
'password' => SignRSA::EncryptFunc('secret'), 'encryption' => 'ssl',
|
|
'from_address' => 'noreply@tgs.com.vn', 'from_name' => 'TGS',
|
|
])]);
|
|
Cache::forget(MailSettings::cacheKey());
|
|
}
|
|
|
|
private function hostSeenByQueuedJob(): ?string
|
|
{
|
|
dispatch(fn () => Cache::put('test:queued-mail-host', config('mail.mailers.smtp.host')));
|
|
|
|
return Cache::pull('test:queued-mail-host');
|
|
}
|
|
|
|
public function test_queued_jobs_use_smtp_from_settings_and_pick_up_changes(): void
|
|
{
|
|
config(['queue.default' => 'sync']);
|
|
|
|
$this->saveSmtp('smtp.settings-one.test');
|
|
$this->assertSame('smtp.settings-one.test', $this->hostSeenByQueuedJob());
|
|
$this->assertSame('noreply@tgs.com.vn', config('mail.from.address'));
|
|
$this->assertSame('secret', config('mail.mailers.smtp.password'));
|
|
|
|
$this->saveSmtp('smtp.settings-two.test');
|
|
$this->assertSame('smtp.settings-two.test', $this->hostSeenByQueuedJob());
|
|
}
|
|
|
|
public function test_smtp_settings_apply_without_general_setting_row(): void
|
|
{
|
|
$this->saveSmtp('smtp.settings-only.test');
|
|
Setting::where('key', 'setting')->delete();
|
|
Cache::forget(MailSettings::cacheKey());
|
|
|
|
MailSettings::apply();
|
|
|
|
$this->assertSame('smtp.settings-only.test', config('mail.mailers.smtp.host'));
|
|
$this->assertSame('TGS', config('mail.from.name'));
|
|
}
|
|
|
|
public function test_system_cc_and_bcc_are_applied_without_duplicate_recipients(): void
|
|
{
|
|
$this->saveSmtp('smtp.settings.test');
|
|
$smtp = Setting::where('key', 'smtp')->firstOrFail();
|
|
$values = json_decode((string) $smtp->value, true);
|
|
$values['cc'] = ['audit@example.com', 'customer@example.com'];
|
|
$values['bcc'] = ['archive@example.com', 'audit@example.com'];
|
|
$smtp->update(['value' => json_encode($values)]);
|
|
Cache::forget(MailSettings::cacheKey());
|
|
|
|
$message = (new Email())->to('customer@example.com')->subject('Test');
|
|
app(ApplySystemRecipients::class)->handle(new MessageSending($message, []));
|
|
|
|
$this->assertSame(['audit@example.com'], array_map(fn ($address) => $address->getAddress(), $message->getCc()));
|
|
$this->assertSame(['archive@example.com'], array_map(fn ($address) => $address->getAddress(), $message->getBcc()));
|
|
}
|
|
}
|