CRM/backend/tests/Feature/ReportKpiTest.php

87 خطوط
3.2 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\Call;
use App\Models\CallResult;
use App\Models\Lead;
use App\Models\User;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ReportKpiTest extends TestCase
{
use RefreshDatabase;
public function test_admin_receives_actionable_kpi_dashboard_with_targets_trend_and_leaderboard(): void
{
$this->seed(RolePermissionSeeder::class);
$admin = User::factory()->create(['is_active' => true]);
$admin->assignRole('admin');
$agent = User::factory()->create(['is_active' => true, 'name' => 'کارشناس تست']);
$agent->assignRole('agent');
CallResult::create([
'name' => 'فروش موفق',
'slug' => 'successful-sale',
'is_positive' => true,
'is_final' => true,
'is_active' => true,
]);
$lead = Lead::create([
'company' => 'مشتری KPI',
'first_name' => 'مینا',
'last_name' => 'احمدی',
'phone' => '09123333333',
'assigned_to' => $agent->id,
'is_unassigned' => false,
'final_result' => 'موفق',
'deal_value' => 5000000,
]);
Call::create([
'lead_id' => $lead->id,
'user_id' => $agent->id,
'direction' => 'outbound',
'phone' => $lead->phone,
'result' => 'فروش موفق',
'duration' => 120,
'is_manual' => true,
]);
$today = now()->toDateString();
$this->actingAs($admin)->getJson("/api/reports/kpi?date_from={$today}&date_to={$today}")
->assertOk()
->assertJsonPath('range.working_days', 1)
->assertJsonCount(9, 'kpis')
->assertJsonPath('kpis.0.key', 'total_calls')
->assertJsonPath('kpis.0.value', 1)
->assertJsonPath('trend.0.calls', 1)
->assertJsonPath('trend.0.won', 1)
->assertJsonPath('leaderboard.0.agent_name', 'کارشناس تست')
->assertJsonPath('leaderboard.0.won_value', 5000000);
}
public function test_agent_report_scope_cannot_be_changed_to_another_agent_or_aggregate_exports(): void
{
$this->seed(RolePermissionSeeder::class);
$agent = User::factory()->create(['is_active' => true]);
$agent->assignRole('agent');
$otherAgent = User::factory()->create(['is_active' => true]);
$otherAgent->assignRole('agent');
$this->actingAs($agent)->getJson("/api/reports/agent-performance?agent_id={$otherAgent->id}")
->assertForbidden();
$this->actingAs($agent)->getJson('/api/reports/team-performance')
->assertForbidden();
$this->actingAs($agent)->getJson('/api/reports/import-quality')
->assertForbidden();
$this->actingAs($agent)->get('/api/reports/export/excel?type=team')
->assertForbidden();
$this->actingAs($agent)->getJson('/api/reports/agent-performance')
->assertOk()
->assertJsonCount(1)
->assertJsonPath('0.agent.id', $agent->id);
}
}