A free open source IT asset/license management system
Added checkout fulfillment tests
snipe8e321c356ee1
diff --git a/tests/Feature/CheckoutRequests/FulfillmentHookTest.php b/tests/Feature/CheckoutRequests/FulfillmentHookTest.phpnew file mode 100644index 0000000000..d5c20b50a3--- /dev/null+++ b/tests/Feature/CheckoutRequests/FulfillmentHookTest.php@@ -0,0 +1,177 @@+<?php++namespace Tests\Feature\CheckoutRequests;++use App\Enums\CheckoutRequestState;+use App\Events\CheckoutableCheckedOut;+use App\Models\Accessory;+use App\Models\Asset;+use App\Models\CheckoutRequest;+use App\Models\Component;+use App\Models\Consumable;+use App\Models\License;+use App\Models\LicenseSeat;+use App\Models\User;+use Tests\TestCase;++/**+ * FulfillCheckoutRequestListener closes the fulfillment loop when a+ * checkout event fires. Runs for every checkoutable type via the+ * shared CheckoutableCheckedOut event, so each polymorphic case+ * gets its own assertion here.+ *+ * The listener maps LicenseSeat -> License for the request lookup+ * because requesters ask for a License (not a specific seat) at this+ * time (though that could change down the line).+ */+class FulfillmentHookTest extends TestCase+{+ public function test_checkout_fulfills_matching_open_asset_request(): void+ {+ $user = User::factory()->create();+ $asset = Asset::factory()->assignedToUser()->create();+ $asset->assigned_to = null;+ $asset->assigned_type = null;+ $asset->save();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);++ $asset->checkOut($user, User::factory()->create());++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ $this->assertNotNull($request->fresh()->fulfilled_at);+ }++ public function test_checkout_fulfills_matching_open_accessory_request(): void+ {+ $user = User::factory()->create();+ $accessory = Accessory::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $accessory->id,+ 'requestable_type' => Accessory::class,+ ]);++ event(new CheckoutableCheckedOut($accessory, $user, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ }++ public function test_checkout_fulfills_matching_open_consumable_request(): void+ {+ $user = User::factory()->create();+ $consumable = Consumable::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $consumable->id,+ 'requestable_type' => Consumable::class,+ ]);++ event(new CheckoutableCheckedOut($consumable, $user, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ }++ public function test_checkout_fulfills_matching_open_component_request(): void+ {+ // Components check out to Assets in the normal flow, but+ // the listener only fires for user-target checkouts (a+ // request is always user-owned). Simulate the user-target+ // shape a direct event dispatch produces.+ $user = User::factory()->create();+ $component = Component::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $component->id,+ 'requestable_type' => Component::class,+ ]);++ event(new CheckoutableCheckedOut($component, $user, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ }++ public function test_license_seat_checkout_fulfills_matching_open_license_request(): void+ {+ // License requests are filed against License, not+ // LicenseSeat. The listener has to translate the event's+ // LicenseSeat back to its parent License for the lookup to+ // hit.+ $user = User::factory()->create();+ $license = License::factory()->create(['seats' => 5]);+ $licenseSeat = LicenseSeat::where('license_id', $license->id)->first();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $license->id,+ 'requestable_type' => License::class,+ ]);++ event(new CheckoutableCheckedOut($licenseSeat, $user, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ }++ public function test_checkout_to_different_user_does_not_fulfill_request(): void+ {+ // The listener matches on user_id. A checkout to someone+ // OTHER than the requester must leave the request open so+ // the requester can still get their turn.+ $requester = User::factory()->create();+ $someoneElse = User::factory()->create();+ $accessory = Accessory::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $requester->id,+ 'requestable_id' => $accessory->id,+ 'requestable_type' => Accessory::class,+ ]);++ event(new CheckoutableCheckedOut($accessory, $someoneElse, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Pending, $request->fresh()->state);+ }++ public function test_checkout_to_location_does_not_fulfill_any_request(): void+ {+ // Location-target checkouts don't correspond to a+ // requester. Guard prevents a checkout to Location #5 from+ // silently closing every request whose user_id happens to+ // equal 5.+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);++ $location = \App\Models\Location::factory()->create();+ event(new CheckoutableCheckedOut($asset, $location, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Pending, $request->fresh()->state);+ }++ public function test_canceled_request_is_not_resurrected_by_a_later_checkout(): void+ {+ // Terminal-state guard test at the listener level. A user+ // cancels their request, then admin later checks out to+ // them anyway (unrelated to the request). The canceled row+ // must stay canceled - the listener's fulfillment lookup+ // filters on state=pending so it doesn't even reach the+ // canceled row.+ $user = User::factory()->create();+ $accessory = Accessory::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $accessory->id,+ 'requestable_type' => Accessory::class,+ ]);+ $accessory->cancelRequest($user->id);++ event(new CheckoutableCheckedOut($accessory, $user, User::factory()->create(), null));++ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ }+}diff --git a/tests/Feature/CheckoutRequests/StateMachineTest.php b/tests/Feature/CheckoutRequests/StateMachineTest.phpnew file mode 100644index 0000000000..ec1629d4f8--- /dev/null+++ b/tests/Feature/CheckoutRequests/StateMachineTest.php@@ -0,0 +1,185 @@+<?php++namespace Tests\Feature\CheckoutRequests;++use App\Actions\CheckoutRequests\FulfillCheckoutRequestAction;+use App\Enums\CheckoutRequestState;+use App\Exceptions\InvalidCheckoutRequestTransition;+use App\Models\Accessory;+use App\Models\Asset;+use App\Models\CheckoutRequest;+use App\Models\User;+use Tests\TestCase;++/**+ * Codifies which state transitions are legal + which throw. The+ * state column drives every lifecycle decision downstream (admin+ * queue filters, requester tab, Requestable::isRequestedBy gate,+ * withCount('openRequests') for the requests_counter replacement)+ * so a regression here silently corrupts the queue.+ *+ * Terminal states (fulfilled, canceled) are irreversible. Same-+ * state writes (fulfill an already-fulfilled row) no-op silently so+ * the fulfillment hook is safe to fire twice on a rapid retry.+ */+class StateMachineTest extends TestCase+{+ public function test_new_request_defaults_to_pending(): void+ {+ $request = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => Asset::factory()->create()->id,+ 'requestable_type' => Asset::class,+ ]);++ $this->assertSame(CheckoutRequestState::Pending, $request->state);+ }++ public function test_pending_transitions_to_fulfilled_via_action(): void+ {+ $request = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => Asset::factory()->create()->id,+ 'requestable_type' => Asset::class,+ ]);++ FulfillCheckoutRequestAction::run($request);+ $request->refresh();++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->state);+ $this->assertNotNull($request->fulfilled_at);+ }++ public function test_pending_transitions_to_canceled_via_cancel_request(): void+ {+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);++ $affected = $asset->cancelRequest($user->id);+ $request->refresh();++ $this->assertSame(1, $affected);+ $this->assertSame(CheckoutRequestState::Canceled, $request->state);+ $this->assertNotNull($request->canceled_at);+ }++ public function test_canceled_cannot_be_fulfilled(): void+ {+ // Terminal-state guard: a canceled row is done, and the+ // fulfillment hook must not resurrect it if the same user+ // later gets a checkout for the same item.+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);+ $asset->cancelRequest($user->id);+ $request->refresh();++ $this->expectException(InvalidCheckoutRequestTransition::class);+ FulfillCheckoutRequestAction::run($request);+ }++ public function test_fulfilled_is_idempotent(): void+ {+ // The fulfillment hook can fire twice (retry, tests). A+ // second call on a fulfilled row must no-op instead of+ // throwing so the checkout controller doesn't 500 on a+ // benign re-entry.+ $request = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => Asset::factory()->create()->id,+ 'requestable_type' => Asset::class,+ ]);+ FulfillCheckoutRequestAction::run($request);+ $request->refresh();+ $firstFulfilledAt = $request->fulfilled_at;++ FulfillCheckoutRequestAction::run($request);+ $request->refresh();++ $this->assertSame(CheckoutRequestState::Fulfilled, $request->state);+ // Second call didn't overwrite the original timestamp,+ // which would happen if the guard was missing.+ $this->assertEquals($firstFulfilledAt->timestamp, $request->fulfilled_at->timestamp);+ }++ public function test_cancel_request_only_touches_pending_rows(): void+ {+ // If a user's request is already fulfilled (e.g. auto-+ // fulfilled by a prior checkout), a later cancel by the+ // same user for the same item must not flip the fulfilled+ // row back to canceled - the pending-state filter in+ // Requestable::cancelRequest gates on state, not just+ // canceled_at.+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);+ FulfillCheckoutRequestAction::run($request);++ $affected = $asset->cancelRequest($user->id);++ $this->assertSame(0, $affected);+ $this->assertSame(CheckoutRequestState::Fulfilled, $request->fresh()->state);+ }++ public function test_open_scope_excludes_terminal_states(): void+ {+ $asset = Asset::factory()->create();+ $accessory = Accessory::factory()->create();++ // Pending: counts as open.+ CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);+ // Fulfilled: does not count as open.+ $fulfilled = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);+ FulfillCheckoutRequestAction::run($fulfilled);+ // Canceled: does not count as open.+ $canceledUser = User::factory()->create();+ CheckoutRequest::factory()->create([+ 'user_id' => $canceledUser->id,+ 'requestable_id' => $accessory->id,+ 'requestable_type' => Accessory::class,+ ]);+ $accessory->cancelRequest($canceledUser->id);++ $this->assertSame(1, CheckoutRequest::open()->count());+ }++ public function test_is_requested_by_returns_null_for_fulfilled_row(): void+ {+ // Requestable::isRequestedBy is what gates the "you already+ // have an open request" duplicate check + the request/+ // cancel button-swap on the requestable-tab. A fulfilled row+ // must not count so the user can request the same item+ // again after their previous request closed.+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ ]);+ FulfillCheckoutRequestAction::run($request);++ $this->assertNull($asset->isRequestedBy($user));+ }+}