ContractControllerTest.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Contract;
  4. use App\Models\Role;
  5. use App\Models\User;
  6. use Illuminate\Foundation\Testing\RefreshDatabase;
  7. use Tests\TestCase;
  8. class ContractControllerTest extends TestCase
  9. {
  10. use RefreshDatabase;
  11. protected $seed = true;
  12. private User $adminUser;
  13. private User $managerUser;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  18. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  19. }
  20. // ==================== Authentication ====================
  21. public function test_guest_cannot_access_contracts_index(): void
  22. {
  23. $response = $this->get(route('contract.index'));
  24. $response->assertRedirect(route('login'));
  25. }
  26. public function test_guest_cannot_access_contract_create(): void
  27. {
  28. $response = $this->get(route('contract.create'));
  29. $response->assertRedirect(route('login'));
  30. }
  31. public function test_guest_cannot_store_contract(): void
  32. {
  33. $response = $this->post(route('contract.store'), []);
  34. $response->assertRedirect(route('login'));
  35. }
  36. public function test_guest_cannot_update_contract(): void
  37. {
  38. $contract = Contract::factory()->create();
  39. $response = $this->post(route('contract.update', $contract), []);
  40. $response->assertRedirect(route('login'));
  41. }
  42. public function test_guest_cannot_delete_contract(): void
  43. {
  44. $contract = Contract::factory()->create();
  45. $response = $this->delete(route('contract.delete', $contract));
  46. $response->assertRedirect(route('login'));
  47. }
  48. // ==================== Index ====================
  49. public function test_admin_can_access_contracts_index(): void
  50. {
  51. $response = $this->actingAs($this->adminUser)
  52. ->get(route('contract.index'));
  53. $response->assertStatus(200);
  54. $response->assertViewIs('contracts.index');
  55. }
  56. public function test_manager_can_access_contracts_index(): void
  57. {
  58. $response = $this->actingAs($this->managerUser)
  59. ->get(route('contract.index'));
  60. $response->assertStatus(200);
  61. $response->assertViewIs('contracts.index');
  62. }
  63. // ==================== Create/Show ====================
  64. public function test_admin_can_access_contract_create_form(): void
  65. {
  66. $response = $this->actingAs($this->adminUser)
  67. ->get(route('contract.create'));
  68. $response->assertStatus(200);
  69. $response->assertViewIs('contracts.edit');
  70. }
  71. public function test_admin_can_view_contract(): void
  72. {
  73. $contract = Contract::factory()->create();
  74. $response = $this->actingAs($this->adminUser)
  75. ->get(route('contract.show', $contract));
  76. $response->assertStatus(200);
  77. $response->assertViewIs('contracts.edit');
  78. }
  79. // ==================== Store ====================
  80. public function test_admin_can_create_contract(): void
  81. {
  82. $contractData = [
  83. 'contract_number' => 'CONTRACT-2026-001',
  84. 'contract_date' => '2026-01-15',
  85. 'year' => 2026,
  86. ];
  87. $response = $this->actingAs($this->adminUser)
  88. ->post(route('contract.store'), $contractData);
  89. $response->assertRedirect(route('contract.index'));
  90. $this->assertDatabaseHas('contracts', [
  91. 'contract_number' => 'CONTRACT-2026-001',
  92. 'year' => 2026,
  93. ]);
  94. }
  95. public function test_manager_cannot_create_contract(): void
  96. {
  97. $contractData = [
  98. 'contract_number' => 'CONTRACT-2026-002',
  99. 'contract_date' => '2026-01-15',
  100. 'year' => 2026,
  101. ];
  102. $response = $this->actingAs($this->managerUser)
  103. ->post(route('contract.store'), $contractData);
  104. $response->assertStatus(403);
  105. $this->assertDatabaseMissing('contracts', [
  106. 'contract_number' => 'CONTRACT-2026-002',
  107. ]);
  108. }
  109. public function test_store_contract_requires_contract_number(): void
  110. {
  111. $response = $this->actingAs($this->adminUser)
  112. ->post(route('contract.store'), [
  113. 'contract_date' => '2026-01-15',
  114. 'year' => 2026,
  115. ]);
  116. $response->assertSessionHasErrors('contract_number');
  117. }
  118. public function test_store_contract_requires_contract_date(): void
  119. {
  120. $response = $this->actingAs($this->adminUser)
  121. ->post(route('contract.store'), [
  122. 'contract_number' => 'CONTRACT-2026-003',
  123. 'year' => 2026,
  124. ]);
  125. $response->assertSessionHasErrors('contract_date');
  126. }
  127. public function test_store_contract_requires_year(): void
  128. {
  129. $response = $this->actingAs($this->adminUser)
  130. ->post(route('contract.store'), [
  131. 'contract_number' => 'CONTRACT-2026-004',
  132. 'contract_date' => '2026-01-15',
  133. ]);
  134. $response->assertSessionHasErrors('year');
  135. }
  136. // ==================== Update ====================
  137. public function test_admin_can_update_contract(): void
  138. {
  139. $contract = Contract::factory()->create([
  140. 'contract_number' => 'OLD-NUMBER',
  141. 'year' => 2025,
  142. ]);
  143. $response = $this->actingAs($this->adminUser)
  144. ->post(route('contract.update', $contract), [
  145. 'contract_number' => 'NEW-NUMBER',
  146. 'contract_date' => '2026-02-01',
  147. 'year' => 2026,
  148. ]);
  149. $response->assertRedirect(route('contract.index'));
  150. $this->assertDatabaseHas('contracts', [
  151. 'id' => $contract->id,
  152. 'contract_number' => 'NEW-NUMBER',
  153. 'year' => 2026,
  154. ]);
  155. }
  156. public function test_manager_cannot_update_contract(): void
  157. {
  158. $contract = Contract::factory()->create([
  159. 'contract_number' => 'ORIGINAL-NUMBER',
  160. ]);
  161. $response = $this->actingAs($this->managerUser)
  162. ->post(route('contract.update', $contract), [
  163. 'contract_number' => 'MODIFIED-NUMBER',
  164. 'contract_date' => '2026-02-01',
  165. 'year' => 2026,
  166. ]);
  167. $response->assertStatus(403);
  168. $this->assertDatabaseHas('contracts', [
  169. 'id' => $contract->id,
  170. 'contract_number' => 'ORIGINAL-NUMBER',
  171. ]);
  172. }
  173. // ==================== Delete ====================
  174. public function test_admin_can_delete_contract(): void
  175. {
  176. $contract = Contract::factory()->create();
  177. $response = $this->actingAs($this->adminUser)
  178. ->delete(route('contract.delete', $contract));
  179. $response->assertRedirect(route('contract.index'));
  180. $this->assertDatabaseMissing('contracts', ['id' => $contract->id]);
  181. }
  182. public function test_manager_can_delete_contract(): void
  183. {
  184. $contract = Contract::factory()->create();
  185. $response = $this->actingAs($this->managerUser)
  186. ->delete(route('contract.delete', $contract));
  187. $response->assertRedirect(route('contract.index'));
  188. $this->assertDatabaseMissing('contracts', ['id' => $contract->id]);
  189. }
  190. }