TenantAtlas/apps/platform/tests/Feature/Filament/SettingsCatalogPolicySyncTest.php
ahmido 1655cc481e Spec 188: canonical provider connection state cleanup (#219)
## Summary
- migrate provider connections to the canonical three-dimension state model: lifecycle via `is_enabled`, consent via `consent_status`, and verification via `verification_status`
- remove legacy provider status and health badge paths, update admin and system directory surfaces, and align onboarding, consent callback, verification, resolver, and mutation flows with the new model
- add the Spec 188 artifact set, schema migrations, guard coverage, and expanded provider-state tests across admin, system, onboarding, verification, and rendering paths

## Verification
- `cd apps/platform && ./vendor/bin/sail bin pint --dirty --format agent`
- `cd apps/platform && ./vendor/bin/sail artisan test --compact tests/Feature/Auth/SystemPanelAuthTest.php tests/Feature/Filament/TenantGlobalSearchLifecycleScopeTest.php tests/Feature/ProviderConnections/ProviderConnectionEnableDisableTest.php tests/Feature/ProviderConnections/ProviderConnectionTruthCleanupSpec179Test.php`
- integrated browser smoke: validated admin provider list/detail/edit, tenant provider summary, system directory tenant detail, provider-connection search exclusion, and cleaned up the temporary smoke record afterward

## Filament / implementation notes
- Livewire v4.0+ compliance: preserved; this change targets Filament v5 on Livewire v4 and does not introduce older APIs
- Provider registration location: unchanged; Laravel 11+ panel providers remain registered in `bootstrap/providers.php`
- Globally searchable resources: `ProviderConnectionResource` remains intentionally excluded from global search; tenant global search remains enabled and continues to resolve to view pages
- Destructive actions: no new destructive action surface was introduced without confirmation or authorization; existing capability checks continue to gate provider mutations
- Asset strategy: unchanged; no new Filament assets were added, so deploy behavior for `php artisan filament:assets` remains unchanged
- Testing plan covered: system auth, tenant global search, provider lifecycle enable/disable behavior, and provider truth cleanup cutover behavior

Co-authored-by: Ahmed Darrazi <ahmed.darrazi@live.de>
Reviewed-on: #219
2026-04-10 11:22:56 +00:00

147 lines
4.8 KiB
PHP

<?php
use App\Models\Policy;
use App\Models\PolicyVersion;
use App\Models\ProviderConnection;
use App\Models\ProviderCredential;
use App\Models\Tenant;
use App\Models\Workspace;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\PolicySyncService;
use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
class SettingsCatalogFakeGraphClient implements GraphClientInterface
{
/**
* @param array<string, GraphResponse> $responses
*/
public function __construct(private array $responses = []) {}
public function listPolicies(string $policyType, array $options = []): GraphResponse
{
return $this->responses[$policyType] ?? new GraphResponse(true, []);
}
public function getPolicy(string $policyType, string $policyId, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function getOrganization(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function applyPolicy(string $policyType, string $policyId, array $payload, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function getServicePrincipalPermissions(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function request(string $method, string $path, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
}
test('settings catalog policies sync as their own type and render in the list', function () {
$responses = [
'deviceConfiguration' => new GraphResponse(true, [
[
'id' => 'config-1',
'displayName' => 'Device Config 1',
'platform' => 'windows',
],
]),
'settingsCatalogPolicy' => new GraphResponse(true, [
[
'id' => 'scp-1',
'name' => 'Settings Catalog Alpha',
'platform' => 'windows',
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationPolicy',
'settings' => [
['displayName' => 'Setting A', 'value' => 'on'],
],
],
]),
];
app()->instance(GraphClientInterface::class, new SettingsCatalogFakeGraphClient($responses));
$tenant = Tenant::create([
'tenant_id' => 'local-tenant',
'name' => 'Tenant One',
'metadata' => [],
'is_current' => true,
]);
$tenant->makeCurrent();
expect(Tenant::current()->id)->toBe($tenant->id);
$workspace = Workspace::factory()->create();
$tenant->forceFill(['workspace_id' => (int) $workspace->getKey()])->save();
$connection = ProviderConnection::factory()->consentGranted()->create([
'tenant_id' => (int) $tenant->getKey(),
'workspace_id' => (int) $workspace->getKey(),
'provider' => 'microsoft',
'entra_tenant_id' => $tenant->tenant_id,
'is_default' => true,
'consent_status' => 'granted',
]);
ProviderCredential::factory()->create([
'provider_connection_id' => (int) $connection->getKey(),
'type' => 'client_secret',
'payload' => [
'client_id' => 'test-client-id',
'client_secret' => 'test-client-secret',
],
]);
app(PolicySyncService::class)->syncPolicies($tenant);
$settingsPolicy = Policy::where('policy_type', 'settingsCatalogPolicy')->first();
expect($settingsPolicy)->not->toBeNull();
expect($settingsPolicy->display_name)->toBe('Settings Catalog Alpha');
expect($settingsPolicy->platform)->toBe('windows');
expect(Policy::where('policy_type', 'deviceConfiguration')->count())->toBe(1);
expect(Policy::where('policy_type', 'deviceConfiguration')->where('external_id', 'scp-1')->exists())->toBeFalse();
PolicyVersion::create([
'tenant_id' => $tenant->id,
'policy_id' => $settingsPolicy->id,
'version_number' => 1,
'policy_type' => $settingsPolicy->policy_type,
'platform' => $settingsPolicy->platform,
'created_by' => 'tester@example.com',
'captured_at' => CarbonImmutable::now(),
'snapshot' => [
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationPolicy',
'settings' => [
['displayName' => 'Setting A', 'value' => 'on'],
],
],
]);
[$user, $tenant] = createUserWithTenant($tenant, role: 'owner');
$response = $this
->actingAs($user)
->get(route('filament.tenant.resources.policies.index', filamentTenantRouteParams($tenant)));
$response->assertOk();
$response->assertSee('Settings Catalog Policy');
$response->assertSee('Settings Catalog Alpha');
$response->assertSee('Available');
});