108 lines
3.7 KiB
PHP
108 lines
3.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Tests\Feature\Api\Admin;
|
|
|
|
use App\Jobs\Mail\SendMailCampaignRecipient;
|
|
use App\Models\Customer;
|
|
use App\Models\Template;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Queue;
|
|
use Spatie\Permission\Models\Permission;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class MarketingEmailOptOutTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
private function admin(): User
|
|
{
|
|
$role = Role::create(['name' => 'role-' . uniqid(), 'guard_name' => 'admin']);
|
|
$role->givePermissionTo(Permission::firstOrCreate(['name' => 'mail-campaign.create', 'guard_name' => 'admin']));
|
|
$admin = User::factory()->create();
|
|
$admin->assignRole($role);
|
|
|
|
return $admin;
|
|
}
|
|
|
|
private function template(?string $key): Template
|
|
{
|
|
return Template::create([
|
|
'type_id' => 0,
|
|
'name' => 'T ' . uniqid(),
|
|
'key' => $key,
|
|
'format' => 'html',
|
|
'subject' => 'Ưu đãi',
|
|
'message' => '<p>Hello</p>',
|
|
'status' => 1,
|
|
]);
|
|
}
|
|
|
|
public function test_marketing_campaign_skips_opted_out_customers_but_system_template_does_not(): void
|
|
{
|
|
Queue::fake();
|
|
$admin = $this->admin();
|
|
$subscribed = Customer::factory()->create(['email_marketing' => 1]);
|
|
$optedOut = Customer::factory()->create(['email_marketing' => 0]);
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->postJson('/api/v1/administrator/mail-campaigns', [
|
|
'template_id' => $this->template(null)->id,
|
|
'recipient_type' => 'selected',
|
|
'uuids' => [$subscribed->uuid, $optedOut->uuid],
|
|
'emails' => [strtoupper($optedOut->email)],
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.total_recipients', 1);
|
|
|
|
Queue::assertPushed(SendMailCampaignRecipient::class, 1);
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->postJson('/api/v1/administrator/mail-campaigns', [
|
|
'template_id' => $this->template('service_renewal_reminder')->id,
|
|
'recipient_type' => 'selected',
|
|
'uuids' => [$optedOut->uuid],
|
|
])
|
|
->assertOk()
|
|
->assertJsonPath('data.total_recipients', 1);
|
|
}
|
|
|
|
public function test_marketing_campaign_with_only_opted_out_recipients_is_rejected(): void
|
|
{
|
|
Queue::fake();
|
|
$optedOut = Customer::factory()->create(['email_marketing' => 0]);
|
|
|
|
$this->actingAs($this->admin(), 'admin')
|
|
->postJson('/api/v1/administrator/mail-campaigns', [
|
|
'subject' => 'Tin tức',
|
|
'message' => '<p>Hi</p>',
|
|
'recipient_type' => 'selected',
|
|
'uuids' => [$optedOut->uuid],
|
|
])
|
|
->assertStatus(422);
|
|
|
|
Queue::assertNotPushed(SendMailCampaignRecipient::class);
|
|
}
|
|
|
|
public function test_campaign_with_template_uses_edited_message_when_given(): void
|
|
{
|
|
Queue::fake();
|
|
$admin = $this->admin();
|
|
$customer = Customer::factory()->create(['email_marketing' => 1]);
|
|
|
|
$this->actingAs($admin, 'admin')
|
|
->postJson('/api/v1/administrator/mail-campaigns', [
|
|
'template_id' => $this->template(null)->id,
|
|
'message' => '<p>Nội dung đã sửa</p>',
|
|
'recipient_type' => 'selected',
|
|
'uuids' => [$customer->uuid],
|
|
])
|
|
->assertOk();
|
|
|
|
$this->assertSame('<p>Nội dung đã sửa</p>', \App\Models\MailCampaign::latest('id')->first()->message);
|
|
}
|
|
}
|