Skip to content

A free open source IT asset/license management system

Better cleanup all the temp files created by running tests

snipe00a5236542d6
diff --git a/tests/Feature/Console/Commands/AuditFileStorageTest.php b/tests/Feature/Console/Commands/AuditFileStorageTest.php
index a5931fb429..1d625ea1b8 100644
--- a/tests/Feature/Console/Commands/AuditFileStorageTest.php
+++ b/tests/Feature/Console/Commands/AuditFileStorageTest.php
@@ -246,7 +246,8 @@ class AuditFileStorageTest extends TestCase
private function tempCsvPath(): string
{
if ($this->csvPath === null) {
- $this->csvPath = tempnam(sys_get_temp_dir(), 'audit-file-storage-').'.csv';
+ $stub = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'audit-file-storage-'));
+ $this->csvPath = $this->trackTempFile($stub.'.csv');
}
return $this->csvPath;
diff --git a/tests/Feature/FileUploads/SvgUploadSanitizationTest.php b/tests/Feature/FileUploads/SvgUploadSanitizationTest.php
index 668841db96..7c260b51c3 100644
--- a/tests/Feature/FileUploads/SvgUploadSanitizationTest.php
+++ b/tests/Feature/FileUploads/SvgUploadSanitizationTest.php
@@ -27,32 +27,20 @@ use Tests\TestCase;
*/
class SvgUploadSanitizationTest extends TestCase
{
- private array $tempFiles = [];
-
protected function setUp(): void
{
parent::setUp();
Storage::fake();
}
- protected function tearDown(): void
- {
- foreach ($this->tempFiles as $path) {
- @unlink($path);
- }
-
- parent::tearDown();
- }
-
private function realUpload(string $clientName, string $content): UploadedFile
{
// A real temp file is required for finfo to sniff the MIME
// correctly. UploadedFile::fake() derives its mime from the
// filename, which would trivially "pass" the SVG detection
// without exercising the real sanitize path.
- $path = tempnam(sys_get_temp_dir(), 'snipeit_svg_');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'snipeit_svg_'));
file_put_contents($path, $content);
- $this->tempFiles[] = $path;
return new UploadedFile($path, $clientName, null, null, true);
}
diff --git a/tests/Feature/FileUploads/UploadFileValidationTest.php b/tests/Feature/FileUploads/UploadFileValidationTest.php
index 12495d8dc8..16ac6afcc3 100644
--- a/tests/Feature/FileUploads/UploadFileValidationTest.php
+++ b/tests/Feature/FileUploads/UploadFileValidationTest.php
@@ -19,28 +19,16 @@ use Tests\TestCase;
// these tests use real temp files.
class UploadFileValidationTest extends TestCase
{
- private array $tempFiles = [];
-
protected function setUp(): void
{
parent::setUp();
Storage::fake();
}
- protected function tearDown(): void
- {
- foreach ($this->tempFiles as $path) {
- @unlink($path);
- }
-
- parent::tearDown();
- }
-
private function realUpload(string $clientName, string $content): UploadedFile
{
- $path = tempnam(sys_get_temp_dir(), 'snipeit_upload_');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'snipeit_upload_'));
file_put_contents($path, $content);
- $this->tempFiles[] = $path;
return new UploadedFile($path, $clientName, null, null, true);
}
diff --git a/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php b/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php
index e7db5cebd6..470e769278 100644
--- a/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php
+++ b/tests/Feature/Importing/Api/ImportDuplicateHeaderDetectionTest.php
@@ -117,7 +117,7 @@ class ImportDuplicateHeaderDetectionTest extends TestCase
private function fakeCsvUpload(string $name, string $contents): UploadedFile
{
- $path = tempnam(sys_get_temp_dir(), 'csv');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'csv'));
file_put_contents($path, $contents);
return new UploadedFile($path, $name, 'text/csv', null, true);
diff --git a/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php b/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php
index 512a4beac7..9198d87e5d 100644
--- a/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php
+++ b/tests/Feature/Importing/Api/ImportStoreMultiFileRowPerFileTest.php
@@ -85,7 +85,7 @@ class ImportStoreMultiFileRowPerFileTest extends TestCase
private function fakeCsvUpload(string $name, string $contents): UploadedFile
{
- $path = tempnam(sys_get_temp_dir(), 'csv');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'csv'));
file_put_contents($path, $contents);
return new UploadedFile($path, $name, 'text/csv', null, true);
diff --git a/tests/Feature/Importing/AssetImportCheckoutAcceptanceTest.php b/tests/Feature/Importing/AssetImportCheckoutAcceptanceTest.php
index 1166355152..823ad06990 100644
--- a/tests/Feature/Importing/AssetImportCheckoutAcceptanceTest.php
+++ b/tests/Feature/Importing/AssetImportCheckoutAcceptanceTest.php
@@ -11,7 +11,6 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -31,14 +30,6 @@ use Tests\TestCase;
*/
class AssetImportCheckoutAcceptanceTest extends TestCase
{
- private function createFakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
-
#[Test]
public function importing_a_user_checkout_creates_an_acceptance_record_when_category_requires_it(): void
{
@@ -65,7 +56,7 @@ class AssetImportCheckoutAcceptanceTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('acceptance.csv', $csv),
+ $this->tempUploadedFile('acceptance.csv', $csv),
],
])
->assertSuccessful();
@@ -124,7 +115,7 @@ class AssetImportCheckoutAcceptanceTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('acceptance-location.csv', $csv),
+ $this->tempUploadedFile('acceptance-location.csv', $csv),
],
])
->assertSuccessful();
diff --git a/tests/Feature/Importing/AssetImportCheckoutTargetsTest.php b/tests/Feature/Importing/AssetImportCheckoutTargetsTest.php
index e351064821..b7778f8e3a 100644
--- a/tests/Feature/Importing/AssetImportCheckoutTargetsTest.php
+++ b/tests/Feature/Importing/AssetImportCheckoutTargetsTest.php
@@ -10,7 +10,6 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -40,7 +39,7 @@ class AssetImportCheckoutTargetsTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('checkout-targets.csv', $csv),
+ $this->tempUploadedFile('checkout-targets.csv', $csv),
],
])
->assertSuccessful();
@@ -56,14 +55,6 @@ class AssetImportCheckoutTargetsTest extends TestCase
->assertSuccessful();
}
- private function createFakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
-
#[Test]
public function checkout_location_column_alone_checks_asset_out_to_the_named_location(): void
{
diff --git a/tests/Feature/Importing/AssetImportCheckoutWelcomeEmailTest.php b/tests/Feature/Importing/AssetImportCheckoutWelcomeEmailTest.php
index 9cb391ddd7..be93732dcc 100644
--- a/tests/Feature/Importing/AssetImportCheckoutWelcomeEmailTest.php
+++ b/tests/Feature/Importing/AssetImportCheckoutWelcomeEmailTest.php
@@ -11,7 +11,6 @@ use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
use App\Notifications\WelcomeNotification;
-use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Notification;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -38,7 +37,7 @@ class AssetImportCheckoutWelcomeEmailTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('welcome.csv', $csv),
+ $this->tempUploadedFile('welcome.csv', $csv),
],
])
->assertSuccessful();
@@ -54,14 +53,6 @@ class AssetImportCheckoutWelcomeEmailTest extends TestCase
->assertSuccessful();
}
- private function createFakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
-
#[Test]
public function fires_when_checkout_creates_a_new_user_and_the_flag_is_on(): void
{
diff --git a/tests/Feature/Importing/AssetImportCreatedAtTest.php b/tests/Feature/Importing/AssetImportCreatedAtTest.php
index 9c859aebbb..3e85375415 100644
--- a/tests/Feature/Importing/AssetImportCreatedAtTest.php
+++ b/tests/Feature/Importing/AssetImportCreatedAtTest.php
@@ -10,7 +10,6 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -54,7 +53,7 @@ class AssetImportCreatedAtTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('test.csv', $csv),
+ $this->tempUploadedFile('test.csv', $csv),
],
])
->assertSuccessful();
@@ -128,7 +127,7 @@ class AssetImportCreatedAtTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('test1.csv', $csv),
+ $this->tempUploadedFile('test1.csv', $csv),
],
])
->assertSuccessful();
@@ -151,7 +150,7 @@ class AssetImportCreatedAtTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('test2.csv', $csv),
+ $this->tempUploadedFile('test2.csv', $csv),
],
])
->assertSuccessful();
@@ -180,21 +179,4 @@ class AssetImportCreatedAtTest extends TestCase
);
}
}
-
- /**
- * Helper method to create a fake uploaded file
- */
- protected function createFakeUploadedFile(string $filename, string $content)
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile(
- $path,
- $filename,
- 'text/csv',
- null,
- true
- );
- }
}
diff --git a/tests/Feature/Importing/AssetImportCreatedByTest.php b/tests/Feature/Importing/AssetImportCreatedByTest.php
index acc149c8e9..3efc5bb655 100644
--- a/tests/Feature/Importing/AssetImportCreatedByTest.php
+++ b/tests/Feature/Importing/AssetImportCreatedByTest.php
@@ -11,7 +11,6 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -44,7 +43,7 @@ class AssetImportCreatedByTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('createdby.csv', $csv),
+ $this->tempUploadedFile('createdby.csv', $csv),
],
])
->assertSuccessful();
@@ -108,7 +107,7 @@ class AssetImportCreatedByTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('createdby-update.csv', $csv),
+ $this->tempUploadedFile('createdby-update.csv', $csv),
],
])
->assertSuccessful();
@@ -159,7 +158,7 @@ class AssetImportCreatedByTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('createdby-user.csv', $csv),
+ $this->tempUploadedFile('createdby-user.csv', $csv),
],
])
->assertSuccessful();
@@ -211,7 +210,7 @@ class AssetImportCreatedByTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('createdby-dept.csv', $csv),
+ $this->tempUploadedFile('createdby-dept.csv', $csv),
],
])
->assertSuccessful();
@@ -248,12 +247,4 @@ class AssetImportCreatedByTest extends TestCase
'The department created for the CSV row must be linked to the auto-created checkout-target user (department propagation).'
);
}
-
- protected function createFakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
}
diff --git a/tests/Feature/Importing/AssetImportDefaultLocationTest.php b/tests/Feature/Importing/AssetImportDefaultLocationTest.php
index 340fc27368..376c8bbaf5 100644
--- a/tests/Feature/Importing/AssetImportDefaultLocationTest.php
+++ b/tests/Feature/Importing/AssetImportDefaultLocationTest.php
@@ -10,40 +10,11 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class AssetImportDefaultLocationTest extends TestCase
{
- /** @var array<int, string> */
- private array $uploadedTempPaths = [];
-
- protected function tearDown(): void
- {
- // tempnam() writes into /tmp and Laravel's test harness doesn't
- // clean them up by itself, so we track + unlink per-test to keep
- // CI runners' /tmp from accreting a CSV per import test. Also
- // clean out the imports storage directory for files the import
- // controller moved into place during the test.
- foreach ($this->uploadedTempPaths as $path) {
- @unlink($path);
- }
- foreach (Import::query()->get() as $import) {
- @unlink(config('app.private_uploads').'/imports/'.$import->file_path);
- }
- parent::tearDown();
- }
-
- private function createFakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
- $this->uploadedTempPaths[] = $path;
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
-
private function runAssetImport(string $csv, array $mappings): void
{
Category::factory()->create(['category_type' => 'asset', 'name' => 'Default Loc Test Category']);
@@ -58,7 +29,7 @@ class AssetImportDefaultLocationTest extends TestCase
$this->actingAsForApi($importer)
->postJson(route('api.imports.store'), [
'files' => [
- $this->createFakeUploadedFile('default-loc.csv', $csv),
+ $this->tempUploadedFile('default-loc.csv', $csv),
],
])
->assertSuccessful();
diff --git a/tests/Feature/Importing/AssetImportPreserveBlanksTest.php b/tests/Feature/Importing/AssetImportPreserveBlanksTest.php
index 3060f68792..214097788b 100644
--- a/tests/Feature/Importing/AssetImportPreserveBlanksTest.php
+++ b/tests/Feature/Importing/AssetImportPreserveBlanksTest.php
@@ -10,7 +10,6 @@ use App\Models\Import;
use App\Models\Location;
use App\Models\Statuslabel;
use App\Models\User;
-use Illuminate\Http\UploadedFile;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
@@ -136,7 +135,7 @@ class AssetImportPreserveBlanksTest extends TestCase
{
$this->actingAsForApi($actor)
->postJson(route('api.imports.store'), [
- 'files' => [$this->fakeUploadedFile('test.csv', $csv)],
+ 'files' => [$this->tempUploadedFile('test.csv', $csv)],
])
->assertSuccessful();
@@ -146,12 +145,4 @@ class AssetImportPreserveBlanksTest extends TestCase
->postJson(route('api.imports.importFile', $import->id), $processPayload)
->assertSuccessful();
}
-
- private function fakeUploadedFile(string $filename, string $content): UploadedFile
- {
- $path = tempnam(sys_get_temp_dir(), 'csv');
- file_put_contents($path, $content);
-
- return new UploadedFile($path, $filename, 'text/csv', null, true);
- }
}
diff --git a/tests/Feature/Uploads/ImageUploadRequestDimensionsLimitsTest.php b/tests/Feature/Uploads/ImageUploadRequestDimensionsLimitsTest.php
index 17c03e9615..5d8bcb82b1 100644
--- a/tests/Feature/Uploads/ImageUploadRequestDimensionsLimitsTest.php
+++ b/tests/Feature/Uploads/ImageUploadRequestDimensionsLimitsTest.php
@@ -28,7 +28,7 @@ class ImageUploadRequestDimensionsLimitsTest extends TestCase
$user = User::factory()->create(['avatar' => null]);
- $path = tempnam(sys_get_temp_dir(), 'png');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'png'));
file_put_contents($path, $this->craftPngWithIhdrDimensions(50000, 50000));
$upload = new UploadedFile($path, 'bomb.png', 'image/png', null, true);
@@ -53,7 +53,7 @@ class ImageUploadRequestDimensionsLimitsTest extends TestCase
{
Storage::fake('public');
- $path = tempnam(sys_get_temp_dir(), 'png');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'png'));
file_put_contents($path, $this->craftPngWithIhdrDimensions(2048, 2048));
$upload = new UploadedFile($path, 'big-favicon.png', 'image/png', null, true);
@@ -81,7 +81,7 @@ class ImageUploadRequestDimensionsLimitsTest extends TestCase
{
Storage::fake('public');
- $path = tempnam(sys_get_temp_dir(), 'png');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'png'));
file_put_contents($path, $this->craftPngWithIhdrDimensions(4097, 4097));
$upload = new UploadedFile($path, 'just-over.png', 'image/png', null, true);
@@ -111,7 +111,7 @@ class ImageUploadRequestDimensionsLimitsTest extends TestCase
// Craft a legitimate-dimension PNG padded to just over the PHP
// upload limit so dimensions + mime pass, and only the `max:`
// rule can be the one that rejects it.
- $path = tempnam(sys_get_temp_dir(), 'png');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'png'));
$header = $this->craftPngWithIhdrDimensions(400, 400);
file_put_contents($path, $header.str_repeat("\0", \App\Helpers\Helper::file_upload_max_size() + 1024 - strlen($header)));
$upload = new UploadedFile($path, 'oversized.png', 'image/png', null, true);
@@ -156,7 +156,7 @@ class ImageUploadRequestDimensionsLimitsTest extends TestCase
$manufacturer = Manufacturer::factory()->create(['image' => null]);
- $path = tempnam(sys_get_temp_dir(), 'png');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'png'));
file_put_contents($path, $this->craftPngWithIhdrDimensions(50000, 50000));
$upload = new UploadedFile($path, 'bomb.png', 'image/png', null, true);
diff --git a/tests/TestCase.php b/tests/TestCase.php
index 2dee21cad3..1e17337a3a 100644
--- a/tests/TestCase.php
+++ b/tests/TestCase.php
@@ -7,6 +7,7 @@ use App\Models\Asset;
use App\Models\Company;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
+use Illuminate\Http\UploadedFile;
use RuntimeException;
use Tests\Support\AssertHasActionLogs;
use Tests\Support\AssertsAgainstSlackNotifications;
@@ -32,6 +33,28 @@ abstract class TestCase extends BaseTestCase
SecurityHeaders::class,
];
+ /**
+ * tempnam() source paths registered via trackTempFile(), unlinked
+ * in tearDown. Laravel's test harness doesn't clean these on its
+ * own and CI /tmp accretes a file per upload test otherwise.
+ *
+ * @var array<int, string>
+ */
+ private array $uploadedTempPaths = [];
+
+ /**
+ * Snapshot of every path under Snipe-IT's upload trees at setUp
+ * time (recursive). tearDown diffs against the current contents to
+ * unlink anything new, so files any controller / Storage::put /
+ * factory happens to write during the test don't survive and
+ * pollute the local dev dirs. Covers both the private-uploads tree
+ * (user avatars, acceptance signatures, eula pdfs, imports, etc)
+ * and the public-uploads tree (barcodes, images).
+ *
+ * @var array<int, string>
+ */
+ private array $uploadTreeSnapshot = [];
+
protected function setUp(): void
{
$this->guardAgainstMissingEnv();
@@ -60,6 +83,84 @@ abstract class TestCase extends BaseTestCase
// Sync-adapter instance cache is another static that would hold
// rolled-back model references between tests otherwise.
\App\Models\SyncAdapterConfig::flushInstanceCache();
+
+ $this->uploadTreeSnapshot = $this->currentUploadTreeListing();
+ }
+
+ protected function tearDown(): void
+ {
+ foreach ($this->uploadedTempPaths as $path) {
+ @unlink($path);
+ }
+ $this->uploadedTempPaths = [];
+
+ $after = $this->currentUploadTreeListing();
+ foreach (array_diff($after, $this->uploadTreeSnapshot) as $path) {
+ @unlink($path);
+ }
+
+ parent::tearDown();
+ }
+
+ /**
+ * Shared helper for feature tests that round-trip a CSV (or any
+ * file) through an upload endpoint. Creates a tempnam-backed
+ * UploadedFile and registers the source path so base tearDown can
+ * unlink it.
+ */
+ protected function tempUploadedFile(string $filename, string $content, string $mime = 'text/csv'): UploadedFile
+ {
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'upload'));
+ file_put_contents($path, $content);
+
+ return new UploadedFile($path, $filename, $mime, null, true);
+ }
+
+ /**
+ * Register a filesystem path for cleanup in base tearDown. Returns
+ * the same path for fluent one-line use alongside tempnam() /
+ * file_put_contents() in tests that need to craft malformed PNGs,
+ * SVG fixtures, cert plants, or anything outside the shape
+ * tempUploadedFile() covers. The path does not need to exist at
+ * registration time (tearDown unlink is noop safe).
+ */
+ protected function trackTempFile(string $path): string
+ {
+ $this->uploadedTempPaths[] = $path;
+
+ return $path;
+ }
+
+ /**
+ * Recursive listing of every file (not directory) under the two
+ * upload trees Snipe-IT writes to. tearDown diffs this against the
+ * setUp snapshot and unlinks anything new. Returns real file paths
+ * only. Symlinks are followed via SplFileInfo's getPathname so a
+ * dev who has set up a bind-mounted dir still gets cleanup.
+ *
+ * @return array<int, string>
+ */
+ private function currentUploadTreeListing(): array
+ {
+ $roots = array_filter([
+ config('app.private_uploads'),
+ public_path('uploads'),
+ ], fn($root) => is_string($root) && is_dir($root));
+
+ $paths = [];
+ foreach ($roots as $root) {
+ $iterator = new \RecursiveIteratorIterator(
+ new \RecursiveDirectoryIterator($root, \FilesystemIterator::SKIP_DOTS),
+ );
+ foreach ($iterator as $info) {
+ /** @var \SplFileInfo $info */
+ if ($info->isFile()) {
+ $paths[] = $info->getPathname();
+ }
+ }
+ }
+
+ return $paths;
}
// ...existing code...
diff --git a/tests/Unit/Importer/ImporterEncodingTest.php b/tests/Unit/Importer/ImporterEncodingTest.php
index 315bcfb9dd..bf69e75d82 100644
--- a/tests/Unit/Importer/ImporterEncodingTest.php
+++ b/tests/Unit/Importer/ImporterEncodingTest.php
@@ -138,7 +138,7 @@ class ImporterEncodingTest extends TestCase
// overwhelming common case) the reader has to be built via
// Reader::createFromPath so League CSV streams from disk instead
// of holding the entire file in memory.
- $tmp = tempnam(sys_get_temp_dir(), 'snipeit_encoding_');
+ $tmp = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'snipeit_encoding_'));
file_put_contents($tmp, "name,note\ntest,greeting\n");
try {
@@ -172,7 +172,7 @@ class ImporterEncodingTest extends TestCase
// memory is proportional to filesize but the tradeoff is
// unavoidable for encoding conversion.
$gbkBody = str_repeat("\xC4\xE3\xBA\xC3", 8);
- $tmp = tempnam(sys_get_temp_dir(), 'snipeit_encoding_');
+ $tmp = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'snipeit_encoding_'));
file_put_contents($tmp, "name,note\n{$gbkBody},{$gbkBody}\n");
try {
diff --git a/tests/Unit/Rules/AllowedUploadExtensionTest.php b/tests/Unit/Rules/AllowedUploadExtensionTest.php
index 6801b314d8..b372b41596 100644
--- a/tests/Unit/Rules/AllowedUploadExtensionTest.php
+++ b/tests/Unit/Rules/AllowedUploadExtensionTest.php
@@ -10,26 +10,14 @@ use Tests\TestCase;
class AllowedUploadExtensionTest extends TestCase
{
- private array $tempFiles = [];
-
- protected function tearDown(): void
- {
- foreach ($this->tempFiles as $path) {
- @unlink($path);
- }
-
- parent::tearDown();
- }
-
// Real UploadedFile pointed at a real temp file. UploadedFile::fake()
// bypasses finfo (its getMimeType() reads MimeType::from($name), which
// is extension-only), so it can't reproduce the sniff-vs-extension
// mismatch this rule exists to tolerate.
private function realUpload(string $clientName, string $content): UploadedFile
{
- $path = tempnam(sys_get_temp_dir(), 'snipeit_rule_');
+ $path = $this->trackTempFile(tempnam(sys_get_temp_dir(), 'snipeit_rule_'));
file_put_contents($path, $content);
- $this->tempFiles[] = $path;
return new UploadedFile($path, $clientName, null, null, true);
}