| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace Tests\Feature;
- use App\Models\Dictionary\Area;
- use App\Models\Dictionary\District;
- use App\Models\Order;
- use App\Models\Reclamation;
- use App\Models\Role;
- use App\Models\Schedule;
- use App\Models\User;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Illuminate\Support\Facades\Bus;
- use Tests\TestCase;
- class ScheduleControllerTest extends TestCase
- {
- use RefreshDatabase;
- protected $seed = true;
- private User $adminUser;
- private User $managerUser;
- private User $brigadierUser;
- protected function setUp(): void
- {
- parent::setUp();
- $this->adminUser = User::factory()->create(['role' => Role::ADMIN]);
- $this->managerUser = User::factory()->create(['role' => Role::MANAGER]);
- $this->brigadierUser = User::factory()->create(['role' => Role::BRIGADIER]);
- }
- // ==================== Authentication ====================
- public function test_guest_cannot_access_schedule_index(): void
- {
- $response = $this->get(route('schedule.index'));
- $response->assertRedirect(route('login'));
- }
- public function test_guest_cannot_create_schedule_from_order(): void
- {
- $response = $this->post(route('schedule.create-from-order'), []);
- $response->assertRedirect(route('login'));
- }
- public function test_guest_cannot_update_schedule(): void
- {
- $response = $this->post(route('schedule.update'), []);
- $response->assertRedirect(route('login'));
- }
- public function test_guest_cannot_delete_schedule(): void
- {
- $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
- $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
- $response = $this->delete(route('schedule.delete', $schedule));
- $response->assertRedirect(route('login'));
- }
- // ==================== Authorization ====================
- public function test_manager_cannot_create_schedule_from_order(): void
- {
- $response = $this->actingAs($this->managerUser)
- ->post(route('schedule.create-from-order'), []);
- $response->assertStatus(403);
- }
- public function test_manager_cannot_update_schedule(): void
- {
- $response = $this->actingAs($this->managerUser)
- ->post(route('schedule.update'), []);
- $response->assertStatus(403);
- }
- public function test_manager_cannot_delete_schedule(): void
- {
- $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
- $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
- $response = $this->actingAs($this->managerUser)
- ->delete(route('schedule.delete', $schedule));
- $response->assertStatus(403);
- }
- // ==================== Index ====================
- public function test_admin_can_access_schedule_index(): void
- {
- $response = $this->actingAs($this->adminUser)
- ->get(route('schedule.index'));
- $response->assertStatus(200);
- $response->assertViewIs('schedule.index');
- }
- public function test_brigadier_can_access_schedule_index(): void
- {
- $response = $this->actingAs($this->brigadierUser)
- ->get(route('schedule.index'));
- $response->assertStatus(200);
- }
- // ==================== Update (create manual schedule) ====================
- public function test_admin_can_create_manual_schedule(): void
- {
- Bus::fake();
- $district = District::query()->first();
- $area = Area::query()->first();
- $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
- $response = $this->actingAs($this->adminUser)
- ->post(route('schedule.update'), [
- 'installation_date' => '2026-03-15',
- 'address_code' => 'TEST-001',
- 'object_address' => 'ул. Тестовая, 1',
- 'object_type' => 'Площадка',
- 'mafs' => 'МАФ-001 - 2',
- 'mafs_count' => 2,
- 'district_id' => $district->id,
- 'area_id' => $area->id,
- 'brigadier_id' => $brigadier->id,
- 'comment' => 'Тестовый комментарий',
- ]);
- $response->assertRedirect();
- $this->assertDatabaseHas('schedules', [
- 'address_code' => 'TEST-001',
- 'object_address' => 'ул. Тестовая, 1',
- 'manual' => true,
- ]);
- }
- public function test_admin_can_update_existing_schedule(): void
- {
- Bus::fake();
- $district = District::query()->first();
- $area = Area::query()->first();
- $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
- $schedule = Schedule::factory()->create([
- 'installation_date' => '2026-03-10',
- 'object_address' => 'Старый адрес',
- 'brigadier_id' => $brigadier->id,
- ]);
- $response = $this->actingAs($this->adminUser)
- ->post(route('schedule.update'), [
- 'id' => $schedule->id,
- 'installation_date' => '2026-03-20',
- 'address_code' => $schedule->address_code ?? 'UPD-001',
- 'object_address' => 'Новый адрес',
- 'object_type' => 'Площадка',
- 'mafs' => 'МАФ-002 - 1',
- 'mafs_count' => 1,
- 'district_id' => $district->id,
- 'area_id' => $area->id,
- 'brigadier_id' => $brigadier->id,
- 'comment' => '',
- ]);
- $response->assertRedirect();
- $this->assertDatabaseHas('schedules', [
- 'id' => $schedule->id,
- 'object_address' => 'Новый адрес',
- 'installation_date' => '2026-03-20',
- ]);
- }
- // ==================== Delete ====================
- public function test_admin_can_delete_schedule(): void
- {
- $brigadier = User::factory()->create(['role' => Role::BRIGADIER]);
- $schedule = Schedule::factory()->create(['brigadier_id' => $brigadier->id]);
- $response = $this->actingAs($this->adminUser)
- ->delete(route('schedule.delete', $schedule));
- $response->assertRedirect();
- $this->assertDatabaseMissing('schedules', ['id' => $schedule->id]);
- }
- // ==================== Export ====================
- public function test_admin_can_export_schedule(): void
- {
- Bus::fake();
- $response = $this->actingAs($this->adminUser)
- ->post(route('schedule.export'), [
- 'start_date' => '2026-03-01',
- 'end_date' => '2026-03-31',
- 'week' => '10',
- 'year' => 2026,
- ]);
- $response->assertRedirect(route('schedule.index', ['week' => 10, 'year' => 2026]));
- $response->assertSessionHas('success');
- }
- }
|