AuditLoggerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /*
  3. * Copyright 2026 Alessandro Chiapparini <sprint_planer_web@chiapparini.org>
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * See the LICENSE file in the project root for the full license text.
  9. */
  10. declare(strict_types=1);
  11. namespace App\Tests\Services;
  12. use App\Services\AuditLogger;
  13. use App\Tests\TestCase;
  14. final class AuditLoggerTest extends TestCase
  15. {
  16. public function testRealUpdateProducesExactlyOneRow(): void
  17. {
  18. $pdo = $this->makeDb();
  19. $logger = new AuditLogger($pdo);
  20. $id = $logger->record(
  21. action: 'UPDATE',
  22. entityType: 'worker',
  23. entityId: 1,
  24. before: ['name' => 'Alice', 'is_active' => 1],
  25. after: ['name' => 'Alice Cooper', 'is_active' => 1],
  26. );
  27. $this->assertIsInt($id);
  28. $this->assertSame(1, (int) $pdo->query('SELECT COUNT(*) FROM audit_log')->fetchColumn());
  29. }
  30. public function testNoopUpdateWritesNoRow(): void
  31. {
  32. $pdo = $this->makeDb();
  33. $logger = new AuditLogger($pdo);
  34. $snap = ['name' => 'Alice', 'is_active' => 1, 'default_rtb' => 0.1];
  35. $result = $logger->record(
  36. action: 'UPDATE',
  37. entityType: 'worker',
  38. entityId: 1,
  39. before: $snap,
  40. after: $snap,
  41. );
  42. $this->assertNull($result);
  43. $this->assertSame(0, (int) $pdo->query('SELECT COUNT(*) FROM audit_log')->fetchColumn());
  44. }
  45. public function testNoopUpdateIsKeyOrderInsensitive(): void
  46. {
  47. $pdo = $this->makeDb();
  48. $logger = new AuditLogger($pdo);
  49. $before = ['name' => 'Alice', 'is_active' => 1];
  50. $after = ['is_active' => 1, 'name' => 'Alice']; // reordered keys
  51. $result = $logger->record('UPDATE', 'worker', 1, $before, $after);
  52. $this->assertNull($result);
  53. $this->assertSame(0, (int) $pdo->query('SELECT COUNT(*) FROM audit_log')->fetchColumn());
  54. }
  55. public function testCreateActionAlwaysWritesEvenWhenBeforeEqualsAfter(): void
  56. {
  57. // The no-op rule applies to UPDATE only. CREATE/DELETE always write.
  58. $pdo = $this->makeDb();
  59. $logger = new AuditLogger($pdo);
  60. $logger->record(
  61. action: 'CREATE',
  62. entityType: 'worker',
  63. entityId: 1,
  64. before: null,
  65. after: ['name' => 'Alice'],
  66. );
  67. $this->assertSame(1, (int) $pdo->query('SELECT COUNT(*) FROM audit_log')->fetchColumn());
  68. }
  69. public function testDeleteCarriesBeforeJsonAndNullAfterJson(): void
  70. {
  71. $pdo = $this->makeDb();
  72. $logger = new AuditLogger($pdo);
  73. $logger->record(
  74. action: 'DELETE',
  75. entityType: 'worker',
  76. entityId: 42,
  77. before: ['id' => 42, 'name' => 'Bob'],
  78. after: null,
  79. userId: 7,
  80. userEmail: 'admin@x',
  81. ipAddress: '1.2.3.4',
  82. userAgent: 'test-ua',
  83. );
  84. $row = $pdo->query('SELECT * FROM audit_log LIMIT 1')->fetch();
  85. $this->assertSame('DELETE', $row['action']);
  86. $this->assertSame('worker', $row['entity_type']);
  87. $this->assertSame(42, (int) $row['entity_id']);
  88. $this->assertSame(7, (int) $row['user_id']);
  89. $this->assertSame('admin@x', $row['user_email']);
  90. $this->assertSame('1.2.3.4', $row['ip_address']);
  91. $this->assertSame('test-ua', $row['user_agent']);
  92. $this->assertIsString($row['before_json']);
  93. $this->assertSame('{"id":42,"name":"Bob"}', $row['before_json']);
  94. $this->assertNull($row['after_json']);
  95. }
  96. public function testLoginEventWritesWithNullBeforeAndAfter(): void
  97. {
  98. // LOGIN events are not mutations and carry no before/after.
  99. $pdo = $this->makeDb();
  100. $logger = new AuditLogger($pdo);
  101. $logger->record(
  102. action: 'LOGIN',
  103. entityType: 'user',
  104. entityId: 1,
  105. before: null,
  106. after: null,
  107. );
  108. $row = $pdo->query('SELECT * FROM audit_log LIMIT 1')->fetch();
  109. $this->assertSame('LOGIN', $row['action']);
  110. $this->assertNull($row['before_json']);
  111. $this->assertNull($row['after_json']);
  112. }
  113. public function testLoginFailedSupportsNullUser(): void
  114. {
  115. $pdo = $this->makeDb();
  116. $logger = new AuditLogger($pdo);
  117. $logger->record(
  118. action: 'LOGIN_FAILED',
  119. entityType: 'user',
  120. entityId: null,
  121. before: null,
  122. after: ['reason' => 'bad_signature'],
  123. );
  124. $row = $pdo->query('SELECT * FROM audit_log LIMIT 1')->fetch();
  125. $this->assertSame('LOGIN_FAILED', $row['action']);
  126. $this->assertNull($row['entity_id']);
  127. $this->assertNull($row['user_id']);
  128. $this->assertSame('{"reason":"bad_signature"}', $row['after_json']);
  129. }
  130. public function testOccurredAtIsIsoUtc(): void
  131. {
  132. $pdo = $this->makeDb();
  133. $logger = new AuditLogger($pdo);
  134. $logger->record('CREATE', 'worker', 1, null, ['name' => 'Alice']);
  135. $ts = (string) $pdo->query('SELECT occurred_at FROM audit_log LIMIT 1')->fetchColumn();
  136. $this->assertMatchesRegularExpression(
  137. '/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/',
  138. $ts,
  139. );
  140. }
  141. }