| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- namespace Tests\Feature;
- use App\Models\Contract;
- use App\Models\Role;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class ContractControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private User $adminUser;
- private User $managerUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->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]);
- }
- }
|