TenantAtlas/apps/platform/tests/Unit/BulkPolicyExportJobTest.php
ahmido e64bae9cfc feat: cut over tenant core to managed environments (#335)
## Summary
- replace the legacy Tenant and TenantMembership core models with ManagedEnvironment and ManagedEnvironmentMembership
- propagate the managed environment naming and key changes across Filament resources, pages, controllers, jobs, models, and supporting runtime paths
- add feature 279 spec artifacts and focused managed-environment test coverage for model behavior, route binding, panel context, authorization, and legacy guardrails

## Validation
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/ManagedEnvironment/LegacyTenantCoreGuardTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentAuthorizationTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentPanelContextTest.php tests/Feature/ManagedEnvironment/ManagedEnvironmentRouteBindingTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentContextResolverTest.php tests/Unit/ManagedEnvironment/ManagedEnvironmentModelTest.php`
- `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent`

## Notes
- branch pushed from commit `1123b122`
- browser smoke test file was added but not run in this pass

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #335
2026-05-07 06:38:14 +00:00

120 lines
4.2 KiB
PHP

<?php
use App\Jobs\BulkPolicyExportJob;
use App\Models\BackupItem;
use App\Models\BackupSet;
use App\Models\OperationRun;
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\ManagedEnvironment;
use App\Models\User;
use App\Services\OperationRunService;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('job processes bulk export successfully', function () {
$tenant = ManagedEnvironment::factory()->create();
$user = User::factory()->create();
$policies = Policy::factory()->count(3)->create(['managed_environment_id' => $tenant->id]);
// Create versions for policies so they can be backed up
$policies->each(function ($policy) use ($tenant) {
PolicyVersion::create([
'managed_environment_id' => $tenant->id,
'policy_id' => $policy->id,
'policy_type' => $policy->policy_type,
'version_number' => 1,
'snapshot' => ['name' => $policy->display_name],
'captured_at' => now(),
]);
});
$policyIds = $policies->pluck('id')->toArray();
$run = OperationRun::factory()->create([
'managed_environment_id' => $tenant->getKey(),
'user_id' => $user->getKey(),
'initiator_name' => $user->name,
'type' => 'policy.export',
'status' => 'queued',
'outcome' => 'pending',
'context' => ['scope' => 'subset', 'policy_ids' => $policyIds],
'summary_counts' => [],
'failure_summary' => [],
]);
$job = new BulkPolicyExportJob(
tenantId: (int) $tenant->getKey(),
userId: (int) $user->getKey(),
policyIds: $policyIds,
backupName: 'My Bulk Backup',
operationRun: $run,
);
$job->handle(app(OperationRunService::class));
$run->refresh();
expect($run->status)->toBe('completed')
->and($run->outcome)->toBe('succeeded')
->and((int) ($run->summary_counts['processed'] ?? 0))->toBe(3)
->and((int) ($run->summary_counts['succeeded'] ?? 0))->toBe(3);
// Verify BackupSet created
$backupSet = BackupSet::where('name', 'My Bulk Backup')->first();
expect($backupSet)->not->toBeNull()
->and($backupSet->managed_environment_id)->toBe($tenant->id);
// Verify BackupItems created
expect(BackupItem::where('backup_set_id', $backupSet->id)->count())->toBe(3);
});
test('job handles policies without versions gracefully', function () {
$tenant = ManagedEnvironment::factory()->create();
$user = User::factory()->create();
$policyWithVersion = Policy::factory()->create(['managed_environment_id' => $tenant->id]);
PolicyVersion::create([
'managed_environment_id' => $tenant->id,
'policy_id' => $policyWithVersion->id,
'policy_type' => $policyWithVersion->policy_type,
'version_number' => 1,
'snapshot' => ['name' => 'ok'],
'captured_at' => now(),
]);
$policyNoVersion = Policy::factory()->create(['managed_environment_id' => $tenant->id]);
$policyIds = [$policyWithVersion->id, $policyNoVersion->id];
$run = OperationRun::factory()->create([
'managed_environment_id' => $tenant->getKey(),
'user_id' => $user->getKey(),
'initiator_name' => $user->name,
'type' => 'policy.export',
'status' => 'queued',
'outcome' => 'pending',
'context' => ['scope' => 'subset', 'policy_ids' => $policyIds],
'summary_counts' => [],
'failure_summary' => [],
]);
$job = new BulkPolicyExportJob(
tenantId: (int) $tenant->getKey(),
userId: (int) $user->getKey(),
policyIds: $policyIds,
backupName: 'Partial Backup',
operationRun: $run,
);
$job->handle(app(OperationRunService::class));
$run->refresh();
expect($run->status)->toBe('completed');
expect($run->outcome)->toBe('partially_succeeded');
expect((int) ($run->summary_counts['processed'] ?? 0))->toBe(2);
expect((int) ($run->summary_counts['succeeded'] ?? 0))->toBe(1);
expect((int) ($run->summary_counts['failed'] ?? 0))->toBe(1);
$failure = collect($run->failure_summary ?? [])->first(fn ($entry) => is_array($entry) && ($entry['code'] ?? null) === 'policy.no_versions');
expect($failure)->not->toBeNull();
});