TenantAtlas/tests/Feature/OpsUx/TenantSyncBulkJobTest.php
ahmido 440e63edff feat: implement tenant action taxonomy lifecycle visibility (#174)
## Summary

Implements Spec 145 for tenant action taxonomy and lifecycle-safe visibility.

This PR:
- adds a central tenant action policy surface and supporting value objects
- aligns tenant list, detail, edit, onboarding, and widget surfaces around lifecycle-safe actions
- standardizes operator-facing lifecycle wording around View, Resume onboarding, Archive, Restore, and Complete onboarding
- tightens onboarding and tenant lifecycle authorization semantics, including honest 404 vs 403 behavior
- updates related regression coverage and spec artifacts for Spec 145
- fixes follow-on full-suite regressions uncovered during validation, including onboarding browser flows, provider consent fixtures, workspace redirect DI expectations, and critical table/action/UI expectation drift

## Validation

Executed and passed:
- vendor/bin/sail bin pint --dirty --format agent
- vendor/bin/sail artisan test --compact

Result:
- 2581 passed
- 8 skipped
- 13534 assertions

## Notes

- Base branch: dev
- Feature branch commit: a33a41b
- Filament v5 / Livewire v4 compliance preserved
- No panel provider registration changes; Laravel 12 provider registration remains in bootstrap/providers.php
- No new globally searchable resource behavior added in this slice
- Destructive lifecycle actions remain confirmation-gated and authorization-protected

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #174
2026-03-16 00:57:17 +00:00

166 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
use App\Jobs\BulkTenantSyncJob;
use App\Jobs\Operations\TenantSyncWorkerJob;
use App\Models\OperationRun;
use App\Models\Tenant;
use App\Services\Intune\PolicySyncService;
use App\Services\OperationRunService;
use App\Services\Operations\TargetScopeConcurrencyLimiter;
use Illuminate\Contracts\Cache\Lock;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use function Pest\Laravel\mock;
it('dispatches tenant sync workers and sets total', function (): void {
[$user, $tenantContext] = createUserWithTenant(role: 'owner');
$run = OperationRun::factory()->create([
'tenant_id' => $tenantContext->id,
'user_id' => $user->id,
'type' => 'tenant.sync',
'status' => 'queued',
'summary_counts' => [],
'context' => [
'target_scope' => [
'entra_tenant_id' => (string) ($tenantContext->tenant_id ?? $tenantContext->external_id),
],
],
]);
Bus::fake();
$job = new BulkTenantSyncJob(
tenantId: (int) $tenantContext->getKey(),
userId: (int) $user->getKey(),
tenantIds: [3, 2, 1],
operationRun: $run,
context: [],
);
$job->handle(app(OperationRunService::class));
$run = $run->fresh();
expect($run)->not->toBeNull();
expect($run?->status)->toBe('running');
expect($run?->summary_counts['total'] ?? null)->toBe(3);
Bus::assertDispatched(TenantSyncWorkerJob::class, 3);
})->group('ops-ux');
it('syncs eligible tenants and updates summary counts', function (): void {
[$user, $tenantContext] = createUserWithTenant(role: 'owner');
$eligible = Tenant::factory()->create([
'status' => 'active',
'deleted_at' => null,
]);
$inactive = Tenant::factory()->create([
'status' => Tenant::STATUS_ARCHIVED,
'deleted_at' => null,
]);
$unauthorized = Tenant::factory()->create([
'status' => 'active',
'deleted_at' => null,
]);
$user->tenants()->syncWithoutDetaching([
$eligible->getKey() => ['role' => 'owner'],
$inactive->getKey() => ['role' => 'owner'],
]);
mock(PolicySyncService::class)
->shouldReceive('syncPolicies')
->andReturn([]);
$lock = new class implements Lock
{
public function get($callback = null): bool
{
return true;
}
public function block($seconds, $callback = null): bool
{
return true;
}
public function release(): bool
{
return true;
}
public function owner(): string
{
return 'test';
}
public function forceRelease(): void
{
// no-op
}
};
Cache::partialMock()
->shouldReceive('lock')
->andReturn($lock);
$run = OperationRun::factory()->create([
'tenant_id' => $tenantContext->id,
'user_id' => $user->id,
'type' => 'tenant.sync',
'status' => 'running',
'summary_counts' => [
'total' => 4,
'processed' => 0,
'failed' => 0,
],
'context' => [
'target_scope' => [
'entra_tenant_id' => (string) ($tenantContext->tenant_id ?? $tenantContext->external_id),
],
],
]);
(new TenantSyncWorkerJob(
tenantId: (int) $eligible->getKey(),
userId: (int) $user->getKey(),
operationRun: $run,
))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class), app(PolicySyncService::class));
(new TenantSyncWorkerJob(
tenantId: (int) $inactive->getKey(),
userId: (int) $user->getKey(),
operationRun: $run,
))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class), app(PolicySyncService::class));
(new TenantSyncWorkerJob(
tenantId: (int) $unauthorized->getKey(),
userId: (int) $user->getKey(),
operationRun: $run,
))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class), app(PolicySyncService::class));
(new TenantSyncWorkerJob(
tenantId: 999999,
userId: (int) $user->getKey(),
operationRun: $run,
))->handle(app(OperationRunService::class), app(TargetScopeConcurrencyLimiter::class), app(PolicySyncService::class));
$run = $run->fresh();
expect($run)->not->toBeNull();
expect($run?->status)->toBe('completed');
expect($run?->outcome)->toBe('partially_succeeded');
expect($run?->summary_counts['processed'] ?? null)->toBe(4);
expect($run?->summary_counts['succeeded'] ?? null)->toBe(1);
expect($run?->summary_counts['skipped'] ?? null)->toBe(2);
expect($run?->summary_counts['failed'] ?? null)->toBe(1);
})->group('ops-ux');