Skip to content

A free open source IT asset/license management system

Added more tests for file storage enums

snipe0f3d10fa2bf2
diff --git a/app/Enums/FileStorage.php b/app/Enums/FileStorage.php
index b09cc06a29..f06b8d2535 100644
--- a/app/Enums/FileStorage.php
+++ b/app/Enums/FileStorage.php
@@ -2,6 +2,8 @@
namespace App\Enums;
+use App\Models\Traits\HasUploads;
+
/**
* Single source of truth for file-storage directory names across the app.
*
@@ -91,28 +93,39 @@ enum FileStorage: string
};
}
+ /**
+ * Private cases without a parent model. Everything model-backed is
+ * resolved by reflecting on HasUploads so adding the trait to a new
+ * model is the only step needed to light up its file-upload path.
+ */
+ private const NON_MODEL_PRIVATE_CASES = [
+ 'audits',
+ 'backups',
+ 'eula-pdfs',
+ 'imports',
+ 'signatures',
+ ];
+
public function hasPrivateScope(): bool
{
- return match ($this) {
- self::Accessories,
- self::Assets,
- self::Audits,
- self::Backups,
- self::Companies,
- self::Components,
- self::Consumables,
- self::Departments,
- self::EulaPdfs,
- self::Imports,
- self::Licenses,
- self::Locations,
- self::Maintenances,
- self::Models,
- self::Signatures,
- self::Suppliers,
- self::Users => true,
- default => false,
- };
+ if (in_array($this->value, self::NON_MODEL_PRIVATE_CASES, true)) {
+ return true;
+ }
+
+ // Avatars is a public-only image alias for User. The private
+ // attachment bucket for User lives in self::Users, so skip
+ // Avatars before the trait check (which would otherwise mark
+ // it private via User's HasUploads use).
+ if ($this === self::Avatars) {
+ return false;
+ }
+
+ $model = $this->modelClass();
+ if ($model === null) {
+ return false;
+ }
+
+ return in_array(HasUploads::class, class_uses_recursive($model), true);
}
/**
diff --git a/tests/Unit/Enums/FileStorageTest.php b/tests/Unit/Enums/FileStorageTest.php
index 244dc889a4..8f341698ce 100644
--- a/tests/Unit/Enums/FileStorageTest.php
+++ b/tests/Unit/Enums/FileStorageTest.php
@@ -172,11 +172,36 @@ class FileStorageTest extends TestCase
// crashed with a TypeError when uploading. Pin the invariant
// that every entry in the controller map produces a usable path.
foreach (Controller::getMapStoragePath() as $resource => $path) {
- $this->assertIsString($path, "No private storage key for '$resource'. Add the matching case to FileStorage::hasPrivateScope().");
+ $this->assertIsString($path, "No private storage key for '$resource'. Add HasUploads to the model, or add the case to FileStorage::NON_MODEL_PRIVATE_CASES.");
$this->assertNotSame('', $path);
}
}
+ public function test_every_has_uploads_model_resolves_to_a_private_file_storage_case(): void
+ {
+ // Walk every HasUploads model and confirm its FileStorage case is marked private.
+ $hasUploadsModels = [];
+ foreach (FileStorage::cases() as $case) {
+ $model = $case->modelClass();
+ if ($model === null) {
+ continue;
+ }
+ if (in_array(\App\Models\Traits\HasUploads::class, class_uses_recursive($model), true)) {
+ $hasUploadsModels[$model] = $case;
+ }
+ }
+
+ $this->assertNotEmpty($hasUploadsModels, 'No HasUploads models mapped through FileStorage::modelClass(). Did the mapping drift?');
+
+ foreach ($hasUploadsModels as $model => $case) {
+ $this->assertTrue(
+ $case->hasPrivateScope(),
+ "Model $model uses HasUploads but FileStorage::{$case->name}->hasPrivateScope() returned false.",
+ );
+ $this->assertIsString($case->privateStorageKey());
+ }
+ }
+
public function test_model_class_is_null_for_non_model_owned_cases(): void
{
// Signatures, EULA PDFs, audits, imports, backups, and the