153 خطوط
5.0 KiB
PHP
153 خطوط
5.0 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use App\Models\Lead;
|
|
use App\Models\PipelineStage;
|
|
use Spatie\Permission\Models\Role;
|
|
use Tests\TestCase;
|
|
|
|
class UserManagementTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
public function test_admin_can_update_user_and_remain_authenticated(): void
|
|
{
|
|
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
|
|
Role::create(['name' => 'agent', 'guard_name' => 'web']);
|
|
|
|
$admin = User::factory()->create([
|
|
'phone' => '09121111111',
|
|
'is_active' => true,
|
|
]);
|
|
$admin->assignRole($adminRole);
|
|
|
|
$this->actingAs($admin);
|
|
|
|
$response = $this->putJson("/api/users/{$admin->id}", [
|
|
'name' => 'مدیر سیستم',
|
|
'email' => $admin->email,
|
|
'phone' => $admin->phone,
|
|
'role' => 'admin',
|
|
]);
|
|
|
|
$response
|
|
->assertOk()
|
|
->assertJsonPath('id', $admin->id)
|
|
->assertJsonPath('name', 'مدیر سیستم');
|
|
|
|
$this->getJson('/api/auth/me')
|
|
->assertOk()
|
|
->assertJsonPath('data.id', $admin->id);
|
|
}
|
|
|
|
public function test_admin_can_store_user_voip_extension(): void
|
|
{
|
|
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
|
|
Role::create(['name' => 'agent', 'guard_name' => 'web']);
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole($adminRole);
|
|
|
|
$response = $this->actingAs($admin)
|
|
->postJson('/api/users', [
|
|
'name' => 'کارشناس فروش',
|
|
'email' => 'agent-extension@example.com',
|
|
'password' => 'Password123!',
|
|
'phone' => '09120000000',
|
|
'voip_extension' => '504',
|
|
'role' => 'agent',
|
|
])
|
|
->assertCreated()
|
|
->assertJsonPath('voip_extension', '504');
|
|
|
|
$this->assertDatabaseHas('users', [
|
|
'id' => $response->json('id'),
|
|
'voip_extension' => '504',
|
|
]);
|
|
}
|
|
|
|
public function test_supervisor_and_agent_can_read_pipeline_stages(): void
|
|
{
|
|
PipelineStage::create([
|
|
'name' => 'لید جدید',
|
|
'color' => '#3B82F6',
|
|
'sort_order' => 0,
|
|
'is_default' => true,
|
|
]);
|
|
|
|
foreach (['supervisor', 'agent'] as $roleName) {
|
|
$role = Role::create(['name' => $roleName, 'guard_name' => 'web']);
|
|
$user = User::factory()->create(['is_active' => true]);
|
|
$user->assignRole($role);
|
|
|
|
$this->actingAs($user)
|
|
->getJson('/api/pipeline-stages')
|
|
->assertOk()
|
|
->assertJsonPath('0.name', 'لید جدید');
|
|
}
|
|
}
|
|
|
|
public function test_agent_only_sees_self_in_agents_endpoint(): void
|
|
{
|
|
$agentRole = Role::create(['name' => 'agent', 'guard_name' => 'web']);
|
|
$firstAgent = User::factory()->create(['name' => 'First Agent', 'is_active' => true]);
|
|
$secondAgent = User::factory()->create(['name' => 'Second Agent', 'is_active' => true]);
|
|
$firstAgent->assignRole($agentRole);
|
|
$secondAgent->assignRole($agentRole);
|
|
|
|
$this->actingAs($firstAgent)
|
|
->getJson('/api/agents')
|
|
->assertOk()
|
|
->assertJsonCount(1)
|
|
->assertJsonPath('0.id', $firstAgent->id)
|
|
->assertJsonPath('0.name', 'First Agent');
|
|
}
|
|
|
|
public function test_admin_dashboard_groups_duplicate_pipeline_stage_names(): void
|
|
{
|
|
$adminRole = Role::create(['name' => 'admin', 'guard_name' => 'web']);
|
|
Role::create(['name' => 'agent', 'guard_name' => 'web']);
|
|
$admin = User::factory()->create(['is_active' => true]);
|
|
$admin->assignRole($adminRole);
|
|
|
|
$firstStage = PipelineStage::create([
|
|
'name' => 'لید جدید',
|
|
'color' => '#3B82F6',
|
|
'sort_order' => 0,
|
|
'is_default' => true,
|
|
]);
|
|
$secondStage = PipelineStage::create([
|
|
'name' => 'لید جدید',
|
|
'color' => '#3B82F6',
|
|
'sort_order' => 0,
|
|
]);
|
|
|
|
Lead::create([
|
|
'company' => 'First Co',
|
|
'first_name' => 'First',
|
|
'last_name' => 'Contact',
|
|
'phone' => '02111111111',
|
|
'pipeline_stage_id' => $firstStage->id,
|
|
]);
|
|
Lead::create([
|
|
'company' => 'Second Co',
|
|
'first_name' => 'Second',
|
|
'last_name' => 'Contact',
|
|
'phone' => '02122222222',
|
|
'pipeline_stage_id' => $secondStage->id,
|
|
]);
|
|
|
|
$response = $this->actingAs($admin)
|
|
->getJson('/api/dashboard/admin')
|
|
->assertOk();
|
|
|
|
$pipelineData = $response->json('pipeline_data');
|
|
$newLeadStages = array_values(array_filter($pipelineData, fn ($item) => $item['stage'] === 'لید جدید'));
|
|
|
|
$this->assertCount(1, $newLeadStages);
|
|
$this->assertSame(2, $newLeadStages[0]['count']);
|
|
}
|
|
}
|