adminUser = User::factory()->create(['role' => Role::ADMIN]); $this->managerUser = User::factory()->create(['role' => Role::MANAGER]); } // ==================== Authentication ==================== public function test_guest_cannot_access_contracts_index(): void { $response = $this->get(route('contract.index')); $response->assertRedirect(route('login')); } public function test_guest_cannot_access_contract_create(): void { $response = $this->get(route('contract.create')); $response->assertRedirect(route('login')); } public function test_guest_cannot_store_contract(): void { $response = $this->post(route('contract.store'), []); $response->assertRedirect(route('login')); } public function test_guest_cannot_update_contract(): void { $contract = Contract::factory()->create(); $response = $this->post(route('contract.update', $contract), []); $response->assertRedirect(route('login')); } public function test_guest_cannot_delete_contract(): void { $contract = Contract::factory()->create(); $response = $this->delete(route('contract.delete', $contract)); $response->assertRedirect(route('login')); } // ==================== Index ==================== public function test_admin_can_access_contracts_index(): void { $response = $this->actingAs($this->adminUser) ->get(route('contract.index')); $response->assertStatus(200); $response->assertViewIs('contracts.index'); } public function test_manager_can_access_contracts_index(): void { $response = $this->actingAs($this->managerUser) ->get(route('contract.index')); $response->assertStatus(200); $response->assertViewIs('contracts.index'); } // ==================== Create/Show ==================== public function test_admin_can_access_contract_create_form(): void { $response = $this->actingAs($this->adminUser) ->get(route('contract.create')); $response->assertStatus(200); $response->assertViewIs('contracts.edit'); } public function test_admin_can_view_contract(): void { $contract = Contract::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('contract.show', $contract)); $response->assertStatus(200); $response->assertViewIs('contracts.edit'); } // ==================== Store ==================== public function test_admin_can_create_contract(): void { $contractData = [ 'contract_number' => 'CONTRACT-2026-001', 'contract_date' => '2026-01-15', 'year' => 2026, ]; $response = $this->actingAs($this->adminUser) ->post(route('contract.store'), $contractData); $response->assertRedirect(route('contract.index')); $this->assertDatabaseHas('contracts', [ 'contract_number' => 'CONTRACT-2026-001', 'year' => 2026, ]); } public function test_manager_cannot_create_contract(): void { $contractData = [ 'contract_number' => 'CONTRACT-2026-002', 'contract_date' => '2026-01-15', 'year' => 2026, ]; $response = $this->actingAs($this->managerUser) ->post(route('contract.store'), $contractData); $response->assertStatus(403); $this->assertDatabaseMissing('contracts', [ 'contract_number' => 'CONTRACT-2026-002', ]); } public function test_store_contract_requires_contract_number(): void { $response = $this->actingAs($this->adminUser) ->post(route('contract.store'), [ 'contract_date' => '2026-01-15', 'year' => 2026, ]); $response->assertSessionHasErrors('contract_number'); } public function test_store_contract_requires_contract_date(): void { $response = $this->actingAs($this->adminUser) ->post(route('contract.store'), [ 'contract_number' => 'CONTRACT-2026-003', 'year' => 2026, ]); $response->assertSessionHasErrors('contract_date'); } public function test_store_contract_requires_year(): void { $response = $this->actingAs($this->adminUser) ->post(route('contract.store'), [ 'contract_number' => 'CONTRACT-2026-004', 'contract_date' => '2026-01-15', ]); $response->assertSessionHasErrors('year'); } // ==================== Update ==================== public function test_admin_can_update_contract(): void { $contract = Contract::factory()->create([ 'contract_number' => 'OLD-NUMBER', 'year' => 2025, ]); $response = $this->actingAs($this->adminUser) ->post(route('contract.update', $contract), [ 'contract_number' => 'NEW-NUMBER', 'contract_date' => '2026-02-01', 'year' => 2026, ]); $response->assertRedirect(route('contract.index')); $this->assertDatabaseHas('contracts', [ 'id' => $contract->id, 'contract_number' => 'NEW-NUMBER', 'year' => 2026, ]); } public function test_manager_cannot_update_contract(): void { $contract = Contract::factory()->create([ 'contract_number' => 'ORIGINAL-NUMBER', ]); $response = $this->actingAs($this->managerUser) ->post(route('contract.update', $contract), [ 'contract_number' => 'MODIFIED-NUMBER', 'contract_date' => '2026-02-01', 'year' => 2026, ]); $response->assertStatus(403); $this->assertDatabaseHas('contracts', [ 'id' => $contract->id, 'contract_number' => 'ORIGINAL-NUMBER', ]); } // ==================== Delete ==================== public function test_admin_can_delete_contract(): void { $contract = Contract::factory()->create(); $response = $this->actingAs($this->adminUser) ->delete(route('contract.delete', $contract)); $response->assertRedirect(route('contract.index')); $this->assertDatabaseMissing('contracts', ['id' => $contract->id]); } public function test_manager_can_delete_contract(): void { $contract = Contract::factory()->create(); $response = $this->actingAs($this->managerUser) ->delete(route('contract.delete', $contract)); $response->assertRedirect(route('contract.index')); $this->assertDatabaseMissing('contracts', ['id' => $contract->id]); } }