Skip to content

A free open source IT asset/license management system

Added tests

snipea5d04d2e6536
diff --git a/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php b/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php
new file mode 100644
index 0000000000..7275994c7d
--- /dev/null
+++ b/tests/Feature/Assets/Ui/ForceCheckinAssetTest.php
@@ -0,0 +1,114 @@
+<?php
+
+namespace Tests\Feature\Assets\Ui;
+
+use App\Models\Asset;
+use App\Models\Location;
+use App\Models\User;
+use Tests\TestCase;
+
+class ForceCheckinAssetTest extends TestCase
+{
+ public function test_permission_required_to_force_checkin_asset()
+ {
+ $asset = Asset::factory()->create();
+
+ $this->actingAs(User::factory()->create())
+ ->post(route('asset.checkin.force', $asset))
+ ->assertForbidden();
+ }
+
+ public function test_can_force_checkin_asset_with_orphaned_assigned_to_and_missing_type()
+ {
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = 999; // Non-existent ID
+ $asset->assigned_type = null; // Missing type
+ $asset->save();
+
+ $response = $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('asset.checkin.force', $asset));
+
+ $response->assertRedirect(route('hardware.show', $asset));
+
+ $asset->refresh();
+ $this->assertNull($asset->assigned_to);
+ $this->assertNull($asset->assigned_type);
+ }
+
+ public function test_can_force_checkin_asset_with_hard_deleted_assigned_to()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ // Hard delete the user
+ $user->forceDelete();
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('asset.checkin.force', $asset))
+ ->assertRedirect(route('hardware.show', $asset))
+ ->assertSessionHas('success');
+
+ $asset->refresh();
+ $this->assertNull($asset->assigned_to);
+ $this->assertNull($asset->assigned_type);
+ }
+
+ public function test_cannot_force_checkin_asset_that_is_not_orphaned()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('asset.checkin.force', $asset))
+ ->assertRedirect(route('hardware.show', $asset))
+ ->assertSessionHas('error');
+
+ $asset->refresh();
+ $this->assertNotNull($asset->assigned_to);
+ }
+
+ public function test_cannot_force_checkin_asset_with_no_assignment()
+ {
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = null;
+ $asset->assigned_type = null;
+ $asset->save();
+
+ $this->actingAs(User::factory()->checkinAssets()->create())
+ ->post(route('asset.checkin.force', $asset))
+ ->assertRedirect(route('hardware.show', $asset))
+ ->assertSessionHas('error');
+ }
+
+ public function test_force_checkin_button_shown_on_asset_view_when_orphaned()
+ {
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = 999; // Non-existent ID
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.show', $asset))
+ ->assertSeeText(trans('general.force_checkin'));
+ }
+
+ public function test_force_checkin_button_not_shown_when_asset_is_not_orphaned()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('hardware.show', $asset))
+ ->assertDontSeeText(trans('general.force_checkin'));
+ }
+}
+
diff --git a/tests/Unit/Models/AssetOrphanedAssignmentTest.php b/tests/Unit/Models/AssetOrphanedAssignmentTest.php
new file mode 100644
index 0000000000..68989317b8
--- /dev/null
+++ b/tests/Unit/Models/AssetOrphanedAssignmentTest.php
@@ -0,0 +1,100 @@
+<?php
+
+namespace Tests\Unit\Models;
+
+use App\Models\Asset;
+use App\Models\Location;
+use App\Models\User;
+use Tests\TestCase;
+
+class AssetOrphanedAssignmentTest extends TestCase
+{
+ public function test_has_orphaned_assignment_returns_true_when_assigned_to_set_but_type_missing()
+ {
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = 999;
+ $asset->assigned_type = null;
+ $asset->save();
+
+ $this->assertTrue($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_true_when_assigned_target_hard_deleted()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ // Hard delete the user
+ $user->forceDelete();
+
+ $asset->refresh();
+ $this->assertTrue($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_false_when_properly_assigned()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ $this->assertFalse($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_false_when_not_assigned()
+ {
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = null;
+ $asset->assigned_type = null;
+ $asset->save();
+
+ $this->assertFalse($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_false_when_assigned_to_location()
+ {
+ $location = Location::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $location->id;
+ $asset->assigned_type = Location::class;
+ $asset->save();
+
+ $this->assertFalse($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_true_when_assigned_to_hard_deleted_location()
+ {
+ $location = Location::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $location->id;
+ $asset->assigned_type = Location::class;
+ $asset->save();
+
+ // Hard delete the location
+ $location->forceDelete();
+
+ $asset->refresh();
+ $this->assertTrue($asset->hasOrphanedAssignment());
+ }
+
+ public function test_has_orphaned_assignment_returns_false_when_assigned_to_soft_deleted_user()
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->create();
+ $asset->assigned_to = $user->id;
+ $asset->assigned_type = User::class;
+ $asset->save();
+
+ // Soft delete the user (not hard delete)
+ $user->delete();
+
+ $asset->refresh();
+ // Should still be false because withTrashed() loads soft-deleted records
+ $this->assertFalse($asset->hasOrphanedAssignment());
+ }
+}
+