- Gửi email theo template cho 1 khách, tab lịch sử email, nhắc gia hạn dịch vụ hàng loạt - Mail lưu from/bcc/sent_by; mẫu MARKETING và mẫu cũ trong TemplateSeeder - Gói hosting: giới hạn, trạng thái, lấy plan Plesk qua CLI gateway - Backfill payment_method và ngày đơn hàng còn thiếu - Bổ sung field CPU/RAM/Disk/Băng thông cho loại sản phẩm vps và cloud Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
162 lines
6.1 KiB
PHP
162 lines
6.1 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\Admin\Customer;
|
|
|
|
use App\Mail\Customer\ManualCustomerEmail;
|
|
use App\Mail\GenericTemplateMail;
|
|
use App\Models\Customer;
|
|
use App\Models\Template;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Mail;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class SendTemplateEmailTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private User $admin;
|
|
private Customer $customer;
|
|
private Template $template;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
Role::firstOrCreate(['name' => 'Full Administrator', 'guard_name' => 'admin']);
|
|
$this->admin = User::factory()->create();
|
|
$this->admin->assignRole('Full Administrator');
|
|
|
|
$this->customer = Customer::factory()->create(['fullname' => 'Nguyễn Văn A']);
|
|
|
|
$this->template = Template::create([
|
|
'type_id' => 0,
|
|
'format' => 'html',
|
|
'name' => 'Khuyến mãi',
|
|
'subject' => 'Chào {{client_name}}',
|
|
'message' => '<p>Xin chào {{$client_name}}, mã {{promo_code}}</p>',
|
|
'metadata' => [
|
|
'variables' => [
|
|
['key' => 'client_name', 'label' => 'Tên', 'type' => 'string', 'example' => 'Quý khách'],
|
|
['key' => 'promo_code', 'label' => 'Mã', 'type' => 'string', 'example' => 'TGS10'],
|
|
],
|
|
'mail' => ['cc' => ['sale@example.com']],
|
|
],
|
|
'status' => 1,
|
|
]);
|
|
}
|
|
|
|
public function test_preview_renders_template_for_customer(): void
|
|
{
|
|
$this->actingAs($this->admin, 'admin')
|
|
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/email-preview", [
|
|
'template_id' => $this->template->id,
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.subject', 'Chào Nguyễn Văn A')
|
|
->assertJsonPath('data.html', '<p>Xin chào Nguyễn Văn A, mã TGS10</p>')
|
|
->assertJsonPath('data.cc', ['sale@example.com']);
|
|
}
|
|
|
|
public function test_send_uses_edited_html_without_template_cc(): void
|
|
{
|
|
Mail::fake();
|
|
|
|
$this->actingAs($this->admin, 'admin')
|
|
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/send-email", [
|
|
'template_id' => $this->template->id,
|
|
'subject' => 'Tiêu đề đã sửa',
|
|
'message' => '<p>Nội dung đã sửa</p>',
|
|
'to' => $this->customer->email,
|
|
])
|
|
->assertOk();
|
|
|
|
Mail::assertSent(GenericTemplateMail::class, fn (GenericTemplateMail $mail) => $mail->hasTo($this->customer->email)
|
|
&& $mail->renderedSubject === 'Tiêu đề đã sửa'
|
|
&& $mail->renderedHtml === '<p>Nội dung đã sửa</p>'
|
|
&& !array_key_exists('cc', $mail->mailConfig));
|
|
|
|
$this->assertDatabaseHas('mail', ['customer_id' => $this->customer->id, 'message' => '<p>Nội dung đã sửa</p>']);
|
|
}
|
|
|
|
public function test_email_layout_wraps_content_with_header_and_footer(): void
|
|
{
|
|
$html = $this->actingAs($this->admin, 'admin')
|
|
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/email-layout", [
|
|
'message' => '<p>Xin chào {{client_name}}</p>',
|
|
])
|
|
->assertOk()
|
|
->json('data.html');
|
|
|
|
$this->assertStringContainsString('<p>Xin chào Nguyễn Văn A</p>', $html);
|
|
$this->assertStringContainsString('class="Header"', $html);
|
|
$this->assertStringContainsString('class="Footer"', $html);
|
|
}
|
|
|
|
public function test_manual_email_with_inserted_layout_is_not_wrapped_again(): void
|
|
{
|
|
Mail::fake();
|
|
|
|
$this->actingAs($this->admin, 'admin')
|
|
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/send-email", [
|
|
'subject' => 'Thông báo',
|
|
'message' => '<table class="Header"></table><p>Nội dung</p>',
|
|
'to' => $this->customer->email,
|
|
'with_layout' => false,
|
|
])
|
|
->assertOk();
|
|
|
|
Mail::assertQueued(ManualCustomerEmail::class, function (ManualCustomerEmail $mail) {
|
|
$html = $mail->render();
|
|
|
|
return substr_count($html, 'class="Header"') === 1 && str_contains($html, '<p>Nội dung</p>');
|
|
});
|
|
}
|
|
|
|
public function test_sent_emails_are_listed_with_sender_and_bcc(): void
|
|
{
|
|
Mail::fake();
|
|
$base = "/api/v1/administrator/customers/{$this->customer->uuid}";
|
|
|
|
$this->actingAs($this->admin, 'admin')->postJson("{$base}/send-email", [
|
|
'subject' => 'Thông tin VPS',
|
|
'message' => '<p>Thông tin</p>',
|
|
'to' => $this->customer->email,
|
|
'bcc' => 'audit@example.com',
|
|
])->assertOk();
|
|
|
|
$item = $this->getJson("{$base}/emails")
|
|
->assertOk()
|
|
->assertJsonPath('data.total', 1)
|
|
->assertJsonPath('data.items.0.bcc', 'audit@example.com')
|
|
->assertJsonPath('data.items.0.to', $this->customer->email)
|
|
->assertJsonPath('data.items.0.sent_by', $this->admin->name ?: $this->admin->username)
|
|
->assertJsonMissingPath('data.items.0.html')
|
|
->json('data.items.0');
|
|
|
|
$this->getJson("{$base}/emails/{$item['id']}")
|
|
->assertOk()
|
|
->assertJsonPath('data.html', '<p>Thông tin</p>');
|
|
|
|
$other = Customer::factory()->create();
|
|
$this->getJson("/api/v1/administrator/customers/{$other->uuid}/emails/{$item['id']}")->assertNotFound();
|
|
}
|
|
|
|
public function test_send_reports_error_when_mail_is_disabled(): void
|
|
{
|
|
\App\Models\Setting::updateOrCreate(['key' => 'email_settings'], ['value' => json_encode(['mail_enabled' => false])]);
|
|
|
|
$this->actingAs($this->admin, 'admin')
|
|
->postJson("/api/v1/administrator/customers/{$this->customer->uuid}/send-email", [
|
|
'subject' => 'Thông báo',
|
|
'message' => '<p>Nội dung</p>',
|
|
])
|
|
->assertStatus(422);
|
|
|
|
$this->assertDatabaseMissing('mail', ['customer_id' => $this->customer->id]);
|
|
}
|
|
}
|