CRM/backend/tests/Feature/CalendarAccessTest.php

67 خطوط
2.7 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\FollowUp;
use App\Models\Lead;
use App\Models\Task;
use App\Models\User;
use Database\Seeders\RolePermissionSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class CalendarAccessTest extends TestCase
{
use RefreshDatabase;
public function test_agent_calendar_only_contains_own_tasks_and_follow_ups(): void
{
$this->seed(RolePermissionSeeder::class);
$agent = User::factory()->create(['is_active' => true]);
$agent->assignRole('agent');
$other = User::factory()->create(['is_active' => true]);
$other->assignRole('agent');
$ownLead = $this->lead($agent, 'مشتری خودی', '09120000001');
$otherLead = $this->lead($other, 'مشتری دیگر', '09120000002');
$this->task($agent, 'کار خودم');
$this->task($other, 'کار کارشناس دیگر');
FollowUp::create(['lead_id' => $ownLead->id, 'user_id' => $agent->id, 'scheduled_at' => now()->addHour(), 'status' => 'pending', 'notes' => 'پیگیری خودم']);
FollowUp::create(['lead_id' => $otherLead->id, 'user_id' => $other->id, 'scheduled_at' => now()->addHour(), 'status' => 'pending', 'notes' => 'پیگیری دیگر']);
$from = now()->subDay()->toDateString();
$to = now()->addDay()->toDateString();
$this->actingAs($agent)->getJson("/api/calendar/events?from={$from}&to={$to}")
->assertOk()
->assertJsonCount(2, 'events')
->assertJsonFragment(['title' => 'کار خودم'])
->assertJsonFragment(['title' => 'پیگیری خودم'])
->assertJsonMissing(['title' => 'کار کارشناس دیگر'])
->assertJsonMissing(['title' => 'پیگیری دیگر']);
$this->actingAs($agent)->getJson("/api/calendar/events?from={$from}&to={$to}&type=task")
->assertOk()
->assertJsonCount(1, 'events')
->assertJsonPath('events.0.type', 'task');
}
private function lead(User $agent, string $company, string $phone): Lead
{
return Lead::create(['first_name' => 'تست', 'last_name' => 'تقویم', 'company' => $company, 'phone' => $phone, 'assigned_to' => $agent->id, 'is_unassigned' => false]);
}
private function task(User $agent, string $subject): Task
{
return Task::create([
'subject' => $subject,
'assigned_to' => $agent->id,
'assigned_by' => $agent->id,
'created_by' => $agent->id,
'priority' => 'normal',
'status' => 'open',
'visibility' => 'private',
'due_at' => now()->addHour(),
]);
}
}