TenantAtlas/apps/platform/tests/Unit/ManagedEnvironment/ManagedEnvironmentModelTest.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

90 lines
3.4 KiB
PHP

<?php
use App\Models\ManagedEnvironment;
use App\Models\ProviderConnection;
use App\Models\Workspace;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
uses(RefreshDatabase::class);
it('owns workspace relation, slug route binding, and selectable lifecycle truth', function (): void {
$workspace = Workspace::factory()->create();
$environment = ManagedEnvironment::factory()->create([
'workspace_id' => (int) $workspace->getKey(),
'name' => 'Contoso Production',
'display_name' => 'Contoso Prod',
'slug' => 'contoso-prod',
'kind' => 'production',
'lifecycle_status' => ManagedEnvironment::STATUS_ACTIVE,
]);
expect($workspace->managedEnvironments()->whereKey($environment->getKey())->exists())->toBeTrue()
->and($workspace->tenants()->whereKey($environment->getKey())->exists())->toBeTrue()
->and($environment->workspace->is($workspace))->toBeTrue()
->and($environment->getRouteKeyName())->toBe('slug')
->and((new ManagedEnvironment)->resolveRouteBinding('contoso-prod')->is($environment))->toBeTrue()
->and($environment->isSelectableAsContext())->toBeTrue()
->and($environment->getFilamentName())->toBe('Contoso Prod (PRODUCTION)');
});
it('keeps archived environments resolvable by route key but unavailable as active context', function (): void {
$environment = ManagedEnvironment::factory()->archived()->create([
'slug' => 'archived-environment',
]);
$resolved = (new ManagedEnvironment)->resolveRouteBinding('archived-environment');
expect($resolved)->toBeInstanceOf(ManagedEnvironment::class)
->and($resolved->is($environment))->toBeTrue()
->and($resolved->isSelectableAsContext())->toBeFalse();
});
it('keeps provider-specific identity off managed environment columns', function (): void {
$columns = Schema::getColumnListing('managed_environments');
expect($columns)->not->toContain(
'external_id',
'tenant_id',
'managed_environment_id',
'status',
'environment',
'app_client_id',
'app_client_secret',
'app_certificate_thumbprint',
'app_notes',
'app_status',
'domain',
'entra_tenant_id',
'graph_tenant_id',
);
});
it('resolves Microsoft tenant identity through provider-owned seams only', function (): void {
$environment = ManagedEnvironment::factory()->create([
'slug' => 'provider-neutral-root',
]);
ProviderConnection::factory()->platform()->consentGranted()->create([
'managed_environment_id' => (int) $environment->getKey(),
'workspace_id' => (int) $environment->workspace_id,
'provider' => 'microsoft',
'entra_tenant_id' => '11111111-1111-1111-1111-111111111111',
'is_default' => true,
]);
expect($environment->refresh()->graphTenantId())->toBe('11111111-1111-1111-1111-111111111111')
->and(fn () => $environment->graphOptions())->toThrow(BadMethodCallException::class);
});
it('treats managed_environment_id as a write-through alias for slug', function (): void {
$environment = ManagedEnvironment::factory()->create([
'managed_environment_id' => 'alias-slug',
]);
expect($environment->slug)->toBe('alias-slug')
->and($environment->managed_environment_id)->toBe('alias-slug')
->and($environment->external_id)->toBe('alias-slug');
});