CRM/backend/tests/Feature/LeadActionsTest.php

220 خطوط
7.1 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\ImportBatch;
use App\Models\Contact;
use App\Models\ContactPhone;
use App\Models\Lead;
use App\Models\LeadStatus;
use App\Models\PipelineStage;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class LeadActionsTest extends TestCase
{
use RefreshDatabase;
public function test_authenticated_user_can_bulk_delete_leads(): void
{
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
$admin = User::factory()->create();
$admin->assignRole($adminRole);
$first = Lead::create([
'company' => 'First Co',
'first_name' => 'First',
'last_name' => 'Contact',
'phone' => '02111111111',
]);
$second = Lead::create([
'company' => 'Second Co',
'first_name' => 'Second',
'last_name' => 'Contact',
'phone' => '02122222222',
]);
$this->actingAs($admin)
->deleteJson('/api/leads/bulk-delete', ['ids' => [$first->id, $second->id]])
->assertOk()
->assertJsonPath('deleted', 2);
$this->assertSoftDeleted('leads', ['id' => $first->id]);
$this->assertSoftDeleted('leads', ['id' => $second->id]);
}
public function test_agent_import_assigns_imported_leads_to_agent(): void
{
$agentRole = Role::create(['name' => 'agent', 'guard_name' => 'web']);
$agent = User::factory()->create();
$agent->assignRole($agentRole);
LeadStatus::create([
'name' => 'در جریان',
'color' => '#2563EB',
'sort_order' => 0,
'is_default' => true,
]);
PipelineStage::create([
'name' => 'لید جدید',
'color' => '#3B82F6',
'sort_order' => 0,
'is_default' => true,
]);
PipelineStage::create([
'name' => 'در انتظار تماس',
'color' => '#6366F1',
'sort_order' => 1,
]);
$batch = ImportBatch::create([
'user_id' => $agent->id,
'filename' => 'leads.xlsx',
'status' => 'pending',
]);
$batch->rows()->create([
'original_data' => ['Acme', '09120000000'],
'status' => 'pending',
]);
$this->actingAs($agent)
->postJson('/api/import/confirm', [
'batch_id' => $batch->id,
'column_mapping' => [
'company' => 0,
'phone' => 1,
],
])
->assertOk()
->assertJsonPath('imported_rows', 1);
$this->assertDatabaseHas('leads', [
'company' => 'Acme',
'phone' => '09120000000',
'assigned_to' => $agent->id,
'is_unassigned' => false,
]);
}
public function test_lead_card_fields_are_persisted(): void
{
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
$admin = User::factory()->create();
$admin->assignRole($adminRole);
$lead = Lead::create([
'company' => 'Acme',
'first_name' => 'Ali',
'last_name' => 'Karimi',
'phone' => '02133333333',
]);
$this->actingAs($admin)
->putJson("/api/leads/{$lead->id}", [
'last_call_result' => 'علاقه‌مند بود',
'interest_level' => 'hot',
])
->assertOk()
->assertJsonPath('last_call_result', 'علاقه‌مند بود')
->assertJsonPath('interest_level', 'hot');
$this->assertDatabaseHas('leads', [
'id' => $lead->id,
'last_call_result' => 'علاقه‌مند بود',
'interest_level' => 'hot',
]);
}
public function test_referral_call_result_creates_contact_route_records(): void
{
$agentRole = Role::create(['name' => 'agent', 'guard_name' => 'web']);
$agent = User::factory()->create();
$agent->assignRole($agentRole);
$lead = Lead::create([
'company' => 'Acme',
'first_name' => 'Ali',
'last_name' => 'Karimi',
'phone' => '02144444444',
'assigned_to' => $agent->id,
'is_unassigned' => false,
]);
$contact = Contact::create([
'lead_id' => $lead->id,
'name' => 'آقای رضایی',
'role' => 'پذیرش',
'status' => 'active',
'is_primary' => true,
'primary_reason' => 'مخاطب اولیه لید',
'created_by' => $agent->id,
]);
$phone = ContactPhone::create([
'contact_id' => $contact->id,
'phone' => '02144444444',
'type' => 'landline',
'status' => 'active',
]);
$callId = $this->actingAs($agent)
->postJson('/api/calls', [
'lead_id' => $lead->id,
'contact_phone_id' => $phone->id,
])
->assertCreated()
->json('call_id');
$followUpAt = now()->addDay()->toISOString();
$this->actingAs($agent)
->postJson('/api/calls/register-result', [
'call_id' => $callId,
'result' => 'معرفی شماره یا شخص جدید',
'notes' => 'با پذیرش صحبت شد',
'referral' => [
'name' => 'خانم احمدی',
'phone' => '09125551234',
'phone_type' => 'mobile',
'role' => 'مسئول خرید',
'relation_description' => 'معرفی‌شده توسط پذیرش',
'description' => 'برای خرید تصمیم می‌گیرد',
'make_primary' => true,
'next_follow_up_at' => $followUpAt,
],
])
->assertOk()
->assertJsonPath('result', 'معرفی شماره یا شخص جدید');
$this->assertDatabaseHas('contacts', [
'lead_id' => $lead->id,
'name' => 'خانم احمدی',
'role' => 'مسئول خرید',
'is_primary' => true,
]);
$this->assertDatabaseHas('contact_phones', [
'phone' => '09125551234',
'type' => 'mobile',
'status' => 'active',
]);
$this->assertDatabaseHas('contact_relations', [
'lead_id' => $lead->id,
'from_contact_id' => $contact->id,
'relation_type' => 'introduced',
]);
$this->assertDatabaseHas('call_logs', [
'lead_id' => $lead->id,
'call_id' => $callId,
'result' => 'معرفی شماره یا شخص جدید',
'next_action' => 'تماس با مخاطب معرفی‌شده',
]);
$this->assertDatabaseHas('follow_ups', [
'lead_id' => $lead->id,
'user_id' => $agent->id,
'status' => 'pending',
]);
}
}