Skip to content

A free open source IT asset/license management system

Added tests

snipe772f4357b97a
diff --git a/tests/Feature/Accessories/AccessoryForceDeletedImageHookTest.php b/tests/Feature/Accessories/AccessoryForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..d2ab26c50b
--- /dev/null
+++ b/tests/Feature/Accessories/AccessoryForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Accessories;
+
+use App\Models\Accessory;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Accessory::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class AccessoryForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $accessory = Accessory::factory()->create(['image' => 'mouse-pad.png']);
+ Storage::disk('public')->put('accessories/mouse-pad.png', 'fake image bytes');
+
+ $accessory->delete();
+
+ Storage::disk('public')->assertExists('accessories/mouse-pad.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $accessory = Accessory::factory()->create(['image' => 'mouse-pad.png']);
+ Storage::disk('public')->put('accessories/mouse-pad.png', 'fake image bytes');
+
+ $accessory->forceDelete();
+
+ Storage::disk('public')->assertMissing('accessories/mouse-pad.png');
+ }
+}
diff --git a/tests/Feature/AssetModels/AssetModelForceDeletedImageHookTest.php b/tests/Feature/AssetModels/AssetModelForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..efda250bb0
--- /dev/null
+++ b/tests/Feature/AssetModels/AssetModelForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\AssetModels;
+
+use App\Models\AssetModel;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers AssetModel::booted()'s forceDeleted hook: hard-delete removes
+ * the image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class AssetModelForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $model = AssetModel::factory()->create(['image' => 'macbook-pro.png']);
+ Storage::disk('public')->put('models/macbook-pro.png', 'fake image bytes');
+
+ $model->delete();
+
+ Storage::disk('public')->assertExists('models/macbook-pro.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $model = AssetModel::factory()->create(['image' => 'macbook-pro.png']);
+ Storage::disk('public')->put('models/macbook-pro.png', 'fake image bytes');
+
+ $model->forceDelete();
+
+ Storage::disk('public')->assertMissing('models/macbook-pro.png');
+ }
+}
diff --git a/tests/Feature/Assets/AssetForceDeletedImageHookTest.php b/tests/Feature/Assets/AssetForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..0984d9634c
--- /dev/null
+++ b/tests/Feature/Assets/AssetForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Assets;
+
+use App\Models\Asset;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Asset::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class AssetForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $asset = Asset::factory()->create(['image' => 'asset-hero.png']);
+ Storage::disk('public')->put('assets/asset-hero.png', 'fake image bytes');
+
+ $asset->delete();
+
+ Storage::disk('public')->assertExists('assets/asset-hero.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $asset = Asset::factory()->create(['image' => 'asset-hero.png']);
+ Storage::disk('public')->put('assets/asset-hero.png', 'fake image bytes');
+
+ $asset->forceDelete();
+
+ Storage::disk('public')->assertMissing('assets/asset-hero.png');
+ }
+}
diff --git a/tests/Feature/Categories/CategoryForceDeletedImageHookTest.php b/tests/Feature/Categories/CategoryForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..5eb7efc5d5
--- /dev/null
+++ b/tests/Feature/Categories/CategoryForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Categories;
+
+use App\Models\Category;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Category::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class CategoryForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $category = Category::factory()->create(['image' => 'cat-icon.png']);
+ Storage::disk('public')->put('categories/cat-icon.png', 'fake image bytes');
+
+ $category->delete();
+
+ Storage::disk('public')->assertExists('categories/cat-icon.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $category = Category::factory()->create(['image' => 'cat-icon.png']);
+ Storage::disk('public')->put('categories/cat-icon.png', 'fake image bytes');
+
+ $category->forceDelete();
+
+ Storage::disk('public')->assertMissing('categories/cat-icon.png');
+ }
+}
diff --git a/tests/Feature/Companies/CompanyForceDeletedImageHookTest.php b/tests/Feature/Companies/CompanyForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..6a898b29c6
--- /dev/null
+++ b/tests/Feature/Companies/CompanyForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Companies;
+
+use App\Models\Company;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Company::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class CompanyForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $company = Company::factory()->create(['image' => 'acme-logo.png']);
+ Storage::disk('public')->put('companies/acme-logo.png', 'fake image bytes');
+
+ $company->delete();
+
+ Storage::disk('public')->assertExists('companies/acme-logo.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $company = Company::factory()->create(['image' => 'acme-logo.png']);
+ Storage::disk('public')->put('companies/acme-logo.png', 'fake image bytes');
+
+ $company->forceDelete();
+
+ Storage::disk('public')->assertMissing('companies/acme-logo.png');
+ }
+}
diff --git a/tests/Feature/Components/ComponentForceDeletedImageHookTest.php b/tests/Feature/Components/ComponentForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..17c081f690
--- /dev/null
+++ b/tests/Feature/Components/ComponentForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Components;
+
+use App\Models\Component;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Component::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class ComponentForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $component = Component::factory()->create(['image' => 'ram-stick.png']);
+ Storage::disk('public')->put('components/ram-stick.png', 'fake image bytes');
+
+ $component->delete();
+
+ Storage::disk('public')->assertExists('components/ram-stick.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $component = Component::factory()->create(['image' => 'ram-stick.png']);
+ Storage::disk('public')->put('components/ram-stick.png', 'fake image bytes');
+
+ $component->forceDelete();
+
+ Storage::disk('public')->assertMissing('components/ram-stick.png');
+ }
+}
diff --git a/tests/Feature/Consumables/ConsumableForceDeletedImageHookTest.php b/tests/Feature/Consumables/ConsumableForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..291ec37cfc
--- /dev/null
+++ b/tests/Feature/Consumables/ConsumableForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Consumables;
+
+use App\Models\Consumable;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Consumable::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class ConsumableForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $consumable = Consumable::factory()->create(['image' => 'ink-cart.png']);
+ Storage::disk('public')->put('consumables/ink-cart.png', 'fake image bytes');
+
+ $consumable->delete();
+
+ Storage::disk('public')->assertExists('consumables/ink-cart.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $consumable = Consumable::factory()->create(['image' => 'ink-cart.png']);
+ Storage::disk('public')->put('consumables/ink-cart.png', 'fake image bytes');
+
+ $consumable->forceDelete();
+
+ Storage::disk('public')->assertMissing('consumables/ink-cart.png');
+ }
+}
diff --git a/tests/Feature/Departments/DepartmentForceDeletedImageHookTest.php b/tests/Feature/Departments/DepartmentForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..4c18d56545
--- /dev/null
+++ b/tests/Feature/Departments/DepartmentForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Departments;
+
+use App\Models\Department;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Department::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class DepartmentForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $department = Department::factory()->create(['image' => 'dept-badge.png']);
+ Storage::disk('public')->put('departments/dept-badge.png', 'fake image bytes');
+
+ $department->delete();
+
+ Storage::disk('public')->assertExists('departments/dept-badge.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $department = Department::factory()->create(['image' => 'dept-badge.png']);
+ Storage::disk('public')->put('departments/dept-badge.png', 'fake image bytes');
+
+ $department->forceDelete();
+
+ Storage::disk('public')->assertMissing('departments/dept-badge.png');
+ }
+}
diff --git a/tests/Feature/Locations/LocationForceDeletedImageHookTest.php b/tests/Feature/Locations/LocationForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..9c8f1cdc53
--- /dev/null
+++ b/tests/Feature/Locations/LocationForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Locations;
+
+use App\Models\Location;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Location::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class LocationForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $location = Location::factory()->create(['image' => 'warehouse.png']);
+ Storage::disk('public')->put('locations/warehouse.png', 'fake image bytes');
+
+ $location->delete();
+
+ Storage::disk('public')->assertExists('locations/warehouse.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $location = Location::factory()->create(['image' => 'warehouse.png']);
+ Storage::disk('public')->put('locations/warehouse.png', 'fake image bytes');
+
+ $location->forceDelete();
+
+ Storage::disk('public')->assertMissing('locations/warehouse.png');
+ }
+}
diff --git a/tests/Feature/Manufacturers/ManufacturerForceDeletedImageHookTest.php b/tests/Feature/Manufacturers/ManufacturerForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..e2210bea7c
--- /dev/null
+++ b/tests/Feature/Manufacturers/ManufacturerForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Manufacturers;
+
+use App\Models\Manufacturer;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Manufacturer::booted()'s forceDeleted hook: hard-delete removes
+ * the image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class ManufacturerForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $manufacturer = Manufacturer::factory()->create(['image' => 'maker-logo.png']);
+ Storage::disk('public')->put('manufacturers/maker-logo.png', 'fake image bytes');
+
+ $manufacturer->delete();
+
+ Storage::disk('public')->assertExists('manufacturers/maker-logo.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $manufacturer = Manufacturer::factory()->create(['image' => 'maker-logo.png']);
+ Storage::disk('public')->put('manufacturers/maker-logo.png', 'fake image bytes');
+
+ $manufacturer->forceDelete();
+
+ Storage::disk('public')->assertMissing('manufacturers/maker-logo.png');
+ }
+}
diff --git a/tests/Feature/Suppliers/SupplierForceDeletedImageHookTest.php b/tests/Feature/Suppliers/SupplierForceDeletedImageHookTest.php
new file mode 100644
index 0000000000..7cb373ea54
--- /dev/null
+++ b/tests/Feature/Suppliers/SupplierForceDeletedImageHookTest.php
@@ -0,0 +1,37 @@
+<?php
+
+namespace Tests\Feature\Suppliers;
+
+use App\Models\Supplier;
+use Illuminate\Support\Facades\Storage;
+use Tests\TestCase;
+
+/**
+ * Covers Supplier::booted()'s forceDeleted hook: hard-delete removes the
+ * image file on the public disk, soft-delete leaves it so a subsequent
+ * restore comes back with the image intact.
+ */
+class SupplierForceDeletedImageHookTest extends TestCase
+{
+ public function test_soft_delete_preserves_image_file(): void
+ {
+ Storage::fake('public');
+ $supplier = Supplier::factory()->create(['image' => 'vendor-logo.png']);
+ Storage::disk('public')->put('suppliers/vendor-logo.png', 'fake image bytes');
+
+ $supplier->delete();
+
+ Storage::disk('public')->assertExists('suppliers/vendor-logo.png');
+ }
+
+ public function test_force_delete_wipes_image_file(): void
+ {
+ Storage::fake('public');
+ $supplier = Supplier::factory()->create(['image' => 'vendor-logo.png']);
+ Storage::disk('public')->put('suppliers/vendor-logo.png', 'fake image bytes');
+
+ $supplier->forceDelete();
+
+ Storage::disk('public')->assertMissing('suppliers/vendor-logo.png');
+ }
+}
diff --git a/tests/Unit/Enums/FileStorageTest.php b/tests/Unit/Enums/FileStorageTest.php
index 796973e11d..4a57839d1e 100644
--- a/tests/Unit/Enums/FileStorageTest.php
+++ b/tests/Unit/Enums/FileStorageTest.php
@@ -111,4 +111,68 @@ class FileStorageTest extends TestCase
// because older archives still ship the old subdir names.
$this->assertArrayHasKey('storage/private_uploads/assetmodels', $pruneable);
}
+
+ public function test_private_storage_key_resolves_to_default_disk_key_with_trailing_slash(): void
+ {
+ $this->assertSame('private_uploads/signatures/', FileStorage::Signatures->privateStorageKey());
+ $this->assertSame('private_uploads/users/', FileStorage::Users->privateStorageKey());
+ $this->assertSame('private_uploads/eula-pdfs/', FileStorage::EulaPdfs->privateStorageKey());
+ $this->assertSame('private_uploads/accessories/', FileStorage::Accessories->privateStorageKey());
+ }
+
+ public function test_private_storage_key_special_cases_backups_outside_private_uploads(): void
+ {
+ // Backups live at storage/app/backups (Spatie's convention), not
+ // under storage/app/private_uploads, so the default-disk key has
+ // no private_uploads/ prefix.
+ $this->assertSame('backups/', FileStorage::Backups->privateStorageKey());
+ }
+
+ public function test_private_storage_key_is_null_for_public_only_cases(): void
+ {
+ $this->assertNull(FileStorage::Avatars->privateStorageKey());
+ $this->assertNull(FileStorage::Barcodes->privateStorageKey());
+ }
+
+ public function test_model_class_resolves_each_case_to_its_eloquent_owner(): void
+ {
+ $this->assertSame(\App\Models\Accessory::class, FileStorage::Accessories->modelClass());
+ $this->assertSame(\App\Models\Asset::class, FileStorage::Assets->modelClass());
+ $this->assertSame(\App\Models\Category::class, FileStorage::Categories->modelClass());
+ $this->assertSame(\App\Models\Company::class, FileStorage::Companies->modelClass());
+ $this->assertSame(\App\Models\Component::class, FileStorage::Components->modelClass());
+ $this->assertSame(\App\Models\Consumable::class, FileStorage::Consumables->modelClass());
+ $this->assertSame(\App\Models\Department::class, FileStorage::Departments->modelClass());
+ $this->assertSame(\App\Models\License::class, FileStorage::Licenses->modelClass());
+ $this->assertSame(\App\Models\Location::class, FileStorage::Locations->modelClass());
+ $this->assertSame(\App\Models\Maintenance::class, FileStorage::Maintenances->modelClass());
+ $this->assertSame(\App\Models\Manufacturer::class, FileStorage::Manufacturers->modelClass());
+ $this->assertSame(\App\Models\Supplier::class, FileStorage::Suppliers->modelClass());
+ }
+
+ public function test_model_class_maps_rename_cases_to_their_owner(): void
+ {
+ // Avatars is the public case for the User model's avatar column,
+ // Users is the private case for User's Files-tab uploads. Both
+ // tie back to User despite the two case names.
+ $this->assertSame(\App\Models\User::class, FileStorage::Avatars->modelClass());
+ $this->assertSame(\App\Models\User::class, FileStorage::Users->modelClass());
+
+ // Models is the public case for the AssetModel model's image.
+ $this->assertSame(\App\Models\AssetModel::class, FileStorage::Models->modelClass());
+ }
+
+ public function test_model_class_is_null_for_non_model_owned_cases(): void
+ {
+ // Signatures, EULA PDFs, audits, imports, backups, and the
+ // barcode cache do not belong to a single parent model, so
+ // modelClass() returns null for all of them. Callers iterating
+ // cases to build a model->path map skip these.
+ $this->assertNull(FileStorage::Audits->modelClass());
+ $this->assertNull(FileStorage::Backups->modelClass());
+ $this->assertNull(FileStorage::Barcodes->modelClass());
+ $this->assertNull(FileStorage::EulaPdfs->modelClass());
+ $this->assertNull(FileStorage::Imports->modelClass());
+ $this->assertNull(FileStorage::Signatures->modelClass());
+ }
}