2026-03-04 15:56:58 +01:00
|
|
|
<?php
|
|
|
|
|
|
refactor(audit): extract audit domain into self-contained module
Move the entire audit subsystem (system audit, API audit, import audit,
user lifecycle audit, frontend telemetry) from core into modules/audit/.
Core decoupling via interface-based injection:
- AuditRecorderInterface replaces SystemAuditService in 10+ core services
- UserLifecycleAuditInterface / ImportAuditInterface for specialized flows
- NullAuditRecorder fallback when audit module is disabled
- ApiBootstrap/ApiResponse use null-safe callable resolvers
Module structure (modules/audit/):
- Manifest with routes, permissions, scheduler jobs, authorization policy
- 9 services, 8 repositories, 6 domain enums, 4 job handlers
- 33 page files, 4 JS files, 8 test files, migration scripts, i18n
Core cleanup:
- OperationsAuthorizationPolicy, UiCapabilityMap, PermissionService
surgically cleaned of audit-specific constants
- Sidebar template cleared of hardcoded audit navigation
- AuditModuleIsolationContractTest ensures no future core→module coupling
All quality gates pass: 1346 tests (19,276 assertions), PHPStan level 5
clean, architecture contracts verified.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 21:12:49 +01:00
|
|
|
namespace MintyPHP\Tests\Module\Audit\Service;
|
2026-03-04 15:56:58 +01:00
|
|
|
|
|
|
|
|
use MintyPHP\Http\RequestContext;
|
test(auth): add 33 unit tests for AuthService and RememberMeService
AuthServiceTest (17 tests): canLoginToTenant, refreshSessionAuthState,
loginUserById, login email-not-verified branch.
RememberMeServiceTest (16 tests): rememberUser, autoLoginFromCookie
(all 11 branches), forgetCurrentUser, forgetAllForUser,
expireAllTokensByAdmin.
Also fixes: 3 PHPStan errors in FrontendTelemetryIngestService,
8 CS-Fixer violations in out-of-scope files (ordered_imports,
braces_position).
All quality gates green: QG-001 (429 tests/0 failures),
QG-002 (0 errors), QG-003 (11 arch tests pass), QG-006 (0 violations).
Task: AUTH-LOGIN-REVIEW-001
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-06 11:43:31 +01:00
|
|
|
use MintyPHP\Http\SessionStore;
|
refactor(audit): extract audit domain into self-contained module
Move the entire audit subsystem (system audit, API audit, import audit,
user lifecycle audit, frontend telemetry) from core into modules/audit/.
Core decoupling via interface-based injection:
- AuditRecorderInterface replaces SystemAuditService in 10+ core services
- UserLifecycleAuditInterface / ImportAuditInterface for specialized flows
- NullAuditRecorder fallback when audit module is disabled
- ApiBootstrap/ApiResponse use null-safe callable resolvers
Module structure (modules/audit/):
- Manifest with routes, permissions, scheduler jobs, authorization policy
- 9 services, 8 repositories, 6 domain enums, 4 job handlers
- 33 page files, 4 JS files, 8 test files, migration scripts, i18n
Core cleanup:
- OperationsAuthorizationPolicy, UiCapabilityMap, PermissionService
surgically cleaned of audit-specific constants
- Sidebar template cleared of hardcoded audit navigation
- AuditModuleIsolationContractTest ensures no future core→module coupling
All quality gates pass: 1346 tests (19,276 assertions), PHPStan level 5
clean, architecture contracts verified.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-25 21:12:49 +01:00
|
|
|
use MintyPHP\Module\Audit\Repository\SystemAuditLogRepository;
|
|
|
|
|
use MintyPHP\Module\Audit\Service\SystemAuditRedactionService;
|
|
|
|
|
use MintyPHP\Module\Audit\Service\SystemAuditService;
|
2026-03-06 00:44:52 +01:00
|
|
|
use MintyPHP\Service\Settings\SettingsSystemAuditGateway;
|
2026-03-04 15:56:58 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
|
|
class SystemAuditServiceTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
protected function tearDown(): void
|
|
|
|
|
{
|
|
|
|
|
RequestContext::resetForTests();
|
2026-03-06 00:44:52 +01:00
|
|
|
$_SESSION = [];
|
2026-03-04 15:56:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRecordReturnsNullWhenDisabled(): void
|
|
|
|
|
{
|
|
|
|
|
$repo = $this->createMock(SystemAuditLogRepository::class);
|
|
|
|
|
$repo->expects($this->never())->method('create');
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$settings = $this->createMock(SettingsSystemAuditGateway::class);
|
2026-03-04 15:56:58 +01:00
|
|
|
$settings->expects($this->once())->method('isSystemAuditEnabled')->willReturn(false);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$service = new SystemAuditService($repo, new SystemAuditRedactionService(), $settings, new SessionStore());
|
2026-03-04 15:56:58 +01:00
|
|
|
|
|
|
|
|
$this->assertNull($service->record('admin.users.update', 'success'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRecordWritesWhenEnabled(): void
|
|
|
|
|
{
|
|
|
|
|
$_SERVER['REQUEST_METHOD'] = 'POST';
|
|
|
|
|
$_SERVER['REQUEST_URI'] = '/admin/users/edit/abc';
|
|
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
|
|
|
|
|
$_SERVER['HTTP_USER_AGENT'] = 'phpunit';
|
|
|
|
|
|
|
|
|
|
$repo = $this->createMock(SystemAuditLogRepository::class);
|
|
|
|
|
$repo->expects($this->once())->method('create')->willReturn(5);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$settings = $this->createMock(SettingsSystemAuditGateway::class);
|
2026-03-04 15:56:58 +01:00
|
|
|
$settings->method('isSystemAuditEnabled')->willReturn(true);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$service = new SystemAuditService($repo, new SystemAuditRedactionService(), $settings, new SessionStore());
|
2026-03-04 15:56:58 +01:00
|
|
|
|
|
|
|
|
$id = $service->record('admin.users.update', 'success', [
|
|
|
|
|
'target_type' => 'user',
|
|
|
|
|
'target_id' => 10,
|
|
|
|
|
'before' => ['active' => 0],
|
|
|
|
|
'after' => ['active' => 1],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->assertSame(5, $id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testRecordIsFailOpenOnRepositoryError(): void
|
|
|
|
|
{
|
|
|
|
|
$repo = $this->createMock(SystemAuditLogRepository::class);
|
|
|
|
|
$repo->method('create')->willThrowException(new \RuntimeException('db down'));
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$settings = $this->createMock(SettingsSystemAuditGateway::class);
|
2026-03-04 15:56:58 +01:00
|
|
|
$settings->method('isSystemAuditEnabled')->willReturn(true);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$service = new SystemAuditService($repo, new SystemAuditRedactionService(), $settings, new SessionStore());
|
2026-03-04 15:56:58 +01:00
|
|
|
|
|
|
|
|
$this->assertNull($service->record('admin.users.update', 'success'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testPurgeUsesConfiguredRetention(): void
|
|
|
|
|
{
|
|
|
|
|
$repo = $this->createMock(SystemAuditLogRepository::class);
|
|
|
|
|
$repo->expects($this->once())->method('purgeOlderThanDays')->with(120)->willReturn(3);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$settings = $this->createMock(SettingsSystemAuditGateway::class);
|
2026-03-04 15:56:58 +01:00
|
|
|
$settings->method('getSystemAuditRetentionDays')->willReturn(120);
|
|
|
|
|
|
2026-03-06 00:44:52 +01:00
|
|
|
$service = new SystemAuditService($repo, new SystemAuditRedactionService(), $settings, new SessionStore());
|
2026-03-04 15:56:58 +01:00
|
|
|
|
|
|
|
|
$this->assertSame(3, $service->purgeExpired());
|
|
|
|
|
}
|
|
|
|
|
}
|