A free open source IT asset/license management system
Added tests
snipe19c986d0c40b
diff --git a/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.phpindex 6313713070..c577ee01aa 100644--- a/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php+++ b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php@@ -2,6 +2,7 @@namespace Tests\Feature\Maintenances\Api;+use App\Models\Asset;use App\Models\Maintenance;use App\Models\MaintenanceType;use App\Models\User;@@ -16,6 +17,43 @@ class IndexMaintenanceTest extends TestCase->assertForbidden();}+ public function test_checked_out_to_id_filter_returns_only_matching_polymorphic_target()+ {+ // Used by the user detail Maintenances tab — pulls every maintenance+ // whose underlying asset was checked out to a specific user. Type+ // defaults to App\Models\User when only the id is supplied.+ //+ // MaintenanceObserver::creating() copies checked_out_to_* from the+ // asset's assigned_to/_type at insert time (so the maintenance pins+ // *who had it when the maintenance was opened*, not whoever has it+ // now). Tests therefore must check the asset out to the target user+ // first — passing checked_out_to_* directly to the factory would be+ // silently overwritten.+ $actor = User::factory()->superuser()->create();+ $alice = User::factory()->create();+ $bob = User::factory()->create();++ $aliceAsset = Asset::factory()->assignedToUser($alice)->create();+ $bobAsset = Asset::factory()->assignedToUser($bob)->create();+ $unassignedAsset = Asset::factory()->create();++ $aliceM = Maintenance::factory()->create(['asset_id' => $aliceAsset->id]);+ $bobM = Maintenance::factory()->create(['asset_id' => $bobAsset->id]);+ $unassigned = Maintenance::factory()->create(['asset_id' => $unassignedAsset->id]);++ $response = $this->actingAsForApi($actor)+ ->getJson(route('api.maintenances.index', [+ 'checked_out_to_id' => $alice->id,+ 'checked_out_to_type' => User::class,+ ]))+ ->assertOk();++ $ids = collect($response->json('rows'))->pluck('id');+ $this->assertContains($aliceM->id, $ids);+ $this->assertNotContains($bobM->id, $ids, 'Different-user maintenances must be filtered out');+ $this->assertNotContains($unassigned->id, $ids, 'Unassigned maintenances must be filtered out');+ }+public function test_completed_filter_returns_only_completed_maintenances(){$actor = User::factory()->superuser()->create();diff --git a/tests/Feature/Users/Api/IndexUsersTest.php b/tests/Feature/Users/Api/IndexUsersTest.phpindex 0024ca6525..a9d4013298 100644--- a/tests/Feature/Users/Api/IndexUsersTest.php+++ b/tests/Feature/Users/Api/IndexUsersTest.php@@ -2,7 +2,9 @@namespace Tests\Feature\Users\Api;+use App\Models\Asset;use App\Models\Location;+use App\Models\Maintenance;use App\Models\User;use Illuminate\Testing\Fluent\AssertableJson;use Tests\TestCase;@@ -58,6 +60,71 @@ class IndexUsersTest extends TestCase});}+ public function test_returns_assigned_maintenances_count_correctly()+ {+ // The withCount/has filter both target the new assignedMaintenances+ // morphMany. checked_out_to_* on a maintenance is populated by+ // MaintenanceObserver::creating() from the asset's assigned_to —+ // so seed via "asset checked out to user, then maintenance on+ // that asset" rather than passing checked_out_to_* directly.+ $busy = User::factory()->create(['first_name' => 'Busy Breaker']);+ User::factory()->create(['first_name' => 'Quiet User']);++ $busyAsset = Asset::factory()->assignedToUser($busy)->create();+ Maintenance::factory()->create(['asset_id' => $busyAsset->id]);+ Maintenance::factory()->create([+ 'asset_id' => $busyAsset->id,+ 'completed_at' => now(),+ ]);++ $response = $this->actingAsForApi(User::factory()->viewUsers()->create())+ ->getJson(route('api.users.index', [+ 'assigned_maintenances_count' => 2,+ ]))+ ->assertOk();++ $response->assertJson(function (AssertableJson $json) {+ $json->has('rows', 1)+ ->where('rows.0.first_name', 'Busy Breaker')+ ->where('rows.0.assigned_maintenances_count', 2)+ ->etc();+ });+ }++ public function test_can_sort_users_index_by_assigned_maintenances_count()+ {+ $most = User::factory()->create();+ $some = User::factory()->create();++ $mostAsset = Asset::factory()->assignedToUser($most)->create();+ Maintenance::factory()->count(3)->create(['asset_id' => $mostAsset->id]);++ $someAsset = Asset::factory()->assignedToUser($some)->create();+ Maintenance::factory()->create(['asset_id' => $someAsset->id]);++ $response = $this->actingAsForApi(User::factory()->viewUsers()->create())+ ->getJson(route('api.users.index', [+ 'sort' => 'assigned_maintenances_count',+ 'order' => 'desc',+ ]))+ ->assertOk();++ // Filter to just the two users we created with maintenances; everyone+ // else seeded by factories has count=0 and their relative order is+ // not what we're testing here. Position of $most must precede $some.+ $orderedIds = collect($response->json('rows'))->pluck('id');+ $mostPos = $orderedIds->search($most->id);+ $somePos = $orderedIds->search($some->id);++ $this->assertNotFalse($mostPos, 'User with 3 maintenances should appear in the result');+ $this->assertNotFalse($somePos, 'User with 1 maintenance should appear in the result');+ $this->assertLessThan(+ $somePos,+ $mostPos,+ 'User with 3 maintenances must sort before user with 1 when ordering desc by assigned_maintenances_count',+ );+ }+public function test_gracefully_handles_malformed_filter(){$this->actingAsForApi(User::factory()->viewUsers()->create())diff --git a/tests/Feature/Users/Ui/ViewUserTest.php b/tests/Feature/Users/Ui/ViewUserTest.phpindex 88e62f33d2..fc1ccb9c33 100644--- a/tests/Feature/Users/Ui/ViewUserTest.php+++ b/tests/Feature/Users/Ui/ViewUserTest.php@@ -2,8 +2,10 @@namespace Tests\Feature\Users\Ui;+use App\Models\Asset;use App\Models\Company;use App\Models\Group;+use App\Models\Maintenance;use App\Models\User;use Tests\TestCase;@@ -80,4 +82,40 @@ class ViewUserTest extends TestCase->assertSee('reports.view')->assertSee('label-danger', false);}++ public function test_user_detail_renders_maintenances_tab_with_count()+ {+ // Tab pane is fed by api.maintenances.index with checked_out_to_id+ // set to this user. Badge count includes open + closed so the+ // number matches what shows inside the pane (which is also+ // unfiltered by completion status — that's the whole point of the+ // tab: tracking who causes the most maintenance events over time).+ //+ // checked_out_to_* is populated from the asset's assigned_to by+ // MaintenanceObserver::creating(), so seed via assets here. Actor+ // needs view-asset perm too because the maintenance-tab nav-item+ // gates on Asset::class (maintenances are asset-anchored data).+ $target = User::factory()->create();+ $asset = Asset::factory()->assignedToUser($target)->create();+ Maintenance::factory()->count(2)->create([+ 'asset_id' => $asset->id,+ 'completed_at' => null,+ ]);+ Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'completed_at' => now(),+ ]);+ // Unrelated maintenance — must not show up via this user's filter.+ $other = Asset::factory()->assignedToUser(User::factory()->create())->create();+ Maintenance::factory()->create(['asset_id' => $other->id]);++ $this->actingAs(User::factory()->viewUsers()->viewAssets()->create())+ ->get(route('users.show', $target))+ ->assertOk()+ // Tab anchor uses #maintenances; pane wires the maintenances API+ // with the right polymorphic filter for this user.+ ->assertSee('#maintenances', false)+ ->assertSee('checked_out_to_id='.$target->id, false)+ ->assertSee('checked_out_to_type=App%5CModels%5CUser', false);+ }}