Skip to content

A free open source IT asset/license management system

Added tests

snipe66a7aa8687af
diff --git a/tests/Feature/Consumables/Api/DeletedIndexRestoreActionTest.php b/tests/Feature/Consumables/Api/DeletedIndexRestoreActionTest.php
new file mode 100644
index 0000000000..035b055e8a
--- /dev/null
+++ b/tests/Feature/Consumables/Api/DeletedIndexRestoreActionTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Tests\Feature\Consumables\Api;
+
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * genericActionsFormatter in partials/bootstrap-table reads
+ * row.available_actions.restore (top-level, not nested in
+ * bulk_selectable) to decide whether to render the per-row restore
+ * button on the Deleted index. Pin that key here so a future
+ * refactor doesn't push it back under bulk_selectable only and
+ * leave admins with a disabled trash fallback instead of restore.
+ */
+class DeletedIndexRestoreActionTest extends TestCase
+{
+ public function test_soft_deleted_consumable_row_offers_restore_action(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $consumable->delete();
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.index', ['status' => 'Deleted']))
+ ->assertOk();
+
+ $row = collect($response->json('rows'))->firstWhere('id', $consumable->id);
+ $this->assertNotNull($row, 'Soft-deleted consumable must appear on the Deleted index.');
+ $this->assertTrue($row['available_actions']['restore'] ?? false);
+ }
+
+ public function test_live_consumable_row_does_not_offer_restore_action(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.index'))
+ ->assertOk();
+
+ $row = collect($response->json('rows'))->firstWhere('id', $consumable->id);
+ $this->assertNotNull($row);
+ $this->assertFalse($row['available_actions']['restore'] ?? true);
+ }
+}
diff --git a/tests/Feature/Consumables/Api/GetDataViewTest.php b/tests/Feature/Consumables/Api/GetDataViewTest.php
new file mode 100644
index 0000000000..29b38b88fd
--- /dev/null
+++ b/tests/Feature/Consumables/Api/GetDataViewTest.php
@@ -0,0 +1,102 @@
+<?php
+
+namespace Tests\Feature\Consumables\Api;
+
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Covers the API endpoint that backs the consumable show page's
+ * Assigned tab datatable. The row shape here drives per-row consume
+ * actions and the checkbox column's enabled/disabled state via
+ * bulk_selectable. Only outstanding (non-consumed) rows surface
+ * because consumed distributions are historical and belong on the
+ * History tab, not the "currently assigned to" list.
+ */
+class GetDataViewTest extends TestCase
+{
+ public function test_endpoint_returns_outstanding_rows(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.show.users', $consumable->id))
+ ->assertOk()
+ ->assertJson([
+ 'total' => 1,
+ 'rows' => [
+ [
+ 'id' => $assignment->id,
+ 'consumable_id' => $consumable->id,
+ ],
+ ],
+ ]);
+ }
+
+ public function test_row_marks_consume_action_available_when_outstanding(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.show.users', $consumable->id))
+ ->assertOk();
+
+ $this->assertTrue($response->json('rows.0.available_actions.consume'));
+ $this->assertTrue($response->json('rows.0.available_actions.bulk_selectable.consume'));
+ }
+
+ public function test_endpoint_hides_consumed_rows(): void
+ {
+ // Mark-consumed is a terminal state. Once a pivot row has a
+ // consumed_at stamp, it stops surfacing on the Assigned tab
+ // (which lists "currently distributed" rows only). The
+ // consume audit log on the History tab preserves the record.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+ $assignment->consumed_at = now();
+ $assignment->save();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.show.users', $consumable->id))
+ ->assertOk()
+ ->assertJson([
+ 'total' => 0,
+ 'rows' => [],
+ ]);
+ }
+
+ public function test_endpoint_returns_mix_of_outstanding_and_hides_consumed(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+ $outstanding = $assignments->first();
+ $consumed = $assignments->last();
+ $consumed->consumed_at = now();
+ $consumed->save();
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.consumables.show.users', $consumable->id))
+ ->assertOk();
+
+ $this->assertSame(1, $response->json('total'));
+ $this->assertSame($outstanding->id, $response->json('rows.0.id'));
+ }
+
+ public function test_row_hides_consume_action_when_caller_lacks_checkout_permission(): void
+ {
+ // Regression pin: Gate::allows('checkout', $consumable) is
+ // computed once at the top of getDataView and threaded through
+ // every row, so a view-only caller sees consume=false on all
+ // outstanding rows.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+
+ $response = $this->actingAsForApi(User::factory()->viewConsumables()->create())
+ ->getJson(route('api.consumables.show.users', $consumable->id))
+ ->assertOk();
+
+ $this->assertFalse($response->json('rows.0.available_actions.consume'));
+ }
+}
diff --git a/tests/Feature/Consumables/ConsumableArchiveValidationTest.php b/tests/Feature/Consumables/ConsumableArchiveValidationTest.php
new file mode 100644
index 0000000000..7e2389dc93
--- /dev/null
+++ b/tests/Feature/Consumables/ConsumableArchiveValidationTest.php
@@ -0,0 +1,145 @@
+<?php
+
+namespace Tests\Feature\Consumables;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\CheckoutRequest;
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use App\Models\User;
+use Tests\TestCase;
+
+class ConsumableArchiveValidationTest extends TestCase
+{
+ public function test_consumable_can_save_as_requestable_without_archived(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $consumable->requestable = 1;
+ $consumable->archived = 0;
+
+ $this->assertTrue($consumable->save());
+ }
+
+ public function test_consumable_can_save_as_archived_without_requestable(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $consumable->archived = 1;
+ $consumable->requestable = 0;
+
+ $this->assertTrue($consumable->save());
+ }
+
+ public function test_consumable_cannot_save_as_both_requestable_and_archived(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $consumable->requestable = 1;
+ $consumable->archived = 1;
+
+ $this->assertFalse($consumable->save());
+ $this->assertArrayHasKey('archived', $consumable->getErrors()->toArray());
+ $this->assertArrayHasKey('requestable', $consumable->getErrors()->toArray());
+ }
+
+ public function test_consumable_with_outstanding_distributions_cannot_be_archived(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $consumable->refresh();
+
+ $consumable->archived = 1;
+
+ $this->assertFalse($consumable->save());
+ $this->assertSame(
+ trans('general.archived_blocked_by_outstanding', ['item_type' => trans('general.consumable')]),
+ $consumable->getErrors()->get('archived')[0],
+ );
+ }
+
+ public function test_consumable_without_outstanding_distributions_can_be_archived(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $consumable->archived = 1;
+
+ $this->assertTrue($consumable->save());
+ }
+
+ public function test_consumable_with_all_consumed_distributions_can_be_archived(): void
+ {
+ // Backfill behavior test: after every pivot row has been marked
+ // consumed, the archive guard no longer fires because
+ // numOutstanding() returns 0. Historical distributions still
+ // sit in the pivot table, they just don't gate the archive.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $consumable->refresh();
+ ConsumableAssignment::query()
+ ->where('consumable_id', $consumable->id)
+ ->update(['consumed_at' => now()]);
+
+ $consumable->archived = 1;
+
+ $this->assertTrue($consumable->save());
+ }
+
+ public function test_archiving_consumable_auto_cancels_pending_requests(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $pending = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ ]);
+
+ $consumable->archived = 1;
+ $consumable->save();
+
+ $pending->refresh();
+ $this->assertNotNull($pending->canceled_at);
+ $this->assertSame(CheckoutRequestState::Canceled, $pending->state);
+ }
+
+ public function test_archiving_does_not_touch_already_canceled_requests(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $previousCancelAt = now()->subDay();
+ $alreadyCanceled = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ 'canceled_at' => $previousCancelAt,
+ ]);
+
+ $consumable->archived = 1;
+ $consumable->save();
+
+ $alreadyCanceled->refresh();
+ $this->assertEquals($previousCancelAt->toDateTimeString(), $alreadyCanceled->canceled_at->toDateTimeString());
+ }
+
+ public function test_un_archiving_does_not_touch_requests(): void
+ {
+ $consumable = Consumable::factory()->archived()->create();
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $consumable->id,
+ 'requestable_type' => Consumable::class,
+ ]);
+
+ $consumable->archived = 0;
+ $consumable->save();
+
+ $request->refresh();
+ $this->assertNull($request->canceled_at);
+ }
+
+ public function test_set_archived_attribute_normalizes_empty_string(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $consumable->archived = '';
+
+ $this->assertFalse($consumable->archived);
+ }
+}
diff --git a/tests/Feature/Consumables/ConsumableSoftDeletePreservesPivotTest.php b/tests/Feature/Consumables/ConsumableSoftDeletePreservesPivotTest.php
new file mode 100644
index 0000000000..0aeb659c17
--- /dev/null
+++ b/tests/Feature/Consumables/ConsumableSoftDeletePreservesPivotTest.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace Tests\Feature\Consumables;
+
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use Tests\TestCase;
+
+/**
+ * Pins the behavior change in the Consumable observer: soft-delete
+ * preserves pivot rows (and their consumed_at stamps) so a restore
+ * brings the item back with its mark-consumed lifecycle intact.
+ * Only force-delete tears the pivot down.
+ */
+class ConsumableSoftDeletePreservesPivotTest extends TestCase
+{
+ public function test_soft_delete_preserves_pivot_rows(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $pivotId = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail()->id;
+
+ $consumable->delete();
+
+ $this->assertDatabaseHas('consumables_users', ['id' => $pivotId]);
+ }
+
+ public function test_soft_delete_preserves_consumed_at_stamps(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+ $stamp = now();
+ $assignment->consumed_at = $stamp;
+ $assignment->save();
+
+ $consumable->delete();
+
+ $this->assertDatabaseHas('consumables_users', [
+ 'id' => $assignment->id,
+ 'consumed_at' => $stamp->toDateTimeString(),
+ ]);
+ }
+
+ public function test_restore_brings_pivot_history_back_intact(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+ $consumedOne = $assignments->first();
+ $consumedOne->consumed_at = now();
+ $consumedOne->save();
+
+ $consumable->delete();
+ $consumable->fresh()->restore();
+
+ $restored = Consumable::query()->findOrFail($consumable->id);
+ $this->assertSame(2, $restored->numCheckedOut());
+ $this->assertSame(1, $restored->numOutstanding());
+ $this->assertSame(1, $restored->numConsumed());
+ }
+
+ public function test_force_delete_removes_pivot_rows(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $pivotId = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail()->id;
+
+ $consumable->forceDelete();
+
+ $this->assertDatabaseMissing('consumables_users', ['id' => $pivotId]);
+ }
+
+ public function test_purge_removes_pivot_rows_for_soft_deleted_consumables(): void
+ {
+ // Purge runs raw DB::table()->delete(), bypassing Eloquent
+ // observers and the forceDeleted hook. The pivot cleanup
+ // must come from Purge's childTables map instead.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $pivotId = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail()->id;
+ $consumable->delete();
+
+ $this->artisan('snipeit:purge', ['--force' => 'true'])->assertExitCode(0);
+
+ $this->assertDatabaseMissing('consumables_users', ['id' => $pivotId]);
+ $this->assertDatabaseMissing('consumables', ['id' => $consumable->id]);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/BulkConsumablesFmcsScopingTest.php b/tests/Feature/Consumables/Ui/BulkConsumablesFmcsScopingTest.php
new file mode 100644
index 0000000000..fe3f9da344
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/BulkConsumablesFmcsScopingTest.php
@@ -0,0 +1,242 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use App\Models\Location;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Adversarial cross-company tests for every bulk endpoint added or
+ * touched in the consumables bulk + archive + mark-consumed slice.
+ * Mirrors BulkAccessoriesFmcsScopingTest. The controllers route
+ * through auto-scoped Consumable queries, so cross-company ids
+ * should never reach the per-row handlers. Pin the behavior anyway
+ * because any future refactor that bypasses the CompanyableScope
+ * would otherwise silently open a cross-tenant hole.
+ */
+class BulkConsumablesFmcsScopingTest extends TestCase
+{
+ public function test_bulk_delete_cannot_delete_cross_company_consumable_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->deleteConsumables()->viewConsumables()->make());
+ $target = Consumable::factory()->for($companyB)->create();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkdelete.store'), ['ids' => [$target->id]]);
+
+ $this->assertNull($target->fresh()->deleted_at);
+ }
+
+ public function test_bulk_edit_dispatcher_does_not_leak_cross_company_consumable_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->viewConsumables()->editConsumables()->checkinConsumables()->make());
+
+ $visible = Consumable::factory()->for($companyA)->create(['name' => 'Visible']);
+ $hidden = Consumable::factory()->for($companyB)->create(['name' => 'Hidden']);
+
+ $response = $this->actingAs($actor)
+ ->post(route('consumables.bulkedit.show'), [
+ 'ids' => [$visible->id, $hidden->id],
+ 'bulk_actions' => 'edit',
+ ])
+ ->assertOk()
+ ->assertViewIs('consumables.bulk-edit');
+
+ $consumables = $response->viewData('consumables');
+ $this->assertTrue($consumables->contains('id', $visible->id));
+ $this->assertFalse($consumables->contains('id', $hidden->id));
+ }
+
+ public function test_bulk_update_cannot_write_cross_company_consumable_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->viewConsumables()->make());
+
+ $target = Consumable::factory()->for($companyB)->create();
+ $originalName = $target->name;
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$target->id],
+ 'archived' => '1',
+ ]);
+
+ $fresh = $target->fresh();
+ $this->assertSame($originalName, $fresh->name);
+ $this->assertFalse((bool) $fresh->archived);
+ }
+
+ public function test_bulk_edit_rejects_write_to_cross_company_company_id_under_fmcs(): void
+ {
+ // Even if a non-superuser hand-crafted a POST with a company_id
+ // outside their pivot set, the fmcs_company validator should
+ // block the write.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->viewConsumables()->make());
+ $target = Consumable::factory()->for($companyA)->create();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$target->id],
+ 'company_id' => (string) $companyB->id,
+ ]);
+
+ $this->assertSame($companyA->id, $target->fresh()->company_id);
+ }
+
+ public function test_bulk_edit_rejects_cross_company_location_under_scope_locations_fmcs(): void
+ {
+ // scope_locations_fmcs=1 means a location "belongs" to the
+ // companies in its parent chain. fmcs_location validator must
+ // reject a bulk-edit that pairs location X with company Y when
+ // X doesn't live in Y.
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->viewConsumables()->make());
+ $homeLocation = Location::factory()->for($companyA)->create();
+ $target = Consumable::factory()->for($companyA)->create(['location_id' => $homeLocation->id]);
+ $foreignLocation = Location::factory()->for($companyB)->create();
+
+ $this->settings->enableScopedLocationsWithFullMultipleCompanySupport();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$target->id],
+ 'location_id' => (string) $foreignLocation->id,
+ ]);
+
+ $this->assertSame($homeLocation->id, $target->fresh()->location_id);
+ }
+
+ public function test_single_consume_cannot_touch_cross_company_assignment_under_fmcs(): void
+ {
+ // Mark-consumed is gated on consumables.checkout plus the
+ // $consumable route-model binding, which runs through the
+ // CompanyableScope. A company A actor should not be able to
+ // close out a pivot row attached to a company B consumable.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->viewConsumables()->checkoutConsumables()->make());
+
+ $foreignConsumable = Consumable::factory()->for($companyB)->checkedOutToUser()->create();
+ $foreignAssignment = ConsumableAssignment::where('consumable_id', $foreignConsumable->id)->firstOrFail();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.consume', [
+ 'consumable' => $foreignConsumable->id,
+ 'assignment' => $foreignAssignment->id,
+ ]));
+
+ $this->assertNull($foreignAssignment->fresh()->consumed_at);
+ }
+
+ public function test_bulk_consume_cannot_mark_cross_company_assignment_under_fmcs(): void
+ {
+ // bulkConsumeStore resolves the parent consumable from the
+ // ConsumableAssignment lookup, then authorizes 'checkout' on
+ // it. A company A actor targeting company B assignments
+ // should fail the authorize step.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->viewConsumables()->checkoutConsumables()->make());
+
+ $foreignConsumable = Consumable::factory()->for($companyB)->checkedOutToUser()->create();
+ $foreignAssignment = ConsumableAssignment::where('consumable_id', $foreignConsumable->id)->firstOrFail();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => [$foreignAssignment->id],
+ ])
+ ->assertForbidden();
+
+ $this->assertNull($foreignAssignment->fresh()->consumed_at);
+ }
+
+ public function test_bulk_consume_show_blocks_cross_company_assignment_under_fmcs(): void
+ {
+ // The interstitial view runs through the same auth gate as the
+ // commit step. A company A actor hand-posting a company B
+ // assignment id should either redirect with a warning or be
+ // forbidden outright. The pivot row must not surface in the
+ // confirmation view either way.
+ $this->settings->enableMultipleFullCompanySupport();
+ $this->settings->disableFloaterMode();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->viewConsumables()->checkoutConsumables()->make());
+
+ $foreignConsumable = Consumable::factory()->for($companyB)->checkedOutToUser()->create();
+ $foreignAssignment = ConsumableAssignment::where('consumable_id', $foreignConsumable->id)->firstOrFail();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkconsume.show'), [
+ 'ids' => [$foreignAssignment->id],
+ ])
+ ->assertForbidden();
+
+ $this->assertNull($foreignAssignment->fresh()->consumed_at);
+ }
+
+ public function test_bulk_consume_floater_can_consume_null_company_assignment(): void
+ {
+ // Floater ON: company A actor is allowed to touch null-company
+ // (floater) consumables. Pin so a future refactor that over-
+ // tightens the subquery does not block a legitimate flow.
+ $this->settings->enableFloaterMode();
+
+ $companyA = Company::factory()->create();
+ $actor = $companyA->users()->save(User::factory()->viewConsumables()->checkoutConsumables()->make());
+
+ $floater = Consumable::factory()->create(['company_id' => null]);
+ $floater->users()->attach(User::factory()->create(), [
+ 'consumable_id' => $floater->id,
+ 'created_by' => $actor->id,
+ 'created_at' => now(),
+ ]);
+ $assignment = ConsumableAssignment::where('consumable_id', $floater->id)->firstOrFail();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => [$assignment->id],
+ ])
+ ->assertRedirect()
+ ->assertSessionHas('success');
+
+ $this->assertNotNull($assignment->fresh()->consumed_at);
+ }
+
+ public function test_unarchive_cannot_touch_cross_company_consumable_under_fmcs(): void
+ {
+ // postUnarchive relies on route-model binding (which runs the
+ // Consumable CompanyableScope) + authorize('update', $consumable).
+ // A cross-tenant target is either scoped out at binding time or
+ // forbidden by the policy. Either way the archived bit must
+ // not flip.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->viewConsumables()->make());
+ $foreign = Consumable::factory()->for($companyB)->create(['archived' => true]);
+
+ $this->actingAs($actor)
+ ->post(route('consumables.unarchive', $foreign));
+
+ $this->assertTrue((bool) $foreign->fresh()->archived);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/BulkDeleteConsumablesTest.php b/tests/Feature/Consumables/Ui/BulkDeleteConsumablesTest.php
new file mode 100644
index 0000000000..030216c327
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/BulkDeleteConsumablesTest.php
@@ -0,0 +1,148 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+class BulkDeleteConsumablesTest extends TestCase
+{
+ public function test_requires_permission(): void
+ {
+ $this->actingAs(User::factory()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [1, 2, 3],
+ ])
+ ->assertForbidden();
+ }
+
+ public function test_consumable_with_outstanding_distribution_is_not_bulk_deleted(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$consumable->id],
+ ])
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertDatabaseHas('consumables', ['id' => $consumable->id, 'deleted_at' => null]);
+ }
+
+ public function test_deletable_consumables_are_bulk_deleted(): void
+ {
+ $one = Consumable::factory()->create();
+ $two = Consumable::factory()->create();
+ $three = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$one->id, $two->id, $three->id],
+ ])
+ ->assertRedirect(route('consumables.index'));
+
+ $this->assertSoftDeleted('consumables', ['id' => $one->id]);
+ $this->assertSoftDeleted('consumables', ['id' => $two->id]);
+ $this->assertSoftDeleted('consumables', ['id' => $three->id]);
+ }
+
+ public function test_partial_success_deletes_the_clean_ones_and_reports_the_rest(): void
+ {
+ $deletable = Consumable::factory()->create();
+ $blocked = Consumable::factory()->checkedOutToUser()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$deletable->id, $blocked->id],
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('consumables', ['id' => $deletable->id]);
+ $this->assertDatabaseHas('consumables', ['id' => $blocked->id, 'deleted_at' => null]);
+ }
+
+ public function test_nonexistent_ids_are_reported_and_do_not_break_the_batch(): void
+ {
+ $deletable = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$deletable->id, 999999],
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('consumables', ['id' => $deletable->id]);
+ }
+
+ public function test_respects_fmcs_scoping_for_non_superuser(): void
+ {
+ // Caller scoped to company A asking to bulk delete [A-owned,
+ // B-owned] sees the B-owned row hidden by CompanyableScope
+ // (Consumable::find() returns null), falling into the
+ // does_not_exist bucket. A-owned deletes normally, so the
+ // request is a partial success.
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $consumableA = Consumable::factory()->for($companyA)->create();
+ $consumableB = Consumable::factory()->for($companyB)->create();
+ $userA = User::factory()->forCompany($companyA)->deleteConsumables()->create();
+
+ $this->actingAs($userA)
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$consumableA->id, $consumableB->id],
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertSoftDeleted('consumables', ['id' => $consumableA->id]);
+ $this->assertDatabaseHas('consumables', ['id' => $consumableB->id, 'deleted_at' => null]);
+ }
+
+ public function test_superuser_bypasses_fmcs_and_deletes_consumables_in_any_company(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $consumableA = Consumable::factory()->for($companyA)->create();
+ $consumableB = Consumable::factory()->for($companyB)->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$consumableA->id, $consumableB->id],
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('success')
+ ->assertSessionMissing('multi_error_messages');
+
+ $this->assertSoftDeleted('consumables', ['id' => $consumableA->id]);
+ $this->assertSoftDeleted('consumables', ['id' => $consumableB->id]);
+ }
+
+ public function test_bulk_success_message_pluralizes_by_count(): void
+ {
+ $solo = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$solo->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/consumables/message.delete.bulk_success', 1, ['count' => 1]));
+
+ $a = Consumable::factory()->create();
+ $b = Consumable::factory()->create();
+ $c = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->deleteConsumables()->create())
+ ->post(route('consumables.bulkdelete.store'), [
+ 'ids' => [$a->id, $b->id, $c->id],
+ ])
+ ->assertSessionHas('success', trans_choice('admin/consumables/message.delete.bulk_success', 3, ['count' => 3]));
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/BulkEditConsumablesTest.php b/tests/Feature/Consumables/Ui/BulkEditConsumablesTest.php
new file mode 100644
index 0000000000..c56a3a3402
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/BulkEditConsumablesTest.php
@@ -0,0 +1,187 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\Location;
+use App\Models\Manufacturer;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+class BulkEditConsumablesTest extends TestCase
+{
+ public function test_requires_update_permission_to_save(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->viewConsumables()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$consumable->id],
+ 'archived' => '1',
+ ])
+ ->assertForbidden();
+
+ $this->assertFalse((bool) $consumable->fresh()->archived);
+ }
+
+ public function test_dispatcher_shows_edit_view_for_edit_action(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.show'), [
+ 'ids' => [$consumable->id],
+ 'bulk_actions' => 'edit',
+ ])
+ ->assertOk()
+ ->assertViewIs('consumables.bulk-edit');
+ }
+
+ public function test_dispatcher_shows_delete_view_for_delete_action(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.show'), [
+ 'ids' => [$consumable->id],
+ 'bulk_actions' => 'delete',
+ ])
+ ->assertOk()
+ ->assertViewIs('consumables.bulk-delete');
+ }
+
+ public function test_update_writes_only_provided_fields(): void
+ {
+ $originalCategory = Category::factory()->consumablePaperCategory()->create();
+ $originalLocation = Location::factory()->create();
+ $newLocation = Location::factory()->create();
+
+ $one = Consumable::factory()->create([
+ 'category_id' => $originalCategory->id,
+ 'location_id' => $originalLocation->id,
+ 'min_amt' => 5,
+ ]);
+ $two = Consumable::factory()->create([
+ 'category_id' => $originalCategory->id,
+ 'location_id' => $originalLocation->id,
+ 'min_amt' => 5,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'company_id' => '',
+ 'category_id' => '',
+ 'manufacturer_id' => '',
+ 'location_id' => (string) $newLocation->id,
+ 'default_supplier_id' => '',
+ 'min_amt' => '',
+ 'requestable' => '',
+ 'archived' => '',
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('success');
+
+ $this->assertSame($newLocation->id, $one->fresh()->location_id);
+ $this->assertSame($newLocation->id, $two->fresh()->location_id);
+ $this->assertSame($originalCategory->id, $one->fresh()->category_id);
+ $this->assertSame(5, (int) $one->fresh()->min_amt);
+ }
+
+ public function test_update_can_set_archived_across_selection(): void
+ {
+ $one = Consumable::factory()->create();
+ $two = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('consumables.index'));
+
+ $this->assertTrue((bool) $one->fresh()->archived);
+ $this->assertTrue((bool) $two->fresh()->archived);
+ }
+
+ public function test_update_flashes_error_when_no_rows_updated_due_to_validation(): void
+ {
+ // Regression test: archive-ing consumables that all have
+ // outstanding distributions should surface a model-validation
+ // error, not a "0 consumables were updated" success flash.
+ $one = Consumable::factory()->checkedOutToUser()->create();
+ $two = Consumable::factory()->checkedOutToUser()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('error')
+ ->assertSessionMissing('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertFalse((bool) $one->fresh()->archived);
+ $this->assertFalse((bool) $two->fresh()->archived);
+ }
+
+ public function test_update_flashes_warning_on_partial_validation_failure(): void
+ {
+ // One row saves, one is rejected by the outstanding-check.
+ // Response should be warning + error details, not plain success.
+ $saveable = Consumable::factory()->create();
+ $blocked = Consumable::factory()->checkedOutToUser()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$saveable->id, $blocked->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('warning')
+ ->assertSessionMissing('success')
+ ->assertSessionHas('multi_error_messages');
+
+ $this->assertTrue((bool) $saveable->fresh()->archived);
+ $this->assertFalse((bool) $blocked->fresh()->archived);
+ }
+
+ public function test_update_covers_full_classification_field_set(): void
+ {
+ $company = Company::factory()->create();
+ $category = Category::factory()->consumablePaperCategory()->create();
+ $manufacturer = Manufacturer::factory()->create();
+ $location = Location::factory()->create();
+ $supplier = Supplier::factory()->create();
+
+ $consumable = Consumable::factory()->create(['min_amt' => 1]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.store'), [
+ 'ids' => [$consumable->id],
+ 'company_id' => (string) $company->id,
+ 'category_id' => (string) $category->id,
+ 'manufacturer_id' => (string) $manufacturer->id,
+ 'location_id' => (string) $location->id,
+ 'default_supplier_id' => (string) $supplier->id,
+ 'min_amt' => '42',
+ 'requestable' => '1',
+ 'archived' => '0',
+ ])
+ ->assertRedirect(route('consumables.index'));
+
+ $fresh = $consumable->fresh();
+ $this->assertSame($company->id, $fresh->company_id);
+ $this->assertSame($category->id, $fresh->category_id);
+ $this->assertSame($manufacturer->id, $fresh->manufacturer_id);
+ $this->assertSame($location->id, $fresh->location_id);
+ $this->assertSame($supplier->id, $fresh->default_supplier_id);
+ $this->assertSame(42, (int) $fresh->min_amt);
+ $this->assertTrue((bool) $fresh->requestable);
+ $this->assertFalse((bool) $fresh->archived);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/MarkConsumedTest.php b/tests/Feature/Consumables/Ui/MarkConsumedTest.php
new file mode 100644
index 0000000000..1166358885
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/MarkConsumedTest.php
@@ -0,0 +1,196 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Actionlog;
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use App\Models\User;
+use Tests\TestCase;
+
+class MarkConsumedTest extends TestCase
+{
+ public function test_single_consume_requires_checkout_permission(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->viewConsumables()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumable->id, 'assignment' => $assignment->id]))
+ ->assertForbidden();
+
+ $this->assertNull($assignment->fresh()->consumed_at);
+ }
+
+ public function test_single_consume_sets_consumed_at(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumable->id, 'assignment' => $assignment->id]))
+ ->assertRedirect(route('consumables.show', $consumable))
+ ->assertSessionHas('success');
+
+ $this->assertNotNull($assignment->fresh()->consumed_at);
+ }
+
+ public function test_single_consume_decrements_outstanding_count(): void
+ {
+ // Regression pin for the archive-gate behavior: an outstanding
+ // row that gets marked consumed should drop out of
+ // numOutstanding() so the parent becomes archivable.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->assertSame(1, $consumable->fresh()->numOutstanding());
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumable->id, 'assignment' => $assignment->id]));
+
+ $this->assertSame(0, $consumable->fresh()->numOutstanding());
+ }
+
+ public function test_single_consume_is_one_way(): void
+ {
+ // Posting again against an already-consumed row warns and
+ // leaves consumed_at pinned to its original stamp.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+ $firstStamp = now()->subHour();
+ $assignment->consumed_at = $firstStamp;
+ $assignment->save();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumable->id, 'assignment' => $assignment->id]))
+ ->assertRedirect(route('consumables.show', $consumable))
+ ->assertSessionHas('warning');
+
+ $this->assertEquals($firstStamp->toDateTimeString(), $assignment->fresh()->consumed_at->toDateTimeString());
+ }
+
+ public function test_single_consume_rejects_cross_consumable_assignment(): void
+ {
+ // A pivot id that belongs to a different consumable than the
+ // one in the URL must 404. Prevents a user with checkout on
+ // consumable A from ending the lifecycle on consumable B's
+ // pivot rows just by swapping the URL id.
+ $consumableA = Consumable::factory()->checkedOutToUser()->create();
+ $consumableB = Consumable::factory()->checkedOutToUser()->create();
+ $assignmentB = ConsumableAssignment::where('consumable_id', $consumableB->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumableA->id, 'assignment' => $assignmentB->id]))
+ ->assertNotFound();
+
+ $this->assertNull($assignmentB->fresh()->consumed_at);
+ }
+
+ public function test_single_consume_writes_audit_log(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.consume', ['consumable' => $consumable->id, 'assignment' => $assignment->id]));
+
+ $this->assertDatabaseHas('action_logs', [
+ 'item_type' => Consumable::class,
+ 'item_id' => $consumable->id,
+ 'target_type' => User::class,
+ 'target_id' => $assignment->assigned_to,
+ 'action_type' => 'consumed',
+ ]);
+ }
+
+ public function test_bulk_consume_show_filters_already_consumed_rows(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+ $outstanding = $assignments->first();
+ $alreadyConsumed = $assignments->last();
+ $alreadyConsumed->consumed_at = now();
+ $alreadyConsumed->save();
+
+ $response = $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkconsume.show'), [
+ 'ids' => [$outstanding->id, $alreadyConsumed->id],
+ ])
+ ->assertOk()
+ ->assertViewIs('consumables.bulk-consume');
+
+ $visible = $response->viewData('assignments');
+ $this->assertTrue($visible->contains('id', $outstanding->id));
+ $this->assertFalse($visible->contains('id', $alreadyConsumed->id));
+ }
+
+ public function test_bulk_consume_store_marks_every_selected_row(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => $assignments->pluck('id')->all(),
+ ])
+ ->assertRedirect(route('consumables.show', $consumable))
+ ->assertSessionHas('success');
+
+ $assignments->each(fn ($a) => $this->assertNotNull($a->fresh()->consumed_at));
+ $this->assertSame(0, $consumable->fresh()->numOutstanding());
+ }
+
+ public function test_bulk_consume_store_requires_checkout_permission(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ $assignment = ConsumableAssignment::where('consumable_id', $consumable->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->viewConsumables()->create())
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => [$assignment->id],
+ ])
+ ->assertForbidden();
+
+ $this->assertNull($assignment->fresh()->consumed_at);
+ }
+
+ public function test_bulk_consume_store_rejects_mixed_consumable_submission(): void
+ {
+ // Mixed-parent submissions are a sign the UI was tampered
+ // with. Reject the whole batch rather than silently closing
+ // out a cross-parent set.
+ $consumableA = Consumable::factory()->checkedOutToUser()->create();
+ $consumableB = Consumable::factory()->checkedOutToUser()->create();
+ $assignmentA = ConsumableAssignment::where('consumable_id', $consumableA->id)->firstOrFail();
+ $assignmentB = ConsumableAssignment::where('consumable_id', $consumableB->id)->firstOrFail();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => [$assignmentA->id, $assignmentB->id],
+ ])
+ ->assertRedirect(route('consumables.index'))
+ ->assertSessionHas('error');
+
+ $this->assertNull($assignmentA->fresh()->consumed_at);
+ $this->assertNull($assignmentB->fresh()->consumed_at);
+ }
+
+ public function test_bulk_consume_store_writes_one_log_per_row(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+ $before = Actionlog::where('item_type', Consumable::class)
+ ->where('action_type', 'consumed')
+ ->count();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkconsume.store'), [
+ 'ids' => $assignments->pluck('id')->all(),
+ ]);
+
+ $after = Actionlog::where('item_type', Consumable::class)
+ ->where('action_type', 'consumed')
+ ->count();
+ $this->assertSame($before + $assignments->count(), $after);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/RestoreConsumableTest.php b/tests/Feature/Consumables/Ui/RestoreConsumableTest.php
new file mode 100644
index 0000000000..8d49953409
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/RestoreConsumableTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Company;
+use App\Models\Consumable;
+use App\Models\User;
+use Tests\TestCase;
+
+class RestoreConsumableTest extends TestCase
+{
+ public function test_single_restore_requires_delete_permission(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $consumable->delete();
+
+ $this->actingAs(User::factory()->viewConsumables()->create())
+ ->post(route('consumables.restore', ['consumableId' => $consumable->id]))
+ ->assertForbidden();
+
+ $this->assertNotNull($consumable->fresh()->deleted_at);
+ }
+
+ public function test_single_restore_brings_consumable_back(): void
+ {
+ $consumable = Consumable::factory()->create();
+ $consumable->delete();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.restore', ['consumableId' => $consumable->id]))
+ ->assertRedirect(route('consumables.show', $consumable));
+
+ $this->assertNull($consumable->fresh()->deleted_at);
+ }
+
+ public function test_single_restore_rejects_already_live_consumable(): void
+ {
+ $consumable = Consumable::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.restore', ['consumableId' => $consumable->id]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull($consumable->fresh()->deleted_at);
+ }
+
+ public function test_bulk_restore_brings_selected_consumables_back(): void
+ {
+ $one = Consumable::factory()->create();
+ $two = Consumable::factory()->create();
+ $one->delete();
+ $two->delete();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('consumables.bulkedit.show'), [
+ 'ids' => [$one->id, $two->id],
+ 'bulk_actions' => 'restore',
+ ])
+ ->assertRedirect(route('consumables.index', ['status' => 'Deleted']))
+ ->assertSessionHas('success');
+
+ $this->assertNull($one->fresh()->deleted_at);
+ $this->assertNull($two->fresh()->deleted_at);
+ }
+
+ public function test_bulk_restore_blocks_cross_company_under_fmcs(): void
+ {
+ $this->settings->enableMultipleFullCompanySupport();
+
+ [$companyA, $companyB] = Company::factory()->count(2)->create();
+ $actor = $companyA->users()->save(User::factory()->editConsumables()->deleteConsumables()->viewConsumables()->make());
+ $foreign = Consumable::factory()->for($companyB)->create();
+ $foreign->delete();
+
+ $this->actingAs($actor)
+ ->post(route('consumables.bulkedit.show'), [
+ 'ids' => [$foreign->id],
+ 'bulk_actions' => 'restore',
+ ])
+ ->assertRedirect();
+
+ $this->assertNotNull($foreign->fresh()->deleted_at);
+ }
+}
diff --git a/tests/Feature/Consumables/Ui/ShowConsumableLifecycleTest.php b/tests/Feature/Consumables/Ui/ShowConsumableLifecycleTest.php
new file mode 100644
index 0000000000..7ec24eb25f
--- /dev/null
+++ b/tests/Feature/Consumables/Ui/ShowConsumableLifecycleTest.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Tests\Feature\Consumables\Ui;
+
+use App\Models\Consumable;
+use App\Models\ConsumableAssignment;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * Pins the view-level reads of the outstanding count against the
+ * data model so a stray numCheckedOut() call (which counts
+ * historical consumed distributions too) can't sneak back in and
+ * desync the tab badge or the archive-blocked checkbox state from
+ * what the Assigned table actually shows.
+ */
+class ShowConsumableLifecycleTest extends TestCase
+{
+ public function test_assigned_tab_badge_reflects_outstanding_not_total_distributions(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->checkedOutToUser()->create();
+ $assignments = ConsumableAssignment::where('consumable_id', $consumable->id)->get();
+ $this->assertCount(2, $assignments);
+
+ $toConsume = $assignments->first();
+ $toConsume->consumed_at = now();
+ $toConsume->save();
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('consumables.show', $consumable))
+ ->assertOk()
+ ->getContent();
+
+ // Badge should read 1 (the one still outstanding), not 2
+ // (total ever distributed). Match the href + the digit so
+ // we're reading the actual Assigned tab's count span.
+ $this->assertMatchesRegularExpression(
+ '/href="[^"]*#assigned"[^>]*>.*?<span[^>]*>1<\/span>/s',
+ $html,
+ 'Assigned tab badge must reflect numOutstanding, not numCheckedOut.',
+ );
+ $this->assertDoesNotMatchRegularExpression(
+ '/href="[^"]*#assigned"[^>]*>.*?<span[^>]*>2<\/span>/s',
+ $html,
+ 'Badge must not include consumed rows in its count.',
+ );
+ }
+
+ public function test_edit_view_enables_archive_checkbox_once_all_distributions_consumed(): void
+ {
+ // Regression pin: the archive checkbox's :disabled used to
+ // read $item->users()->count() (every pivot row ever), which
+ // stayed > 0 even after admin bulk-consumed everything, so
+ // the admin couldn't flip the box. It must track
+ // numOutstanding() instead.
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+ ConsumableAssignment::query()
+ ->where('consumable_id', $consumable->id)
+ ->update(['consumed_at' => now()]);
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('consumables.edit', $consumable))
+ ->assertOk()
+ ->getContent();
+
+ $this->assertMatchesRegularExpression(
+ '/<input[^>]*name="archived"[^>]*(?<!disabled=")(?<!disabled)>/',
+ $html,
+ 'Archived checkbox must be enabled when nothing is outstanding.',
+ );
+ }
+
+ public function test_edit_view_disables_archive_checkbox_while_distributions_outstanding(): void
+ {
+ $consumable = Consumable::factory()->checkedOutToUser()->create();
+
+ $html = $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('consumables.edit', $consumable))
+ ->assertOk()
+ ->getContent();
+
+ $this->assertMatchesRegularExpression(
+ '/<input[^>]*name="archived"[^>]*disabled/',
+ $html,
+ 'Archived checkbox must be disabled while an outstanding distribution exists.',
+ );
+ }
+
+ public function test_show_page_loads_for_soft_deleted_consumable(): void
+ {
+ // Regression pin: the resource route needs withTrashed(['show'])
+ // AND the show() method's internal re-fetch has to withTrashed()
+ // too. Without both, the Deleted-index "View" link bounces back
+ // with "That Consumable was not found" for an admin who can see
+ // the deleted row in the first place.
+ $consumable = Consumable::factory()->create();
+ $consumable->delete();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->get(route('consumables.show', $consumable->id))
+ ->assertOk();
+ }
+}