lockForUpdate()') && str_contains($serviceSource, 'hash_equals($currentName, $name)') && str_contains($serviceSource, 'hash_equals($currentName, $initialName)'), 'Display-name persistence lost its compare-and-swap concurrency guard.'); $expectError = static function (callable $callback, int $status, string $message) use ($assert): void { try { $callback(); } catch (CloudError $error) { $assert($error->httpStatus === $status, $message . ' Unexpected status: ' . $error->httpStatus); return; } throw new RuntimeException($message); }; $capsule = new Capsule(); $capsule->addConnection(['driver' => 'sqlite', 'database' => ':memory:'], 'default'); $capsule->setAsGlobal(); $capsule->bootEloquent(); $capsule->schema('default')->create('cloud_instances', static function (Blueprint $table): void { $table->increments('id'); $table->string('uuid'); $table->integer('user_id'); $table->string('name'); $table->string('status'); $table->text('spec_json'); $table->integer('updated_at'); }); $rows = [ ['id' => 1, 'uuid' => '11111111-1111-4111-8111-111111111111', 'user_id' => 1, 'name' => 'vm-owner1', 'status' => 'active', 'spec_json' => '{"resource_name":"vc-immutable-owner"}', 'updated_at' => 10], ['id' => 2, 'uuid' => '22222222-2222-4222-8222-222222222222', 'user_id' => 2, 'name' => 'vm-owner2', 'status' => 'active', 'spec_json' => '{"resource_name":"vc-immutable-foreign"}', 'updated_at' => 20], ['id' => 3, 'uuid' => '33333333-3333-4333-8333-333333333333', 'user_id' => 1, 'name' => 'vm-deleting', 'status' => 'deleting', 'spec_json' => '{}', 'updated_at' => 30], ['id' => 4, 'uuid' => '44444444-4444-4444-8444-444444444444', 'user_id' => 1, 'name' => 'vm-deleted', 'status' => 'deleted', 'spec_json' => '{}', 'updated_at' => 40], ]; Capsule::table('cloud_instances')->insert($rows); $owner = ['user_id' => 1, 'is_admin' => false, 'is_super_admin' => false, 'can_manage' => false]; $readonlyAdmin = ['user_id' => 1, 'is_admin' => true, 'is_super_admin' => false, 'can_manage' => false]; $global = ['user_id' => 99, 'is_admin' => true, 'is_super_admin' => true, 'can_manage' => true]; $result = CloudInstanceNameService::rename(1, $owner, 'Prod.Web_01'); $renamed = Capsule::table('cloud_instances')->where('id', 1)->first(); $assert($result === ['id' => '1', 'name' => 'Prod.Web_01'] && $renamed->name === 'Prod.Web_01' && $renamed->uuid === $rows[0]['uuid'] && $renamed->spec_json === $rows[0]['spec_json'] && $renamed->status === 'active' && (int) $renamed->updated_at > 10, 'Owner rename did not persist only the panel display identity.'); $sameTimestamp = (int) $renamed->updated_at; $assert(CloudInstanceNameService::rename(1, $owner, 'Prod.Web_01')['name'] === 'Prod.Web_01' && (int) Capsule::table('cloud_instances')->where('id', 1)->value('updated_at') === $sameTimestamp, 'Idempotent rename rewrote the instance row.'); foreach ([null, '', '-bad', 'space name', str_repeat('a', 64), 'bad/segment'] as $invalid) { $expectError(static fn () => CloudInstanceNameService::rename(1, $owner, $invalid), 422, 'Invalid display name was accepted.'); } $apiRequest = Request::createFromEnvironment(Environment::mock([ 'REQUEST_METHOD' => 'PATCH', 'REQUEST_URI' => '/api/v1/vms/1/display-name', ]))->withParsedBody(['name' => 'Api-Label_02']) ->withAttribute('cloud_actor', $owner); $apiResponse = (new CloudApiController())->displayName( $apiRequest, new Response(), ['id' => '1'] ); $apiPayload = json_decode((string) $apiResponse->getBody(), true); $assert($apiResponse->getStatusCode() === 200 && $apiResponse->getHeaderLine('Cache-Control') === 'no-store' && $apiPayload === ['id' => '1', 'name' => 'Api-Label_02'] && Capsule::table('cloud_instances')->where('id', 1)->value('name') === 'Api-Label_02', 'Display-name controller did not return a non-cacheable bounded response.'); $extraFieldRequest = $apiRequest->withParsedBody([ 'name' => 'Rejected', 'uuid' => $rows[0]['uuid'], ]); $expectError(static fn () => (new CloudApiController())->displayName( $extraFieldRequest, new Response(), ['id' => '1'] ), 422, 'Display-name endpoint accepted an extra mutable field.'); $expectError(static fn () => CloudInstanceNameService::rename(2, $owner, 'cross-tenant'), 404, 'Ordinary owner renamed another tenant instance.'); $expectError(static fn () => CloudInstanceNameService::rename(2, $readonlyAdmin, 'cross-tenant'), 404, 'Read-only administrator inherited global rename permission.'); $expectError(static fn () => CloudInstanceNameService::rename(3, $owner, 'too-late'), 409, 'Deleting instance accepted a display-name mutation.'); $expectError(static fn () => CloudInstanceNameService::rename(4, $owner, 'resurrected'), 404, 'Deleted instance accepted a display-name mutation.'); $globalResult = CloudInstanceNameService::rename(2, $global, 'Prod.Web_01'); $assert($globalResult['name'] === 'Prod.Web_01' && Capsule::table('cloud_instances')->where('id', 2)->value('name') === 'Prod.Web_01', 'Super administrator rename failed or an unsupported uniqueness rule was introduced.'); echo "Cloud instance display-name ownership and persistence passed.\n";