Skip to content

A free open source IT asset/license management system

Fixed #19119 - updated structure for accessort export, added tests

snipe01b1c3923d4c
diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php
index 9b763cef8b..76557f2ed6 100644
--- a/app/Http/Controllers/ReportsController.php
+++ b/app/Http/Controllers/ReportsController.php
@@ -105,36 +105,46 @@ class ReportsController extends Controller
* @see ManufacturersController::getDatatable() method that generates the JSON response
* @since [v1.0]
*/
- public function exportAccessoryReport(): Response
+ public function exportAccessoryReport(): StreamedResponse
{
$this->authorize('reports.view');
- $accessories = Accessory::orderBy('created_at', 'DESC')->get();
- $rows = [];
- $header = [
- trans('admin/accessories/table.title'),
- trans('admin/accessories/general.accessory_category'),
- trans('admin/accessories/general.total'),
- trans('admin/accessories/general.remaining'),
- ];
- $header = array_map('trim', $header);
- $rows[] = implode(', ', $header);
+ $response = new StreamedResponse(function () {
+ $handle = fopen('php://output', 'w');
- // Row per accessory
- foreach ($accessories as $accessory) {
- $row = [];
- $row[] = e($accessory->accessory_name);
- $row[] = e($accessory->accessory_category);
- $row[] = e($accessory->total);
- $row[] = e($accessory->remaining);
+ $header = [
+ trans('admin/accessories/table.title'),
+ trans('admin/accessories/general.accessory_category'),
+ trans('admin/accessories/general.total'),
+ trans('admin/accessories/general.remaining'),
+ ];
+ fputcsv($handle, $header);
- $rows[] = implode(',', $row);
- }
+ $formatter = new EscapeFormula('`');
- $csv = implode("\n", $rows);
- $response = response()->make($csv, 200);
- $response->header('Content-Type', 'text/csv');
- $response->header('Content-disposition', 'attachment;filename=report.csv');
+ Accessory::with('category')->orderBy('created_at', 'DESC')
+ ->chunk(500, function ($accessories) use ($handle, $formatter) {
+ foreach ($accessories as $accessory) {
+ $row = [
+ $accessory->name,
+ $accessory->category?->name,
+ $accessory->qty,
+ $accessory->numRemaining(),
+ ];
+
+ if (config('app.escape_formulas') === false) {
+ fputcsv($handle, $row);
+ } else {
+ fputcsv($handle, $formatter->escapeRecord($row));
+ }
+ }
+ });
+
+ fclose($handle);
+ }, 200, [
+ 'Content-Type' => 'text/csv',
+ 'Content-Disposition' => 'attachment; filename="accessories-report-'.date('Y-m-d-his').'.csv"',
+ ]);
return $response;
}
diff --git a/tests/Feature/Reporting/AccessoryReportTest.php b/tests/Feature/Reporting/AccessoryReportTest.php
new file mode 100644
index 0000000000..60e4fcff1e
--- /dev/null
+++ b/tests/Feature/Reporting/AccessoryReportTest.php
@@ -0,0 +1,72 @@
+<?php
+
+namespace Tests\Feature\Reporting;
+
+use App\Models\Accessory;
+use App\Models\User;
+use Tests\TestCase;
+
+class AccessoryReportTest extends TestCase
+{
+ public function test_requires_permission_to_export_accessory_report()
+ {
+ $this->actingAs(User::factory()->create())
+ ->get(route('reports/export/accessories'))
+ ->assertForbidden();
+ }
+
+ public function test_export_returns_csv_with_correct_headers()
+ {
+ $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/export/accessories'))
+ ->assertOk()
+ ->assertHeader('content-type', 'text/csv; charset=utf-8');
+ }
+
+ public function test_export_contains_accessory_data()
+ {
+ $accessory = Accessory::factory()->create(['name' => 'Test Widget', 'qty' => 10]);
+
+ $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/export/accessories'))
+ ->assertOk()
+ ->assertSeeTextInStreamedResponse('Test Widget');
+ }
+
+ public function test_export_contains_category_name()
+ {
+ $accessory = Accessory::factory()->create(['name' => 'Categorized Item', 'qty' => 5]);
+
+ $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/export/accessories'))
+ ->assertOk()
+ ->assertSeeTextInStreamedResponse($accessory->category->name);
+ }
+
+ public function test_header_row_appears_only_once()
+ {
+ Accessory::factory()->count(3)->create();
+
+ $response = $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/export/accessories'));
+
+ $response->assertOk();
+
+ $content = $response->streamedContent();
+ $this->assertSame(1, substr_count($content, 'Accessory Name'));
+ }
+
+ public function test_export_reflects_qty_and_remaining()
+ {
+ $accessory = Accessory::factory()->create(['name' => 'Qty Check', 'qty' => 8]);
+
+ $response = $this->actingAs(User::factory()->canViewReports()->create())
+ ->get(route('reports/export/accessories'));
+
+ $response->assertOk();
+
+ $content = $response->streamedContent();
+ $this->assertStringContainsString('Qty Check', $content);
+ $this->assertStringContainsString('8', $content);
+ }
+}