Files
breadcrumb-the-shire/modules/audit/tests/Module/Audit/Http/ApiSystemAuditReporterTest.php
fs 0c351f6aff 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

133 lines
4.6 KiB
PHP

<?php
namespace MintyPHP\Tests\Module\Audit\Http;
use MintyPHP\Http\ApiAuth;
use MintyPHP\Http\RequestContext;
use MintyPHP\Module\Audit\Http\ApiSystemAuditReporter;
use MintyPHP\Module\Audit\Service\SystemAuditService;
use PHPUnit\Framework\TestCase;
class ApiSystemAuditReporterTest extends TestCase
{
private array $serverBackup = [];
protected function setUp(): void
{
$this->serverBackup = $_SERVER;
$_SERVER = [];
RequestContext::resetForTests();
ApiAuth::authenticate();
}
protected function tearDown(): void
{
$_SERVER = $this->serverBackup;
RequestContext::resetForTests();
ApiAuth::authenticate();
}
public function testItRecordsMutatingRequest(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REQUEST_URI'] = '/api/v1/users/123';
$audit = $this->createMock(SystemAuditService::class);
$audit->expects($this->once())
->method('record')
->with(
'api.request',
'success',
$this->callback(static function (array $context): bool {
$metadata = is_array($context['metadata'] ?? null) ? $context['metadata'] : [];
return ($metadata['endpoint_key'] ?? '') === '/api/v1/users/{id}'
&& ($metadata['status_code'] ?? null) === 201
&& ($metadata['status_class'] ?? '') === '2xx'
&& ($metadata['auth_mode'] ?? '') === 'public'
&& ($metadata['write_method'] ?? null) === true
&& ($metadata['security_endpoint'] ?? null) === false;
})
);
$reporter = new ApiSystemAuditReporter($audit);
$reporter->start();
$reporter->finish(201);
}
public function testItSkipsNonSecurityGetSuccess(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/users';
$audit = $this->createMock(SystemAuditService::class);
$audit->expects($this->never())->method('record');
$reporter = new ApiSystemAuditReporter($audit);
$reporter->start();
$reporter->finish(200);
}
public function testItRecordsDeniedSecurityGetFailure(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/me/tokens';
$audit = $this->createMock(SystemAuditService::class);
$audit->expects($this->once())
->method('record')
->with(
'api.request',
'denied',
$this->callback(static function (array $context): bool {
$metadata = is_array($context['metadata'] ?? null) ? $context['metadata'] : [];
return ($metadata['endpoint_key'] ?? '') === '/api/v1/me/tokens'
&& ($metadata['status_code'] ?? null) === 401
&& ($metadata['status_class'] ?? '') === '4xx'
&& ($metadata['security_endpoint'] ?? null) === true;
})
);
$reporter = new ApiSystemAuditReporter($audit);
$reporter->start();
$reporter->finish(401, 'unauthorized');
}
public function testItRecordsFailedServerError(): void
{
$_SERVER['REQUEST_METHOD'] = 'GET';
$_SERVER['REQUEST_URI'] = '/api/v1/users';
$audit = $this->createMock(SystemAuditService::class);
$audit->expects($this->once())
->method('record')
->with(
'api.request',
'failed',
$this->callback(static function (array $context): bool {
$metadata = is_array($context['metadata'] ?? null) ? $context['metadata'] : [];
return ($metadata['status_code'] ?? null) === 500
&& ($metadata['status_class'] ?? '') === '5xx'
&& ($metadata['write_method'] ?? null) === false;
})
);
$reporter = new ApiSystemAuditReporter($audit);
$reporter->start();
$reporter->finish(500, 'unexpected_error');
}
public function testFinishIsIdempotent(): void
{
$_SERVER['REQUEST_METHOD'] = 'POST';
$_SERVER['REQUEST_URI'] = '/api/v1/tenants';
$audit = $this->createMock(SystemAuditService::class);
$audit->expects($this->once())->method('record');
$reporter = new ApiSystemAuditReporter($audit);
$reporter->start();
$reporter->finish(201);
$reporter->finish(201);
}
}