Skip to content

A free open source IT asset/license management system

Added/updated tests

snipedde71b37a65b
diff --git a/tests/Feature/Importing/Api/ImportComponentsTest.php b/tests/Feature/Importing/Api/ImportComponentsTest.php
index c9689ed4c1..7cc6f79a7e 100644
--- a/tests/Feature/Importing/Api/ImportComponentsTest.php
+++ b/tests/Feature/Importing/Api/ImportComponentsTest.php
@@ -325,6 +325,32 @@ class ImportComponentsTest extends ImportDataTestCase implements TestsPermission
$this->assertEquals($originalNotes, $component->notes);
}
+ #[Test]
+ public function import_persists_the_requestable_flag_from_csv(): void
+ {
+ // Component gained the Requestable trait + a per-row column.
+ // Pins that a CSV Requestable=TRUE on create lands as `1` in
+ // the DB, matching the shape the accessory / consumable
+ // importers ship.
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ // Overriding via `new()` merges into the seeded definition
+ // (unlike PHP's `+` union operator which keeps the definition's
+ // empty-string default and silently drops the override).
+ $importFileBuilder = ImportFileBuilder::new(['requestable' => 'TRUE']);
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->component()->create([
+ 'file_path' => $importFileBuilder->saveToImportsDirectory(),
+ ]);
+
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $component = Component::query()
+ ->where('name', $row['itemName'])
+ ->sole();
+ $this->assertTrue((bool) $component->requestable);
+ }
+
#[Test]
public function update_mode_logs_component_update_in_actionlog(): void
{
diff --git a/tests/Feature/Importing/Api/ImportLicenseTest.php b/tests/Feature/Importing/Api/ImportLicenseTest.php
index daac9be30f..e1a7eb83d7 100644
--- a/tests/Feature/Importing/Api/ImportLicenseTest.php
+++ b/tests/Feature/Importing/Api/ImportLicenseTest.php
@@ -279,6 +279,29 @@ class ImportLicenseTest extends ImportDataTestCase implements TestsPermissionsRe
$this->assertEquals($license->min_amt, $updatedLicense->min_amt);
}
+ #[Test]
+ public function import_persists_the_requestable_flag_from_csv(): void
+ {
+ // License gained the Requestable trait + a per-row column.
+ // Pins that a CSV Requestable=TRUE on create lands as `1` in
+ // the DB, matching the shape the accessory / consumable /
+ // component importers ship.
+ $this->actingAsForApi(User::factory()->superuser()->create());
+
+ $importFileBuilder = ImportFileBuilder::new(['requestable' => 'TRUE']);
+ $row = $importFileBuilder->firstRow();
+ $import = Import::factory()->license()->create([
+ 'file_path' => $importFileBuilder->saveToImportsDirectory(),
+ ]);
+
+ $this->importFileResponse(['import' => $import->id])->assertOk();
+
+ $license = License::query()
+ ->where('serial', $row['serialNumber'])
+ ->sole();
+ $this->assertTrue((bool) $license->requestable);
+ }
+
#[Test]
public function update_mode_clears_field_when_csv_column_is_present_but_empty(): void
{
diff --git a/tests/Feature/Requests/Api/IndexCheckoutRequestsTest.php b/tests/Feature/Requests/Api/IndexCheckoutRequestsTest.php
new file mode 100644
index 0000000000..ef76081fa7
--- /dev/null
+++ b/tests/Feature/Requests/Api/IndexCheckoutRequestsTest.php
@@ -0,0 +1,345 @@
+<?php
+
+namespace Tests\Feature\Requests\Api;
+
+use App\Models\Asset;
+use App\Models\CheckoutRequest;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Coverage for GET /api/v1/requests, the admin-scoped list of open
+ * checkout requests. Ships as the read half of #15541 so integrators
+ * can build their own approval / fulfilment UIs. Approve/deny/fulfill
+ * actions are intentionally not part of this endpoint.
+ */
+class IndexCheckoutRequestsTest extends TestCase
+{
+ public function test_requires_view_assets_permission(): void
+ {
+ // Same gate as the Blade /hardware/requested page. A user with
+ // no view-assets permission (e.g. an ordinary requester) must
+ // not be able to enumerate every other user's request.
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->getJson(route('api.requests.index'))
+ ->assertForbidden();
+ }
+
+ public function test_returns_all_open_requests_for_a_privileged_user(): void
+ {
+ $admin = User::factory()->viewAssets()->create();
+ $requesterA = User::factory()->create();
+ $requesterB = User::factory()->create();
+
+ $requestA = CheckoutRequest::factory()->create(['user_id' => $requesterA->id]);
+ $requestB = CheckoutRequest::factory()->create(['user_id' => $requesterB->id]);
+
+ $response = $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->assertJsonStructure(['total', 'rows']);
+
+ $ids = collect($response->json('rows'))->pluck('id')->all();
+ $this->assertContains($requestA->id, $ids);
+ $this->assertContains($requestB->id, $ids);
+ }
+
+ public function test_does_not_return_canceled_requests(): void
+ {
+ // The canceled_at column is the current "closed" signal. Once
+ // the v2 state machine ships this test's expectation should
+ // expand to also exclude denied / expired / fulfilled rows,
+ // but for today only canceled rows are filtered.
+ $admin = User::factory()->viewAssets()->create();
+
+ $open = CheckoutRequest::factory()->create();
+ $canceled = CheckoutRequest::factory()->create(['canceled_at' => now()]);
+
+ $ids = collect(
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->pluck('id')->all();
+
+ $this->assertContains($open->id, $ids);
+ $this->assertNotContains($canceled->id, $ids);
+ }
+
+ public function test_response_row_includes_requester_and_requestable(): void
+ {
+ // Payload contract check: enough per-row context to render a
+ // "who is requesting what" UI without a follow-up round trip.
+ // The admin /hardware/requested page hydrates itself from these
+ // fields, so the shape doubles as internal-UI plumbing.
+ $admin = User::factory()->viewAssets()->create();
+ $requester = User::factory()->create([
+ 'first_name' => 'Alex',
+ 'last_name' => 'Requester',
+ 'username' => 'alexreq',
+ 'email' => '[email protected]',
+ ]);
+ $asset = Asset::factory()->requestable()->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $rows = $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows');
+
+ $row = collect($rows)->firstWhere('id', $request->id);
+
+ $this->assertNotNull($row);
+ $this->assertSame($requester->id, $row['user']['id']);
+ $this->assertSame('alexreq', $row['user']['username']);
+ $this->assertSame(route('users.show', $requester->id), $row['user']['url']);
+ $this->assertFalse($row['user']['deleted']);
+ $this->assertSame($asset->id, $row['requestable']['id']);
+ $this->assertSame('Asset', $row['requestable']['type']);
+ $this->assertSame(route('hardware.show', $asset->id), $row['requestable']['url']);
+ }
+
+ public function test_pending_requesters_column_lists_other_open_requesters_for_the_same_item(): void
+ {
+ // The admin queue's "Also Requested By" column reads this
+ // field via ordersSummaryFormatter. Excluding the current
+ // row's requester keeps the column meaning "who ELSE is
+ // waiting on this item", so a solo request renders empty.
+ $admin = User::factory()->viewAssets()->create();
+
+ $asset = Asset::factory()->requestable()->create();
+ $alone = User::factory()->create(['first_name' => 'Alone', 'last_name' => 'Requester']);
+ $alsoA = User::factory()->create(['first_name' => 'Alsoa', 'last_name' => 'Requester']);
+ $alsoB = User::factory()->create(['first_name' => 'Alsob', 'last_name' => 'Requester']);
+
+ $soloAsset = Asset::factory()->requestable()->create();
+ $soloRequest = CheckoutRequest::factory()->create([
+ 'user_id' => $alone->id,
+ 'requestable_id' => $soloAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $primary = CheckoutRequest::factory()->create([
+ 'user_id' => $alone->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => $alsoA->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => $alsoB->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $rows = collect(
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->keyBy('id');
+
+ // Solo request: only requester on that item -> empty list.
+ $this->assertSame([], $rows[$soloRequest->id]['pending_requesters']);
+
+ // Contested asset: two other users besides the row's own
+ // requester should be surfaced, and the row's own requester
+ // must NOT appear (the sibling "Requested By" column already
+ // shows them).
+ $others = $rows[$primary->id]['pending_requesters'];
+ $this->assertCount(2, $others);
+ $this->assertNotContains($alone->display_name, $others);
+ $this->assertContains($alsoA->display_name, $others);
+ $this->assertContains($alsoB->display_name, $others);
+ }
+
+ public function test_pending_requesters_excludes_canceled_rows(): void
+ {
+ // Canceled requests must not show up in the "also requested
+ // by" list - the column is for OPEN requests only.
+ $admin = User::factory()->viewAssets()->create();
+ $asset = Asset::factory()->requestable()->create();
+
+ $primaryRequester = User::factory()->create(['first_name' => 'Primary', 'last_name' => 'Requester']);
+ $canceledRequester = User::factory()->create(['first_name' => 'Cancelled', 'last_name' => 'User']);
+
+ $primary = CheckoutRequest::factory()->create([
+ 'user_id' => $primaryRequester->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => $canceledRequester->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ 'canceled_at' => now(),
+ ]);
+
+ $rows = collect(
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->keyBy('id');
+
+ $this->assertSame([], $rows[$primary->id]['pending_requesters']);
+ }
+
+ public function test_asset_requestable_exposes_assigned_flag_and_action_hints(): void
+ {
+ // The Blade page renders "Checkout" vs "Checkin" based on
+ // whether the asset is currently checked out. The transformer
+ // surfaces that as requestable.assigned + available_actions,
+ // so the JS formatter can pick a button without another lookup.
+ $admin = User::factory()->viewAssets()->create();
+
+ $availableAsset = Asset::factory()->requestable()->create(['assigned_to' => null]);
+ $availableRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $availableAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $assignedAsset = Asset::factory()->assignedToUser()->create();
+ $assignedRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $assignedAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $rows = collect(
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->keyBy('id');
+
+ $this->assertFalse($rows[$availableRequest->id]['requestable']['assigned']);
+ $this->assertTrue($rows[$availableRequest->id]['available_actions']['checkout']);
+ $this->assertFalse($rows[$availableRequest->id]['available_actions']['checkin']);
+
+ $this->assertTrue($rows[$assignedRequest->id]['requestable']['assigned']);
+ $this->assertFalse($rows[$assignedRequest->id]['available_actions']['checkout']);
+ $this->assertTrue($rows[$assignedRequest->id]['available_actions']['checkin']);
+ }
+
+ public function test_user_id_filter_limits_results_to_that_requesters_rows(): void
+ {
+ $admin = User::factory()->viewAssets()->create();
+ $requesterA = User::factory()->create();
+ $requesterB = User::factory()->create();
+
+ $mine = CheckoutRequest::factory()->create(['user_id' => $requesterA->id]);
+ $theirs = CheckoutRequest::factory()->create(['user_id' => $requesterB->id]);
+
+ $ids = collect(
+ $this->actingAsForApi($admin)
+ ->getJson(route('api.requests.index', ['user_id' => $requesterA->id]))
+ ->assertOk()
+ ->json('rows')
+ )->pluck('id')->all();
+
+ $this->assertContains($mine->id, $ids);
+ $this->assertNotContains($theirs->id, $ids);
+ }
+
+ public function test_fmcs_hides_requests_for_items_the_admin_cannot_see(): void
+ {
+ // Non-superuser admin in company A must not see requests
+ // targeting company B's assets. Superuser bypass is covered
+ // separately below.
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $adminInA = $companyA->users()->save(User::factory()->viewAssets()->make());
+ $requesterInA = $companyA->users()->save(User::factory()->make());
+ $requesterInB = $companyB->users()->save(User::factory()->make());
+
+ $assetInA = Asset::factory()->for($companyA)->requestable()->create();
+ $assetInB = Asset::factory()->for($companyB)->requestable()->create();
+
+ $requestForA = CheckoutRequest::factory()->create([
+ 'user_id' => $requesterInA->id,
+ 'requestable_id' => $assetInA->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ $requestForB = CheckoutRequest::factory()->create([
+ 'user_id' => $requesterInB->id,
+ 'requestable_id' => $assetInB->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $ids = collect(
+ $this->actingAsForApi($adminInA)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->pluck('id')->all();
+
+ $this->assertContains($requestForA->id, $ids);
+ $this->assertNotContains($requestForB->id, $ids);
+ }
+
+ public function test_fmcs_floater_mode_surfaces_null_company_items_to_scoped_admins(): void
+ {
+ // Floater mode intentionally makes company_id=null items
+ // visible across every company under FMCS. The old
+ // /hardware/requested page relied on Company::isCurrentUserHasAccess
+ // (which respects floater); this test pins that the API-based
+ // migration still honors it.
+ [$companyA] = Company::factory()->count(2)->create();
+
+ $adminInA = $companyA->users()->save(User::factory()->viewAssets()->make());
+ $floaterAsset = Asset::factory()->create(['company_id' => null]);
+ $floaterRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $floaterAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->enableFloaterMode();
+
+ $ids = collect(
+ $this->actingAsForApi($adminInA)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->pluck('id')->all();
+
+ $this->assertContains($floaterRequest->id, $ids);
+ }
+
+ public function test_fmcs_superuser_sees_every_companys_requests(): void
+ {
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $superuser = User::factory()->superuser()->create();
+ $requesterInA = $companyA->users()->save(User::factory()->make());
+ $requesterInB = $companyB->users()->save(User::factory()->make());
+
+ $requestForA = CheckoutRequest::factory()->create(['user_id' => $requesterInA->id]);
+ $requestForB = CheckoutRequest::factory()->create(['user_id' => $requesterInB->id]);
+
+ $this->settings->enableMultipleFullCompanySupport();
+
+ $ids = collect(
+ $this->actingAsForApi($superuser)
+ ->getJson(route('api.requests.index'))
+ ->assertOk()
+ ->json('rows')
+ )->pluck('id')->all();
+
+ $this->assertContains($requestForA->id, $ids);
+ $this->assertContains($requestForB->id, $ids);
+ }
+}
diff --git a/tests/Feature/Requests/AssetModelRequestGateTest.php b/tests/Feature/Requests/AssetModelRequestGateTest.php
index 40e71d8f48..21cb7fb66d 100644
--- a/tests/Feature/Requests/AssetModelRequestGateTest.php
+++ b/tests/Feature/Requests/AssetModelRequestGateTest.php
@@ -75,9 +75,15 @@ class AssetModelRequestGateTest extends TestCase
public function test_ignored_item_types_do_not_reach_the_controller(): void
{
// Belt-and-suspenders check for a couple more shapes.
+ // `component`, `consumable`, and `license` were removed from
+ // this list as each became a first-class requestable (see the
+ // matching ConsumableRequestTest / ComponentRequestTest /
+ // LicenseRequestTest for the coverage that replaced their
+ // slots here). `location` still route-rejects at the regex
+ // level.
$user = User::factory()->create();
- foreach (['location', 'component', 'consumable', 'license'] as $itemType) {
+ foreach (['location'] as $itemType) {
$response = $this->actingAs($user)
->post('/account/request/'.$itemType.'/1');
$response->assertNotFound();
diff --git a/tests/Feature/Requests/PurgePendingRequestsTest.php b/tests/Feature/Requests/PurgePendingRequestsTest.php
new file mode 100644
index 0000000000..98369c4249
--- /dev/null
+++ b/tests/Feature/Requests/PurgePendingRequestsTest.php
@@ -0,0 +1,183 @@
+<?php
+
+namespace Tests\Feature\Requests;
+
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\AssetModel;
+use App\Models\CheckoutRequest;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use Tests\TestCase;
+
+/**
+ * Cascade rules for CheckoutRequest rows when their requestable is
+ * force-deleted or purged. Soft-delete deliberately leaves history
+ * intact (a restore should bring the requests back); only a real
+ * hard-delete wipes them.
+ *
+ * Two hooks cover the two entry points:
+ * - snipeit:purge iterates raw query-builder DELETEs (bypasses
+ * Eloquent events), so Purge::$childTables has its own explicit
+ * cascade entry.
+ * - Direct forceDelete() bypasses the purge command; the trait's
+ * bootRequestable() forceDeleted listener handles it.
+ */
+class PurgePendingRequestsTest extends TestCase
+{
+ public function test_direct_force_delete_of_asset_wipes_its_pending_requests(): void
+ {
+ $asset = Asset::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $asset->forceDelete();
+
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_direct_force_delete_of_accessory_wipes_its_pending_requests(): void
+ {
+ $accessory = Accessory::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ ]);
+
+ $accessory->forceDelete();
+
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_direct_force_delete_of_consumable_wipes_its_pending_requests(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ ]);
+
+ $consumable->forceDelete();
+
+ // Sanity check: the consumable itself is genuinely gone. If
+ // ConsumableObserver::deleting aborted mid-flight (a common
+ // trap since it calls users()->detach() and $consumable->save()
+ // during the deleting event), the row would still be here and
+ // the request-cascade assertion below would be a false negative.
+ $this->assertDatabaseMissing('consumables', ['id' => $consumable->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_direct_force_delete_of_component_wipes_its_pending_requests(): void
+ {
+ $component = Component::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ ]);
+
+ $component->forceDelete();
+
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_direct_force_delete_of_license_wipes_its_pending_requests(): void
+ {
+ $license = License::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ ]);
+
+ $license->forceDelete();
+
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_soft_delete_does_not_wipe_pending_requests(): void
+ {
+ // The request stays around after a soft-delete so admins can
+ // still see the intent + restore recovers cleanly. Only
+ // forceDelete / purge should nuke.
+ $asset = Asset::factory()->create();
+ $req = CheckoutRequest::factory()->create([
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $asset->delete();
+
+ $this->assertDatabaseHas('checkout_requests', ['id' => $req->id]);
+ }
+
+ public function test_snipeit_purge_cascades_pending_requests_for_every_requestable_type(): void
+ {
+ // Purge walks raw query-builder DELETEs (no model events), so
+ // this pins the explicit Purge::$childTables entry for every
+ // Requestable model. One trashed row per type, one pending
+ // request per row, plus a live control row per type that must
+ // survive the purge.
+ $trashedAsset = Asset::factory()->create();
+ $trashedAsset->delete();
+ $trashedAssetRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $liveAsset = Asset::factory()->create();
+ $liveAssetRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $liveAsset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $trashedAccessory = Accessory::factory()->create();
+ $trashedAccessory->delete();
+ $trashedAccessoryRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedAccessory->id,
+ 'requestable_type' => Accessory::class,
+ ]);
+
+ $trashedConsumable = Consumable::factory()->create();
+ $trashedConsumable->delete();
+ $trashedConsumableRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedConsumable->id,
+ 'requestable_type' => Consumable::class,
+ ]);
+
+ $trashedComponent = Component::factory()->create();
+ $trashedComponent->delete();
+ $trashedComponentRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedComponent->id,
+ 'requestable_type' => Component::class,
+ ]);
+
+ $trashedAssetModel = AssetModel::factory()->create();
+ $trashedAssetModel->delete();
+ $trashedAssetModelRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedAssetModel->id,
+ 'requestable_type' => AssetModel::class,
+ ]);
+
+ $trashedLicense = License::factory()->create();
+ $trashedLicense->delete();
+ $trashedLicenseRequest = CheckoutRequest::factory()->create([
+ 'requestable_id' => $trashedLicense->id,
+ 'requestable_type' => License::class,
+ ]);
+
+ $this->artisan('snipeit:purge', ['--force' => 'true'])->assertExitCode(0);
+
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedAssetRequest->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedAccessoryRequest->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedConsumableRequest->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedComponentRequest->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedAssetModelRequest->id]);
+ $this->assertDatabaseMissing('checkout_requests', ['id' => $trashedLicenseRequest->id]);
+
+ // Control row: live asset's request must survive the purge.
+ $this->assertDatabaseHas('checkout_requests', ['id' => $liveAssetRequest->id]);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/AccessoryRequestTest.php b/tests/Feature/Requests/Ui/AccessoryRequestTest.php
index 3d810e4e9f..99f63d8214 100644
--- a/tests/Feature/Requests/Ui/AccessoryRequestTest.php
+++ b/tests/Feature/Requests/Ui/AccessoryRequestTest.php
@@ -89,21 +89,83 @@ class AccessoryRequestTest extends TestCase
);
}
- public function test_admin_requested_index_renders_accessory_requests(): void
+ public function test_user_can_request_an_accessory_with_a_reservation_window(): void
{
+ // Reservation dates are optional (persist the "start blank ->
+ // no window" story), but when supplied both should land on the
+ // CheckoutRequest row so the future calendar-events pipeline
+ // has something to hydrate from.
$accessory = Accessory::factory()->create(['requestable' => true]);
- CheckoutRequest::factory()->create([
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'accessory',
+ 'itemId' => $accessory->id,
+ ]), [
+ 'request-quantity' => 2,
+ 'start_date' => '2026-09-01',
+ 'end_date' => '2026-09-05',
+ ])
+ ->assertRedirect();
+
+ $request = CheckoutRequest::where('requestable_id', $accessory->id)
+ ->where('requestable_type', Accessory::class)
+ ->where('user_id', $user->id)
+ ->first();
+
+ $this->assertNotNull($request);
+ $this->assertSame('2026-09-01', $request->start_date->toDateString());
+ $this->assertSame('2026-09-05', $request->end_date->toDateString());
+ }
+
+ public function test_request_is_rejected_when_end_date_precedes_start_date(): void
+ {
+ $accessory = Accessory::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'accessory',
+ 'itemId' => $accessory->id,
+ ]), [
+ 'request-quantity' => 1,
+ 'start_date' => '2026-09-10',
+ 'end_date' => '2026-09-05',
+ ])
+ ->assertRedirect()
+ ->assertSessionHasErrors('end_date');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $accessory->id)
+ ->where('requestable_type', Accessory::class)
+ ->first(),
+ 'A request with an inverted date range must not persist.'
+ );
+ }
+
+ public function test_admin_requested_api_endpoint_includes_accessory_requests(): void
+ {
+ // The admin "Requested" queue is a polymorphic list of all
+ // checkout requests. It renders from GET /api/v1/requests
+ // (the /hardware/requested Blade page is now a shell that
+ // hydrates from the same endpoint), so this test asserts on
+ // the API row rather than the server-rendered HTML.
+ $accessory = Accessory::factory()->create(['requestable' => true]);
+ $request = CheckoutRequest::factory()->create([
'requestable_id' => $accessory->id,
'requestable_type' => Accessory::class,
'user_id' => User::factory()->create()->id,
]);
- // The admin "Requested" queue is a polymorphic list of all checkout
- // requests, so an accessory request should render there by name.
- $this->actingAs(User::factory()->viewAssets()->create())
- ->get(route('assets.requested'))
+ $rows = $this->actingAsForApi(User::factory()->viewAssets()->create())
+ ->getJson(route('api.requests.index'))
->assertOk()
- ->assertViewHas('requestedItems')
- ->assertSeeText($accessory->name);
+ ->json('rows');
+
+ $row = collect($rows)->firstWhere('id', $request->id);
+ $this->assertNotNull($row);
+ $this->assertSame($accessory->name, $row['requestable']['name']);
+ $this->assertSame('Accessory', $row['requestable']['type']);
}
}
diff --git a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
index 1542ecf84a..6c3b7bdd01 100644
--- a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
+++ b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
@@ -2,12 +2,10 @@
namespace Tests\Feature\Requests\Ui;
-use App\Models\Asset;
-use App\Models\CheckoutRequest;
-use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
+
class AssetRequestIndexTest extends TestCase
{
public function test_requires_permission_to_view_asset_request_index()
@@ -17,124 +15,10 @@ class AssetRequestIndexTest extends TestCase
->assertForbidden();
}
- public function test_can_view_request_asset_request_index()
+ public function test_privileged_user_gets_the_shell_page()
{
- $checkoutRequest = CheckoutRequest::factory()->create();
-
$this->actingAs(User::factory()->viewAssets()->create())
->get(route('assets.requested'))
- ->assertOk()
- ->assertViewHas('requestedItems')
- ->assertSeeText($checkoutRequest->requestedItem->asset_tag);
- }
-
- public function test_user_sees_requests_for_assets_in_their_own_company()
- {
- $this->settings->enableMultipleFullCompanySupport();
-
- [$companyA] = Company::factory()->count(2)->create();
-
- $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
- $request = CheckoutRequest::factory()->create(['requestable_id' => $assetA->id, 'requestable_type' => Asset::class]);
- $viewer = User::factory()->viewAssets()->forCompany($companyA->id)->create();
-
- $this->actingAs($viewer)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertSeeText($request->requestedItem->asset_tag);
- }
-
- public function test_user_does_not_see_requests_for_assets_in_another_company()
- {
- $this->settings->enableMultipleFullCompanySupport();
-
- [$companyA, $companyB] = Company::factory()->count(2)->create();
-
- $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
- $crossCompanyRequest = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
- $viewer = User::factory()->viewAssets()->forCompany($companyA->id)->create();
-
- $this->actingAs($viewer)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertViewHas(
- 'requestedItems',
- fn ($items) => $items->doesntContain(fn ($r) => $r->id === $crossCompanyRequest->id)
- );
- }
-
- public function test_pivot_only_user_does_not_see_requests_for_assets_in_another_company()
- {
- $this->settings->enableMultipleFullCompanySupport();
-
- [$companyA, $companyB] = Company::factory()->count(2)->create();
-
- $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
- $crossCompanyRequest = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
- $viewer = User::factory()->viewAssets()->withoutCompany()->create();
- $viewer->companies()->sync([$companyA->id]);
-
- $this->actingAs($viewer)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertViewHas(
- 'requestedItems',
- fn ($items) => $items->doesntContain(fn ($r) => $r->id === $crossCompanyRequest->id)
- );
- }
-
- public function test_floater_mode_makes_null_company_requests_visible_to_company_scoped_users()
- {
- $this->settings->enableMultipleFullCompanySupport();
- $this->settings->enableFloaterMode();
-
- [$companyA] = Company::factory()->count(2)->create();
-
- $floaterAsset = Asset::factory()->create(['company_id' => null]);
- $request = CheckoutRequest::factory()->create(['requestable_id' => $floaterAsset->id, 'requestable_type' => Asset::class]);
- $viewer = User::factory()->viewAssets()->forCompany($companyA->id)->create();
-
- $this->actingAs($viewer)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertViewHas(
- 'requestedItems',
- fn ($items) => $items->contains(fn ($r) => $r->id === $request->id)
- );
- }
-
- public function test_superuser_sees_requests_across_all_companies()
- {
- $this->settings->enableMultipleFullCompanySupport();
-
- [$companyA, $companyB] = Company::factory()->count(2)->create();
-
- $assetA = Asset::factory()->create(['company_id' => $companyA->id]);
- $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
- $requestA = CheckoutRequest::factory()->create(['requestable_id' => $assetA->id, 'requestable_type' => Asset::class]);
- $requestB = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
- $superuser = User::factory()->superuser()->forCompany($companyA->id)->create();
-
- $this->actingAs($superuser)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertSeeText($requestA->requestedItem->asset_tag)
- ->assertSeeText($requestB->requestedItem->asset_tag);
- }
-
- public function test_company_scoping_not_enforced_when_fmcs_disabled()
- {
- $this->settings->disableMultipleFullCompanySupport();
-
- [$companyA, $companyB] = Company::factory()->count(2)->create();
-
- $assetB = Asset::factory()->create(['company_id' => $companyB->id]);
- $request = CheckoutRequest::factory()->create(['requestable_id' => $assetB->id, 'requestable_type' => Asset::class]);
- $viewer = User::factory()->viewAssets()->forCompany($companyA->id)->create();
-
- $this->actingAs($viewer)
- ->get(route('assets.requested'))
- ->assertOk()
- ->assertSeeText($request->requestedItem->asset_tag);
+ ->assertOk();
}
}
diff --git a/tests/Feature/Requests/Ui/BulkCancelRequestTest.php b/tests/Feature/Requests/Ui/BulkCancelRequestTest.php
new file mode 100644
index 0000000000..76d7102d7a
--- /dev/null
+++ b/tests/Feature/Requests/Ui/BulkCancelRequestTest.php
@@ -0,0 +1,127 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\Asset;
+use App\Models\CheckoutRequest;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Coverage for the /hardware/requested bulk-cancel action. The
+ * endpoint accepts ids[] from the queue-page checkbox selection and
+ * cancels each open row the caller can see. Idempotent for rows
+ * that are already canceled, missing requestables, or FMCS-scoped
+ * out from under the caller.
+ */
+class BulkCancelRequestTest extends TestCase
+{
+ public function test_requires_view_assets_permission(): void
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('assets.requested.bulk-cancel'), ['ids' => [1]])
+ ->assertForbidden();
+ }
+
+ public function test_empty_selection_flashes_an_error(): void
+ {
+ $this->actingAs(User::factory()->viewAssets()->create())
+ ->post(route('assets.requested.bulk-cancel'), ['ids' => []])
+ ->assertRedirectToRoute('assets.requested')
+ ->assertSessionHas('error');
+ }
+
+ public function test_cancels_every_selected_open_request(): void
+ {
+ $admin = User::factory()->viewAssets()->create();
+ $requests = CheckoutRequest::factory()->count(3)->create();
+
+ $this->actingAs($admin)
+ ->post(route('assets.requested.bulk-cancel'), [
+ 'ids' => $requests->pluck('id')->all(),
+ ])
+ ->assertRedirectToRoute('assets.requested')
+ ->assertSessionHas('success');
+
+ foreach ($requests as $request) {
+ $this->assertNotNull($request->fresh()->canceled_at, "Request {$request->id} should have been canceled.");
+ }
+ }
+
+ public function test_already_canceled_rows_are_silently_skipped(): void
+ {
+ // The action fires from a checkbox selection where any subset
+ // can go stale between "load table" and "click Go". Rows that
+ // are already canceled don't count toward the summary but
+ // don't trigger an error either.
+ $admin = User::factory()->viewAssets()->create();
+ $canceled = CheckoutRequest::factory()->create(['canceled_at' => now()->subMinute()]);
+
+ $this->actingAs($admin)
+ ->post(route('assets.requested.bulk-cancel'), [
+ 'ids' => [$canceled->id],
+ ])
+ ->assertRedirectToRoute('assets.requested')
+ ->assertSessionHas('warning');
+ }
+
+ public function test_fmcs_prevents_admin_from_canceling_requests_for_other_companies(): void
+ {
+ // Non-superuser admin in company A must not be able to bulk-
+ // cancel a request against a company B asset even if they
+ // craft the id into the payload directly. Superuser bypass
+ // is exercised in test_superuser_can_cancel_across_companies.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $adminInA = $companyA->users()->save(User::factory()->viewAssets()->make());
+ $assetInB = Asset::factory()->for($companyB)->requestable()->create();
+ $requestInB = CheckoutRequest::factory()->create([
+ 'requestable_id' => $assetInB->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $this->actingAs($adminInA)
+ ->post(route('assets.requested.bulk-cancel'), [
+ 'ids' => [$requestInB->id],
+ ])
+ ->assertRedirectToRoute('assets.requested');
+
+ $this->assertNull(
+ $requestInB->fresh()->canceled_at,
+ 'Cross-company request must not be canceled by an admin who cannot see it.'
+ );
+ }
+
+ public function test_superuser_can_cancel_across_companies(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+
+ $superuser = User::factory()->superuser()->create();
+ $assetInA = Asset::factory()->for($companyA)->requestable()->create();
+ $assetInB = Asset::factory()->for($companyB)->requestable()->create();
+
+ $requestA = CheckoutRequest::factory()->create([
+ 'requestable_id' => $assetInA->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ $requestB = CheckoutRequest::factory()->create([
+ 'requestable_id' => $assetInB->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $this->actingAs($superuser)
+ ->post(route('assets.requested.bulk-cancel'), [
+ 'ids' => [$requestA->id, $requestB->id],
+ ])
+ ->assertRedirectToRoute('assets.requested')
+ ->assertSessionHas('success');
+
+ $this->assertNotNull($requestA->fresh()->canceled_at);
+ $this->assertNotNull($requestB->fresh()->canceled_at);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/ComponentRequestTest.php b/tests/Feature/Requests/Ui/ComponentRequestTest.php
new file mode 100644
index 0000000000..5adde82bbf
--- /dev/null
+++ b/tests/Feature/Requests/Ui/ComponentRequestTest.php
@@ -0,0 +1,190 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\CheckoutRequest;
+use App\Models\Company;
+use App\Models\Component;
+use App\Models\User;
+use Tests\TestCase;
+
+
+class ComponentRequestTest extends TestCase
+{
+ public function test_requestable_index_lists_requestable_components(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ Component::factory()->create(['requestable' => false]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('requestable-assets'))
+ ->assertOk()
+ ->assertViewHas('components')
+ ->assertSeeText($component->name);
+ }
+
+ public function test_user_can_request_a_requestable_component(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'component',
+ 'itemId' => $component->id,
+ ]), ['request-quantity' => 4])
+ ->assertRedirect();
+
+ $request = CheckoutRequest::where('requestable_id', $component->id)
+ ->where('requestable_type', Component::class)
+ ->where('user_id', $user->id)
+ ->first();
+
+ $this->assertNotNull($request);
+ $this->assertEquals(4, $request->quantity);
+ $this->assertNull($request->canceled_at);
+ }
+
+ public function test_user_cannot_request_a_component_that_is_not_requestable(): void
+ {
+ $component = Component::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'component',
+ 'itemId' => $component->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $component->id)
+ ->where('requestable_type', Component::class)
+ ->first()
+ );
+ }
+
+ public function test_user_can_cancel_their_own_component_request(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'component',
+ 'itemId' => $component->id,
+ ]))
+ ->assertRedirect();
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $component->id)
+ ->where('requestable_type', Component::class)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_fmcs_prevents_cross_company_component_request(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $userInA = $companyA->users()->save(User::factory()->make());
+ $componentInB = Component::factory()->for($companyB)->create(['requestable' => true]);
+
+ $this->actingAs($userInA)
+ ->post(route('account/request-item', [
+ 'itemType' => 'component',
+ 'itemId' => $componentInB->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $componentInB->id)
+ ->where('requestable_type', Component::class)
+ ->first(),
+ 'Cross-company user must not be able to create a request against a company they cannot see.'
+ );
+ }
+
+ public function test_api_endpoint_creates_a_component_request(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.components.requests.store', ['component' => $component->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $component->id)
+ ->where('requestable_type', Component::class)
+ ->where('user_id', $user->id)
+ ->whereNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_cancel_endpoint_cancels_the_users_own_request(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.components.requests.destroy', ['component' => $component->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $component->id)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_non_requestable_component(): void
+ {
+ $component = Component::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.components.requests.store', ['component' => $component->id]))
+ ->assertStatus(403);
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $component->id)
+ ->where('requestable_type', Component::class)
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_duplicate_component_request(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.components.requests.store', ['component' => $component->id]))
+ ->assertStatus(409);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/ConsumableRequestTest.php b/tests/Feature/Requests/Ui/ConsumableRequestTest.php
new file mode 100644
index 0000000000..b1acebd2c5
--- /dev/null
+++ b/tests/Feature/Requests/Ui/ConsumableRequestTest.php
@@ -0,0 +1,201 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\CheckoutRequest;
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Consumable is a first-class checkoutable, so it now participates in
+ * the Requestable trait's user-facing flow the same way Accessory does.
+ * Coverage mirrors AccessoryRequestTest and adds cross-company FMCS
+ * negative cases per the adversarial-tests rule.
+ */
+class ConsumableRequestTest extends TestCase
+{
+ public function test_requestable_index_lists_requestable_consumables(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ Consumable::factory()->create(['requestable' => false]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('requestable-assets'))
+ ->assertOk()
+ ->assertViewHas('consumables')
+ ->assertSeeText($consumable->name);
+ }
+
+ public function test_user_can_request_a_requestable_consumable(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'consumable',
+ 'itemId' => $consumable->id,
+ ]), ['request-quantity' => 2])
+ ->assertRedirect();
+
+ $request = CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('requestable_type', Consumable::class)
+ ->where('user_id', $user->id)
+ ->first();
+
+ $this->assertNotNull($request);
+ $this->assertEquals(2, $request->quantity);
+ $this->assertNull($request->canceled_at);
+ }
+
+ public function test_user_cannot_request_a_consumable_that_is_not_requestable(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'consumable',
+ 'itemId' => $consumable->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('requestable_type', Consumable::class)
+ ->first()
+ );
+ }
+
+ public function test_user_can_cancel_their_own_consumable_request(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'consumable',
+ 'itemId' => $consumable->id,
+ ]))
+ ->assertRedirect();
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('requestable_type', Consumable::class)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_fmcs_prevents_cross_company_consumable_request(): void
+ {
+ // A user in company A must not be able to request a consumable
+ // that belongs to company B. The requestable-flag gate loads
+ // through Consumable::RequestableConsumables which respects
+ // the CompanyableTrait global scope, so the item never
+ // resolves for the cross-company caller and the request is
+ // refused with the standard "not requestable" error.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $userInA = $companyA->users()->save(User::factory()->make());
+ $consumableInB = Consumable::factory()->for($companyB)->create(['requestable' => true]);
+
+ $this->actingAs($userInA)
+ ->post(route('account/request-item', [
+ 'itemType' => 'consumable',
+ 'itemId' => $consumableInB->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $consumableInB->id)
+ ->where('requestable_type', Consumable::class)
+ ->first(),
+ 'Cross-company user must not be able to create a request against a company they cannot see.'
+ );
+ }
+
+ public function test_api_endpoint_creates_a_consumable_request(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.consumables.requests.store', ['consumable' => $consumable->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('requestable_type', Consumable::class)
+ ->where('user_id', $user->id)
+ ->whereNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_cancel_endpoint_cancels_the_users_own_request(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.consumables.requests.destroy', ['consumable' => $consumable->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_non_requestable_consumable(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.consumables.requests.store', ['consumable' => $consumable->id]))
+ ->assertStatus(403);
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $consumable->id)
+ ->where('requestable_type', Consumable::class)
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_duplicate_consumable_request(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.consumables.requests.store', ['consumable' => $consumable->id]))
+ ->assertStatus(409);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/LicenseRequestTest.php b/tests/Feature/Requests/Ui/LicenseRequestTest.php
new file mode 100644
index 0000000000..9a055fb6bb
--- /dev/null
+++ b/tests/Feature/Requests/Ui/LicenseRequestTest.php
@@ -0,0 +1,194 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\CheckoutRequest;
+use App\Models\Company;
+use App\Models\License;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * License gained the Requestable trait + per-row `requestable`
+ * boolean alongside Consumable / Component / Accessory. Coverage
+ * matches ConsumableRequestTest / ComponentRequestTest to keep the
+ * behavior symmetric across every requestable type.
+ */
+class LicenseRequestTest extends TestCase
+{
+ public function test_requestable_index_lists_requestable_licenses(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ License::factory()->create(['requestable' => false]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('requestable-assets'))
+ ->assertOk()
+ ->assertViewHas('licenses')
+ ->assertSeeText($license->name);
+ }
+
+ public function test_user_can_request_a_requestable_license(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'license',
+ 'itemId' => $license->id,
+ ]), ['request-quantity' => 1])
+ ->assertRedirect();
+
+ $request = CheckoutRequest::where('requestable_id', $license->id)
+ ->where('requestable_type', License::class)
+ ->where('user_id', $user->id)
+ ->first();
+
+ $this->assertNotNull($request);
+ $this->assertNull($request->canceled_at);
+ }
+
+ public function test_user_cannot_request_a_license_that_is_not_requestable(): void
+ {
+ $license = License::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'license',
+ 'itemId' => $license->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $license->id)
+ ->where('requestable_type', License::class)
+ ->first()
+ );
+ }
+
+ public function test_user_can_cancel_their_own_license_request(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'license',
+ 'itemId' => $license->id,
+ ]))
+ ->assertRedirect();
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $license->id)
+ ->where('requestable_type', License::class)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_fmcs_prevents_cross_company_license_request(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $userInA = $companyA->users()->save(User::factory()->make());
+ $licenseInB = License::factory()->for($companyB)->create(['requestable' => true]);
+
+ $this->actingAs($userInA)
+ ->post(route('account/request-item', [
+ 'itemType' => 'license',
+ 'itemId' => $licenseInB->id,
+ ]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $licenseInB->id)
+ ->where('requestable_type', License::class)
+ ->first(),
+ 'Cross-company user must not be able to create a request against a license they cannot see.'
+ );
+ }
+
+ public function test_api_endpoint_creates_a_license_request(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.licenses.requests.store', ['license' => $license->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $license->id)
+ ->where('requestable_type', License::class)
+ ->where('user_id', $user->id)
+ ->whereNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_cancel_endpoint_cancels_the_users_own_license_request(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.licenses.requests.destroy', ['license' => $license->id]))
+ ->assertOk()
+ ->assertJson(['status' => 'success']);
+
+ $this->assertNotNull(
+ CheckoutRequest::where('requestable_id', $license->id)
+ ->where('user_id', $user->id)
+ ->whereNotNull('canceled_at')
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_non_requestable_license(): void
+ {
+ $license = License::factory()->create(['requestable' => false]);
+ $user = User::factory()->create();
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.licenses.requests.store', ['license' => $license->id]))
+ ->assertStatus(403);
+
+ $this->assertNull(
+ CheckoutRequest::where('requestable_id', $license->id)
+ ->where('requestable_type', License::class)
+ ->first()
+ );
+ }
+
+ public function test_api_endpoint_rejects_a_duplicate_license_request(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+ $user = User::factory()->create();
+ CheckoutRequest::factory()->create([
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ 'user_id' => $user->id,
+ ]);
+
+ $this->actingAsForApi($user)
+ ->postJson(route('api.licenses.requests.store', ['license' => $license->id]))
+ ->assertStatus(409);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/RequestableCheckboxPersistenceTest.php b/tests/Feature/Requests/Ui/RequestableCheckboxPersistenceTest.php
new file mode 100644
index 0000000000..f3c2f6a49e
--- /dev/null
+++ b/tests/Feature/Requests/Ui/RequestableCheckboxPersistenceTest.php
@@ -0,0 +1,140 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Component;
+use App\Models\Consumable;
+use App\Models\License;
+use App\Models\Location;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Form-submit boundary coverage for the requestable checkbox on the
+ * consumable / component / license edit + create screens. The
+ * factory-create paths exercised by the sibling *RequestTest files
+ * bypass the controller entirely, so a controller that silently
+ * drops `requestable` on `$request->input(...)` would have every
+ * request-flow test still pass. These POST through the actual
+ * controller so an omitted field surfaces as a regression here.
+ *
+ * Unchecked checkbox coverage is important on its own: HTML forms
+ * do not submit unchecked checkboxes at all. The controller must
+ * coerce the missing key to false or "unchecking the box" would
+ * silently do nothing.
+ */
+class RequestableCheckboxPersistenceTest extends TestCase
+{
+ private function commonPayload(): array
+ {
+ // Minimum-viable payload for the create + update forms. The
+ // per-type methods below merge model-specific requireds in.
+ return [
+ 'company_id' => Company::factory()->create()->id,
+ 'location_id' => Location::factory()->create()->id,
+ ];
+ }
+
+ private function consumablePayload(): array
+ {
+ return array_merge($this->commonPayload(), [
+ 'name' => 'Test Consumable',
+ 'category_id' => Category::factory()->forConsumables()->create()->id,
+ 'qty' => 5,
+ ]);
+ }
+
+ private function componentPayload(): array
+ {
+ return array_merge($this->commonPayload(), [
+ 'name' => 'Test Component',
+ 'category_id' => Category::factory()->forComponents()->create()->id,
+ 'qty' => 5,
+ 'serial' => 'C-'.uniqid(),
+ ]);
+ }
+
+ private function licensePayload(): array
+ {
+ return array_merge($this->commonPayload(), [
+ 'name' => 'Test License',
+ 'category_id' => Category::factory()->forLicenses()->create()->id,
+ 'seats' => 3,
+ ]);
+ }
+
+ public function test_consumable_create_form_persists_requestable_when_checked(): void
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.store'), $this->consumablePayload() + ['requestable' => '1'])
+ ->assertRedirect();
+
+ $this->assertTrue((bool) Consumable::where('name', 'Test Consumable')->sole()->requestable);
+ }
+
+ public function test_consumable_update_can_uncheck_requestable(): void
+ {
+ $consumable = Consumable::factory()->create(['requestable' => true]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('consumables.update', $consumable), array_merge(
+ $this->consumablePayload(),
+ ['name' => $consumable->name],
+ // Deliberately no `requestable` key - matches the shape
+ // a browser sends when the box is unchecked.
+ ))
+ ->assertRedirect();
+
+ $this->assertFalse((bool) $consumable->fresh()->requestable);
+ }
+
+ public function test_component_create_form_persists_requestable_when_checked(): void
+ {
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('components.store'), $this->componentPayload() + ['requestable' => '1'])
+ ->assertRedirect();
+
+ $this->assertTrue((bool) Component::where('name', 'Test Component')->sole()->requestable);
+ }
+
+ public function test_component_update_can_uncheck_requestable(): void
+ {
+ $component = Component::factory()->create(['requestable' => true]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('components.update', $component), array_merge(
+ $this->componentPayload(),
+ ['name' => $component->name, 'serial' => $component->serial],
+ ))
+ ->assertRedirect();
+
+ $this->assertFalse((bool) $component->fresh()->requestable);
+ }
+
+ public function test_license_create_form_persists_requestable_when_checked(): void
+ {
+ // License store() also needs a serial-shaped identity or seats
+ // validation may bite - the payload's seats=3 handles it.
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('licenses.store'), $this->licensePayload() + ['requestable' => '1'])
+ ->assertRedirect();
+
+ $this->assertTrue((bool) License::where('name', 'Test License')->sole()->requestable);
+ }
+
+ public function test_license_update_can_uncheck_requestable(): void
+ {
+ $license = License::factory()->create(['requestable' => true]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->put(route('licenses.update', $license), array_merge(
+ $this->licensePayload(),
+ ['name' => $license->name, 'seats' => $license->seats],
+ ))
+ ->assertRedirect();
+
+ $this->assertFalse((bool) $license->fresh()->requestable);
+ }
+}
diff --git a/tests/Feature/Requests/Ui/UserRequestedIndexTest.php b/tests/Feature/Requests/Ui/UserRequestedIndexTest.php
new file mode 100644
index 0000000000..1f6b2bd1d7
--- /dev/null
+++ b/tests/Feature/Requests/Ui/UserRequestedIndexTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Tests\Feature\Requests\Ui;
+
+use App\Models\Accessory;
+use App\Models\Asset;
+use App\Models\CheckoutRequest;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Coverage for the "my open requests" surfaces at /account/requested
+ * (the shell page) and GET /api/v1/account/requests (the data source
+ * the shell page hydrates from). Adds self-cancel wiring: each row
+ * carries a cancel_url and POSTing to it from the request owner
+ * cancels the row without needing an admin gate.
+ */
+class UserRequestedIndexTest extends TestCase
+{
+ public function test_api_row_scopes_to_the_authed_user_and_open_requests_only(): void
+ {
+ $user = User::factory()->create();
+ $other = User::factory()->create();
+ $asset = Asset::factory()->requestable()->create();
+ $accessory = Accessory::factory()->create(['requestable' => true]);
+
+ $openOwn = CheckoutRequest::factory()->create([
+ 'user_id' => $user->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => $user->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'canceled_at' => now(),
+ ]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => $other->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $response = $this->actingAsForApi($user)
+ ->getJson(route('api.assets.requested'))
+ ->assertOk();
+
+ $rows = $response->json('rows') ?? [];
+ $this->assertCount(1, $rows, 'Only the auth user\'s single open request should surface.');
+ $this->assertSame($openOwn->id, $rows[0]['id']);
+ $this->assertArrayHasKey('cancel_url', $rows[0]);
+ $this->assertStringContainsString('/account/request/asset/'.$asset->id, $rows[0]['cancel_url']);
+ }
+
+ public function test_posting_to_the_cancel_url_cancels_the_users_own_request(): void
+ {
+ $user = User::factory()->create();
+ $asset = Asset::factory()->requestable()->create();
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $user->id,
+ 'requestable_id' => $asset->id,
+ 'requestable_type' => Asset::class,
+ ]);
+
+ $this->actingAs($user)
+ ->post(route('account/request-item', [
+ 'itemType' => 'asset',
+ 'itemId' => $asset->id,
+ ]))
+ ->assertRedirect();
+
+ $this->assertNotNull(
+ $request->fresh()->canceled_at,
+ 'Self-POST from the row owner must flip canceled_at.'
+ );
+ }
+
+ public function test_shell_page_loads_for_authed_user(): void
+ {
+ // The page is API-backed so this only pins the route + view
+ // wire-up. The self-cancel column contents themselves render
+ // client-side via userRequestCancelFormatter (JS), which is
+ // outside the test scope.
+ $this->actingAs(User::factory()->create())
+ ->get(route('account.requested'))
+ ->assertOk();
+ }
+}
diff --git a/tests/Support/Importing/ComponentsImportFileBuilder.php b/tests/Support/Importing/ComponentsImportFileBuilder.php
index 13e162a896..49968668ef 100644
--- a/tests/Support/Importing/ComponentsImportFileBuilder.php
+++ b/tests/Support/Importing/ComponentsImportFileBuilder.php
@@ -43,6 +43,7 @@ class ComponentsImportFileBuilder extends FileBuilder
'purchaseDate' => 'Purchase Date',
'quantity' => 'Quantity',
'serialNumber' => 'Serial number',
+ 'requestable' => 'Requestable',
];
}
@@ -65,6 +66,7 @@ class ComponentsImportFileBuilder extends FileBuilder
'purchaseDate' => $faker->date,
'quantity' => rand(1, 100_000),
'serialNumber' => 'SN:COM:'.Str::random(),
+ 'requestable' => '',
];
}
}
diff --git a/tests/Support/Importing/LicensesImportFileBuilder.php b/tests/Support/Importing/LicensesImportFileBuilder.php
index 33cd09a38e..3012bff40d 100644
--- a/tests/Support/Importing/LicensesImportFileBuilder.php
+++ b/tests/Support/Importing/LicensesImportFileBuilder.php
@@ -57,6 +57,7 @@ class LicensesImportFileBuilder extends FileBuilder
'seats' => 'seats',
'serialNumber' => 'Serial number',
'supplierName' => 'supplier',
+ 'requestable' => 'Requestable',
];
}
@@ -87,6 +88,7 @@ class LicensesImportFileBuilder extends FileBuilder
'seats' => rand(1, 10),
'serialNumber' => 'SN:LIC:'.Str::random(),
'supplierName' => $faker->company,
+ 'requestable' => '',
];
}
}