Files
GSB-Construction/tests/Feature/DashboardTest.php

117 lines
3.6 KiB
PHP

<?php
namespace Tests\Feature;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Modules\ProjectManagement\Models\Project;
use Spatie\Permission\Models\Role;
use Tests\TestCase;
class DashboardTest extends TestCase
{
use RefreshDatabase;
protected User $user;
protected Project $project;
protected function setUp(): void
{
parent::setUp();
// Seed roles if necessary
Role::firstOrCreate(['name' => 'admin']);
// Create a regular user
$this->user = User::factory()->create([
'status' => 'active',
'user_type' => 'admin',
]);
// Assign admin role
$this->user->assignRole('admin');
// Create a project
$this->project = Project::create([
'name' => 'Test Skyline Project',
'code' => 'PRJ-2026-001',
'description' => 'A test skyline project',
'client_name' => 'Demo Client',
'location' => 'San Francisco, CA',
'status' => \Modules\ProjectManagement\Enums\ProjectStatus::InProgress,
'contract_value' => 500000.00,
'contract_duration' => 12,
'start_date' => now()->format('Y-m-d'),
'target_end_date' => now()->addMonths(12)->format('Y-m-d'),
'completion_percentage' => 15.00,
'total_capitalization' => 0.00,
]);
}
public function test_guest_cannot_access_dashboard(): void
{
$response = $this->get('/dashboard');
$response->assertRedirect('/login');
}
public function test_authenticated_user_can_access_dashboard_global(): void
{
$response = $this->actingAs($this->user)->get('/dashboard');
$response->assertStatus(200);
// Assert Inertia page and props exist
$response->assertInertia(fn ($page) => $page
->component('Dashboard')
->has('projects')
->has('selectedProject', null)
->has('weather')
->has('activities')
->has('blockers')
->has('resources')
);
}
public function test_authenticated_user_can_filter_dashboard_by_project(): void
{
$response = $this->actingAs($this->user)->get('/dashboard?project=' . $this->project->ulid);
$response->assertStatus(200);
// Assert Inertia page and project data are populated
$response->assertInertia(fn ($page) => $page
->component('Dashboard')
->has('projects')
->where('selectedProject.ulid', $this->project->ulid)
->where('selectedProject.name', 'Test Skyline Project')
->has('weather')
->has('activities')
->has('blockers')
->has('resources')
);
}
public function test_contractor_user_can_access_dashboard(): void
{
$contractor = \Modules\ContractorManagement\Models\Contractor::create([
'company_name' => 'Acme Contractors',
'contact_person' => 'John Builder',
'email' => 'john@acme.test',
'phone' => '1234567890',
'status' => 'active',
]);
$contractorRole = Role::firstOrCreate(['name' => 'Contractor Admin']);
$contractorUser = User::factory()->create([
'status' => 'active',
'user_type' => 'contractor',
'contractor_id' => $contractor->id,
]);
$contractorUser->assignRole($contractorRole);
$response = $this->actingAs($contractorUser)->get('/dashboard');
$response->assertStatus(200);
}
}