TenantAtlas/tests/Unit/Onboarding/OnboardingLifecycleServiceTest.php
ahmido 641bb4afde feat: implement tenant lifecycle operability semantics (#172)
## Summary
- implement Spec 143 tenant lifecycle, operability, and tenant-context semantics across chooser, tenant management, onboarding, and canonical operation viewers
- add centralized tenant lifecycle and operability support types, audit action coverage, and lifecycle-aware badge and action handling
- add feature and unit coverage for tenant chooser eligibility, global search scoping, canonical operation access, onboarding authorization, and lifecycle presentation

## Testing
- vendor/bin/sail artisan test --compact
- vendor/bin/sail bin pint --dirty --format agent

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #172
2026-03-15 09:08:36 +00:00

225 lines
8.7 KiB
PHP

<?php
declare(strict_types=1);
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Services\Onboarding\OnboardingLifecycleService;
use App\Support\Onboarding\OnboardingCheckpoint;
use App\Support\Onboarding\OnboardingLifecycleState;
use App\Support\OperationRunOutcome;
use App\Support\OperationRunStatus;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
it('marks a draft as action required when the provider connection changed before verification reruns', function (): void {
$tenant = Tenant::factory()->create();
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'current_step' => 'verify',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'provider_connection_id' => 42,
'connection_recently_updated' => true,
],
]);
$snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::ConnectProvider)
->and($snapshot['reason_code'])->toBe('provider_connection_changed')
->and($snapshot['blocking_reason_code'])->toBe('provider_connection_changed');
});
it('marks a draft as ready for activation when verification succeeded for the selected connection', function (): void {
$tenant = Tenant::factory()->create();
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'current_step' => 'verify',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'provider_connection_id' => 84,
],
]);
$run = OperationRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'status' => OperationRunStatus::Completed->value,
'outcome' => OperationRunOutcome::Succeeded->value,
'context' => [
'provider_connection_id' => 84,
],
]);
$draft->forceFill([
'state' => array_merge($draft->state ?? [], [
'verification_operation_run_id' => (int) $run->getKey(),
]),
])->save();
$service = app(OnboardingLifecycleService::class);
$snapshot = $service->snapshot($draft->fresh());
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ReadyForActivation)
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::CompleteActivate)
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
->and($service->isReadyForActivation($draft->fresh()))->toBeTrue();
});
it('marks a draft as bootstrapping while a selected bootstrap run is still active', function (): void {
$tenant = Tenant::factory()->create();
$verificationRun = OperationRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'status' => OperationRunStatus::Completed->value,
'outcome' => OperationRunOutcome::Succeeded->value,
'context' => [
'provider_connection_id' => 126,
],
]);
$bootstrapRun = OperationRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'status' => OperationRunStatus::Running->value,
'outcome' => OperationRunOutcome::Pending->value,
'type' => 'inventory_sync',
'context' => [
'provider_connection_id' => 126,
],
]);
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'current_step' => 'bootstrap',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'provider_connection_id' => 126,
'verification_operation_run_id' => (int) $verificationRun->getKey(),
'bootstrap_operation_types' => ['inventory_sync'],
'bootstrap_operation_runs' => [
'inventory_sync' => (int) $bootstrapRun->getKey(),
],
],
]);
$service = app(OnboardingLifecycleService::class);
$snapshot = $service->snapshot($draft);
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::Bootstrapping)
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
->and($service->hasActiveCheckpoint($draft))->toBeTrue();
});
it('marks a draft as action required when a selected bootstrap run fails', function (): void {
$tenant = Tenant::factory()->create();
$verificationRun = OperationRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'status' => OperationRunStatus::Completed->value,
'outcome' => OperationRunOutcome::Succeeded->value,
'context' => [
'provider_connection_id' => 256,
],
]);
$bootstrapRun = OperationRun::factory()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $tenant->workspace_id,
'status' => OperationRunStatus::Completed->value,
'outcome' => OperationRunOutcome::Failed->value,
'type' => 'inventory_sync',
'context' => [
'provider_connection_id' => 256,
],
]);
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'current_step' => 'bootstrap',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'provider_connection_id' => 256,
'verification_operation_run_id' => (int) $verificationRun->getKey(),
'bootstrap_operation_types' => ['inventory_sync'],
'bootstrap_operation_runs' => [
'inventory_sync' => (int) $bootstrapRun->getKey(),
],
],
]);
$snapshot = app(OnboardingLifecycleService::class)->snapshot($draft);
expect($snapshot['lifecycle_state'])->toBe(OnboardingLifecycleState::ActionRequired)
->and($snapshot['current_checkpoint'])->toBe(OnboardingCheckpoint::Bootstrap)
->and($snapshot['last_completed_checkpoint'])->toBe(OnboardingCheckpoint::VerifyAccess)
->and($snapshot['reason_code'])->toBe('bootstrap_failed')
->and($snapshot['blocking_reason_code'])->toBe('bootstrap_failed');
});
it('applies the canonical lifecycle fields and normalizes the version floor', function (): void {
$tenant = Tenant::factory()->create();
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'version' => 0,
'lifecycle_state' => OnboardingLifecycleState::Draft->value,
'current_checkpoint' => OnboardingCheckpoint::Identify->value,
'last_completed_checkpoint' => null,
'current_step' => 'verify',
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
'provider_connection_id' => 999,
'connection_recently_updated' => true,
],
]);
$changed = app(OnboardingLifecycleService::class)->applySnapshot($draft, false);
expect($changed)->toBeTrue()
->and($draft->version)->toBe(1)
->and($draft->lifecycle_state)->toBe(OnboardingLifecycleState::ActionRequired)
->and($draft->current_checkpoint)->toBe(OnboardingCheckpoint::VerifyAccess)
->and($draft->reason_code)->toBe('provider_connection_changed');
});
it('keeps linked archived tenants loaded while suppressing onboarding resume affordances', function (): void {
$tenant = Tenant::factory()->archived()->create();
$draft = createOnboardingDraft([
'workspace' => $tenant->workspace,
'tenant' => $tenant,
'state' => [
'entra_tenant_id' => (string) $tenant->tenant_id,
'tenant_name' => (string) $tenant->name,
],
]);
$draft = $draft->fresh()->load('tenant');
$service = app(OnboardingLifecycleService::class);
expect($draft->tenant)->toBeInstanceOf(Tenant::class)
->and($draft->tenant?->trashed())->toBeTrue()
->and($draft->isWorkflowResumable())->toBeTrue()
->and($service->canResumeDraft($draft))->toBeFalse();
});