Skip to content

A free open source IT asset/license management system

Added tests

snipe2f49aa3eb9af
diff --git a/tests/Feature/Components/Api/ComponentIndexArchivedTest.php b/tests/Feature/Components/Api/ComponentIndexArchivedTest.php
new file mode 100644
index 0000000000..b6786bef17
--- /dev/null
+++ b/tests/Feature/Components/Api/ComponentIndexArchivedTest.php
@@ -0,0 +1,73 @@
+<?php
+
+namespace Tests\Feature\Components\Api;
+
+use App\Models\Component;
+use App\Models\User;
+use Tests\TestCase;
+
+class ComponentIndexArchivedTest extends TestCase
+{
+ public function test_default_list_hides_archived_components_when_setting_off(): void
+ {
+ $this->settings->set(['show_archived_in_list' => 0]);
+
+ $live = Component::factory()->create();
+ $archived = Component::factory()->archived()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index'))
+ ->assertOk()
+ ->assertResponseContainsInRows($live)
+ ->assertResponseDoesNotContainInRows($archived);
+ }
+
+ public function test_default_list_shows_archived_when_setting_on(): void
+ {
+ $this->settings->set(['show_archived_in_list' => 1]);
+
+ $live = Component::factory()->create();
+ $archived = Component::factory()->archived()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index'))
+ ->assertOk()
+ ->assertResponseContainsInRows($live)
+ ->assertResponseContainsInRows($archived);
+ }
+
+ public function test_status_archived_returns_only_archived_components(): void
+ {
+ $live = Component::factory()->create();
+ $archived = Component::factory()->archived()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index', ['status' => 'Archived']))
+ ->assertOk()
+ ->assertResponseContainsInRows($archived)
+ ->assertResponseDoesNotContainInRows($live);
+ }
+
+ public function test_status_deleted_returns_only_soft_deleted_components(): void
+ {
+ $live = Component::factory()->create();
+ $deleted = Component::factory()->create();
+ $deleted->delete();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index', ['status' => 'Deleted']))
+ ->assertOk()
+ ->assertResponseContainsInRows($deleted)
+ ->assertResponseDoesNotContainInRows($live);
+ }
+
+ public function test_transformer_exposes_archived_flag(): void
+ {
+ $archived = Component::factory()->archived()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index', ['status' => 'Archived']))
+ ->assertOk()
+ ->assertJsonFragment(['id' => $archived->id, 'archived' => true]);
+ }
+}
diff --git a/tests/Feature/Components/Api/ComponentIndexBulkSelectableTest.php b/tests/Feature/Components/Api/ComponentIndexBulkSelectableTest.php
new file mode 100644
index 0000000000..959c128c86
--- /dev/null
+++ b/tests/Feature/Components/Api/ComponentIndexBulkSelectableTest.php
@@ -0,0 +1,53 @@
+<?php
+
+namespace Tests\Feature\Components\Api;
+
+use App\Models\Asset;
+use App\Models\Component;
+use App\Models\User;
+use Tests\TestCase;
+
+/**
+ * The index payload's available_actions.bulk_selectable drives the
+ * row-checkbox gating on the components list (see
+ * applyActionDependentRowStyling + checkboxEnabledFormatter in
+ * partials/bootstrap-table). Both keys must flow through for every
+ * row so the dropdown-driven disable can react correctly, otherwise
+ * the "pick delete, inapplicable rows auto-uncheck" flow silently
+ * breaks.
+ */
+class ComponentIndexBulkSelectableTest extends TestCase
+{
+ public function test_deletable_component_reports_edit_and_delete_selectable(): void
+ {
+ $component = Component::factory()->create();
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index'))
+ ->assertOk()
+ ->assertJsonFragment([
+ 'id' => $component->id,
+ ])
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.edit', true)
+ ->assertJsonPath('rows.0.available_actions.bulk_selectable.delete', true);
+ }
+
+ public function test_component_with_active_checkouts_reports_delete_unselectable(): void
+ {
+ $component = Component::factory()->create(['qty' => 10]);
+ $component->assets()->attach(Asset::factory()->create()->id, [
+ 'assigned_qty' => 2,
+ 'created_at' => now(),
+ 'created_by' => 1,
+ ]);
+
+ $response = $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.components.index'))
+ ->assertOk();
+
+ $row = collect($response->json('rows'))->firstWhere('id', $component->id);
+ $this->assertNotNull($row);
+ $this->assertTrue($row['available_actions']['bulk_selectable']['edit']);
+ $this->assertFalse($row['available_actions']['bulk_selectable']['delete']);
+ }
+}