DashboardPageTest.php 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\App;
  4. use App\Auth\UserContext;
  5. use App\Tests\Integration\Support\AppTestCase;
  6. final class DashboardPageTest extends AppTestCase
  7. {
  8. protected function setUp(): void
  9. {
  10. $this->bootApp();
  11. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  12. $_SESSION['_last_active'] = time();
  13. $_SESSION['_authenticated_at'] = time();
  14. }
  15. public function testDashboardRendersStatsAndChartCanvas(): void
  16. {
  17. $this->enqueueApiResponse(200, [
  18. 'active_blocks' => 12,
  19. 'manual_blocks_count' => 3,
  20. 'allowlist_count' => 1,
  21. 'reports_24h' => 42,
  22. 'reports_24h_by_hour' => [
  23. ['hour' => '2026-04-29T10:00:00Z', 'count' => 7],
  24. ['hour' => '2026-04-29T11:00:00Z', 'count' => 35],
  25. ],
  26. 'top_reporters_24h' => [['name' => 'web-prod-01', 'count' => 30]],
  27. 'top_categories_24h' => [['slug' => 'brute_force', 'count' => 25]],
  28. 'jobs_status' => [['name' => 'recompute-scores', 'last_finished_at' => '2026-04-29T10:55:00Z', 'status' => 'success', 'overdue' => false]],
  29. 'reference_policy' => 'moderate',
  30. ]);
  31. $response = $this->request('GET', '/app/dashboard');
  32. self::assertSame(200, $response->getStatusCode());
  33. $body = (string) $response->getBody();
  34. self::assertStringContainsString('Dashboard', $body);
  35. self::assertStringContainsString('reports', $body);
  36. self::assertStringContainsString('id="reports-chart"', $body);
  37. self::assertStringContainsString('web-prod-01', $body);
  38. self::assertStringContainsString('brute_force', $body);
  39. self::assertStringContainsString('recompute-scores', $body);
  40. self::assertStringContainsString('moderate', $body);
  41. }
  42. public function testDashboardDegradesWhenApiUnreachable(): void
  43. {
  44. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  45. 'down',
  46. new \GuzzleHttp\Psr7\Request('GET', '/'),
  47. ));
  48. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  49. 'down',
  50. new \GuzzleHttp\Psr7\Request('GET', '/'),
  51. ));
  52. $response = $this->request('GET', '/app/dashboard');
  53. self::assertSame(200, $response->getStatusCode());
  54. $body = (string) $response->getBody();
  55. self::assertStringContainsString('Dashboard', $body);
  56. self::assertStringContainsString('API unreachable', $body);
  57. }
  58. }