Skip to content

A free open source IT asset/license management system

Still more tests

snipe386473451e70
diff --git a/tests/Feature/Components/Ui/BulkFulfillComponentTest.php b/tests/Feature/Components/Ui/BulkFulfillComponentTest.php
new file mode 100644
index 0000000000..b8f0a7f2a9
--- /dev/null
+++ b/tests/Feature/Components/Ui/BulkFulfillComponentTest.php
@@ -0,0 +1,161 @@
+<?php
+
+namespace Tests\Feature\Components\Ui;
+
+use App\Actions\CheckoutRequests\FulfillCheckoutRequestAction;
+use App\Enums\CheckoutRequestState;
+use App\Models\Asset;
+use App\Models\CheckoutRequest;
+use App\Models\Component;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+/**
+ * Bulk-fulfill flow for Components. Distinct from the user-target
+ * qty-tracked types (accessory/consumable/license) because a
+ * component checkout targets an Asset (not a User) - the component
+ * gets installed INTO an asset belonging to the requester. The
+ * per-row picker is scoped to the requester's assigned assets.
+ *
+ * The state-machine listener only fires fulfillment for User-
+ * target checkouts, so this controller closes the request
+ * explicitly via FulfillCheckoutRequestAction after each row
+ * commits. Partial-fulfillment tracking rides through via the
+ * qty argument.
+ */
+class BulkFulfillComponentTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Notification::fake();
+ }
+
+ public function test_requires_checkout_permission_on_components(): void
+ {
+ $component = Component::factory()->create(['qty' => 10]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('components.fulfill-requests.create', $component))
+ ->assertForbidden();
+ }
+
+ public function test_row_disabled_for_requester_with_no_assigned_assets(): void
+ {
+ // Component installs INTO an asset the requester owns. A
+ // requester with zero assigned assets has no target to
+ // install into, so their row surfaces with an empty
+ // available-asset set (view renders disabled with an
+ // explanatory message).
+ $admin = User::factory()->checkoutComponents()->create();
+ $component = Component::factory()->create(['qty' => 10]);
+ $noAssetsRequester = User::factory()->create();
+
+ CheckoutRequest::factory()->create([
+ 'user_id' => $noAssetsRequester->id,
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->get(route('components.fulfill-requests.create', $component))
+ ->assertOk()
+ ->assertViewHas('rowContext', function ($ctx) use ($noAssetsRequester) {
+ $req = CheckoutRequest::where('user_id', $noAssetsRequester->id)->first();
+
+ return isset($ctx[$req->id])
+ && $ctx[$req->id]['availableAssets']->isEmpty();
+ });
+ }
+
+ public function test_fulfills_ticked_row_and_closes_request_explicitly(): void
+ {
+ // Component targets an Asset, and the state-machine
+ // listener only fires for User-target checkouts. Verify
+ // the controller closes the request explicitly (the
+ // listener would silently skip it).
+ $admin = User::factory()->checkoutComponents()->create();
+ $component = Component::factory()->create(['qty' => 10]);
+ $requester = User::factory()->create();
+ $targetAsset = Asset::factory()->assignedToUser($requester)->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'quantity' => 2,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('components.fulfill-requests.store', $component), [
+ 'enabled_requests' => [$request->id],
+ 'asset_id' => [$request->id => $targetAsset->id],
+ 'qty' => [$request->id => 2],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);
+ $this->assertSame(2, $request->fresh()->fulfilled_quantity);
+ }
+
+ public function test_asset_not_belonging_to_requester_is_rejected(): void
+ {
+ // Requester-scoping guard: hand-crafted POST pointing at
+ // an asset that belongs to a DIFFERENT user must not
+ // install the component into that other user's asset.
+ $admin = User::factory()->checkoutComponents()->create();
+ $component = Component::factory()->create(['qty' => 10]);
+ $requester = User::factory()->create();
+ $someoneElse = User::factory()->create();
+ $someoneElsesAsset = Asset::factory()->assignedToUser($someoneElse)->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'quantity' => 1,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('components.fulfill-requests.store', $component), [
+ 'enabled_requests' => [$request->id],
+ 'asset_id' => [$request->id => $someoneElsesAsset->id],
+ 'qty' => [$request->id => 1],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Pending, $request->fresh()->state);
+ }
+
+ public function test_partial_qty_leaves_row_pending_with_counter_advanced(): void
+ {
+ $admin = User::factory()->checkoutComponents()->create();
+ $component = Component::factory()->create(['qty' => 10]);
+ $requester = User::factory()->create();
+ $targetAsset = Asset::factory()->assignedToUser($requester)->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $component->id,
+ 'requestable_type' => Component::class,
+ 'quantity' => 5,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('components.fulfill-requests.store', $component), [
+ 'enabled_requests' => [$request->id],
+ 'asset_id' => [$request->id => $targetAsset->id],
+ 'qty' => [$request->id => 2],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $fresh = $request->fresh();
+ $this->assertSame(CheckoutRequestState::Pending, $fresh->state);
+ $this->assertSame(2, $fresh->fulfilled_quantity);
+ $this->assertSame(3, $fresh->remainingQuantity());
+ }
+}
diff --git a/tests/Feature/Licenses/Ui/BulkFulfillLicenseTest.php b/tests/Feature/Licenses/Ui/BulkFulfillLicenseTest.php
new file mode 100644
index 0000000000..5a020de518
--- /dev/null
+++ b/tests/Feature/Licenses/Ui/BulkFulfillLicenseTest.php
@@ -0,0 +1,128 @@
+<?php
+
+namespace Tests\Feature\Licenses\Ui;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\CheckoutRequest;
+use App\Models\License;
+use App\Models\LicenseSeat;
+use App\Models\User;
+use Illuminate\Support\Facades\Notification;
+use Tests\TestCase;
+
+/**
+ * Bulk-fulfill flow for Licenses. Same user-target contract as
+ * accessories/consumables but qty is pinned to 1 per row - licenses
+ * are one-seat-per-requester by convention (nobody realistically
+ * asks for N seats of Photoshop for themselves). The controller
+ * pins qty server-side so a hand-crafted POST can't over-allocate.
+ * Each ticked row claims one seat via freeSeat(lock:true).
+ */
+class BulkFulfillLicenseTest extends TestCase
+{
+ protected function setUp(): void
+ {
+ parent::setUp();
+ Notification::fake();
+ }
+
+ public function test_requires_checkout_permission_on_licenses(): void
+ {
+ $license = License::factory()->create(['seats' => 5]);
+
+ $this->actingAs(User::factory()->create())
+ ->get(route('licenses.fulfill-requests.create', $license))
+ ->assertForbidden();
+ }
+
+ public function test_show_hides_qty_input(): void
+ {
+ // License-specific: qty is fixed at 1 per row, so the
+ // per-row picker doesn't render a qty input. Controller
+ // passes hideQty=true to the view.
+ $admin = User::factory()->checkoutLicenses()->create();
+ $license = License::factory()->create(['seats' => 5]);
+ CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->get(route('licenses.fulfill-requests.create', $license))
+ ->assertOk()
+ ->assertViewHas('hideQty', true);
+ }
+
+ public function test_fulfills_ticked_rows_by_claiming_one_seat_each(): void
+ {
+ $admin = User::factory()->checkoutLicenses()->create();
+ $license = License::factory()->create(['seats' => 5]);
+ $alice = User::factory()->create();
+ $bob = User::factory()->create();
+
+ $aliceReq = CheckoutRequest::factory()->create([
+ 'user_id' => $alice->id,
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ ]);
+ $bobReq = CheckoutRequest::factory()->create([
+ 'user_id' => $bob->id,
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('licenses.fulfill-requests.store', $license), [
+ 'enabled_requests' => [$aliceReq->id, $bobReq->id],
+ 'user_id' => [
+ $aliceReq->id => $alice->id,
+ $bobReq->id => $bob->id,
+ ],
+ // Even if the POST claims a higher qty, the
+ // controller pins to 1 (see next test).
+ 'qty' => [
+ $aliceReq->id => 1,
+ $bobReq->id => 1,
+ ],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(CheckoutRequestState::Fulfilled, $aliceReq->fresh()->state);
+ $this->assertSame(CheckoutRequestState::Fulfilled, $bobReq->fresh()->state);
+
+ $this->assertSame(1, LicenseSeat::where('license_id', $license->id)
+ ->where('assigned_to', $alice->id)->count());
+ $this->assertSame(1, LicenseSeat::where('license_id', $license->id)
+ ->where('assigned_to', $bob->id)->count());
+ }
+
+ public function test_hand_crafted_qty_above_one_is_pinned_to_one_server_side(): void
+ {
+ // Belt: even if the POST claims qty=5, the controller
+ // ignores it and claims exactly one seat. One-seat-per-
+ // request is a hard invariant, not just a UI convention.
+ $admin = User::factory()->checkoutLicenses()->create();
+ $license = License::factory()->create(['seats' => 10]);
+ $requester = User::factory()->create();
+
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => $requester->id,
+ 'requestable_id' => $license->id,
+ 'requestable_type' => License::class,
+ ]);
+
+ $this->actingAs($admin)
+ ->post(route('licenses.fulfill-requests.store', $license), [
+ 'enabled_requests' => [$request->id],
+ 'user_id' => [$request->id => $requester->id],
+ 'qty' => [$request->id => 99],
+ 'notes' => [],
+ ])
+ ->assertRedirect(route('requests.index'));
+
+ $this->assertSame(1, LicenseSeat::where('license_id', $license->id)
+ ->where('assigned_to', $requester->id)->count());
+ }
+}
diff --git a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
index 6877092058..86f9703447 100644
--- a/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
+++ b/tests/Feature/Requests/Ui/AssetRequestIndexTest.php
@@ -16,7 +16,7 @@ class AssetRequestIndexTest extends TestCase
public function test_privileged_user_gets_the_shell_page()
{
- // Gate is viewAnyCheckoutRequests - satisfied by checkout
+ // Gate is canCheckoutAtLeastOneItemType - satisfied by checkout
// perm on any of the five checkoutable types. viewAssets
// alone wouldn't pass (that's the view perm, not checkout).
$this->actingAs(User::factory()->checkoutAssets()->create())
diff --git a/tests/Feature/Requests/Ui/BulkCancelRequestTest.php b/tests/Feature/Requests/Ui/BulkCancelRequestTest.php
index 78edb59a42..5011bc61de 100644
--- a/tests/Feature/Requests/Ui/BulkCancelRequestTest.php
+++ b/tests/Feature/Requests/Ui/BulkCancelRequestTest.php
@@ -26,7 +26,7 @@ class BulkCancelRequestTest extends TestCase
public function test_empty_selection_flashes_an_error(): void
{
- $this->actingAs(User::factory()->viewAssets()->create())
+ $this->actingAs(User::factory()->checkoutAssets()->create())
->post(route('requests.bulk-cancel'), ['ids' => []])
->assertRedirectToRoute('requests.index')
->assertSessionHas('error');
@@ -34,7 +34,7 @@ class BulkCancelRequestTest extends TestCase
public function test_cancels_every_selected_open_request(): void
{
- $admin = User::factory()->viewAssets()->create();
+ $admin = User::factory()->checkoutAssets()->create();
$requests = CheckoutRequest::factory()->count(3)->create();
$this->actingAs($admin)
@@ -55,7 +55,7 @@ class BulkCancelRequestTest extends TestCase
// 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();
+ $admin = User::factory()->checkoutAssets()->create();
$canceled = CheckoutRequest::factory()->create(['canceled_at' => now()->subMinute()]);
$this->actingAs($admin)
@@ -76,7 +76,7 @@ class BulkCancelRequestTest extends TestCase
[$companyA, $companyB] = Company::factory()->count(2)->create();
- $adminInA = $companyA->users()->save(User::factory()->viewAssets()->make());
+ $adminInA = $companyA->users()->save(User::factory()->checkoutAssets()->make());
$assetInB = Asset::factory()->for($companyB)->requestable()->create();
$requestInB = CheckoutRequest::factory()->create([
'requestable_id' => $assetInB->id,