A free open source IT asset/license management system
Added tests
snipe1db51164f9ae
diff --git a/tests/Feature/CalendarEvents/CalendarEventsApiTest.php b/tests/Feature/CalendarEvents/CalendarEventsApiTest.phpnew file mode 100644index 0000000000..a723df5fe8--- /dev/null+++ b/tests/Feature/CalendarEvents/CalendarEventsApiTest.php@@ -0,0 +1,158 @@+<?php++namespace Tests\Feature\CalendarEvents;++use App\Models\Asset;+use App\Models\Company;+use App\Models\Maintenance;+use App\Models\User;+use Tests\Concerns\TestsFullMultipleCompaniesSupport;+use Tests\Concerns\TestsPermissionsRequirement;+use Tests\TestCase;++/**+ * Coverage for the unified /api/v1/calendar/events endpoint. Verifies:+ *+ * - Base permission gate blocks users without view-Asset.+ * - Date range filter honors ?start and ?end.+ * - event_type[] and comma-separated ?filter both narrow results.+ * - Per-row access check (via the source's own view policy) hides+ * rows whose source model the actor can't see - the FMCS scoping+ * path.+ * - Row hydrates title / url / color from the source's presenter.+ * - Orphan rows (source missing) are silently skipped rather than+ * erroring out.+ */+class CalendarEventsApiTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement+{+ public function test_requires_permission()+ {+ $this->actingAsForApi(User::factory()->create())+ ->getJson(route('api.calendar.events'))+ ->assertForbidden();+ }++ public function test_returns_events_for_the_seeded_maintenance()+ {+ $actor = User::factory()->superuser()->create();+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $rows = $this->actingAsForApi($actor)+ ->getJson(route('api.calendar.events'))+ ->assertOk()+ ->json('events');++ $matching = collect($rows)->firstWhere('extendedProps.source_id', $maintenance->id);+ $this->assertNotNull($matching);+ $this->assertSame('maintenance.start', $matching['extendedProps']['event_type']);+ $this->assertNotEmpty($matching['title']);+ $this->assertNotEmpty($matching['url']);+ }++ public function test_range_filter_hides_events_outside_visible_window()+ {+ $actor = User::factory()->superuser()->create();+ $asset = Asset::factory()->create();++ $inRange = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);+ $outOfRange = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subYears(2),+ 'expected_completion_date' => now()->subYears(2)->addDay(),+ ]);++ $ids = collect($this->actingAsForApi($actor)+ ->getJson(route('api.calendar.events', [+ 'start' => now()->subWeek()->toIso8601String(),+ 'end' => now()->addWeek()->toIso8601String(),+ ]))+ ->assertOk()+ ->json('events'))+ ->pluck('extendedProps.source_id')+ ->all();++ $this->assertContains($inRange->id, $ids);+ $this->assertNotContains($outOfRange->id, $ids);+ }++ public function test_event_type_filter_narrows_results()+ {+ $actor = User::factory()->superuser()->create();+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ // Positive: filter includes the seeded event type.+ $ids = collect($this->actingAsForApi($actor)+ ->getJson(route('api.calendar.events', ['event_type' => ['maintenance.start']]))+ ->assertOk()+ ->json('events'))+ ->pluck('extendedProps.source_id')+ ->all();+ $this->assertContains($maintenance->id, $ids);++ // Negative: filter excludes the seeded event type.+ $ids = collect($this->actingAsForApi($actor)+ ->getJson(route('api.calendar.events', ['event_type' => ['some.other.type']]))+ ->assertOk()+ ->json('events'))+ ->pluck('extendedProps.source_id')+ ->all();+ $this->assertNotContains($maintenance->id, $ids);+ }++ public function test_adheres_to_full_multiple_companies_support_scoping()+ {+ // The endpoint's per-row can('view', $source) check delegates+ // to each source's own policy, which honors FMCS. Verifies+ // that a user scoped to Company A does NOT see a maintenance+ // event for an asset in Company B.+ [$companyA, $companyB] = Company::factory()->count(2)->create();+ // MaintenancePolicy::view() requires update-on-parent-asset,+ // not just view; use editAssets so the actor has the right+ // access to their own company's maintenance.+ // MaintenancePolicy::view() requires update-on-parent-asset;+ // the base endpoint gate also requires view-Asset. Grant both.+ $userInCompanyA = $companyA->users()->save(User::factory()->viewAssets()->editAssets()->make());++ $assetA = Asset::factory()->for($companyA)->create();+ $assetB = Asset::factory()->for($companyB)->create();++ $maintenanceA = Maintenance::factory()->create([+ 'asset_id' => $assetA->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);+ $maintenanceB = Maintenance::factory()->create([+ 'asset_id' => $assetB->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $this->settings->enableMultipleFullCompanySupport();++ $sourceIds = collect($this->actingAsForApi($userInCompanyA)+ ->getJson(route('api.calendar.events'))+ ->assertOk()+ ->json('events'))+ ->pluck('extendedProps.source_id')+ ->all();++ $this->assertContains($maintenanceA->id, $sourceIds);+ $this->assertNotContains($maintenanceB->id, $sourceIds);+ }+}diff --git a/tests/Feature/CalendarEvents/CalendarEventsPageTest.php b/tests/Feature/CalendarEvents/CalendarEventsPageTest.phpnew file mode 100644index 0000000000..068f71a314--- /dev/null+++ b/tests/Feature/CalendarEvents/CalendarEventsPageTest.php@@ -0,0 +1,23 @@+<?php++namespace Tests\Feature\CalendarEvents;++use App\Models\User;+use Tests\TestCase;++class CalendarEventsPageTest extends TestCase+{+ public function test_page_renders()+ {+ $this->actingAs(User::factory()->superuser()->create())+ ->get(route('calendar.index'))+ ->assertOk();+ }++ public function test_requires_permission()+ {+ $this->actingAs(User::factory()->create())+ ->get(route('calendar.index'))+ ->assertForbidden();+ }+}diff --git a/tests/Feature/CalendarEvents/CalendarEventsSyncTest.php b/tests/Feature/CalendarEvents/CalendarEventsSyncTest.phpnew file mode 100644index 0000000000..2656cb83df--- /dev/null+++ b/tests/Feature/CalendarEvents/CalendarEventsSyncTest.php@@ -0,0 +1,216 @@+<?php++namespace Tests\Feature\CalendarEvents;++use App\Models\Asset;+use App\Models\CalendarEvent;+use App\Models\Maintenance;+use Tests\TestCase;++/**+ * Coverage for the HasCalendarEvents trait's observer + sync path,+ * exercised via Maintenance (currently the only source). Verifies:+ *+ * - Save creates rows keyed on (source_type, source_id, source_field).+ * - Date-column update reflects on the row.+ * - Nulling a declared date-column deletes the row.+ * - Soft-delete cascades to soft-delete of the row.+ * - Restore restores the row.+ * - Force-delete cascades to hard-delete of the row.+ * - Reconcile heals drift (direct DB update on the source) and+ * removes orphaned rows.+ */+class CalendarEventsSyncTest extends TestCase+{+ public function test_saving_a_maintenance_creates_a_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $event = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->where('source_field', 'start_date')+ ->first();++ $this->assertNotNull($event);+ $this->assertSame('maintenance.start', $event->event_type);+ $this->assertNotNull($event->start);+ $this->assertNotNull($event->end);+ }++ public function test_updating_the_start_date_reflects_on_the_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subWeek(),+ 'expected_completion_date' => now()->addWeek(),+ ]);++ $newStart = now()->addDay()->startOfDay();+ $maintenance->update(['start_date' => $newStart]);++ $event = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();++ $this->assertNotNull($event);+ $this->assertTrue($event->start->equalTo($newStart));+ }++ public function test_nulling_the_end_field_reflects_on_the_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $maintenance->update(['expected_completion_date' => null]);++ $event = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();++ $this->assertNotNull($event);+ $this->assertNull($event->end);+ }++ public function test_non_date_update_does_not_touch_the_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $eventBefore = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();+ $updatedAtBefore = $eventBefore->updated_at;++ // Advance the clock so a spurious re-save would produce a+ // visibly different updated_at than the initial one.+ $this->travel(1)->minutes();++ $maintenance->update(['name' => 'Renamed Only']);++ $eventAfter = $eventBefore->fresh();+ $this->assertTrue($eventAfter->updated_at->equalTo($updatedAtBefore),+ 'Non-date update should not have triggered a calendar_events write.');+ }++ public function test_soft_deleting_the_source_soft_deletes_the_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ $maintenance->delete();++ $this->assertDatabaseMissing('calendar_events', [+ 'source_type' => Maintenance::class,+ 'source_id' => $maintenance->id,+ 'deleted_at' => null,+ ]);++ // The row is still there under soft-delete, just excluded+ // from the default scope.+ $trashed = CalendarEvent::withTrashed()+ ->where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();+ $this->assertNotNull($trashed);+ $this->assertNotNull($trashed->deleted_at);+ }++ public function test_restoring_the_source_restores_the_calendar_event_row(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);+ $maintenance->delete();+ $maintenance->restore();++ $event = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();++ $this->assertNotNull($event);+ $this->assertNull($event->deleted_at);+ }++ public function test_reconcile_command_heals_direct_db_date_drift(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ // Simulate direct DB drift: change the source's start_date+ // via query builder so the observer never fires.+ $driftedStart = now()->addWeeks(2)->startOfDay();+ \DB::table('maintenances')+ ->where('id', $maintenance->id)+ ->update(['start_date' => $driftedStart]);++ $this->artisan('snipeit:reconcile-calendar-events')+ ->assertSuccessful();++ $event = CalendarEvent::where('source_type', Maintenance::class)+ ->where('source_id', $maintenance->id)+ ->first();++ $this->assertTrue($event->start->equalTo($driftedStart));+ }++ public function test_reconcile_command_deletes_orphaned_rows(): void+ {+ $asset = Asset::factory()->create();++ $maintenance = Maintenance::factory()->create([+ 'asset_id' => $asset->id,+ 'start_date' => now()->subDay(),+ 'expected_completion_date' => now()->addDay(),+ ]);++ // Simulate a source hard-delete that bypassed the observer+ // (e.g. raw SQL, migration): remove the maintenance row+ // directly but leave the calendar_events row behind.+ \DB::table('maintenances')->where('id', $maintenance->id)->delete();++ $this->assertDatabaseHas('calendar_events', [+ 'source_type' => Maintenance::class,+ 'source_id' => $maintenance->id,+ ]);++ $this->artisan('snipeit:reconcile-calendar-events')+ ->assertSuccessful();++ $this->assertDatabaseMissing('calendar_events', [+ 'source_type' => Maintenance::class,+ 'source_id' => $maintenance->id,+ ]);+ }+}