Skip to content

A free open source IT asset/license management system

Added/updated tests

snipe986f101a7cd1
diff --git a/tests/Feature/Maintenances/Api/CreateMaintenanceTest.php b/tests/Feature/Maintenances/Api/CreateMaintenanceTest.php
index c743f67beb..f6a563e2d5 100644
--- a/tests/Feature/Maintenances/Api/CreateMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Api/CreateMaintenanceTest.php
@@ -55,7 +55,8 @@ class CreateMaintenanceTest extends TestCase
Storage::disk('public')->assertExists(app('maintenances_path').$maintenance->image);
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => \App\Models\Asset::class,
'supplier_id' => $supplier->id,
'maintenance_type_id' => $type->id,
'asset_maintenance_type' => $type->name,
@@ -93,7 +94,8 @@ class CreateMaintenanceTest extends TestCase
foreach ($assets as $asset) {
$this->assertDatabaseHas('maintenances', [
'name' => 'Bulk Test',
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => \App\Models\Asset::class,
'maintenance_type_id' => $type->id,
]);
}
@@ -122,8 +124,8 @@ class CreateMaintenanceTest extends TestCase
->assertJsonPath('status', 'success')
->assertJsonPath('payload.total', 1);
- $this->assertDatabaseHas('maintenances', ['asset_id' => $ownAsset->id, 'name' => 'FMCS Bulk Test']);
- $this->assertDatabaseMissing('maintenances', ['asset_id' => $otherAsset->id, 'name' => 'FMCS Bulk Test']);
+ $this->assertDatabaseHas('maintenances', ['item_id' => $ownAsset->id, 'item_type' => \App\Models\Asset::class, 'name' => 'FMCS Bulk Test']);
+ $this->assertDatabaseMissing('maintenances', ['item_id' => $otherAsset->id, 'item_type' => \App\Models\Asset::class, 'name' => 'FMCS Bulk Test']);
}
public function test_bulk_create_returns_error_when_all_assets_inaccessible()
diff --git a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php b/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
index c11ad0460a..fd50bbfa72 100644
--- a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
@@ -118,7 +118,8 @@ class EditMaintenanceTest extends TestCase
$this->assertDatabaseHas('maintenances', [
'id' => $maintenance->id,
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => \App\Models\Asset::class,
'name' => 'Updated Name',
]);
}
@@ -143,7 +144,8 @@ class EditMaintenanceTest extends TestCase
$this->assertDatabaseHas('maintenances', [
'id' => $maintenance->id,
- 'asset_id' => $assetB->id,
+ 'item_id' => $assetB->id,
+ 'item_type' => \App\Models\Asset::class,
]);
}
@@ -168,7 +170,8 @@ class EditMaintenanceTest extends TestCase
$this->assertDatabaseHas('maintenances', [
'id' => $maintenance->id,
- 'asset_id' => $assetA->id,
+ 'item_id' => $assetA->id,
+ 'item_type' => \App\Models\Asset::class,
]);
}
}
diff --git a/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php
index 204c5e6659..a1b480d351 100644
--- a/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php
@@ -142,6 +142,7 @@ class IndexMaintenanceTest extends TestCase
->assertOk();
$this->assertEquals('Annual Checkup', $response->json('maintenance_type'));
+ $this->assertEquals('Annual Checkup', $response->json('maintenance_type_details.name'));
}
public function test_sort_by_maintenance_type_does_not_error()
diff --git a/tests/Feature/Maintenances/Api/MaintenanceTypesTest.php b/tests/Feature/Maintenances/Api/MaintenanceTypesTest.php
index 055f66a569..a1f312e669 100644
--- a/tests/Feature/Maintenances/Api/MaintenanceTypesTest.php
+++ b/tests/Feature/Maintenances/Api/MaintenanceTypesTest.php
@@ -2,6 +2,8 @@
namespace Tests\Feature\Maintenances\Api;
+use App\Models\Asset;
+use App\Models\Maintenance;
use App\Models\MaintenanceType;
use App\Models\User;
use Tests\TestCase;
@@ -75,4 +77,36 @@ class MaintenanceTypesTest extends TestCase
$this->assertSoftDeleted($type);
}
+
+ public function test_cannot_delete_maintenance_type_that_is_in_use()
+ {
+ $type = MaintenanceType::factory()->create();
+ Maintenance::factory()->create([
+ 'maintenance_type_id' => $type->id,
+ 'item_id' => Asset::factory()->create()->id,
+ 'item_type' => Asset::class,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->deleteJson(route('api.maintenance-types.destroy', $type))
+ ->assertStatus(409)
+ ->assertStatusMessageIs('error');
+
+ $this->assertDatabaseHas('maintenance_types', ['id' => $type->id, 'deleted_at' => null]);
+ }
+
+ public function test_index_exposes_maintenances_count()
+ {
+ $type = MaintenanceType::factory()->create();
+ Maintenance::factory()->count(2)->create([
+ 'maintenance_type_id' => $type->id,
+ 'item_id' => Asset::factory()->create()->id,
+ 'item_type' => Asset::class,
+ ]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.maintenance-types.index'))
+ ->assertOk()
+ ->assertJsonFragment(['id' => $type->id, 'maintenances_count' => 2]);
+ }
}
diff --git a/tests/Feature/Maintenances/Ui/BulkMaintenanceTypeDeleteTest.php b/tests/Feature/Maintenances/Ui/BulkMaintenanceTypeDeleteTest.php
new file mode 100644
index 0000000000..264b59dd93
--- /dev/null
+++ b/tests/Feature/Maintenances/Ui/BulkMaintenanceTypeDeleteTest.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Tests\Feature\Maintenances\Ui;
+
+use App\Models\Asset;
+use App\Models\Maintenance;
+use App\Models\MaintenanceType;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Covers the bulk-delete endpoint on BulkMaintenanceTypesController.
+ * Same in-use guard as the per-row destroy: a type with referring
+ * maintenances is skipped and its skip surfaces on the
+ * multi_error_messages flash; unused siblings in the same batch still
+ * get deleted so a partial-success batch applies the safe rows and
+ * flags the unsafe ones by name.
+ */
+class BulkMaintenanceTypeDeleteTest extends TestCase
+{
+ public function test_bulk_delete_requires_permission()
+ {
+ $type = MaintenanceType::factory()->create();
+
+ $this->actingAs(User::factory()->create())
+ ->post(route('maintenance-types.bulk.delete'), ['ids' => [$type->id]])
+ ->assertForbidden();
+
+ $this->assertDatabaseHas('maintenance_types', ['id' => $type->id, 'deleted_at' => null]);
+ }
+
+ public function test_bulk_delete_removes_selected_unused_types()
+ {
+ $doomed = MaintenanceType::factory()->count(3)->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('maintenance-types.bulk.delete'), [
+ 'ids' => $doomed->pluck('id')->all(),
+ ])
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionHas('success')
+ ->assertSessionMissing('multi_error_messages');
+
+ $doomed->each(fn ($t) => $this->assertSoftDeleted('maintenance_types', ['id' => $t->id]));
+ }
+
+ public function test_bulk_delete_skips_types_still_in_use()
+ {
+ $inUse = MaintenanceType::factory()->create();
+ Maintenance::factory()->create([
+ 'maintenance_type_id' => $inUse->id,
+ 'item_id' => Asset::factory()->create()->id,
+ 'item_type' => Asset::class,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('maintenance-types.bulk.delete'), [
+ 'ids' => [$inUse->id],
+ ])
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionMissing('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseHas('maintenance_types', ['id' => $inUse->id, 'deleted_at' => null]);
+ }
+
+ public function test_bulk_delete_handles_mixed_batch()
+ {
+ $unused = MaintenanceType::factory()->count(2)->create();
+ $inUse = MaintenanceType::factory()->create();
+ Maintenance::factory()->create([
+ 'maintenance_type_id' => $inUse->id,
+ 'item_id' => Asset::factory()->create()->id,
+ 'item_type' => Asset::class,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('maintenance-types.bulk.delete'), [
+ 'ids' => array_merge($unused->pluck('id')->all(), [$inUse->id]),
+ ])
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $unused->each(fn ($t) => $this->assertSoftDeleted('maintenance_types', ['id' => $t->id]));
+ $this->assertDatabaseHas('maintenance_types', ['id' => $inUse->id, 'deleted_at' => null]);
+ }
+
+ public function test_bulk_delete_skips_missing_ids_without_crashing()
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('maintenance-types.bulk.delete'), [
+ 'ids' => [999999],
+ ])
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionHas('multi_error_messages');
+ }
+}
diff --git a/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php b/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
index 3311785687..a7a9390e7b 100644
--- a/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
@@ -72,7 +72,8 @@ class CreateMaintenanceTest extends TestCase
Storage::disk('local')->assertExists('private_uploads/maintenances/'.$uploadedLog->filename);
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => \App\Models\Asset::class,
'supplier_id' => $supplier->id,
'maintenance_type_id' => $type->id,
'asset_maintenance_type' => $type->name,
diff --git a/tests/Feature/Maintenances/Ui/CreateMaintenanceTrackingTest.php b/tests/Feature/Maintenances/Ui/CreateMaintenanceTrackingTest.php
index 0e5608a96b..e18f821d41 100644
--- a/tests/Feature/Maintenances/Ui/CreateMaintenanceTrackingTest.php
+++ b/tests/Feature/Maintenances/Ui/CreateMaintenanceTrackingTest.php
@@ -27,7 +27,8 @@ class CreateMaintenanceTrackingTest extends TestCase
->assertRedirect(route('maintenances.index'));
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
'checked_out_to_id' => $assignedUser->id,
'checked_out_to_type' => User::class,
]);
@@ -49,7 +50,8 @@ class CreateMaintenanceTrackingTest extends TestCase
->assertSessionHasNoErrors();
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
'checked_out_to_id' => null,
'checked_out_to_type' => null,
]);
@@ -79,7 +81,8 @@ class CreateMaintenanceTrackingTest extends TestCase
->assertSessionHasNoErrors();
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
'responsible_party_id' => null,
]);
}
@@ -102,7 +105,8 @@ class CreateMaintenanceTrackingTest extends TestCase
->assertSessionHasNoErrors();
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
'responsible_party_id' => $technician->id,
]);
}
@@ -123,7 +127,8 @@ class CreateMaintenanceTrackingTest extends TestCase
->assertSessionHasNoErrors();
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => Asset::class,
'maintenance_type_id' => $type->id,
'asset_maintenance_type' => 'Custom Calibration',
]);
diff --git a/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php b/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
index d0960f72a5..a48e2623ad 100644
--- a/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
@@ -68,7 +68,8 @@ class EditMaintenanceTest extends TestCase
Storage::disk('local')->assertExists('private_uploads/maintenances/'.$uploadedLog->filename);
$this->assertDatabaseHas('maintenances', [
- 'asset_id' => $asset->id,
+ 'item_id' => $asset->id,
+ 'item_type' => \App\Models\Asset::class,
'supplier_id' => $supplier->id,
'maintenance_type_id' => $type->id,
'asset_maintenance_type' => $type->name,
diff --git a/tests/Feature/Maintenances/Ui/MaintenanceTypeControllerTest.php b/tests/Feature/Maintenances/Ui/MaintenanceTypeControllerTest.php
new file mode 100644
index 0000000000..44991fc97e
--- /dev/null
+++ b/tests/Feature/Maintenances/Ui/MaintenanceTypeControllerTest.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Tests\Feature\Maintenances\Ui;
+
+use App\Models\Asset;
+use App\Models\Maintenance;
+use App\Models\MaintenanceType;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Web-side coverage for MaintenanceTypesController: show route renders
+ * and gates on the view permission, and destroy honors the in-use
+ * guard (mirrors the API-side destroy test in tests/Feature/Maintenances/
+ * Api/MaintenanceTypesTest).
+ */
+class MaintenanceTypeControllerTest extends TestCase
+{
+ public function test_show_page_renders()
+ {
+ $type = MaintenanceType::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('maintenance-types.show', $type))
+ ->assertOk()
+ ->assertSeeText($type->name);
+ }
+
+ public function test_show_requires_permission()
+ {
+ $type = MaintenanceType::factory()->create();
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('maintenance-types.show', $type))
+ ->assertForbidden();
+ }
+
+ public function test_can_delete_unused_maintenance_type()
+ {
+ $type = MaintenanceType::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->delete(route('maintenance-types.destroy', $type))
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionHas('success');
+
+ $this->assertSoftDeleted($type);
+ }
+
+ public function test_cannot_delete_maintenance_type_that_is_in_use()
+ {
+ $type = MaintenanceType::factory()->create();
+ Maintenance::factory()->create([
+ 'maintenance_type_id' => $type->id,
+ 'item_id' => Asset::factory()->create()->id,
+ 'item_type' => Asset::class,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->delete(route('maintenance-types.destroy', $type))
+ ->assertRedirect(route('maintenance-types.index'))
+ ->assertSessionHas('error');
+
+ $this->assertDatabaseHas('maintenance_types', ['id' => $type->id, 'deleted_at' => null]);
+ }
+}