1
0

AuditPageTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Tests\Integration\Audit;
  4. use App\Auth\UserContext;
  5. use App\Tests\Integration\Support\AppTestCase;
  6. /**
  7. * `/app/audit` — list view + filter round-trip.
  8. */
  9. final class AuditPageTest extends AppTestCase
  10. {
  11. protected function setUp(): void
  12. {
  13. $this->bootApp();
  14. $_SESSION['_user'] = (new UserContext(1, 'Admin', 'admin', null, UserContext::SOURCE_LOCAL))->toArray();
  15. $_SESSION['_last_active'] = time();
  16. $_SESSION['_authenticated_at'] = time();
  17. }
  18. public function testRendersList(): void
  19. {
  20. $this->enqueueApiResponse(200, [
  21. 'page' => 1,
  22. 'page_size' => 50,
  23. 'total' => 1,
  24. 'items' => [
  25. [
  26. 'id' => 42,
  27. 'occurred_at' => '2026-04-29T10:00:00Z',
  28. 'actor_kind' => 'user',
  29. 'actor_id' => '7',
  30. 'action' => 'manual_block.created',
  31. 'entity_type' => 'manual_block',
  32. 'entity_id' => '12',
  33. 'details' => ['ip' => '203.0.113.99', 'reason' => 'audit-test'],
  34. 'source_ip' => '127.0.0.1',
  35. ],
  36. ],
  37. ]);
  38. $resp = $this->request('GET', '/app/audit');
  39. self::assertSame(200, $resp->getStatusCode());
  40. $body = (string) $resp->getBody();
  41. self::assertStringContainsString('manual_block.created', $body);
  42. self::assertStringContainsString('203.0.113.99', $body);
  43. self::assertStringContainsString('1 total', $body);
  44. }
  45. public function testRendersEmptyState(): void
  46. {
  47. $this->enqueueApiResponse(200, ['page' => 1, 'page_size' => 50, 'total' => 0, 'items' => []]);
  48. $resp = $this->request('GET', '/app/audit');
  49. self::assertSame(200, $resp->getStatusCode());
  50. self::assertStringContainsString('No events match', (string) $resp->getBody());
  51. }
  52. public function testFilterRoundTrip(): void
  53. {
  54. $this->enqueueApiResponse(200, ['page' => 1, 'page_size' => 50, 'total' => 0, 'items' => []]);
  55. $resp = $this->request('GET', '/app/audit?action=token.created&actor_kind=user');
  56. $body = (string) $resp->getBody();
  57. self::assertSame(200, $resp->getStatusCode());
  58. // The form preserves the user's selection.
  59. self::assertMatchesRegularExpression('/<option value="token\.created"\s+selected/', $body);
  60. self::assertMatchesRegularExpression('/<option value="user"\s+selected/', $body);
  61. }
  62. public function testRedirectsAnonymousToLogin(): void
  63. {
  64. $_SESSION = [];
  65. $resp = $this->request('GET', '/app/audit');
  66. self::assertSame(302, $resp->getStatusCode());
  67. self::assertSame('/login', $resp->getHeaderLine('Location'));
  68. }
  69. }