ScheduleControllerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace Tests\Feature;
  3. use App\Models\Dictionary\Area;
  4. use App\Models\Dictionary\District;
  5. use App\Models\Order;
  6. use App\Models\Reclamation;
  7. use App\Models\Role;
  8. use App\Models\Schedule;
  9. use App\Models\User;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. use Illuminate\Support\Facades\Bus;
  12. use Tests\TestCase;
  13. class ScheduleControllerTest extends TestCase
  14. {
  15. use RefreshDatabase;
  16. protected $seed = true;
  17. private User $adminUser;
  18. private User $managerUser;
  19. private User $brigadierUser;
  20. protected function setUp(): void
  21. {
  22. parent::setUp();
  23. $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
  24. $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
  25. $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
  26. }
  27. // ==================== Authentication ====================
  28. public function test_guest_cannot_access_schedule_index(): void
  29. {
  30. $response = $this->get(route('schedule.index'));
  31. $response->assertRedirect(route('login'));
  32. }
  33. public function test_guest_cannot_create_schedule_from_order(): void
  34. {
  35. $response = $this->post(route('schedule.create-from-order'), []);
  36. $response->assertRedirect(route('login'));
  37. }
  38. public function test_guest_cannot_update_schedule(): void
  39. {
  40. $response = $this->post(route('schedule.update'), []);
  41. $response->assertRedirect(route('login'));
  42. }
  43. public function test_guest_cannot_delete_schedule(): void
  44. {
  45. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  46. $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
  47. $response = $this->delete(route('schedule.delete', $schedule));
  48. $response->assertRedirect(route('login'));
  49. }
  50. // ==================== Authorization ====================
  51. public function test_manager_cannot_create_schedule_from_order(): void
  52. {
  53. $response = $this->actingAs($this->managerUser)
  54. ->post(route('schedule.create-from-order'), []);
  55. $response->assertStatus(403);
  56. }
  57. public function test_manager_cannot_update_schedule(): void
  58. {
  59. $response = $this->actingAs($this->managerUser)
  60. ->post(route('schedule.update'), []);
  61. $response->assertStatus(403);
  62. }
  63. public function test_manager_cannot_delete_schedule(): void
  64. {
  65. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  66. $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
  67. $response = $this->actingAs($this->managerUser)
  68. ->delete(route('schedule.delete', $schedule));
  69. $response->assertStatus(403);
  70. }
  71. // ==================== Index ====================
  72. public function test_admin_can_access_schedule_index(): void
  73. {
  74. $response = $this->actingAs($this->adminUser)
  75. ->get(route('schedule.index'));
  76. $response->assertStatus(200);
  77. $response->assertViewIs('schedule.index');
  78. }
  79. public function test_brigadier_can_access_schedule_index(): void
  80. {
  81. $response = $this->actingAs($this->brigadierUser)
  82. ->get(route('schedule.index'));
  83. $response->assertStatus(200);
  84. }
  85. // ==================== Update (create manual schedule) ====================
  86. public function test_admin_can_create_manual_schedule(): void
  87. {
  88. Bus::fake();
  89. $district = District::query()->first();
  90. $area = Area::query()->first();
  91. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  92. $response = $this->actingAs($this->adminUser)
  93. ->post(route('schedule.update'), [
  94. 'installation_date' => '2026-03-15',
  95. 'address_code' => 'TEST-001',
  96. 'object_address' => 'ул. Тестовая, 1',
  97. 'object_type' => 'Площадка',
  98. 'mafs' => 'МАФ-001 - 2',
  99. 'mafs_count' => 2,
  100. 'district_id' => $district->id,
  101. 'area_id' => $area->id,
  102. 'brigadier_id' => $brigadier->id,
  103. 'comment' => 'Тестовый комментарий',
  104. ]);
  105. $response->assertRedirect();
  106. $this->assertDatabaseHas('schedules', [
  107. 'address_code' => 'TEST-001',
  108. 'object_address' => 'ул. Тестовая, 1',
  109. 'manual' => true,
  110. ]);
  111. }
  112. public function test_admin_can_update_existing_schedule(): void
  113. {
  114. Bus::fake();
  115. $district = District::query()->first();
  116. $area = Area::query()->first();
  117. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  118. $schedule = Schedule::factory()->create([
  119. 'installation_date' => '2026-03-10',
  120. 'object_address' => 'Старый адрес',
  121. 'brigadier_id' => $brigadier->id,
  122. ]);
  123. $response = $this->actingAs($this->adminUser)
  124. ->post(route('schedule.update'), [
  125. 'id' => $schedule->id,
  126. 'installation_date' => '2026-03-20',
  127. 'address_code' => $schedule->address_code ?? 'UPD-001',
  128. 'object_address' => 'Новый адрес',
  129. 'object_type' => 'Площадка',
  130. 'mafs' => 'МАФ-002 - 1',
  131. 'mafs_count' => 1,
  132. 'district_id' => $district->id,
  133. 'area_id' => $area->id,
  134. 'brigadier_id' => $brigadier->id,
  135. 'comment' => '',
  136. ]);
  137. $response->assertRedirect();
  138. $this->assertDatabaseHas('schedules', [
  139. 'id' => $schedule->id,
  140. 'object_address' => 'Новый адрес',
  141. 'installation_date' => '2026-03-20',
  142. ]);
  143. }
  144. // ==================== Delete ====================
  145. public function test_admin_can_delete_schedule(): void
  146. {
  147. $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
  148. $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
  149. $response = $this->actingAs($this->adminUser)
  150. ->delete(route('schedule.delete', $schedule));
  151. $response->assertRedirect();
  152. $this->assertDatabaseMissing('schedules', ['id' => $schedule->id]);
  153. }
  154. // ==================== Export ====================
  155. public function test_admin_can_export_schedule(): void
  156. {
  157. Bus::fake();
  158. $response = $this->actingAs($this->adminUser)
  159. ->post(route('schedule.export'), [
  160. 'start_date' => '2026-03-01',
  161. 'end_date' => '2026-03-31',
  162. 'week' => '10',
  163. 'year' => 2026,
  164. ]);
  165. $response->assertRedirect(route('schedule.index', ['week' => 10, 'year' => 2026]));
  166. $response->assertSessionHas('success');
  167. }
  168. }