A free open source IT asset/license management system
Added test
snipe17fe2de249c8
diff --git a/tests/Feature/Livewire/NeedsAttentionTest.php b/tests/Feature/Livewire/NeedsAttentionTest.phpnew file mode 100644index 0000000000..2cf916fc8c--- /dev/null+++ b/tests/Feature/Livewire/NeedsAttentionTest.php@@ -0,0 +1,198 @@+<?php++namespace Tests\Feature\Livewire;++use App\Livewire\NeedsAttention;+use App\Models\Asset;+use App\Models\CheckoutRequest;+use App\Models\Company;+use App\Models\License;+use App\Models\Maintenance;+use App\Models\User;+use Livewire\Livewire;+use Tests\Concerns\TestsFullMultipleCompaniesSupport;+use Tests\Concerns\TestsPermissionsRequirement;+use Tests\TestCase;++/**+ * Coverage for the dashboard's Needs Attention Livewire widget.+ * Focuses on:+ *+ * - boot() authorization: hasAccess('admin') gate refuses+ * unauthenticated + non-admin users so a snapshot-replay POST+ * can't leak counts they'd never see in the parent view.+ * - Placeholder shape: renders a skeleton with the same title ++ * list-group chrome so hydration doesn't visibly shift the row.+ * - Count accuracy: each of the eight counts reflects seeded data.+ * - FMCS scoping: a company-scoped admin sees only the counts+ * matching rows in their own company, not cross-company totals.+ */+class NeedsAttentionTest extends TestCase implements TestsFullMultipleCompaniesSupport, TestsPermissionsRequirement+{+ public function test_requires_permission(): void+ {+ // Livewire's test helper intercepts HttpExceptions raised in+ // boot() and surfaces them as test failures rather than a+ // response status, so call boot() directly. Non-admin user+ // must trip the abort_unless.+ $this->actingAs(User::factory()->create());++ try {+ (new NeedsAttention)->boot();+ $this->fail('Expected boot() to abort with a 403.');+ } catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {+ $this->assertSame(403, $e->getStatusCode());+ }+ }++ public function test_boot_permits_admin(): void+ {+ // Superuser passes hasAccess('admin'). boot() should return+ // silently (no throw).+ $this->actingAs(User::factory()->superuser()->create());++ (new NeedsAttention)->boot();++ // Reaching this line means boot() didn't throw.+ $this->assertTrue(true);+ }++ public function test_the_component_renders_for_admin(): void+ {+ Livewire::withoutLazyLoading();+ Livewire::actingAs(User::factory()->superuser()->create())+ ->test(NeedsAttention::class)+ ->assertStatus(200);+ }++ public function test_placeholder_reserves_widget_footprint(): void+ {+ $placeholder = (new NeedsAttention)->placeholder();++ // Placeholder shape mirrors the loaded view's box + list-group+ // markup so first-paint layout matches post-hydration layout+ // and the row doesn't visibly jump when the real counts land.+ $this->assertStringContainsString('box box-default', $placeholder);+ $this->assertStringContainsString('box-title', $placeholder);+ $this->assertStringContainsString('list-group', $placeholder);+ }++ public function test_counts_reflect_seeded_data(): void+ {+ // Seed rows that should push each count above zero.+ Asset::factory()->count(3)->create(['next_audit_date' => now()->subDays(5)]);+ Asset::factory()->count(2)->create([+ 'expected_checkin' => now()->subDays(2),+ 'assigned_to' => User::factory()->create()->id,+ 'assigned_type' => User::class,+ ]);+ // AssetFactory's configure() afterMaking hook unconditionally+ // overwrites asset_eol_date (computed from purchase_date ++ // model.eol). Bypass by creating first and then forceFilling+ // the exact date we want.+ Asset::factory()->count(4)->create()->each(function ($asset) {+ $asset->forceFill(['asset_eol_date' => now()->subDays(10)])->save();+ });++ License::factory()->count(2)->create([+ 'expiration_date' => now()->addDays(10)->format('Y-m-d'),+ ]);++ Maintenance::factory()->create([+ 'item_id' => Asset::factory()->create()->id,+ 'item_type' => Asset::class,+ 'start_date' => now()->subDays(20),+ 'expected_completion_date' => now()->subDays(5),+ 'completed_at' => null,+ ]);++ Livewire::withoutLazyLoading();+ Livewire::actingAs(User::factory()->superuser()->create())+ ->test(NeedsAttention::class)+ ->assertSet('overdueAudits', 3)+ ->assertSet('overdueCheckins', 2)+ ->assertSet('assetsPastEol', 4)+ ->assertSet('licensesExpiringSoon', 2)+ ->assertSet('overdueMaintenances', 1);+ }++ public function test_counts_ignore_completed_maintenances(): void+ {+ // A past-due maintenance that's been marked complete should+ // drop out of the overdue count; only the still-open one below+ // should register.+ $asset = Asset::factory()->create();++ Maintenance::factory()->create([+ 'item_id' => $asset->id,+ 'item_type' => Asset::class,+ 'start_date' => now()->subDays(20),+ 'expected_completion_date' => now()->subDays(5),+ 'completed_at' => now()->subDays(1),+ ]);+ Maintenance::factory()->create([+ 'item_id' => $asset->id,+ 'item_type' => Asset::class,+ 'start_date' => now()->subDays(20),+ 'expected_completion_date' => now()->subDays(5),+ 'completed_at' => null,+ ]);++ Livewire::withoutLazyLoading();+ Livewire::actingAs(User::factory()->superuser()->create())+ ->test(NeedsAttention::class)+ ->assertSet('overdueMaintenances', 1);+ }++ public function test_pending_checkout_requests_count_ignores_canceled_and_fulfilled(): void+ {+ $requestable = Asset::factory()->create();+ $requester = User::factory()->create();+ $base = [+ 'requestable_id' => $requestable->id,+ 'requestable_type' => Asset::class,+ 'user_id' => $requester->id,+ 'quantity' => 1,+ ];++ // CheckoutRequest's $fillable is narrow (user_id + quantity+ // only), so mass-assign the morphable fields via forceFill ++ // save. Matches how the application's Actions class writes+ // these rows.+ (new CheckoutRequest)->forceFill($base)->save();+ (new CheckoutRequest)->forceFill($base + ['canceled_at' => now()])->save();+ (new CheckoutRequest)->forceFill($base + ['fulfilled_at' => now()])->save();++ Livewire::withoutLazyLoading();+ Livewire::actingAs(User::factory()->superuser()->create())+ ->test(NeedsAttention::class)+ ->assertSet('pendingRequestsCount', 1);+ }++ public function test_adheres_to_full_multiple_companies_support_scoping()+ {+ [$companyA, $companyB] = Company::factory()->count(2)->create();++ // 3 overdue-audit assets in Company A, 5 in Company B.+ Asset::factory()->count(3)->for($companyA)->create(['next_audit_date' => now()->subDays(5)]);+ Asset::factory()->count(5)->for($companyB)->create(['next_audit_date' => now()->subDays(5)]);++ // 2 expiring licenses in each company.+ License::factory()->count(2)->for($companyA)->create([+ 'expiration_date' => now()->addDays(10)->format('Y-m-d'),+ ]);+ License::factory()->count(2)->for($companyB)->create([+ 'expiration_date' => now()->addDays(10)->format('Y-m-d'),+ ]);++ $adminInCompanyA = $companyA->users()->save(User::factory()->admin()->make());++ $this->settings->enableMultipleFullCompanySupport();++ Livewire::withoutLazyLoading();+ Livewire::actingAs($adminInCompanyA)+ ->test(NeedsAttention::class)+ ->assertSet('overdueAudits', 3)+ ->assertSet('licensesExpiringSoon', 2);+ }+}