1
0

DashboardPageTest.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. 'bans_by_day_7d' => [
  29. ['day' => '2026-04-23', 'count' => 0],
  30. ['day' => '2026-04-24', 'count' => 2],
  31. ['day' => '2026-04-25', 'count' => 1],
  32. ['day' => '2026-04-26', 'count' => 0],
  33. ['day' => '2026-04-27', 'count' => 3],
  34. ['day' => '2026-04-28', 'count' => 5],
  35. ['day' => '2026-04-29', 'count' => 1],
  36. ],
  37. 'jobs_status' => [['name' => 'recompute-scores', 'last_finished_at' => '2026-04-29T10:55:00Z', 'status' => 'success', 'overdue' => false]],
  38. 'reference_policy' => 'moderate',
  39. ]);
  40. $response = $this->request('GET', '/app/dashboard');
  41. self::assertSame(200, $response->getStatusCode());
  42. $body = (string) $response->getBody();
  43. self::assertStringContainsString('Dashboard', $body);
  44. self::assertStringContainsString('reports', $body);
  45. self::assertStringContainsString('id="reports-chart"', $body);
  46. self::assertStringContainsString('id="top-reporters-chart"', $body);
  47. self::assertStringContainsString('id="top-categories-chart"', $body);
  48. self::assertStringContainsString('id="bans-trend-chart"', $body);
  49. self::assertStringContainsString('web-prod-01', $body);
  50. self::assertStringContainsString('brute_force', $body);
  51. self::assertStringContainsString('recompute-scores', $body);
  52. self::assertStringContainsString('moderate', $body);
  53. }
  54. public function testDashboardDegradesWhenApiUnreachable(): void
  55. {
  56. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  57. 'down',
  58. new \GuzzleHttp\Psr7\Request('GET', '/'),
  59. ));
  60. $this->enqueueApiException(new \GuzzleHttp\Exception\ConnectException(
  61. 'down',
  62. new \GuzzleHttp\Psr7\Request('GET', '/'),
  63. ));
  64. $response = $this->request('GET', '/app/dashboard');
  65. self::assertSame(200, $response->getStatusCode());
  66. $body = (string) $response->getBody();
  67. self::assertStringContainsString('Dashboard', $body);
  68. self::assertStringContainsString('API unreachable', $body);
  69. }
  70. }