adminUser = User::factory()->create(['role' => Role::ADMIN]); $this->managerUser = User::factory()->create(['role' => Role::MANAGER]); } // ==================== Authentication ==================== public function test_guest_cannot_access_import_index(): void { $response = $this->get(route('import.index')); $response->assertRedirect(route('login')); } public function test_guest_cannot_access_import_show(): void { $import = Import::factory()->create(); $response = $this->get(route('import.show', $import)); $response->assertRedirect(route('login')); } public function test_guest_cannot_create_import(): void { $response = $this->post(route('import.create'), []); $response->assertRedirect(route('login')); } // ==================== Authorization ==================== public function test_manager_cannot_access_import_index(): void { $response = $this->actingAs($this->managerUser) ->get(route('import.index')); $response->assertStatus(403); } // ==================== Index ==================== public function test_admin_can_access_import_index(): void { $response = $this->actingAs($this->adminUser) ->get(route('import.index')); $response->assertStatus(200); $response->assertViewIs('import.index'); } // ==================== Show ==================== public function test_admin_can_view_import(): void { $import = Import::factory()->create(); $response = $this->actingAs($this->adminUser) ->get(route('import.show', $import)); $response->assertStatus(200); $response->assertViewIs('import.show'); } public function test_import_show_uses_nav_context_back_url(): void { $import = Import::factory()->create(); $indexResponse = $this->actingAs($this->adminUser) ->get(route('import.index', [ 'filters' => ['type' => 'orders'], ])); $nav = $indexResponse->viewData('nav'); $response = $this->actingAs($this->adminUser) ->get(route('import.show', [ 'import' => $import, 'nav' => $nav, ])); $response->assertOk(); $response->assertViewHas('nav', $nav); $response->assertViewHas('back_url', function (string $backUrl): bool { if (!str_starts_with($backUrl, route('import.index'))) { return false; } $query = parse_url($backUrl, PHP_URL_QUERY); parse_str((string) $query, $params); return ($params['filters']['type'] ?? null) === 'orders'; }); } // ==================== Store (upload file + dispatch job) ==================== public function test_admin_can_upload_valid_xlsx_and_dispatches_job(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('orders_import.xlsx', 50, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'orders', 'import_file' => $file, ]); $response->assertRedirect(route('import.index')); $response->assertSessionHas('success'); Bus::assertDispatched(ImportJob::class); $this->assertDatabaseHas('imports', [ 'type' => 'orders', 'status' => 'new', ]); } public function test_import_store_requires_type(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('test.xlsx', 50); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'import_file' => $file, ]); $response->assertSessionHasErrors('type'); Bus::assertNotDispatched(ImportJob::class); } public function test_import_store_requires_valid_type(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('test.xlsx', 50); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'invalid_type', 'import_file' => $file, ]); $response->assertSessionHasErrors('type'); Bus::assertNotDispatched(ImportJob::class); } public function test_import_store_requires_file(): void { Bus::fake(); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'orders', ]); $response->assertSessionHasErrors('import_file'); Bus::assertNotDispatched(ImportJob::class); } public function test_admin_can_import_reclamations_type(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('reclamations.xlsx', 50); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'reclamations', 'import_file' => $file, ]); $response->assertRedirect(route('import.index')); Bus::assertDispatched(ImportJob::class); $this->assertDatabaseHas('imports', ['type' => 'reclamations', 'status' => 'new']); } public function test_admin_can_import_mafs_type(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('mafs.xlsx', 50); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'mafs', 'import_file' => $file, ]); $response->assertRedirect(route('import.index')); Bus::assertDispatched(ImportJob::class); } public function test_admin_can_import_catalog_type(): void { Bus::fake(); Storage::fake('upload'); $file = UploadedFile::fake()->create('catalog.xlsx', 50); $response = $this->actingAs($this->adminUser) ->post(route('import.create'), [ 'type' => 'catalog', 'import_file' => $file, ]); $response->assertRedirect(route('import.index')); Bus::assertDispatched(ImportJob::class); } }