feat(custom-fields) : autocomplete des noms + corrections formule de référence auto #3

Merged
matthieu merged 12 commits from feat/custom-field-name-autocomplete into develop 2026-05-11 14:25:15 +00:00
3 changed files with 120 additions and 0 deletions
Showing only changes of commit 9027917ea2 - Show all commits

View File

@@ -69,3 +69,8 @@ when@test:
autowire: true
autoconfigure: true
public: true
App\Service\SkeletonStructureService:
autowire: true
autoconfigure: true
public: true

View File

@@ -226,6 +226,13 @@ class SkeletonStructureService
}
if ($existingField) {
// Propagate rename to the parent ModelType's reference formula and required-fields list
// so existing `{oldName}` placeholders keep resolving after the field is renamed.
$oldName = $existingField->getName();
if ($oldName !== $normalized['name']) {
$this->propagateCustomFieldRename($modelType, $oldName, $normalized['name']);
}
// Update existing field
$existingField->setName($normalized['name']);
$existingField->setType($normalized['type']);
@@ -264,6 +271,38 @@ class SkeletonStructureService
}
}
private function propagateCustomFieldRename(ModelType $modelType, string $oldName, string $newName): void
{
$formula = $modelType->getReferenceFormula();
if (null !== $formula && '' !== $formula) {
$newFormula = preg_replace(
'/\{'.preg_quote($oldName, '/').'\}/',
'{'.$newName.'}',
$formula
);
if (null !== $newFormula && $newFormula !== $formula) {
$modelType->setReferenceFormula($newFormula);
}
}
$required = $modelType->getRequiredFieldsForReference();
if ($required) {
$changed = false;
$newRequired = [];
foreach ($required as $fieldName) {
if ($fieldName === $oldName) {
$newRequired[] = $newName;
$changed = true;
} else {
$newRequired[] = $fieldName;
}
}
if ($changed) {
$modelType->setRequiredFieldsForReference($newRequired);
}
}
}
/**
* Normalize frontend custom field data to a common shape.
*

View File

@@ -0,0 +1,76 @@
<?php
declare(strict_types=1);
namespace App\Tests\Service;
use App\Enum\ModelCategory;
use App\Service\SkeletonStructureService;
use App\Tests\AbstractApiTestCase;
/**
* @internal
*/
class SkeletonStructureServiceTest extends AbstractApiTestCase
{
public function testRenameCustomFieldPropagatesToReferenceFormulaAndRequiredFields(): void
{
$mt = $this->createModelType('Roulement', 'ROUL-RENAME', ModelCategory::PIECE);
$mt->setReferenceFormula('{material}-{size}');
$mt->setRequiredFieldsForReference(['material', 'size']);
$em = $this->getEntityManager();
$em->flush();
$cfMaterial = $this->createCustomField('material', 'text', typePiece: $mt, orderIndex: 0);
$cfSize = $this->createCustomField('size', 'text', typePiece: $mt, orderIndex: 1);
/** @var SkeletonStructureService $service */
$service = static::getContainer()->get(SkeletonStructureService::class);
// Same fields, but `material` is renamed to `materiau` (matched by customFieldId)
$service->updateSkeletonRequirements($mt, [
'customFields' => [
['customFieldId' => $cfMaterial->getId(), 'name' => 'materiau', 'type' => 'text', 'orderIndex' => 0],
['customFieldId' => $cfSize->getId(), 'name' => 'size', 'type' => 'text', 'orderIndex' => 1],
],
]);
$em->flush();
$em->refresh($mt);
$em->refresh($cfMaterial);
self::assertSame('materiau', $cfMaterial->getName());
self::assertSame('{materiau}-{size}', $mt->getReferenceFormula());
self::assertSame(['materiau', 'size'], $mt->getRequiredFieldsForReference());
}
public function testRenameLeavesFormulaUnchangedWhenFieldNotInFormula(): void
{
$mt = $this->createModelType('Roulement2', 'ROUL-RENAME2', ModelCategory::PIECE);
$mt->setReferenceFormula('{material}');
$mt->setRequiredFieldsForReference(['material']);
$em = $this->getEntityManager();
$em->flush();
$cfMaterial = $this->createCustomField('material', 'text', typePiece: $mt, orderIndex: 0);
$cfUnused = $this->createCustomField('unused', 'text', typePiece: $mt, orderIndex: 1);
/** @var SkeletonStructureService $service */
$service = static::getContainer()->get(SkeletonStructureService::class);
$service->updateSkeletonRequirements($mt, [
'customFields' => [
['customFieldId' => $cfMaterial->getId(), 'name' => 'material', 'type' => 'text', 'orderIndex' => 0],
['customFieldId' => $cfUnused->getId(), 'name' => 'renamed', 'type' => 'text', 'orderIndex' => 1],
],
]);
$em->flush();
$em->refresh($mt);
self::assertSame('{material}', $mt->getReferenceFormula());
self::assertSame(['material'], $mt->getRequiredFieldsForReference());
}
}