Files
breadcrumb-the-shire/modules/audit/tests/Module/Audit/Service/ImportAuditServiceTest.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

72 lines
2.7 KiB
PHP

<?php
namespace MintyPHP\Tests\Module\Audit\Service;
use MintyPHP\Module\Audit\Repository\ImportAuditRunRepository;
use MintyPHP\Module\Audit\Service\ImportAuditService;
use PHPUnit\Framework\TestCase;
class ImportAuditServiceTest extends TestCase
{
public function testStartRunNormalizesFilenameAndMappedTargets(): void
{
$captured = null;
$repository = $this->createMock(ImportAuditRunRepository::class);
$repository->expects($this->once())
->method('createRunning')
->with($this->callback(function (array $payload) use (&$captured): bool {
$captured = $payload;
return true;
}))
->willReturn(77);
$service = new ImportAuditService($repository);
$runId = $service->startRun('users', ['email', 'first_name', 'email'], '/tmp/../users.csv', 12, 5);
$this->assertSame(77, $runId);
$this->assertSame('users.csv', (string) ($captured['source_filename'] ?? ''));
$this->assertSame('email,first_name', (string) ($captured['mapped_targets_csv'] ?? ''));
$this->assertSame(12, (int) ($captured['user_id'] ?? 0));
$this->assertSame(5, (int) ($captured['current_tenant_id'] ?? 0));
}
public function testFinishRunDerivesPartialStatusAndAggregatesErrors(): void
{
$captured = null;
$repository = $this->createMock(ImportAuditRunRepository::class);
$repository->expects($this->once())
->method('finishById')
->with(
88,
$this->callback(function (array $payload) use (&$captured): bool {
$captured = $payload;
return true;
})
)
->willReturn(true);
$service = new ImportAuditService($repository);
$service->finishRun(88, [
'ok' => true,
'processed' => 3,
'created' => 1,
'skipped' => 1,
'failed' => 1,
'errors' => [
['code' => 'email_exists'],
['code' => 'email_exists'],
['code' => 'invalid_email'],
],
]);
$this->assertSame('partial', (string) ($captured['status'] ?? ''));
$this->assertSame(3, (int) ($captured['rows_total'] ?? 0));
$this->assertSame(1, (int) ($captured['created_count'] ?? 0));
$this->assertSame(1, (int) ($captured['skipped_count'] ?? 0));
$this->assertSame(1, (int) ($captured['failed_count'] ?? 0));
$errorJson = (string) ($captured['error_codes_json'] ?? '');
$this->assertStringContainsString('email_exists', $errorJson);
$this->assertStringContainsString('invalid_email', $errorJson);
}
}