Skip to content

A free open source IT asset/license management system

Added tests

snipe2350b782df8f
diff --git a/tests/Feature/Accessories/AccessoryArchiveValidationTest.php b/tests/Feature/Accessories/AccessoryArchiveValidationTest.php
new file mode 100644
index 0000000000..6a840b1af8
--- /dev/null
+++ b/tests/Feature/Accessories/AccessoryArchiveValidationTest.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Tests\Feature\Accessories;
+
+use App\Enums\CheckoutRequestState;
+use App\Models\Accessory;
+use App\Models\CheckoutRequest;
+use App\Models\User;
+use Tests\TestCase;
+
+class AccessoryArchiveValidationTest extends TestCase
+{
+ public function test_accessory_can_save_as_requestable_without_archived(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $accessory->requestable = 1;
+ $accessory->archived = 0;
+
+ $this->assertTrue($accessory->save());
+ }
+
+ public function test_accessory_can_save_as_archived_without_requestable(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $accessory->archived = 1;
+ $accessory->requestable = 0;
+
+ $this->assertTrue($accessory->save());
+ }
+
+ public function test_accessory_cannot_save_as_both_requestable_and_archived(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $accessory->requestable = 1;
+ $accessory->archived = 1;
+
+ $this->assertFalse($accessory->save());
+ $this->assertArrayHasKey('archived', $accessory->getErrors()->toArray());
+ $this->assertArrayHasKey('requestable', $accessory->getErrors()->toArray());
+ }
+
+ public function test_accessory_with_active_checkouts_cannot_be_archived(): void
+ {
+ $accessory = Accessory::factory()->checkedOutToUser()->create();
+ $accessory->refresh();
+
+ $accessory->archived = 1;
+
+ $this->assertFalse($accessory->save());
+ $this->assertSame(
+ trans('admin/accessories/message.archived_blocked_by_checkouts'),
+ $accessory->getErrors()->get('archived')[0],
+ );
+ }
+
+ public function test_accessory_without_active_checkouts_can_be_archived(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $accessory->archived = 1;
+
+ $this->assertTrue($accessory->save());
+ }
+
+ public function test_archiving_accessory_auto_cancels_pending_requests(): void
+ {
+ $accessory = Accessory::factory()->create();
+ $pending = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ ]);
+
+ $accessory->archived = 1;
+ $accessory->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
+ {
+ $accessory = Accessory::factory()->create();
+ $previousCancelAt = now()->subDay();
+ $alreadyCanceled = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ 'canceled_at' => $previousCancelAt,
+ ]);
+
+ $accessory->archived = 1;
+ $accessory->save();
+
+ $alreadyCanceled->refresh();
+ $this->assertEquals($previousCancelAt->toDateTimeString(), $alreadyCanceled->canceled_at->toDateTimeString());
+ }
+
+ public function test_un_archiving_does_not_touch_requests(): void
+ {
+ $accessory = Accessory::factory()->archived()->create();
+ $request = CheckoutRequest::factory()->create([
+ 'user_id' => User::factory()->create()->id,
+ 'requestable_id' => $accessory->id,
+ 'requestable_type' => Accessory::class,
+ ]);
+
+ $accessory->archived = 0;
+ $accessory->save();
+
+ $request->refresh();
+ $this->assertNull($request->canceled_at);
+ }
+}
diff --git a/tests/Feature/Accessories/Ui/BulkEditAccessoriesTest.php b/tests/Feature/Accessories/Ui/BulkEditAccessoriesTest.php
new file mode 100644
index 0000000000..9f2776594d
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/BulkEditAccessoriesTest.php
@@ -0,0 +1,192 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\AccessoryCheckout;
+use App\Models\Category;
+use App\Models\Company;
+use App\Models\Location;
+use App\Models\Manufacturer;
+use App\Models\Supplier;
+use App\Models\User;
+use Tests\TestCase;
+
+class BulkEditAccessoriesTest extends TestCase
+{
+ public function test_requires_update_permission_to_save(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->viewAccessories()->create())
+ ->post(route('accessories.bulkedit.store'), [
+ 'ids' => [$accessory->id],
+ 'archived' => '1',
+ ])
+ ->assertForbidden();
+
+ $this->assertFalse((bool) $accessory->fresh()->archived);
+ }
+
+ public function test_dispatcher_shows_edit_view_for_edit_action(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.show'), [
+ 'ids' => [$accessory->id],
+ 'bulk_actions' => 'edit',
+ ])
+ ->assertOk()
+ ->assertViewIs('accessories.bulk-edit');
+ }
+
+ public function test_dispatcher_shows_delete_view_for_delete_action(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.show'), [
+ 'ids' => [$accessory->id],
+ 'bulk_actions' => 'delete',
+ ])
+ ->assertOk()
+ ->assertViewIs('accessories.bulk-delete');
+ }
+
+ public function test_update_writes_only_provided_fields(): void
+ {
+ $originalCategory = Category::factory()->create();
+ $originalLocation = Location::factory()->create();
+ $newLocation = Location::factory()->create();
+
+ $one = Accessory::factory()->create([
+ 'category_id' => $originalCategory->id,
+ 'location_id' => $originalLocation->id,
+ 'min_amt' => 5,
+ ]);
+ $two = Accessory::factory()->create([
+ 'category_id' => $originalCategory->id,
+ 'location_id' => $originalLocation->id,
+ 'min_amt' => 5,
+ ]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.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('accessories.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 = Accessory::factory()->create();
+ $two = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('accessories.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 accessories that all have active
+ // checkouts should surface a model-validation error, not a
+ // "0 accessories were updated" success flash.
+ $one = Accessory::factory()->create();
+ $two = Accessory::factory()->create();
+ AccessoryCheckout::factory()->for($one, 'accessory')->create();
+ AccessoryCheckout::factory()->for($two, 'accessory')->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('accessories.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
+ {
+ // Regression test: one accessory saves, one is rejected by
+ // validation. Response should be warning + error details, not
+ // plain success.
+ $saveable = Accessory::factory()->create();
+ $blocked = Accessory::factory()->create();
+ AccessoryCheckout::factory()->for($blocked, 'accessory')->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.store'), [
+ 'ids' => [$saveable->id, $blocked->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('accessories.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()->create();
+ $manufacturer = Manufacturer::factory()->create();
+ $location = Location::factory()->create();
+ $supplier = Supplier::factory()->create();
+
+ $accessory = Accessory::factory()->create(['min_amt' => 1]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.store'), [
+ 'ids' => [$accessory->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('accessories.index'));
+
+ $fresh = $accessory->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/Accessories/Ui/RestoreAccessoryTest.php b/tests/Feature/Accessories/Ui/RestoreAccessoryTest.php
new file mode 100644
index 0000000000..daa16e312a
--- /dev/null
+++ b/tests/Feature/Accessories/Ui/RestoreAccessoryTest.php
@@ -0,0 +1,85 @@
+<?php
+
+namespace Tests\Feature\Accessories\Ui;
+
+use App\Models\Accessory;
+use App\Models\Company;
+use App\Models\User;
+use Tests\TestCase;
+
+class RestoreAccessoryTest extends TestCase
+{
+ public function test_single_restore_requires_delete_permission(): void
+ {
+ $accessory = Accessory::factory()->create();
+ $accessory->delete();
+
+ $this->actingAs(User::factory()->viewAccessories()->create())
+ ->post(route('accessories.restore', ['accessoryId' => $accessory->id]))
+ ->assertForbidden();
+
+ $this->assertNotNull($accessory->fresh()->deleted_at);
+ }
+
+ public function test_single_restore_brings_accessory_back(): void
+ {
+ $accessory = Accessory::factory()->create();
+ $accessory->delete();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.restore', ['accessoryId' => $accessory->id]))
+ ->assertRedirect(route('accessories.show', $accessory));
+
+ $this->assertNull($accessory->fresh()->deleted_at);
+ }
+
+ public function test_single_restore_rejects_already_live_accessory(): void
+ {
+ $accessory = Accessory::factory()->create();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.restore', ['accessoryId' => $accessory->id]))
+ ->assertRedirect()
+ ->assertSessionHas('error');
+
+ $this->assertNull($accessory->fresh()->deleted_at);
+ }
+
+ public function test_bulk_restore_brings_selected_accessories_back(): void
+ {
+ $one = Accessory::factory()->create();
+ $two = Accessory::factory()->create();
+ $one->delete();
+ $two->delete();
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('accessories.bulkedit.show'), [
+ 'ids' => [$one->id, $two->id],
+ 'bulk_actions' => 'restore',
+ ])
+ ->assertRedirect(route('accessories.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()->editAccessories()->deleteAccessories()->viewAccessories()->make());
+ $foreign = Accessory::factory()->for($companyB)->create();
+ $foreign->delete();
+
+ $this->actingAs($actor)
+ ->post(route('accessories.bulkedit.show'), [
+ 'ids' => [$foreign->id],
+ 'bulk_actions' => 'restore',
+ ])
+ ->assertRedirect();
+
+ $this->assertNotNull($foreign->fresh()->deleted_at);
+ }
+}
diff --git a/tests/Feature/Components/Ui/BulkEditComponentsTest.php b/tests/Feature/Components/Ui/BulkEditComponentsTest.php
index ffbcaa6566..2c096bbc87 100644
--- a/tests/Feature/Components/Ui/BulkEditComponentsTest.php
+++ b/tests/Feature/Components/Ui/BulkEditComponentsTest.php
@@ -2,6 +2,7 @@
namespace Tests\Feature\Components\Ui;
+use App\Models\Asset;
use App\Models\Category;
use App\Models\Company;
use App\Models\Component;
@@ -118,6 +119,56 @@ class BulkEditComponentsTest extends TestCase
$this->assertTrue((bool) $two->fresh()->archived);
}
+ public function test_update_flashes_error_when_no_rows_updated_due_to_validation(): void
+ {
+ // Regression test: archive-ing components that all have active
+ // checkouts should surface a model-validation error, not a
+ // "0 components were updated" success flash.
+ $one = Component::factory()->create(['qty' => 5]);
+ $two = Component::factory()->create(['qty' => 5]);
+ $assetOne = Asset::factory()->create();
+ $assetTwo = Asset::factory()->create();
+ $one->assets()->attach($assetOne->id, ['assigned_qty' => 1, 'created_at' => now(), 'created_by' => 1]);
+ $two->assets()->attach($assetTwo->id, ['assigned_qty' => 1, 'created_at' => now(), 'created_by' => 1]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('components.bulkedit.store'), [
+ 'ids' => [$one->id, $two->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('components.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
+ {
+ // Regression test: one component saves, one is rejected by
+ // validation. Response should be warning + error details, not
+ // plain success.
+ $saveable = Component::factory()->create(['qty' => 5]);
+ $blocked = Component::factory()->create(['qty' => 5]);
+ $asset = Asset::factory()->create();
+ $blocked->assets()->attach($asset->id, ['assigned_qty' => 1, 'created_at' => now(), 'created_by' => 1]);
+
+ $this->actingAs(User::factory()->superuser()->create())
+ ->post(route('components.bulkedit.store'), [
+ 'ids' => [$saveable->id, $blocked->id],
+ 'archived' => '1',
+ ])
+ ->assertRedirect(route('components.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_rejects_empty_selection(): void
{
$this->actingAs(User::factory()->superuser()->create())
diff --git a/tests/Unit/Accessories/AccessoryQueryBuilderArchivedScopeTest.php b/tests/Unit/Accessories/AccessoryQueryBuilderArchivedScopeTest.php
new file mode 100644
index 0000000000..76f950de25
--- /dev/null
+++ b/tests/Unit/Accessories/AccessoryQueryBuilderArchivedScopeTest.php
@@ -0,0 +1,68 @@
+<?php
+
+namespace Tests\Unit\Accessories;
+
+use App\Models\Accessory;
+use Tests\TestCase;
+
+class AccessoryQueryBuilderArchivedScopeTest extends TestCase
+{
+ public function test_archived_scope_returns_only_archived(): void
+ {
+ $live = Accessory::factory()->create();
+ $archived = Accessory::factory()->archived()->create();
+
+ $ids = Accessory::query()->archived()->pluck('id')->all();
+
+ $this->assertContains($archived->id, $ids);
+ $this->assertNotContains($live->id, $ids);
+ }
+
+ public function test_not_archived_scope_excludes_archived(): void
+ {
+ $live = Accessory::factory()->create();
+ $archived = Accessory::factory()->archived()->create();
+
+ $ids = Accessory::query()->notArchived()->pluck('id')->all();
+
+ $this->assertContains($live->id, $ids);
+ $this->assertNotContains($archived->id, $ids);
+ }
+
+ public function test_accessories_for_show_hides_archived_when_setting_off(): void
+ {
+ $this->settings->set(['show_archived_in_list' => 0]);
+
+ $live = Accessory::factory()->create();
+ $archived = Accessory::factory()->archived()->create();
+
+ $ids = Accessory::query()->accessoriesForShow()->pluck('id')->all();
+
+ $this->assertContains($live->id, $ids);
+ $this->assertNotContains($archived->id, $ids);
+ }
+
+ public function test_accessories_for_show_includes_archived_when_setting_on(): void
+ {
+ $this->settings->set(['show_archived_in_list' => 1]);
+
+ $live = Accessory::factory()->create();
+ $archived = Accessory::factory()->archived()->create();
+
+ $ids = Accessory::query()->accessoriesForShow()->pluck('id')->all();
+
+ $this->assertContains($live->id, $ids);
+ $this->assertContains($archived->id, $ids);
+ }
+
+ public function test_requestable_scope_excludes_archived(): void
+ {
+ $requestable = Accessory::factory()->create(['requestable' => 1]);
+ $archivedButRequestable = Accessory::factory()->archived()->create(['requestable' => 1]);
+
+ $ids = Accessory::query()->requestable()->pluck('id')->all();
+
+ $this->assertContains($requestable->id, $ids);
+ $this->assertNotContains($archivedButRequestable->id, $ids);
+ }
+}