TenantAtlas/tests/Feature/Filament/SettingsCatalogRestoreApplySettingsPatchTest.php
2025-12-14 20:23:18 +01:00

175 lines
5.7 KiB
PHP

<?php
use App\Models\BackupItem;
use App\Models\BackupSet;
use App\Models\Policy;
use App\Models\Tenant;
use App\Models\User;
use App\Services\Graph\GraphClientInterface;
use App\Services\Graph\GraphResponse;
use App\Services\Intune\RestoreService;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
test('settings catalog restore marks manual_required when a setting PATCH returns 404', function () {
$policyResponse = new GraphResponse(
success: true,
data: [],
status: 200,
errors: [],
warnings: [],
meta: ['request_id' => 'req-policy', 'client_request_id' => 'client-policy'],
);
$settingResponse = new GraphResponse(
success: false,
data: ['error' => ['code' => 'NotFound', 'message' => 'Setting missing']],
status: 404,
errors: [['code' => 'NotFound', 'message' => 'Setting missing']],
warnings: [],
meta: [
'error_code' => 'NotFound',
'error_message' => 'Setting missing',
'request_id' => 'req-setting-404',
'client_request_id' => 'client-setting-404',
],
);
$client = new class($policyResponse, $settingResponse) implements GraphClientInterface
{
/**
* @var array<int, array{policy_type:string,policy_id:string,payload:array}>
*/
public array $applyPolicyCalls = [];
/**
* @var array<int, array{method:string,path:string,payload:array|null}>
*/
public array $requestCalls = [];
public function __construct(
private readonly GraphResponse $policyResponse,
private readonly GraphResponse $settingResponse,
) {}
public function listPolicies(string $policyType, array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function getPolicy(string $policyType, string $policyId, array $options = []): GraphResponse
{
return new GraphResponse(true, ['payload' => []]);
}
public function getOrganization(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function applyPolicy(string $policyType, string $policyId, array $payload, array $options = []): GraphResponse
{
$this->applyPolicyCalls[] = [
'policy_type' => $policyType,
'policy_id' => $policyId,
'payload' => $payload,
];
return $this->policyResponse;
}
public function getServicePrincipalPermissions(array $options = []): GraphResponse
{
return new GraphResponse(true, []);
}
public function request(string $method, string $path, array $options = []): GraphResponse
{
$this->requestCalls[] = [
'method' => strtoupper($method),
'path' => $path,
'payload' => $options['json'] ?? null,
];
return $this->settingResponse;
}
};
app()->instance(GraphClientInterface::class, $client);
$tenant = Tenant::create([
'tenant_id' => 'tenant-3',
'name' => 'Tenant Three',
'metadata' => [],
]);
$policy = Policy::create([
'tenant_id' => $tenant->id,
'external_id' => 'scp-3',
'policy_type' => 'settingsCatalogPolicy',
'display_name' => 'Settings Catalog Gamma',
'platform' => 'windows',
]);
$backupSet = BackupSet::create([
'tenant_id' => $tenant->id,
'name' => 'Backup',
'status' => 'completed',
'item_count' => 1,
]);
$payload = [
'displayName' => 'Settings Catalog Gamma',
'Settings' => [
[
'id' => 'setting-404',
'settingInstance' => [
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationSimpleSettingInstance',
'settingDefinitionId' => 'setting_definition',
'simpleSettingValue' => [
'@odata.type' => '#microsoft.graph.deviceManagementConfigurationStringSettingValue',
'value' => 'test-value',
],
],
],
],
];
$backupItem = BackupItem::create([
'tenant_id' => $tenant->id,
'backup_set_id' => $backupSet->id,
'policy_id' => $policy->id,
'policy_identifier' => $policy->external_id,
'policy_type' => $policy->policy_type,
'platform' => $policy->platform,
'payload' => $payload,
]);
$user = User::factory()->create();
$this->actingAs($user);
$service = app(RestoreService::class);
$run = $service->execute(
tenant: $tenant,
backupSet: $backupSet,
selectedItemIds: [$backupItem->id],
dryRun: false,
actorEmail: $user->email,
actorName: $user->name,
)->refresh();
expect($run->status)->toBe('partial');
expect($run->results[0]['status'])->toBe('manual_required');
expect($run->results[0]['settings_apply']['manual_required'])->toBe(1);
expect($run->results[0]['settings_apply']['issues'][0]['graph_request_id'])->toBe('req-setting-404');
expect($client->applyPolicyCalls[0]['payload'])->not->toHaveKey('settings');
expect($client->requestCalls[0]['path'])->toBe('deviceManagement/configurationPolicies/scp-3/settings/setting-404');
$response = $this->get(route('filament.admin.resources.restore-runs.view', ['record' => $run]));
$response->assertOk();
$response->assertSee('Setting not found on target policy (404).');
$response->assertSee('req-setting-404');
});