Skip to content

A free open source IT asset/license management system

Added/updated tests

snipef9567af55af3
diff --git a/tests/Feature/Maintenances/Api/CompleteMaintenanceTest.php b/tests/Feature/Maintenances/Api/CompleteMaintenanceTest.php
index 82c66d608d..5dc84de4ce 100644
--- a/tests/Feature/Maintenances/Api/CompleteMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Api/CompleteMaintenanceTest.php
@@ -5,6 +5,7 @@ namespace Tests\Feature\Maintenances\Api;
use App\Models\Actionlog;
use App\Models\Maintenance;
use App\Models\User;
+use Carbon\Carbon;
use Tests\TestCase;
class CompleteMaintenanceTest extends TestCase
@@ -62,4 +63,42 @@ class CompleteMaintenanceTest extends TestCase
$this->assertHasTheseActionLogs($maintenance, ['create']);
}
+
+ public function test_completion_note_is_saved_in_actionlog()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.maintenances.complete', $maintenance), ['note' => 'Fixed the thing'])
+ ->assertOk();
+
+ $log = Actionlog::where('item_type', Maintenance::class)
+ ->where('item_id', $maintenance->id)
+ ->where('action_type', 'completed')
+ ->first();
+
+ $this->assertNotNull($log);
+ $this->assertEquals('Fixed the thing', $log->note);
+ }
+
+ public function test_duration_is_calculated_from_created_at()
+ {
+ $actor = User::factory()->superuser()->create();
+
+ Carbon::setTestNow(Carbon::create(2026, 1, 1));
+ $maintenance = Maintenance::factory()->create(['start_date' => '2025-06-01']);
+ Carbon::setTestNow(Carbon::create(2026, 1, 11));
+
+ try {
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.maintenances.complete', $maintenance))
+ ->assertOk();
+
+ $maintenance->refresh();
+ $this->assertEquals(10, $maintenance->asset_maintenance_time);
+ } finally {
+ Carbon::setTestNow();
+ }
+ }
}
diff --git a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php b/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
index da65d8ed43..8e625e7a86 100644
--- a/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Api/EditMaintenanceTest.php
@@ -58,7 +58,6 @@ class EditMaintenanceTest extends TestCase
'is_warranty' => 1,
'start_date' => '2021-01-01',
'completion_date' => '2021-01-10',
- 'asset_maintenance_time' => '9',
'notes' => 'A note',
'url' => 'https://snipeitapp.com',
'image' => $maintenance->image,
diff --git a/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php
new file mode 100644
index 0000000000..6313713070
--- /dev/null
+++ b/tests/Feature/Maintenances/Api/IndexMaintenanceTest.php
@@ -0,0 +1,129 @@
+<?php
+
+namespace Tests\Feature\Maintenances\Api;
+
+use App\Models\Maintenance;
+use App\Models\MaintenanceType;
+use App\Models\User;
+use Tests\TestCase;
+
+class IndexMaintenanceTest extends TestCase
+{
+ public function test_requires_permission()
+ {
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.maintenances.index'))
+ ->assertForbidden();
+ }
+
+ public function test_completed_filter_returns_only_completed_maintenances()
+ {
+ $actor = User::factory()->superuser()->create();
+ $active = Maintenance::factory()->create(['completed_at' => null]);
+ $done = Maintenance::factory()->create(['completed_at' => now()]);
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['completed' => 'true']))
+ ->assertOk();
+
+ $ids = collect($response->json('rows'))->pluck('id');
+ $this->assertContains($done->id, $ids);
+ $this->assertNotContains($active->id, $ids);
+ }
+
+ public function test_completed_false_filter_returns_only_active_maintenances()
+ {
+ $actor = User::factory()->superuser()->create();
+ $active = Maintenance::factory()->create(['completed_at' => null]);
+ $done = Maintenance::factory()->create(['completed_at' => now()]);
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['completed' => 'false']))
+ ->assertOk();
+
+ $ids = collect($response->json('rows'))->pluck('id');
+ $this->assertContains($active->id, $ids);
+ $this->assertNotContains($done->id, $ids);
+ }
+
+ public function test_upcoming_status_overdue_returns_only_overdue()
+ {
+ $actor = User::factory()->superuser()->create();
+
+ $overdue = Maintenance::factory()->create([
+ 'completion_date' => now()->subDay()->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $fine = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(30)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['upcoming_status' => 'overdue']))
+ ->assertOk();
+
+ $ids = collect($response->json('rows'))->pluck('id');
+ $this->assertContains($overdue->id, $ids);
+ $this->assertNotContains($fine->id, $ids);
+ }
+
+ public function test_upcoming_status_due_respects_warning_window()
+ {
+ $this->settings->setAuditWarningDays(7);
+ $actor = User::factory()->superuser()->create();
+
+ $due = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(3)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $notDueYet = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(30)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['upcoming_status' => 'due']))
+ ->assertOk();
+
+ $ids = collect($response->json('rows'))->pluck('id');
+ $this->assertContains($due->id, $ids);
+ $this->assertNotContains($notDueYet->id, $ids);
+ }
+
+ public function test_maintenance_type_is_returned_as_flat_string()
+ {
+ $actor = User::factory()->superuser()->create();
+ $type = MaintenanceType::factory()->create(['name' => 'Annual Checkup']);
+ $maintenance = Maintenance::factory()->create(['maintenance_type_id' => $type->id]);
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.show', $maintenance))
+ ->assertOk();
+
+ $this->assertEquals('Annual Checkup', $response->json('maintenance_type'));
+ }
+
+ public function test_sort_by_maintenance_type_does_not_error()
+ {
+ $actor = User::factory()->superuser()->create();
+ Maintenance::factory()->count(3)->create();
+
+ $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['sort' => 'maintenance_type', 'order' => 'asc']))
+ ->assertOk();
+ }
+
+ public function test_sort_by_completed_at_does_not_error()
+ {
+ $actor = User::factory()->superuser()->create();
+ Maintenance::factory()->count(2)->create(['completed_at' => null]);
+ Maintenance::factory()->create(['completed_at' => now()]);
+
+ $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.index', ['sort' => 'completed_at', 'order' => 'desc']))
+ ->assertOk();
+ }
+}
diff --git a/tests/Feature/Maintenances/Api/MaintenanceNotesTest.php b/tests/Feature/Maintenances/Api/MaintenanceNotesTest.php
new file mode 100644
index 0000000000..825a75159c
--- /dev/null
+++ b/tests/Feature/Maintenances/Api/MaintenanceNotesTest.php
@@ -0,0 +1,111 @@
+<?php
+
+namespace Tests\Feature\Maintenances\Api;
+
+use App\Models\Actionlog;
+use App\Models\Maintenance;
+use App\Models\User;
+use Tests\TestCase;
+
+class MaintenanceNotesTest extends TestCase
+{
+ public function test_index_requires_permission()
+ {
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi(User::factory()->create())
+ ->getJson(route('api.maintenances.notes.index', $maintenance))
+ ->assertForbidden();
+ }
+
+ public function test_index_returns_notes_for_maintenance()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $log = new Actionlog;
+ $log->item_type = Maintenance::class;
+ $log->item_id = $maintenance->id;
+ $log->note = 'Test note content';
+ $log->created_by = $actor->id;
+ $log->logaction('note added');
+
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.notes.index', $maintenance))
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $notes = $response->json('payload.notes');
+ $this->assertCount(1, $notes);
+ $this->assertEquals('Test note content', $notes[0]['note']);
+ }
+
+ public function test_index_does_not_return_other_action_types()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.notes.index', $maintenance))
+ ->assertOk();
+
+ // The create actionlog from factory should not appear (it's 'create' not 'note added')
+ $response = $this->actingAsForApi($actor)
+ ->getJson(route('api.maintenances.notes.index', $maintenance))
+ ->assertOk();
+
+ $notes = $response->json('payload.notes');
+ $this->assertEmpty($notes);
+ }
+
+ public function test_store_requires_permission()
+ {
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi(User::factory()->create())
+ ->postJson(route('api.maintenances.notes.store', $maintenance), ['note' => 'Test'])
+ ->assertForbidden();
+ }
+
+ public function test_store_validates_note_is_required()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.maintenances.notes.store', $maintenance), ['note' => ''])
+ ->assertStatus(422);
+ }
+
+ public function test_store_creates_note_actionlog()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAsForApi($actor)
+ ->postJson(route('api.maintenances.notes.store', $maintenance), ['note' => 'Important note'])
+ ->assertOk()
+ ->assertStatusMessageIs('success');
+
+ $this->assertDatabaseHas('action_logs', [
+ 'item_type' => Maintenance::class,
+ 'item_id' => $maintenance->id,
+ 'action_type' => 'note added',
+ 'note' => 'Important note',
+ 'created_by' => $actor->id,
+ ]);
+ }
+
+ public function test_store_returns_note_in_response()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $response = $this->actingAsForApi($actor)
+ ->postJson(route('api.maintenances.notes.store', $maintenance), ['note' => 'My note'])
+ ->assertOk();
+
+ $this->assertEquals('My note', $response->json('payload.note'));
+ $this->assertEquals($maintenance->id, $response->json('payload.item_id'));
+ }
+}
diff --git a/tests/Feature/Maintenances/Ui/CompleteMaintenanceTest.php b/tests/Feature/Maintenances/Ui/CompleteMaintenanceTest.php
index b2a79baf7d..92d22b0384 100644
--- a/tests/Feature/Maintenances/Ui/CompleteMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Ui/CompleteMaintenanceTest.php
@@ -5,6 +5,7 @@ namespace Tests\Feature\Maintenances\Ui;
use App\Models\Actionlog;
use App\Models\Maintenance;
use App\Models\User;
+use Carbon\Carbon;
use Tests\TestCase;
class CompleteMaintenanceTest extends TestCase
@@ -68,4 +69,41 @@ class CompleteMaintenanceTest extends TestCase
$this->assertHasTheseActionLogs($maintenance, ['create']);
}
+
+ public function test_completion_note_is_saved_in_actionlog()
+ {
+ $actor = User::factory()->superuser()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAs($actor)
+ ->post(route('maintenances.complete', $maintenance), ['note' => 'Widget replaced']);
+
+ $log = Actionlog::where('item_type', Maintenance::class)
+ ->where('item_id', $maintenance->id)
+ ->where('action_type', 'completed')
+ ->first();
+
+ $this->assertNotNull($log);
+ $this->assertEquals('Widget replaced', $log->note);
+ }
+
+ public function test_duration_is_calculated_from_created_at()
+ {
+ $actor = User::factory()->superuser()->create();
+
+ Carbon::setTestNow(Carbon::create(2026, 1, 1));
+ $maintenance = Maintenance::factory()->create(['start_date' => '2025-06-01']);
+ Carbon::setTestNow(Carbon::create(2026, 1, 11));
+
+ try {
+ $this->actingAs($actor)
+ ->post(route('maintenances.complete', $maintenance))
+ ->assertRedirect();
+
+ $maintenance->refresh();
+ $this->assertEquals(10, $maintenance->asset_maintenance_time);
+ } finally {
+ Carbon::setTestNow();
+ }
+ }
}
diff --git a/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php b/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
index 5430a6b066..18f26c1902 100644
--- a/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Ui/CreateMaintenanceTest.php
@@ -80,7 +80,6 @@ class CreateMaintenanceTest extends TestCase
'is_warranty' => 1,
'start_date' => '2021-01-01',
'completion_date' => '2021-01-10',
- 'asset_maintenance_time' => '9',
'notes' => 'A note',
'url' => 'https://snipeitapp.com',
'cost' => '100.00',
diff --git a/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php b/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
index 39c021adcc..fc954d5221 100644
--- a/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
+++ b/tests/Feature/Maintenances/Ui/EditMaintenanceTest.php
@@ -76,7 +76,6 @@ class EditMaintenanceTest extends TestCase
'is_warranty' => 1,
'start_date' => '2021-01-01',
'completion_date' => '2021-01-10',
- 'asset_maintenance_time' => '9',
'notes' => 'A note',
'url' => 'https://snipeitapp.com',
'cost' => '100.99',
diff --git a/tests/Feature/Notes/CreateNotesTest.php b/tests/Feature/Notes/CreateNotesTest.php
index c5f153714f..4b44214e46 100644
--- a/tests/Feature/Notes/CreateNotesTest.php
+++ b/tests/Feature/Notes/CreateNotesTest.php
@@ -3,6 +3,7 @@
namespace Tests\Feature\Notes;
use App\Models\Asset;
+use App\Models\Maintenance;
use App\Models\User;
use Tests\TestCase;
@@ -67,4 +68,51 @@ class CreateNotesTest extends TestCase
'user_agent' => 'Custom User Agent For Test',
]);
}
+
+ public function test_can_create_note_for_maintenance()
+ {
+ $actor = User::factory()->editAssets()->create();
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAs($actor)
+ ->post(route('notes.store'), [
+ 'id' => $maintenance->id,
+ 'type' => 'maintenance',
+ 'note' => 'maintenance note text',
+ ])
+ ->assertRedirect(route('maintenances.show', $maintenance->id).'#notes')
+ ->assertSessionHas('success', trans('general.note_added'));
+
+ $this->assertDatabaseHas('action_logs', [
+ 'created_by' => $actor->id,
+ 'action_type' => 'note added',
+ 'note' => 'maintenance note text',
+ 'item_type' => Maintenance::class,
+ 'item_id' => $maintenance->id,
+ ]);
+ }
+
+ public function test_maintenance_note_requires_asset_update_permission()
+ {
+ $maintenance = Maintenance::factory()->create();
+
+ $this->actingAs(User::factory()->create())
+ ->post(route('notes.store'), [
+ 'id' => $maintenance->id,
+ 'type' => 'maintenance',
+ 'note' => 'should fail',
+ ])
+ ->assertForbidden();
+ }
+
+ public function test_maintenance_must_exist_for_note()
+ {
+ $this->actingAs(User::factory()->editAssets()->create())
+ ->post(route('notes.store'), [
+ 'id' => 999_999,
+ 'type' => 'maintenance',
+ 'note' => 'ghost note',
+ ])
+ ->assertStatus(302);
+ }
}
diff --git a/tests/Unit/MaintenanceQueryBuilderTest.php b/tests/Unit/MaintenanceQueryBuilderTest.php
new file mode 100644
index 0000000000..a91ee3779d
--- /dev/null
+++ b/tests/Unit/MaintenanceQueryBuilderTest.php
@@ -0,0 +1,126 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Models\Maintenance;
+use App\Models\Setting;
+use Tests\TestCase;
+
+class MaintenanceQueryBuilderTest extends TestCase
+{
+ public function test_active_scope_excludes_completed_maintenances()
+ {
+ $active = Maintenance::factory()->create(['completed_at' => null]);
+ $done = Maintenance::factory()->create(['completed_at' => now()]);
+
+ $ids = Maintenance::active()->pluck('id');
+
+ $this->assertContains($active->id, $ids);
+ $this->assertNotContains($done->id, $ids);
+ }
+
+ public function test_completed_scope_excludes_active_maintenances()
+ {
+ $active = Maintenance::factory()->create(['completed_at' => null]);
+ $done = Maintenance::factory()->create(['completed_at' => now()]);
+
+ $ids = Maintenance::completed()->pluck('id');
+
+ $this->assertNotContains($active->id, $ids);
+ $this->assertContains($done->id, $ids);
+ }
+
+ public function test_due_for_completion_returns_items_in_warning_window()
+ {
+ $settings = Setting::factory()->create(['audit_warning_days' => 7]);
+
+ $due = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(3)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $notDueYet = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(30)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $alreadyDone = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(3)->format('Y-m-d'),
+ 'completed_at' => now(),
+ ]);
+
+ $ids = Maintenance::dueForCompletion($settings)->pluck('id');
+
+ $this->assertContains($due->id, $ids);
+ $this->assertNotContains($notDueYet->id, $ids);
+ $this->assertNotContains($alreadyDone->id, $ids);
+ }
+
+ public function test_overdue_for_completion_returns_past_due_items()
+ {
+ $overdue = Maintenance::factory()->create([
+ 'completion_date' => now()->subDay()->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $futureDate = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(5)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $alreadyDone = Maintenance::factory()->create([
+ 'completion_date' => now()->subDay()->format('Y-m-d'),
+ 'completed_at' => now(),
+ ]);
+
+ $ids = Maintenance::overdueForCompletion()->pluck('id');
+
+ $this->assertContains($overdue->id, $ids);
+ $this->assertNotContains($futureDate->id, $ids);
+ $this->assertNotContains($alreadyDone->id, $ids);
+ }
+
+ public function test_overdue_for_completion_excludes_items_with_no_completion_date()
+ {
+ $noDate = Maintenance::factory()->create([
+ 'completion_date' => null,
+ 'completed_at' => null,
+ ]);
+
+ $ids = Maintenance::overdueForCompletion()->pluck('id');
+
+ $this->assertNotContains($noDate->id, $ids);
+ }
+
+ public function test_due_or_overdue_returns_both_overdue_and_due()
+ {
+ $settings = Setting::factory()->create(['audit_warning_days' => 7]);
+
+ $overdue = Maintenance::factory()->create([
+ 'completion_date' => now()->subDay()->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $due = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(3)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $fine = Maintenance::factory()->create([
+ 'completion_date' => now()->addDays(30)->format('Y-m-d'),
+ 'completed_at' => null,
+ ]);
+
+ $done = Maintenance::factory()->create([
+ 'completion_date' => now()->subDay()->format('Y-m-d'),
+ 'completed_at' => now(),
+ ]);
+
+ $ids = Maintenance::dueOrOverdueForCompletion($settings)->pluck('id');
+
+ $this->assertContains($overdue->id, $ids);
+ $this->assertContains($due->id, $ids);
+ $this->assertNotContains($fine->id, $ids);
+ $this->assertNotContains($done->id, $ids);
+ }
+}