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>
93 lines
3.7 KiB
PHP
93 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace MintyPHP\Tests\Module\Audit\Service;
|
|
|
|
use MintyPHP\Module\Audit\Service\AuditMetadataEnricher;
|
|
use MintyPHP\Repository\User\UserReadRepositoryInterface;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class AuditMetadataEnricherTest extends TestCase
|
|
{
|
|
private function newEnricher(UserReadRepositoryInterface $userReadRepository): AuditMetadataEnricher
|
|
{
|
|
return new AuditMetadataEnricher($userReadRepository);
|
|
}
|
|
|
|
public function testEnrichResolvesCreatedByAndModifiedBy(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->method('find')->willReturnMap([
|
|
[10, ['first_name' => 'Alice', 'last_name' => 'Smith', 'email' => 'alice@example.com', 'uuid' => 'uuid-10']],
|
|
[20, ['first_name' => 'Bob', 'last_name' => 'Jones', 'email' => 'bob@example.com', 'uuid' => 'uuid-20']],
|
|
]);
|
|
|
|
$entity = ['created_by' => 10, 'modified_by' => 20];
|
|
$this->newEnricher($repo)->enrich($entity);
|
|
|
|
$this->assertSame('Alice Smith', $entity['created_by_label']);
|
|
$this->assertSame('uuid-10', $entity['created_by_uuid']);
|
|
$this->assertSame('Bob Jones', $entity['modified_by_label']);
|
|
$this->assertSame('uuid-20', $entity['modified_by_uuid']);
|
|
}
|
|
|
|
public function testEnrichFallsBackToEmailWhenNameEmpty(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->method('find')->willReturn(['first_name' => '', 'last_name' => '', 'email' => 'no-name@example.com', 'uuid' => 'uuid-x']);
|
|
|
|
$entity = ['created_by' => 5];
|
|
$this->newEnricher($repo)->enrich($entity, ['created_by']);
|
|
|
|
$this->assertSame('no-name@example.com', $entity['created_by_label']);
|
|
$this->assertSame('uuid-x', $entity['created_by_uuid']);
|
|
}
|
|
|
|
public function testEnrichSkipsZeroAndNegativeIds(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->expects($this->never())->method('find');
|
|
|
|
$entity = ['created_by' => 0, 'modified_by' => -1];
|
|
$this->newEnricher($repo)->enrich($entity);
|
|
|
|
$this->assertArrayNotHasKey('created_by_label', $entity);
|
|
$this->assertArrayNotHasKey('modified_by_label', $entity);
|
|
}
|
|
|
|
public function testEnrichSkipsMissingFields(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->expects($this->never())->method('find');
|
|
|
|
$entity = [];
|
|
$this->newEnricher($repo)->enrich($entity, ['created_by', 'modified_by']);
|
|
|
|
$this->assertArrayNotHasKey('created_by_label', $entity);
|
|
}
|
|
|
|
public function testEnrichHandlesUserNotFound(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->method('find')->willReturn(null);
|
|
|
|
$entity = ['created_by' => 999];
|
|
$this->newEnricher($repo)->enrich($entity, ['created_by']);
|
|
|
|
$this->assertArrayNotHasKey('created_by_label', $entity);
|
|
}
|
|
|
|
public function testEnrichWithCustomFields(): void
|
|
{
|
|
$repo = $this->createMock(UserReadRepositoryInterface::class);
|
|
$repo->method('find')->willReturn(['first_name' => 'Admin', 'last_name' => '', 'email' => 'admin@co.com', 'uuid' => 'uuid-a']);
|
|
|
|
$entity = ['status_changed_by' => 7, 'active_changed_by' => 7];
|
|
$this->newEnricher($repo)->enrich($entity, ['status_changed_by', 'active_changed_by']);
|
|
|
|
$this->assertSame('Admin', $entity['status_changed_by_label']);
|
|
$this->assertSame('uuid-a', $entity['status_changed_by_uuid']);
|
|
$this->assertSame('Admin', $entity['active_changed_by_label']);
|
|
$this->assertSame('uuid-a', $entity['active_changed_by_uuid']);
|
|
}
|
|
}
|