feat(commercial) : messages de validation FR sur les contraintes back + garde-fou (ERP-107) (#59)
Auto Tag Develop / tag (push) Successful in 8s
Auto Tag Develop / tag (push) Successful in 8s
## Contexte Résout **ERP-107** — pendant back du mapping d'erreur par champ front (ERP-101). Le front (`useFormErrors` / `mapViolationsToRecord`) affiche sous chaque champ le `message` renvoyé par le back. Ce ticket garantit que ces messages existent, sont en FR et rattachés au bon champ. ## Changements - **Messages FR explicites** sur toutes les contraintes `#[Assert\*]` des entités métier : `Client`, `ClientContact`, `ClientAddress`, `ClientRib`, `Category`, `Role`, `User` (Email, NotBlank, Length, Bic, Iban, PositiveOrZero, Count…). - **Contraintes `Assert\Length` manquantes ajoutées**, calées sur le `length` de la colonne ORM (téléphones `VARCHAR(20)`, `siren`, `nTva`, `accountNumber`, `username`…). Évite une erreur Postgres 500 non rattachée au champ → 422 propre. - **Locale FR globale** (`symfony/translation` + `default_locale: fr`) comme filet pour les messages natifs Symfony non surchargés. - **Garde-fou** `tests/Architecture/EntityConstraintsHaveFrenchMessageTest` : échoue si une contrainte n'a pas de message FR explicite (comparaison au défaut Symfony) ou si `Assert\Length.max` diverge du `length` ORM. Whitelist justifiée pour les formats auto-bornés (Bic/Iban/Regex CP/couleur hex). - **Test fonctionnel** du JSON 422 réel : message FR + `propertyPath` consommable par le front. - **Convention documentée** dans `.claude/rules/backend.md`. ## Décisions - Stratégie retenue : message FR **explicite sur toutes** les contraintes + locale FR en filet (les deux leviers du ticket). - Garde-fou `Length == ORM length` : **test bloquant** (anti-dérive). - RG-1.03 (distributor/broker) : pas de `Assert\Callback` ajouté — le `ClientProcessor` gère **déjà** l'exclusivité (422 + `propertyPath`). Pas de doublon. ## Hors périmètre / à suivre - **Alignement `nullable`(DB) / `NotBlank`(back) / `required`(front)** : les champs obligatoires existants ont été confirmés, mais aucun changement de nullabilité DB n'a été fait sans arbitrage métier. À recroiser avec les astérisques front (ERP-101 / PR #58) si divergence constatée. ## Vérifications - `make test` : **469 tests verts** (1793 assertions), 0 échec/erreur. - `php-cs-fixer` : 0 fichier à corriger. --------- Co-authored-by: admin malio <malio@yuno.malio.fr> Co-authored-by: Matthieu <contact@malio.fr> Reviewed-on: #59 Co-authored-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr> Co-committed-by: THOLOT DECHENE Matthieu <matthieu@yuno.malio.fr>
This commit was merged in pull request #59.
This commit is contained in:
@@ -6,6 +6,42 @@
|
||||
- PHP CS Fixer : regles Symfony + PSR-12 + strict types (commande : `make php-cs-fixer-allow-risky`)
|
||||
- Commentaires (docblock, inline, bloc) **en francais** ; code (classes, methodes, variables) en anglais
|
||||
|
||||
## Messages de validation (obligatoire)
|
||||
|
||||
**Toute contrainte `#[Assert\*]` portee par une entite metier doit avoir un message FR explicite**, et **`Assert\Length.max` doit refleter le `length` de la colonne ORM**. Pendant logique back de la regle de mapping d'erreur par champ cote front (ERP-101 : `useFormErrors` / `mapViolationsToRecord` affiche sous chaque champ le `message` renvoye par le back).
|
||||
|
||||
Pourquoi :
|
||||
- Sans `message:` explicite, Symfony renvoie le defaut **anglais** (« This value is not a valid email address. »). La locale FR globale (`default_locale: fr` dans `framework.yaml`) sert de FILET via `validators.fr.xlf`, mais les contraintes metier portent en plus leur message FR pour un controle total.
|
||||
- Une colonne string bornee **sans `Assert\Length`** echoue au niveau Postgres (500 generique, non rattachee au champ) au lieu d'une 422 propre. Le `max` doit egaler le `length` ORM (anti-derive).
|
||||
|
||||
Pattern par champ scalaire :
|
||||
|
||||
```php
|
||||
// Email metier
|
||||
#[Assert\Email(message: 'L\'adresse email n\'est pas valide.')]
|
||||
|
||||
// Longueur calee sur la colonne (VARCHAR(120))
|
||||
#[ORM\Column(length: 120)]
|
||||
#[Assert\Length(max: 120, maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
|
||||
// Obligatoire (aligner nullable DB / NotBlank back / required front)
|
||||
#[Assert\NotBlank(message: 'Le téléphone est obligatoire.', normalizer: 'trim')]
|
||||
```
|
||||
|
||||
Coherence a 3 niveaux pour un champ obligatoire : colonne `nullable` (DB) <-> `Assert\NotBlank` (back) <-> `:required` + asterisque (front ERP-101). Les trois doivent s'accorder.
|
||||
|
||||
Exceptions au miroir `Length` : un format deja borne par `Assert\Bic` / `Assert\Iban` (longueur garantie) ou par un `Assert\Regex` borne (ex. code postal `{4,5}`, couleur hex `#RRGGBB`) — whitelister alors la propriete dans `EntityConstraintsHaveFrenchMessageTest::EXCLUDED_LENGTH_MIRROR` avec justification.
|
||||
|
||||
Les regles inter-champs (RG metier : exclusivite distributor/broker RG-1.03, billingEmail RG-1.11, etc.) passent par un `#[Assert\Callback]` qui construit la violation avec `->atPath('<champ>')` — indispensable pour que le front la mappe en inline plutot qu'en toast.
|
||||
|
||||
### Garde-fou architecture
|
||||
|
||||
`tests/Architecture/EntityConstraintsHaveFrenchMessageTest` scanne reflexivement les entites sous `src/Module/*/Domain/Entity/` et echoue si :
|
||||
1. une contrainte connue n'a pas de message FR explicite (compare au defaut Symfony) ;
|
||||
2. une colonne string bornee writable n'a pas de `Assert\Length(max == ORM length)` (hors whitelist).
|
||||
|
||||
Une contrainte non geree par le mapping du test le fait echouer : il faut l'ajouter explicitement (anti faux positif vert).
|
||||
|
||||
## API Platform (pas de controllers)
|
||||
|
||||
- Toujours utiliser `#[ApiResource]` + Providers + Processors — pas de controllers Symfony classiques
|
||||
|
||||
@@ -142,6 +142,18 @@ A NE PAS faire :
|
||||
- Seuls les deep links "de navigation metier" (ex: ouvrir un detail precis `/users/42`) sont dans l'URL
|
||||
- Exceptions autorisees **sur demande explicite** de l'utilisateur
|
||||
|
||||
## Validation des formulaires (standard ERP-101)
|
||||
|
||||
Regle transverse a TOUS les formulaires front (et a rappeler a l'ecriture de chaque ticket back/front portant un formulaire). Decidee en ERP-101 (declencheur : ecran « Ajouter un client » ERP-63).
|
||||
|
||||
- **Champs obligatoires** : prop `required` du composant `Malio*` + etoile (asterisque) rouge dans le label. Ne JAMAIS griser le bouton « Valider » sans feedback : bouton toujours actif + erreurs affichees sous les champs.
|
||||
- **Couche de validation autoritaire = le back** : les RG sont re-validees serveur (mode strict). Au `422`, mapper `violations[].propertyPath` vers la prop `error` du champ via `extractApiViolations` (deja utilise par `useCategoryForm`). Zero duplication de RG, zero drift.
|
||||
- **Feedback instantane au blur** : uniquement requis / min / max / format (pas de re-implementation des RG metier cote front).
|
||||
- **Regles front-only** : celles sans equivalent back (ex. FK nullable cote back mais obligatoire selon un choix UI) sont validees et affichees cote front.
|
||||
- **Email — PAS de masque** : un email n'a pas de structure fixe. Normalisation via la prop `lowercase` de `MalioInputEmail` (trim + suppression des espaces + lowercase, coherent avec la normalisation serveur RG-1.21). Le format est valide par la prop `error` (violations serveur ou check au blur), jamais par un masque. Retirer tout shaping email ad hoc des ecrans.
|
||||
- **Contrat back attendu** : tout `422` issu d'un Processor/Validator doit porter `violations[].propertyPath` aligne sur les noms de champs du formulaire, pour etre consommable par `extractApiViolations`.
|
||||
- **Dependance** : le branchement des props `required` suppose `@malio/layer-ui` a jour (props `required` + etoile — MUI-41 / ERP-101).
|
||||
|
||||
## Interdits
|
||||
|
||||
- `modules-loader.ts`, `.module.ts` — le scan des layers est automatique
|
||||
|
||||
@@ -33,6 +33,7 @@
|
||||
"symfony/runtime": "8.0.*",
|
||||
"symfony/security-bundle": "8.0.*",
|
||||
"symfony/serializer": "8.0.*",
|
||||
"symfony/translation": "8.0.*",
|
||||
"symfony/twig-bundle": "8.0.*",
|
||||
"symfony/uid": "8.0.*",
|
||||
"symfony/validator": "8.0.*",
|
||||
|
||||
Generated
+94
-1
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "aada2e60fd7563f1498b5505b37e3f4b",
|
||||
"content-hash": "2dc5db01e7f5d6aecd5956749b21a092",
|
||||
"packages": [
|
||||
{
|
||||
"name": "api-platform/doctrine-common",
|
||||
@@ -7657,6 +7657,99 @@
|
||||
],
|
||||
"time": "2026-03-30T15:14:47+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/translation",
|
||||
"version": "v8.0.10",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/symfony/translation.git",
|
||||
"reference": "f63e9342e12646a57c91ef8a366a4f9d8e557b67"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/symfony/translation/zipball/f63e9342e12646a57c91ef8a366a4f9d8e557b67",
|
||||
"reference": "f63e9342e12646a57c91ef8a366a4f9d8e557b67",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.4",
|
||||
"symfony/polyfill-mbstring": "^1.0",
|
||||
"symfony/translation-contracts": "^3.6.1"
|
||||
},
|
||||
"conflict": {
|
||||
"nikic/php-parser": "<5.0",
|
||||
"symfony/http-client-contracts": "<2.5",
|
||||
"symfony/service-contracts": "<2.5"
|
||||
},
|
||||
"provide": {
|
||||
"symfony/translation-implementation": "2.3|3.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"nikic/php-parser": "^5.0",
|
||||
"psr/log": "^1|^2|^3",
|
||||
"symfony/config": "^7.4|^8.0",
|
||||
"symfony/console": "^7.4|^8.0",
|
||||
"symfony/dependency-injection": "^7.4|^8.0",
|
||||
"symfony/finder": "^7.4|^8.0",
|
||||
"symfony/http-client-contracts": "^2.5|^3.0",
|
||||
"symfony/http-kernel": "^7.4|^8.0",
|
||||
"symfony/intl": "^7.4|^8.0",
|
||||
"symfony/polyfill-intl-icu": "^1.21",
|
||||
"symfony/routing": "^7.4|^8.0",
|
||||
"symfony/service-contracts": "^2.5|^3",
|
||||
"symfony/yaml": "^7.4|^8.0"
|
||||
},
|
||||
"type": "library",
|
||||
"autoload": {
|
||||
"files": [
|
||||
"Resources/functions.php"
|
||||
],
|
||||
"psr-4": {
|
||||
"Symfony\\Component\\Translation\\": ""
|
||||
},
|
||||
"exclude-from-classmap": [
|
||||
"/Tests/"
|
||||
]
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "Fabien Potencier",
|
||||
"email": "fabien@symfony.com"
|
||||
},
|
||||
{
|
||||
"name": "Symfony Community",
|
||||
"homepage": "https://symfony.com/contributors"
|
||||
}
|
||||
],
|
||||
"description": "Provides tools to internationalize your application",
|
||||
"homepage": "https://symfony.com",
|
||||
"support": {
|
||||
"source": "https://github.com/symfony/translation/tree/v8.0.10"
|
||||
},
|
||||
"funding": [
|
||||
{
|
||||
"url": "https://symfony.com/sponsor",
|
||||
"type": "custom"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/fabpot",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://github.com/nicolas-grekas",
|
||||
"type": "github"
|
||||
},
|
||||
{
|
||||
"url": "https://tidelift.com/funding/github/packagist/symfony/symfony",
|
||||
"type": "tidelift"
|
||||
}
|
||||
],
|
||||
"time": "2026-05-06T11:30:54+00:00"
|
||||
},
|
||||
{
|
||||
"name": "symfony/translation-contracts",
|
||||
"version": "v3.6.1",
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
framework:
|
||||
# Locale par defaut FR (ERP-107) : les messages natifs des contraintes
|
||||
# Symfony (Email, NotBlank, Length, Iban, Bic...) sont alors servis en
|
||||
# francais via validators.fr.xlf. C'est le FILET ; les contraintes metier
|
||||
# portent en plus un `message:` FR explicite, teste par
|
||||
# tests/Architecture/EntityConstraintsHaveFrenchMessageTest.
|
||||
default_locale: fr
|
||||
translator:
|
||||
default_path: '%kernel.project_dir%/translations'
|
||||
fallbacks:
|
||||
- fr
|
||||
providers:
|
||||
@@ -0,0 +1,146 @@
|
||||
# Validation « tous les blocs » sur les onglets à blocs dynamiques (Client M1)
|
||||
|
||||
> Date : 2026-06-04 · Module : Commercial (M1 Clients) · Tickets liés : ERP-101 / ERP-107
|
||||
> Écrans : `clients/new.vue`, `clients/[id]/edit.vue` · Onglets concernés : Contacts, Adresses, RIB
|
||||
|
||||
## 1. Problème
|
||||
|
||||
À la soumission des onglets à **blocs d'ajout dynamiques** (Contacts / Adresses / RIB), la validation
|
||||
par champ ne s'affiche pas correctement. Deux causes **distinctes et cumulées** :
|
||||
|
||||
### Cause A — 500 back qui court-circuite la validation (cause racine)
|
||||
|
||||
Les opérations `Post` des sous-ressources sont déclarées ainsi :
|
||||
|
||||
```php
|
||||
new Post(
|
||||
uriTemplate: '/clients/{clientId}/contacts',
|
||||
uriVariables: ['clientId' => new Link(fromClass: Client::class, toProperty: 'client')],
|
||||
processor: ClientContactProcessor::class,
|
||||
)
|
||||
```
|
||||
|
||||
Au stade « read » du POST, API Platform résout `clientId` via `LinksHandlerTrait` (branche `toProperty`,
|
||||
`vendor/api-platform/doctrine-orm/State/LinksHandlerTrait.php:134-141`). La requête générée porte sur
|
||||
l'entité **enfant** :
|
||||
|
||||
```sql
|
||||
SELECT o FROM ClientContact o INNER JOIN o.client c WHERE c.id = :clientId
|
||||
```
|
||||
|
||||
exécutée via `ItemProvider::provide` → `getOneOrNullResult()`
|
||||
(`vendor/api-platform/doctrine-orm/State/ItemProvider.php:89`). Donc :
|
||||
|
||||
| Nb d'enfants du client | Lignes retournées | Résultat |
|
||||
|---|---|---|
|
||||
| 0 | 0 | `null` → OK (cas du test CI actuel) |
|
||||
| 1 | 1 | OK |
|
||||
| **≥ 2** | **≥ 2** | **`NonUniqueResultException` → HTTP 500** |
|
||||
|
||||
Conséquence : un client à ≥2 contacts (resp. adresses, RIB) ne peut plus en recevoir un nouveau.
|
||||
La 500 survient **avant** la déserialisation/validation → aucune 422 n'est produite → `mapRowError`
|
||||
(qui ne mappe que les 422) retombe sur un toast générique.
|
||||
|
||||
Les **3** sous-ressources ont strictement la même config → même bug latent (contacts est juste le
|
||||
premier à sauter car les clients de démo ont 3 contacts).
|
||||
|
||||
### Cause B — la boucle front s'arrête au premier bloc en erreur
|
||||
|
||||
`submitContacts` / `submitAddresses` / boucle RIB de `submitAccounting` (dans `new.vue` ET `edit.vue`)
|
||||
font `return` dans le `catch` du premier bloc en échec :
|
||||
|
||||
```js
|
||||
catch (error) {
|
||||
if (!mapRowError(error, contactErrors, index)) { toast(...) }
|
||||
return // ← stoppe : les blocs suivants ne sont jamais validés ni affichés
|
||||
}
|
||||
```
|
||||
|
||||
→ même une fois le 500 corrigé, seules les erreurs du **premier** bloc fautif s'afficheraient.
|
||||
|
||||
## 2. Objectif
|
||||
|
||||
À la validation d'un onglet collection, **tenter tous les blocs** et **afficher l'erreur inline sous
|
||||
chaque champ fautif, pour chaque bloc**, en un seul aller-retour de soumission. Pas de toast récapitulatif
|
||||
(décision : inline seul, cohérent ERP-101). Pas de toast succès tant qu'au moins un bloc reste en erreur.
|
||||
|
||||
Hors périmètre : le workflow incrémental (créer le client, puis débloquer les onglets) reste inchangé ;
|
||||
les onglets scalaires (Principal / Information / Comptabilité-scalaires) fonctionnent déjà et ne sont pas
|
||||
touchés.
|
||||
|
||||
## 3. Conception
|
||||
|
||||
### 3.1 Back — supprimer le read cassé du POST (cause racine)
|
||||
|
||||
Sur les opérations `Post` de `ClientContact`, `ClientAddress`, `ClientRib` :
|
||||
|
||||
- Ajouter **`read: false`**. Le stade « read » est inutile : le `*Processor::linkParent` rattache déjà le
|
||||
parent manuellement via `$em->getRepository(Client::class)->find($clientId)`. Pattern déjà employé dans
|
||||
le projet (`Sites/.../CurrentSiteResource.php`).
|
||||
- Durcir les 3 `linkParent` : si `find($clientId)` renvoie `null`, lever
|
||||
`Symfony\Component\HttpKernel\Exception\NotFoundHttpException` (préserve le **404** sur parent
|
||||
inexistant — sans le read, on régresserait sinon en 500 au persist sur `client_id NOT NULL`).
|
||||
|
||||
Effet : plus de `getOneOrNullResult` foireux → déserialisation + validation Symfony s'exécutent → **422
|
||||
propre par champ** avec `violations[].propertyPath` (déjà garanti par ERP-107 : messages FR explicites).
|
||||
|
||||
Aucune autre modification (security, normalizationContext, processor restant) n'est nécessaire.
|
||||
|
||||
### 3.2 Front — collecter les erreurs de tous les blocs
|
||||
|
||||
Dans `submitContacts`, `submitAddresses`, et la boucle RIB de `submitAccounting`, **dans `new.vue` ET
|
||||
`edit.vue`** :
|
||||
|
||||
- Conserver la réinitialisation du tableau d'erreurs en début de submit (`xxxErrors.value = []`).
|
||||
- Introduire un drapeau local `hasError`. Dans le `catch`, remplacer `return` par
|
||||
`hasError = true; continue` → la boucle tente/valide **tous** les blocs ; chaque 422 se mappe sur
|
||||
`xxxErrors[index]` via `mapRowError` (mécanique existante, inchangée).
|
||||
- Après la boucle : si `hasError` → **ne pas** appeler `completeTab(...)`, **pas** de toast succès. Sinon
|
||||
→ comportement actuel (`completeTab` + toast succès).
|
||||
- Les blocs déjà créés (id non-null) repassent en `PATCH` au resubmit → idempotent, pas de doublon.
|
||||
- Awaits **séquentiels** conservés (volume faible, ordre des blocs préservé, pas de course).
|
||||
|
||||
Le binding inline est déjà en place côté template (`:errors="contactErrors[index]"` /
|
||||
`:error="ribErrors[index]?.iban"` …). Aucun changement de composant `Malio*` requis.
|
||||
|
||||
### 3.3 Réutilisation / isolation
|
||||
|
||||
Le bloc « boucle de soumission d'une collection avec collecte d'erreurs par index » est dupliqué 3× × 2
|
||||
pages. Pour rester testable et DRY, extraire un helper de soumission de collection (ex.
|
||||
`submitCollection(rows, { buildBody, post, patch, errors })` retournant `{ hasError }`) consommé par les
|
||||
6 sites d'appel. À acter dans le plan d'implémentation (option : garder inline si l'extraction dégrade la
|
||||
lisibilité — décision lors du plan).
|
||||
|
||||
## 4. Tests
|
||||
|
||||
### Back (TDD — échouent d'abord)
|
||||
|
||||
Dans `tests/Module/Commercial/Api/ClientSubResourceApiTest` :
|
||||
|
||||
- `testPostContactToClientWithTwoExistingContactsReturns201` : seed un client + 2 contacts, POST un 3ᵉ →
|
||||
attendu **201** (rouge aujourd'hui : 500).
|
||||
- `testPostContactInvalidEmailOnClientWithExistingContactsReturns422` : même seed, POST email invalide →
|
||||
**422** avec `propertyPath=email` et message FR (vérifie que la validation est bien atteinte).
|
||||
- Variantes germes pour adresses et RIB (au moins une chacune) pour verrouiller les 3 sous-ressources.
|
||||
|
||||
Pré-requis : helper de seed de contacts/adresses/RIB dans `AbstractCommercialApiTestCase` (ajouter si
|
||||
absent).
|
||||
|
||||
### Front (Vitest)
|
||||
|
||||
- Si helper `submitCollection` extrait : test unitaire « 3 blocs, le 2ᵉ renvoie 422 → les erreurs du 2ᵉ
|
||||
sont mappées, les blocs 1 et 3 sont tentés, `hasError = true`, tab non complété ».
|
||||
- Sinon : test de composant sur `ClientContactBlock` + page, vérifiant l'affichage inline multi-blocs.
|
||||
|
||||
### Vérifications finales
|
||||
|
||||
`make test` + `make php-cs-fixer-allow-risky` (back), `make nuxt-test` (front). Golden path manuel :
|
||||
client à 3 contacts, ajouter un 4ᵉ avec email invalide → 422 inline sous l'email du bon bloc, pas de 500.
|
||||
|
||||
## 5. Impact / risques
|
||||
|
||||
- API contract : POST sous-ressource passe de 500→201/422 (correction) ; 404 préservé sur parent
|
||||
inexistant. Pas de changement de payload ni de réponse de succès.
|
||||
- Le test fonctionnel CI actuel (POST sur client à 0 contact) reste vert.
|
||||
- Régression possible si un consommateur dépendait du read implicite du parent au POST : aucun identifié
|
||||
(les 3 processors gèrent déjà le rattachement manuellement).
|
||||
@@ -0,0 +1,633 @@
|
||||
# Validation « tous les blocs » — onglets à blocs dynamiques (Client M1) — Plan d'implémentation
|
||||
|
||||
> **For agentic workers:** REQUIRED SUB-SKILL: Use superpowers:subagent-driven-development (recommended) or superpowers:executing-plans to implement this plan task-by-task. Steps use checkbox (`- [ ]`) syntax for tracking.
|
||||
|
||||
**Goal:** Permettre la validation 422 par champ sur TOUS les blocs des onglets Contacts / Adresses / RIB d'un client (création + édition), en supprimant la 500 `NonUniqueResultException` qui les bloque dès ≥2 enfants et en ne stoppant plus la boucle front au premier bloc en erreur.
|
||||
|
||||
**Architecture:** Côté back, on retire le stade « read » inutile du POST des 3 sous-ressources (`read: false`) — le parent est déjà rattaché manuellement par le processor — et on durcit ce rattachement (404 si parent absent). Côté front, on factorise la boucle de soumission de collection dans `useClientFormErrors().submitRows(...)` qui tente tous les blocs et collecte les erreurs par index, puis on branche les 6 sites d'appel (`new.vue` + `edit.vue` × contacts/adresses/RIB).
|
||||
|
||||
**Tech Stack:** Symfony 8 / API Platform 4 (PHP 8.4, PHPUnit) ; Nuxt 4 / Vue 3 / TypeScript / Vitest.
|
||||
|
||||
**Spec de référence :** `docs/superpowers/specs/2026-06-04-client-collection-blocks-validation-design.md`
|
||||
|
||||
**Pré-vol :** `make start` (containers up), branche de travail = celle de la MR (`feat/erp-107-validation-messages-fr`) ou une branche dédiée selon décision utilisateur.
|
||||
|
||||
---
|
||||
|
||||
## Structure des fichiers
|
||||
|
||||
**Back — modifiés :**
|
||||
- `src/Module/Commercial/Domain/Entity/ClientContact.php` — `read: false` sur `Post`
|
||||
- `src/Module/Commercial/Domain/Entity/ClientAddress.php` — `read: false` sur `Post`
|
||||
- `src/Module/Commercial/Domain/Entity/ClientRib.php` — `read: false` sur `Post`
|
||||
- `src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientContactProcessor.php` — `linkParent` → 404
|
||||
- `.../Processor/ClientAddressProcessor.php` — idem
|
||||
- `.../Processor/ClientRibProcessor.php` — idem
|
||||
- `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php` — helper `seedContact()`
|
||||
- `tests/Module/Commercial/Api/ClientSubResourceApiTest.php` — tests de non-régression
|
||||
|
||||
**Front — modifiés :**
|
||||
- `frontend/modules/commercial/composables/useClientFormErrors.ts` — méthode `submitRows()`
|
||||
- `frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts` — créé (test unitaire)
|
||||
- `frontend/modules/commercial/pages/clients/new.vue` — branchements (3 submits)
|
||||
- `frontend/modules/commercial/pages/clients/[id]/edit.vue` — branchements (3 submits)
|
||||
|
||||
---
|
||||
|
||||
## Task 1 : Back — test rouge (POST sur client à ≥2 enfants)
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php`
|
||||
- Test: `tests/Module/Commercial/Api/ClientSubResourceApiTest.php`
|
||||
|
||||
- [ ] **Step 1 : Ajouter un helper de seed de contact à la base de test**
|
||||
|
||||
Dans `AbstractCommercialApiTestCase.php`, ajouter (sous `seedClient`, avant `cleanupCommercialTestData`) :
|
||||
|
||||
```php
|
||||
/**
|
||||
* Seede directement un ClientContact en base (sans passer par l'API), pour
|
||||
* preparer un client deja dote de N contacts. Au moins le prenom est pose
|
||||
* (RG-1.05 / CHECK chk_client_contact_name).
|
||||
*/
|
||||
protected function seedContact(ClientEntity $client, string $firstName): \App\Module\Commercial\Domain\Entity\ClientContact
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$contact = new \App\Module\Commercial\Domain\Entity\ClientContact();
|
||||
$contact->setClient($client);
|
||||
$contact->setFirstName($firstName);
|
||||
$em->persist($contact);
|
||||
$em->flush();
|
||||
|
||||
return $contact;
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 2 : Écrire les tests rouges**
|
||||
|
||||
Dans `ClientSubResourceApiTest.php`, ajouter dans la section `// === Contacts ===` :
|
||||
|
||||
```php
|
||||
/**
|
||||
* Regression ERP (bug subresource Link toProperty) : POST d'un contact sur un
|
||||
* client qui en a DEJA >= 2 ne doit pas exploser en 500
|
||||
* (NonUniqueResultException sur la resolution du parent), mais creer (201).
|
||||
*/
|
||||
public function testPostContactOnClientWithTwoExistingContactsReturns201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Contact Multi');
|
||||
$this->seedContact($seed, 'Alpha');
|
||||
$this->seedContact($seed, 'Beta');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['firstName' => 'Gamma'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Meme contexte (>= 2 contacts existants) : un email invalide doit produire
|
||||
* une 422 par champ (la validation est bien atteinte), pas une 500.
|
||||
*/
|
||||
public function testPostInvalidContactOnPopulatedClientReturns422OnField(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Contact Multi Bad');
|
||||
$this->seedContact($seed, 'Alpha');
|
||||
$this->seedContact($seed, 'Beta');
|
||||
|
||||
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['firstName' => 'Gamma', 'email' => 'pas-un-email'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
self::assertArrayHasKey('email', $byPath);
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 3 : Lancer les tests, vérifier qu'ils échouent (500 au lieu de 201/422)**
|
||||
|
||||
Run : `make test` (ou ciblé dans le container : `docker exec php-starseed-fpm php bin/phpunit --filter ClientSubResourceApiTest`)
|
||||
Expected : les 2 nouveaux tests ÉCHOUENT (HTTP 500 `NonUniqueResultException`). `testPostContactOnClient...` reçoit 500, pas 201.
|
||||
|
||||
- [ ] **Step 4 : Commit (test rouge)**
|
||||
|
||||
```bash
|
||||
git add tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php tests/Module/Commercial/Api/ClientSubResourceApiTest.php
|
||||
git commit -m "test(commercial) : reproduit la 500 NonUniqueResult au POST contact sur client peuple (ERP-107)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 2 : Back — fix (read:false + linkParent durci) → tests verts
|
||||
|
||||
**Files:**
|
||||
- Modify: `src/Module/Commercial/Domain/Entity/ClientContact.php:48-57`
|
||||
- Modify: `src/Module/Commercial/Domain/Entity/ClientAddress.php:61-70`
|
||||
- Modify: `src/Module/Commercial/Domain/Entity/ClientRib.php:52-61`
|
||||
- Modify: `.../State/Processor/ClientContactProcessor.php:76-94`
|
||||
- Modify: `.../State/Processor/ClientAddressProcessor.php:63-81`
|
||||
- Modify: `.../State/Processor/ClientRibProcessor.php:65-83`
|
||||
|
||||
- [ ] **Step 1 : `read: false` sur les 3 opérations `Post`**
|
||||
|
||||
`ClientContact.php`, opération `Post` — ajouter la ligne `read: false,` :
|
||||
|
||||
```php
|
||||
new Post(
|
||||
uriTemplate: '/clients/{clientId}/contacts',
|
||||
uriVariables: [
|
||||
'clientId' => new Link(fromClass: Client::class, toProperty: 'client'),
|
||||
],
|
||||
// read:false : pas de stade lecture du parent (le Link toProperty
|
||||
// resoudrait l'enfant et casse en NonUniqueResult des >= 2 enfants).
|
||||
// Le parent est rattache par ClientContactProcessor::linkParent.
|
||||
read: false,
|
||||
security: "is_granted('commercial.clients.manage')",
|
||||
normalizationContext: ['groups' => ['client_contact:read']],
|
||||
denormalizationContext: ['groups' => ['client_contact:write']],
|
||||
processor: ClientContactProcessor::class,
|
||||
),
|
||||
```
|
||||
|
||||
`ClientAddress.php` — idem dans son `Post` (`security: commercial.clients.manage`, processor `ClientAddressProcessor`), commentaire pointant `ClientAddressProcessor::linkParent`.
|
||||
|
||||
`ClientRib.php` — idem dans son `Post` (`security: commercial.clients.accounting.manage`, processor `ClientRibProcessor`), commentaire pointant `ClientRibProcessor::linkParent`.
|
||||
|
||||
- [ ] **Step 2 : Durcir les 3 `linkParent` (404 si parent absent)**
|
||||
|
||||
Dans chaque processor, ajouter l'import en tête de fichier :
|
||||
|
||||
```php
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
```
|
||||
|
||||
`ClientContactProcessor::linkParent` — remplacer le bloc final par :
|
||||
|
||||
```php
|
||||
if (null === $clientId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$client = $clientId instanceof Client
|
||||
? $clientId
|
||||
: $this->em->getRepository(Client::class)->find($clientId);
|
||||
|
||||
// read:false sur le POST : sans stade lecture, un parent introuvable
|
||||
// n'est plus intercepte en amont -> 404 explicite (sinon 500 au persist
|
||||
// sur client_id NOT NULL).
|
||||
if (!$client instanceof Client) {
|
||||
throw new NotFoundHttpException('Client introuvable.');
|
||||
}
|
||||
|
||||
$contact->setClient($client);
|
||||
```
|
||||
|
||||
`ClientAddressProcessor::linkParent` — idem avec `$address->setClient($client);`.
|
||||
`ClientRibProcessor::linkParent` — idem avec `$rib->setClient($client);`.
|
||||
|
||||
- [ ] **Step 3 : Lancer les tests, vérifier qu'ils passent**
|
||||
|
||||
Run : `make test`
|
||||
Expected : les 2 tests de Task 1 PASSENT (201 + 422 `propertyPath=email`). Aucun test existant cassé (notamment `testPostContactInvalidEmailReturns422WithFrenchMessageOnField` et les tests d'archi ERP-107 restent verts).
|
||||
|
||||
- [ ] **Step 4 : Lint PHP**
|
||||
|
||||
Run : `make php-cs-fixer-allow-risky`
|
||||
Expected : 0 fichier à corriger (ou corrections appliquées et re-vérifiées).
|
||||
|
||||
- [ ] **Step 5 : Commit (fix back)**
|
||||
|
||||
```bash
|
||||
git add src/Module/Commercial/Domain/Entity/ClientContact.php src/Module/Commercial/Domain/Entity/ClientAddress.php src/Module/Commercial/Domain/Entity/ClientRib.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientContactProcessor.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientAddressProcessor.php src/Module/Commercial/Infrastructure/ApiPlatform/State/Processor/ClientRibProcessor.php
|
||||
git commit -m "fix(commercial) : POST sous-ressource client en read:false + parent 404 (corrige 500 NonUniqueResult, ERP-107)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 3 : Back — germes adresses + RIB (verrouille les 3 sous-ressources)
|
||||
|
||||
**Files:**
|
||||
- Modify: `tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php` (helpers `seedAddress`, `seedRib`)
|
||||
- Test: `tests/Module/Commercial/Api/ClientSubResourceApiTest.php`
|
||||
|
||||
- [ ] **Step 1 : Helpers de seed adresse + RIB**
|
||||
|
||||
Dans `AbstractCommercialApiTestCase.php`, ajouter :
|
||||
|
||||
```php
|
||||
/** Seede une adresse minimale valide (RG : CP/ville/rue requis). */
|
||||
protected function seedAddress(ClientEntity $client, string $city): \App\Module\Commercial\Domain\Entity\ClientAddress
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$address = new \App\Module\Commercial\Domain\Entity\ClientAddress();
|
||||
$address->setClient($client);
|
||||
$address->setPostalCode('33000');
|
||||
$address->setCity($city);
|
||||
$address->setStreet('1 rue du Test');
|
||||
$em->persist($address);
|
||||
$em->flush();
|
||||
|
||||
return $address;
|
||||
}
|
||||
|
||||
/** Seede un RIB valide (BIC/IBAN conformes). */
|
||||
protected function seedRib(ClientEntity $client, string $label): \App\Module\Commercial\Domain\Entity\ClientRib
|
||||
{
|
||||
$em = $this->getEm();
|
||||
$rib = new \App\Module\Commercial\Domain\Entity\ClientRib();
|
||||
$rib->setClient($client);
|
||||
$rib->setLabel($label);
|
||||
$rib->setBic('BNPAFRPPXXX');
|
||||
$rib->setIban('FR1420041010050500013M02606');
|
||||
$em->persist($rib);
|
||||
$em->flush();
|
||||
|
||||
return $rib;
|
||||
}
|
||||
```
|
||||
|
||||
> Note : si une propriété est non-nullable et absente ci-dessus (ex. `position`, flags d'adresse), poser les setters correspondants avec une valeur par défaut neutre — vérifier les entités `ClientAddress` / `ClientRib` au moment de l'écriture.
|
||||
|
||||
- [ ] **Step 2 : Tests de non-régression adresses + RIB**
|
||||
|
||||
Dans `ClientSubResourceApiTest.php`, section adresses puis RIB :
|
||||
|
||||
```php
|
||||
public function testPostAddressOnClientWithTwoExistingAddressesReturns201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Addr Multi');
|
||||
$this->seedAddress($seed, 'Bordeaux');
|
||||
$this->seedAddress($seed, 'Lyon');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/addresses', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['postalCode' => '75001', 'city' => 'Paris', 'street' => '2 rue Neuve'],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
|
||||
public function testPostRibOnClientWithTwoExistingRibsReturns201(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Rib Multi');
|
||||
$this->seedRib($seed, 'Compte 1');
|
||||
$this->seedRib($seed, 'Compte 2');
|
||||
|
||||
$client->request('POST', '/api/clients/'.$seed->getId().'/ribs', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => ['label' => 'Compte 3', 'bic' => self::VALID_BIC, 'iban' => self::VALID_IBAN],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(201);
|
||||
}
|
||||
```
|
||||
|
||||
> Le POST RIB exige `commercial.clients.accounting.manage` — `admin` (ROLE_ADMIN) l'a. Si une 403 apparaît, vérifier le compte de test.
|
||||
|
||||
- [ ] **Step 3 : Lancer, vérifier vert**
|
||||
|
||||
Run : `make test`
|
||||
Expected : PASS (les 2 nouveaux tests verts grâce au fix de Task 2).
|
||||
|
||||
- [ ] **Step 4 : Commit**
|
||||
|
||||
```bash
|
||||
git add tests/Module/Commercial/Api/AbstractCommercialApiTestCase.php tests/Module/Commercial/Api/ClientSubResourceApiTest.php
|
||||
git commit -m "test(commercial) : verrouille POST adresses/RIB sur client peuple (ERP-107)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 4 : Front — helper `submitRows` + test unitaire
|
||||
|
||||
**Files:**
|
||||
- Modify: `frontend/modules/commercial/composables/useClientFormErrors.ts`
|
||||
- Create: `frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts`
|
||||
|
||||
- [ ] **Step 1 : Écrire le test rouge**
|
||||
|
||||
Créer `useClientFormErrors.spec.ts` :
|
||||
|
||||
```ts
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { useClientFormErrors } from '../useClientFormErrors'
|
||||
|
||||
// Construit une erreur facon useApi : 422 avec violations Hydra.
|
||||
function http422(path: string, message: string) {
|
||||
return { response: { status: 422, _data: { violations: [{ propertyPath: path, message }] } } }
|
||||
}
|
||||
|
||||
describe('useClientFormErrors.submitRows', () => {
|
||||
it('tente TOUS les blocs et mappe les erreurs par index, sans stopper au premier echec', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const seen: number[] = []
|
||||
const onUnmapped = vi.fn()
|
||||
|
||||
const saveRow = async (_row: unknown, index: number) => {
|
||||
seen.push(index)
|
||||
if (index === 1) throw http422('email', 'Email invalide')
|
||||
}
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ a: 0 }, { a: 1 }, { a: 2 }],
|
||||
contactErrors,
|
||||
saveRow,
|
||||
onUnmapped,
|
||||
)
|
||||
|
||||
expect(seen).toEqual([0, 1, 2]) // tous les blocs tentes
|
||||
expect(hasError).toBe(true)
|
||||
expect(contactErrors.value[1]).toEqual({ email: 'Email invalide' })
|
||||
expect(contactErrors.value[0]).toBeUndefined()
|
||||
expect(onUnmapped).not.toHaveBeenCalled() // 422 mappee, pas de fallback
|
||||
})
|
||||
|
||||
it('saute les lignes filtrees par shouldSkip et renvoie false si tout passe', async () => {
|
||||
const { contactErrors, submitRows } = useClientFormErrors()
|
||||
const saved: number[] = []
|
||||
|
||||
const hasError = await submitRows(
|
||||
[{ skip: true }, { skip: false }],
|
||||
contactErrors,
|
||||
async (_row, index) => { saved.push(index) },
|
||||
vi.fn(),
|
||||
(row: { skip: boolean }) => row.skip,
|
||||
)
|
||||
|
||||
expect(saved).toEqual([1])
|
||||
expect(hasError).toBe(false)
|
||||
})
|
||||
})
|
||||
```
|
||||
|
||||
- [ ] **Step 2 : Lancer, vérifier l'échec**
|
||||
|
||||
Run : `make nuxt-test` (ou ciblé : `docker exec <node> npx vitest run useClientFormErrors`)
|
||||
Expected : FAIL — `submitRows` n'existe pas encore.
|
||||
|
||||
- [ ] **Step 3 : Implémenter `submitRows`**
|
||||
|
||||
Dans `useClientFormErrors.ts`, ajouter la méthode (dans la fonction, après `mapRowError`) et l'exposer dans le `return` :
|
||||
|
||||
```ts
|
||||
/**
|
||||
* Soumet TOUS les blocs d'une collection (contacts/adresses/RIB) en collectant
|
||||
* les erreurs par index : on n'arrete PAS au premier bloc en echec (ERP-101).
|
||||
* Reinitialise le tableau d'erreurs cible, tente chaque ligne via `saveRow`,
|
||||
* mappe les 422 inline (mapRowError) ou delegue le fallback a `onUnmappedError`.
|
||||
* Retourne true si au moins un bloc a echoue (le caller ne valide alors pas l'onglet).
|
||||
*/
|
||||
async function submitRows<T>(
|
||||
rows: T[],
|
||||
target: Ref<Record<string, string>[]>,
|
||||
saveRow: (row: T, index: number) => Promise<void>,
|
||||
onUnmappedError: (error: unknown, index: number) => void,
|
||||
shouldSkip?: (row: T, index: number) => boolean,
|
||||
): Promise<boolean> {
|
||||
target.value = []
|
||||
let hasError = false
|
||||
for (let index = 0; index < rows.length; index++) {
|
||||
if (shouldSkip?.(rows[index], index)) {
|
||||
continue
|
||||
}
|
||||
try {
|
||||
await saveRow(rows[index], index)
|
||||
}
|
||||
catch (error) {
|
||||
if (!mapRowError(error, target, index)) {
|
||||
onUnmappedError(error, index)
|
||||
}
|
||||
hasError = true
|
||||
}
|
||||
}
|
||||
|
||||
return hasError
|
||||
}
|
||||
```
|
||||
|
||||
Ajouter `submitRows` à l'objet retourné par `useClientFormErrors`.
|
||||
|
||||
- [ ] **Step 4 : Lancer, vérifier vert**
|
||||
|
||||
Run : `make nuxt-test`
|
||||
Expected : PASS (les 2 cas verts).
|
||||
|
||||
- [ ] **Step 5 : Commit**
|
||||
|
||||
```bash
|
||||
git add frontend/modules/commercial/composables/useClientFormErrors.ts frontend/modules/commercial/composables/__tests__/useClientFormErrors.spec.ts
|
||||
git commit -m "feat(commercial) : submitRows collecte les erreurs de tous les blocs de collection (ERP-101)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 5 : Front — brancher `submitRows` dans new.vue + edit.vue
|
||||
|
||||
**Files:**
|
||||
- Modify: `frontend/modules/commercial/pages/clients/new.vue` (`submitContacts`, `submitAddresses`, boucle RIB de `submitAccounting`)
|
||||
- Modify: `frontend/modules/commercial/pages/clients/[id]/edit.vue` (les 3 équivalents)
|
||||
|
||||
- [ ] **Step 1 : Récupérer `submitRows` du composable**
|
||||
|
||||
Dans `new.vue` ET `edit.vue`, ajouter `submitRows` à la déstructuration de `useClientFormErrors()` :
|
||||
|
||||
```ts
|
||||
const {
|
||||
mainErrors,
|
||||
informationErrors,
|
||||
accountingErrors,
|
||||
contactErrors,
|
||||
addressErrors,
|
||||
ribErrors,
|
||||
mapRowError,
|
||||
submitRows,
|
||||
} = useClientFormErrors()
|
||||
```
|
||||
|
||||
- [ ] **Step 2 : Réécrire `submitContacts` (new.vue)**
|
||||
|
||||
Remplacer le corps de la boucle par un appel à `submitRows` :
|
||||
|
||||
```ts
|
||||
async function submitContacts(): Promise<void> {
|
||||
if (clientId.value === null || !canValidateContacts.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
try {
|
||||
const hasError = await submitRows(
|
||||
contacts.value,
|
||||
contactErrors,
|
||||
async (contact) => {
|
||||
const body = {
|
||||
firstName: contact.firstName || null,
|
||||
lastName: contact.lastName || null,
|
||||
jobTitle: contact.jobTitle || null,
|
||||
phonePrimary: contact.phonePrimary || null,
|
||||
phoneSecondary: contact.hasSecondaryPhone ? (contact.phoneSecondary || null) : null,
|
||||
email: contact.email || null,
|
||||
}
|
||||
if (contact.id === null) {
|
||||
const created = await api.post<ContactResponse>(
|
||||
`/clients/${clientId.value}/contacts`,
|
||||
body,
|
||||
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||
)
|
||||
contact.id = created.id
|
||||
contact.iri = created['@id'] ?? null
|
||||
}
|
||||
else {
|
||||
await api.patch(`/client_contacts/${contact.id}`, body, { toast: false })
|
||||
}
|
||||
},
|
||||
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
(contact) => !isContactNamed(contact),
|
||||
)
|
||||
if (hasError) return
|
||||
completeTab('contact')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
finally {
|
||||
tabSubmitting.value = false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 3 : Réécrire `submitAddresses` (new.vue)**
|
||||
|
||||
```ts
|
||||
async function submitAddresses(): Promise<void> {
|
||||
if (clientId.value === null || !canValidateAddresses.value || tabSubmitting.value) return
|
||||
tabSubmitting.value = true
|
||||
try {
|
||||
const hasError = await submitRows(
|
||||
addresses.value,
|
||||
addressErrors,
|
||||
async (address) => {
|
||||
const body = {
|
||||
isProspect: address.isProspect,
|
||||
isDelivery: address.isDelivery,
|
||||
isBilling: address.isBilling,
|
||||
country: address.country,
|
||||
postalCode: address.postalCode || null,
|
||||
city: address.city || null,
|
||||
street: address.street || null,
|
||||
streetComplement: address.streetComplement || null,
|
||||
categories: address.categoryIris,
|
||||
sites: address.siteIris,
|
||||
contacts: address.contactIris,
|
||||
billingEmail: isBillingEmailRequired(address) ? (address.billingEmail || null) : null,
|
||||
}
|
||||
if (address.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId.value}/addresses`,
|
||||
body,
|
||||
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||
)
|
||||
address.id = created.id
|
||||
}
|
||||
else {
|
||||
await api.patch(`/client_addresses/${address.id}`, body, { toast: false })
|
||||
}
|
||||
},
|
||||
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
)
|
||||
if (hasError) return
|
||||
completeTab('address')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
}
|
||||
finally {
|
||||
tabSubmitting.value = false
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
- [ ] **Step 4 : Réécrire la boucle RIB de `submitAccounting` (new.vue)**
|
||||
|
||||
Garder le PATCH scalaire inchangé (1) ; remplacer la boucle (2) :
|
||||
|
||||
```ts
|
||||
// 2) POST/PATCH des RIB (erreurs inline par ligne, tous les blocs).
|
||||
const ribHasError = await submitRows(
|
||||
ribs.value,
|
||||
ribErrors,
|
||||
async (rib) => {
|
||||
const body = { label: rib.label, bic: rib.bic, iban: rib.iban }
|
||||
if (rib.id === null) {
|
||||
const created = await api.post<{ id: number }>(
|
||||
`/clients/${clientId.value}/ribs`,
|
||||
body,
|
||||
{ headers: { Accept: 'application/ld+json' }, toast: false },
|
||||
)
|
||||
rib.id = created.id
|
||||
}
|
||||
else {
|
||||
await api.patch(`/client_ribs/${rib.id}`, body, { toast: false })
|
||||
}
|
||||
},
|
||||
(error) => toast.error({ title: t('commercial.clients.toast.error'), message: apiErrorMessage(error) }),
|
||||
(rib) => !ribIsComplete(rib),
|
||||
)
|
||||
if (ribHasError) return
|
||||
|
||||
completeTab('accounting')
|
||||
toast.success({ title: t('commercial.clients.toast.updateSuccess') })
|
||||
```
|
||||
|
||||
> Retirer le `ribErrors.value = []` désormais fait par `submitRows`. Le `accountingErrors.clearErrors()` du PATCH scalaire reste.
|
||||
|
||||
- [ ] **Step 5 : Mirror dans edit.vue**
|
||||
|
||||
Appliquer les mêmes réécritures aux `submitContacts` / `submitAddresses` / boucle RIB de `submitAccounting` d'`edit.vue`. Conserver le **fallback d'erreur propre à edit.vue** (si edit.vue utilise `showError(...)` au lieu de `toast.error(...)`, passer ce fallback comme `onUnmappedError`). Vérifier les noms des refs (`clientId` peut y être l'id de route).
|
||||
|
||||
- [ ] **Step 6 : Vérifier le typecheck + tests front**
|
||||
|
||||
Run : `make nuxt-test`
|
||||
Expected : PASS. Aucune régression des specs existantes (`ClientContactBlock.spec.ts`, etc.).
|
||||
|
||||
- [ ] **Step 7 : Commit**
|
||||
|
||||
```bash
|
||||
git add frontend/modules/commercial/pages/clients/new.vue "frontend/modules/commercial/pages/clients/[id]/edit.vue"
|
||||
git commit -m "feat(commercial) : valide tous les blocs contacts/adresses/RIB et affiche les erreurs par bloc (ERP-101)"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Task 6 : Vérification finale + golden path manuel
|
||||
|
||||
- [ ] **Step 1 : Suite complète back**
|
||||
|
||||
Run : `make test` puis `make php-cs-fixer-allow-risky`
|
||||
Expected : tout vert, 0 fichier à corriger.
|
||||
|
||||
- [ ] **Step 2 : Suite complète front**
|
||||
|
||||
Run : `make nuxt-test`
|
||||
Expected : tout vert.
|
||||
|
||||
- [ ] **Step 3 : Golden path manuel (`make dev-nuxt`, port 3004)**
|
||||
|
||||
Scénario : ouvrir un client à 3 contacts (compte `admin`), onglet Contacts, ajouter un bloc avec email invalide + un autre bloc avec prénom/nom vides → Valider.
|
||||
Attendu : **pas de 500** ; « L'adresse email n'est pas valide. » sous l'email du bon bloc ET « Le prénom ou le nom du contact est obligatoire. » sous le prénom de l'autre bloc, **affichés simultanément**. L'onglet ne se valide pas tant qu'une erreur subsiste. Idem à vérifier rapidement sur Adresses et RIB.
|
||||
|
||||
- [ ] **Step 4 : Si une vérif échoue ou ne peut être lancée, le dire explicitement** (ne pas annoncer « fini »).
|
||||
|
||||
---
|
||||
|
||||
## Self-review (auteur du plan)
|
||||
|
||||
- **Couverture spec §3.1 (back)** : Task 2 (read:false + linkParent 404) ✓ ; §3.2 (front collect-all) : Tasks 4-5 ✓ ; §3.3 (helper réutilisable) : Task 4 `submitRows` ✓ ; §4 tests : Tasks 1, 3 (back), 4 (front) + Task 6 golden path ✓.
|
||||
- **Périmètre 3 sous-ressources** : contacts (Task 1-2), adresses + RIB (Task 3 + branchements Task 5) ✓.
|
||||
- **Décision « inline seul »** : aucun toast succès si `hasError` ; pas de toast récap ✓.
|
||||
- **Pas de placeholder** : le seul point ouvert est la note Task 3 Step 1 (setters non-nullables éventuels d'adresse/RIB à compléter en lisant les entités) — à lever à l'écriture. Cohérence des noms : `submitRows` utilisé identiquement en Task 4 et Task 5.
|
||||
@@ -112,7 +112,7 @@ class Category implements TimestampableInterface, BlamableInterface, CategoryInt
|
||||
// persiste, sans contradiction entre l'ordre Validate / Process.
|
||||
#[ORM\Column(length: 120)]
|
||||
#[Assert\NotBlank(message: 'Le nom est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(min: 2, max: 120, normalizer: 'trim')]
|
||||
#[Assert\Length(min: 2, max: 120, minMessage: 'Le nom doit comporter au moins {{ limit }} caractères.', maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['category:read', 'category:write'])]
|
||||
private ?string $name = null;
|
||||
|
||||
|
||||
@@ -147,7 +147,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
// === Formulaire principal ===
|
||||
#[ORM\Column(length: 180)]
|
||||
#[Assert\NotBlank(message: 'Le nom de l\'entreprise est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(min: 2, max: 180, normalizer: 'trim')]
|
||||
#[Assert\Length(min: 2, max: 180, minMessage: 'Le nom de l\'entreprise doit comporter au moins {{ limit }} caractères.', maxMessage: 'Le nom de l\'entreprise ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read', 'client:write:main'])]
|
||||
private ?string $companyName = null;
|
||||
|
||||
@@ -188,6 +188,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
private ?string $description = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
#[Assert\Length(max: 255, maxMessage: 'Ce champ ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read', 'client:write:information'])]
|
||||
private ?string $competitors = null;
|
||||
|
||||
@@ -196,7 +197,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
private ?DateTimeImmutable $foundedAt = null;
|
||||
|
||||
#[ORM\Column(nullable: true)]
|
||||
#[Assert\PositiveOrZero]
|
||||
#[Assert\PositiveOrZero(message: 'L\'effectif doit être un nombre positif ou nul.')]
|
||||
#[Groups(['client:read', 'client:write:information'])]
|
||||
private ?int $employeesCount = null;
|
||||
|
||||
@@ -205,6 +206,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
private ?string $revenueAmount = null;
|
||||
|
||||
#[ORM\Column(length: 120, nullable: true)]
|
||||
#[Assert\Length(max: 120, maxMessage: 'Le nom du dirigeant ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read', 'client:write:information'])]
|
||||
private ?string $directorName = null;
|
||||
|
||||
@@ -217,10 +219,12 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
// futur Provider si l'user a la permission accounting.view). Ecriture via
|
||||
// `client:write:accounting` (le futur Processor exige accounting.manage).
|
||||
#[ORM\Column(length: 20, nullable: true)]
|
||||
#[Assert\Length(max: 20, maxMessage: 'Le SIREN ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||
private ?string $siren = null;
|
||||
|
||||
#[ORM\Column(length: 40, nullable: true)]
|
||||
#[Assert\Length(max: 40, maxMessage: 'Le numéro de compte ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||
private ?string $accountNumber = null;
|
||||
|
||||
@@ -230,6 +234,7 @@ class Client implements TimestampableInterface, BlamableInterface
|
||||
private ?TvaMode $tvaMode = null;
|
||||
|
||||
#[ORM\Column(length: 40, nullable: true)]
|
||||
#[Assert\Length(max: 40, maxMessage: 'Le numéro de TVA ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client:read:accounting', 'client:write:accounting'])]
|
||||
private ?string $nTva = null;
|
||||
|
||||
|
||||
@@ -125,33 +125,39 @@ class ClientAddress implements TimestampableInterface, BlamableInterface
|
||||
private bool $isBilling = false;
|
||||
|
||||
#[ORM\Column(length: 80, options: ['default' => 'France'])]
|
||||
#[Assert\Length(max: 80, maxMessage: 'Le pays ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private string $country = 'France';
|
||||
|
||||
// RG-1.09 : code postal a 4 ou 5 chiffres (pas de controle CP/ville serveur).
|
||||
// Le Regex borne deja la longueur (≤ 5) : pas de Length redondant.
|
||||
#[ORM\Column(length: 20)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\NotBlank(message: 'Le code postal est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Regex(pattern: '/^[0-9]{4,5}$/', message: 'Le code postal doit comporter 4 ou 5 chiffres.')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private ?string $postalCode = null;
|
||||
|
||||
#[ORM\Column(length: 120)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\NotBlank(message: 'La ville est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(max: 120, maxMessage: 'La ville ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private ?string $city = null;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\NotBlank(message: 'La rue est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(max: 255, maxMessage: 'La rue ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private ?string $street = null;
|
||||
|
||||
#[ORM\Column(length: 255, nullable: true)]
|
||||
#[Assert\Length(max: 255, maxMessage: 'Le complément d\'adresse ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private ?string $streetComplement = null;
|
||||
|
||||
// RG-1.11 : obligatoire ssi isBilling (validateBillingEmailPresence + CHECK BDD).
|
||||
#[ORM\Column(length: 180, nullable: true)]
|
||||
#[Assert\Email]
|
||||
#[Assert\Email(message: 'L\'email de facturation n\'est pas valide.')]
|
||||
#[Assert\Length(max: 180, maxMessage: 'L\'email de facturation ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_address:read', 'client_address:write'])]
|
||||
private ?string $billingEmail = null;
|
||||
|
||||
|
||||
@@ -88,30 +88,36 @@ class ClientContact implements TimestampableInterface, BlamableInterface
|
||||
// RG-1.05 : firstName OU lastName obligatoire (CHECK BDD + Processor). Les
|
||||
// deux restent nullable au niveau ORM.
|
||||
#[ORM\Column(length: 120, nullable: true)]
|
||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
||||
#[Assert\Length(max: 120, maxMessage: 'Le prénom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $firstName = null;
|
||||
|
||||
#[ORM\Column(length: 120, nullable: true)]
|
||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
||||
#[Assert\Length(max: 120, maxMessage: 'Le nom ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $lastName = null;
|
||||
|
||||
#[ORM\Column(length: 120, nullable: true)]
|
||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
||||
#[Assert\Length(max: 120, maxMessage: 'La fonction ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $jobTitle = null;
|
||||
|
||||
// RG : pas de validation de format telephone (saisie libre), mais une
|
||||
// Assert\Length calee sur la colonne VARCHAR(20) evite l'erreur Postgres
|
||||
// (500 non rattachee au champ) au profit d'une 422 propre (ERP-107).
|
||||
#[ORM\Column(length: 20, nullable: true)]
|
||||
#[Assert\Length(max: 20, maxMessage: 'Le téléphone ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $phonePrimary = null;
|
||||
|
||||
#[ORM\Column(length: 20, nullable: true)]
|
||||
#[Assert\Length(max: 20, maxMessage: 'Le téléphone secondaire ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $phoneSecondary = null;
|
||||
|
||||
#[ORM\Column(length: 180, nullable: true)]
|
||||
#[Assert\Email]
|
||||
#[Assert\Email(message: 'L\'adresse email n\'est pas valide.')]
|
||||
#[Assert\Length(max: 180, maxMessage: 'L\'email ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_contact:read', 'client_contact:write'])]
|
||||
private ?string $email = null;
|
||||
|
||||
|
||||
@@ -97,20 +97,22 @@ class ClientRib implements TimestampableInterface, BlamableInterface
|
||||
private ?Client $client = null;
|
||||
|
||||
#[ORM\Column(length: 120)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Length(max: 120, normalizer: 'trim')]
|
||||
#[Assert\NotBlank(message: 'Le libellé du RIB est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(max: 120, maxMessage: 'Le libellé ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||
private ?string $label = null;
|
||||
|
||||
// Bic/Iban bornent deja le format (et donc la longueur) : pas de Length
|
||||
// redondant calee sur la colonne (whitelist du garde-fou ERP-107).
|
||||
#[ORM\Column(length: 20)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Bic]
|
||||
#[Assert\NotBlank(message: 'Le BIC est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Bic(message: 'Le BIC n\'est pas valide.')]
|
||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||
private ?string $bic = null;
|
||||
|
||||
#[ORM\Column(length: 34)]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Iban]
|
||||
#[Assert\NotBlank(message: 'L\'IBAN est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Iban(message: 'L\'IBAN n\'est pas valide.')]
|
||||
#[Groups(['client_rib:read', 'client:read:accounting', 'client_rib:write'])]
|
||||
private ?string $iban = null;
|
||||
|
||||
|
||||
@@ -79,13 +79,15 @@ class Role
|
||||
|
||||
#[ORM\Column(length: 100)]
|
||||
#[Groups(['role:read', 'role:write'])]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\Regex(pattern: '/^[a-z][a-z0-9_]*$/', message: 'Le code doit etre en snake_case et commencer par une lettre minuscule.')]
|
||||
#[Assert\NotBlank(message: 'Le code du rôle est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Regex(pattern: '/^[a-z][a-z0-9_]*$/', message: 'Le code doit être en snake_case et commencer par une lettre minuscule.')]
|
||||
#[Assert\Length(max: 100, maxMessage: 'Le code ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
private string $code;
|
||||
|
||||
#[ORM\Column(length: 255)]
|
||||
#[Groups(['role:read', 'role:write'])]
|
||||
#[Assert\NotBlank]
|
||||
#[Assert\NotBlank(message: 'Le libellé du rôle est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(max: 255, maxMessage: 'Le libellé ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
private string $label;
|
||||
|
||||
#[ORM\Column(type: Types::TEXT, nullable: true)]
|
||||
|
||||
@@ -33,6 +33,7 @@ use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
|
||||
use Symfony\Component\Security\Core\User\UserInterface;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Serializer\Attribute\SerializedName;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
@@ -85,6 +86,8 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface, Busines
|
||||
private ?int $id = null;
|
||||
|
||||
#[ORM\Column(length: 180, unique: true)]
|
||||
#[Assert\NotBlank(message: 'Le nom d\'utilisateur est obligatoire.', normalizer: 'trim')]
|
||||
#[Assert\Length(max: 180, maxMessage: 'Le nom d\'utilisateur ne peut dépasser {{ limit }} caractères.', normalizer: 'trim')]
|
||||
#[Groups(['me:read', 'user:list', 'user:write'])]
|
||||
private ?string $username = null;
|
||||
|
||||
|
||||
@@ -219,6 +219,19 @@
|
||||
"config/routes/security.yaml"
|
||||
]
|
||||
},
|
||||
"symfony/translation": {
|
||||
"version": "8.0",
|
||||
"recipe": {
|
||||
"repo": "github.com/symfony/recipes",
|
||||
"branch": "main",
|
||||
"version": "6.3",
|
||||
"ref": "620a1b84865ceb2ba304c8f8bf2a185fbf32a843"
|
||||
},
|
||||
"files": [
|
||||
"config/packages/translation.yaml",
|
||||
"translations/.gitignore"
|
||||
]
|
||||
},
|
||||
"symfony/twig-bundle": {
|
||||
"version": "8.0",
|
||||
"recipe": {
|
||||
|
||||
@@ -0,0 +1,363 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Architecture;
|
||||
|
||||
use Doctrine\ORM\Mapping\Column;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionProperty;
|
||||
use Symfony\Component\Finder\Finder;
|
||||
use Symfony\Component\Serializer\Attribute\Groups;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
use function in_array;
|
||||
use function is_string;
|
||||
use function sprintf;
|
||||
use function str_contains;
|
||||
|
||||
/**
|
||||
* Garde-fou architecture ERP-107 : toute contrainte `#[Assert\*]` portee par une
|
||||
* entite metier doit avoir un message FR EXPLICITE (et non le defaut anglais de
|
||||
* Symfony), et toute colonne string bornee writable doit avoir une `Assert\Length`
|
||||
* calee sur le `length` de la colonne ORM.
|
||||
*
|
||||
* Pourquoi (lien ERP-101) : le front (useFormErrors / mapViolationsToRecord)
|
||||
* affiche sous chaque champ le `message` renvoye par le back. Un message absent
|
||||
* = defaut anglais ; une colonne bornee sans Assert\Length = erreur Postgres
|
||||
* (500) au lieu d'une 422 propre rattachee au champ.
|
||||
*
|
||||
* Deux verifications, sur le modele de AuditableEntitiesHaveI18nLabelTest :
|
||||
* 1. MESSAGE EXPLICITE : pour chaque contrainte connue, la (ou les) propriete(s)
|
||||
* de message pertinente(s) doivent differer du defaut Symfony. La comparaison
|
||||
* au defaut (instance « nue » de la meme contrainte) evite de valider un
|
||||
* message anglais natif laisse tel quel.
|
||||
* 2. LENGTH == ORM length : toute propriete string writable avec `ORM\Column(length:)`
|
||||
* doit porter `Assert\Length(max:)` egal a ce length — sauf si le format est
|
||||
* deja borne par Bic/Iban, ou whitelistee dans EXCLUDED_LENGTH_MIRROR.
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
final class EntityConstraintsHaveFrenchMessageTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* Proprietes writable exemptees du miroir Assert\Length == ORM length, avec
|
||||
* justification. Toute entree doit citer la raison (format deja borne par une
|
||||
* autre contrainte). Cle : "<ClasseCourte>::<propriete>".
|
||||
*
|
||||
* @var array<string, string>
|
||||
*/
|
||||
private const array EXCLUDED_LENGTH_MIRROR = [
|
||||
// Le Regex /^[0-9]{4,5}$/ borne deja la longueur a 5 caracteres (< 20).
|
||||
'ClientAddress::postalCode' => 'Regex {4,5} borne deja la longueur.',
|
||||
// Le Regex /^#[0-9A-Fa-f]{6}$/ borne la longueur a exactement 7 caracteres.
|
||||
'Site::color' => 'Regex code hex #RRGGBB borne deja la longueur.',
|
||||
];
|
||||
|
||||
/**
|
||||
* Mapping contrainte -> proprietes de message a verifier. Une contrainte
|
||||
* absente de ce mapping (hors Callback) fait ECHOUER le test : il faut
|
||||
* l'ajouter explicitement (anti faux positif vert sur une contrainte inconnue).
|
||||
*
|
||||
* Pour Length / Count, la liste est calculee dynamiquement (minMessage si
|
||||
* `min` est pose, maxMessage si `max` est pose).
|
||||
*
|
||||
* @var list<class-string<Constraint>>
|
||||
*/
|
||||
private const array SIMPLE_MESSAGE_CONSTRAINTS = [
|
||||
Assert\NotBlank::class,
|
||||
Assert\NotNull::class,
|
||||
Assert\Email::class,
|
||||
Assert\Regex::class,
|
||||
Assert\Bic::class,
|
||||
Assert\Iban::class,
|
||||
Assert\PositiveOrZero::class,
|
||||
Assert\Positive::class,
|
||||
Assert\NegativeOrZero::class,
|
||||
Assert\Negative::class,
|
||||
];
|
||||
|
||||
public function testEveryConstraintHasAnExplicitFrenchMessage(): void
|
||||
{
|
||||
$checked = 0;
|
||||
|
||||
foreach ($this->entityProperties() as [$shortClass, $property]) {
|
||||
foreach ($property->getAttributes() as $attribute) {
|
||||
$name = $attribute->getName();
|
||||
if (!is_subclass_of($name, Constraint::class)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Les Callback portent leur message dans la closure : hors scope.
|
||||
if (Assert\Callback::class === $name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @var Constraint $constraint */
|
||||
$constraint = $attribute->newInstance();
|
||||
$messageProps = $this->messagePropertiesFor($constraint);
|
||||
|
||||
self::assertNotNull(
|
||||
$messageProps,
|
||||
sprintf(
|
||||
'Contrainte non geree par le garde-fou : %s sur %s::$%s. '
|
||||
.'Ajouter sa classe au mapping de EntityConstraintsHaveFrenchMessageTest.',
|
||||
$name,
|
||||
$shortClass,
|
||||
$property->getName(),
|
||||
),
|
||||
);
|
||||
|
||||
foreach ($messageProps as $prop) {
|
||||
$actual = $constraint->{$prop} ?? null;
|
||||
$default = $this->defaultMessageFor($name, $prop);
|
||||
|
||||
self::assertTrue(
|
||||
is_string($actual) && '' !== $actual && $actual !== $default,
|
||||
sprintf(
|
||||
'La contrainte %s sur %s::$%s n\'a pas de %s FR explicite '
|
||||
.'(message absent ou laisse au defaut anglais). Cf. ERP-107.',
|
||||
$name,
|
||||
$shortClass,
|
||||
$property->getName(),
|
||||
$prop,
|
||||
),
|
||||
);
|
||||
++$checked;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self::assertGreaterThan(0, $checked, 'Aucune contrainte verifiee : detection d\'attributs cassee ?');
|
||||
}
|
||||
|
||||
public function testBoundedStringColumnsHaveMatchingLength(): void
|
||||
{
|
||||
$checked = 0;
|
||||
|
||||
foreach ($this->entityProperties() as [$shortClass, $property]) {
|
||||
$column = $this->ormColumn($property);
|
||||
if (null === $column || null === $column->length) {
|
||||
continue;
|
||||
}
|
||||
// Colonnes non-string (text, decimal, date...) : pas de length scalaire a calquer.
|
||||
if (null !== $column->type && 'string' !== $column->type) {
|
||||
continue;
|
||||
}
|
||||
// Le miroir ne protege que la saisie utilisateur (champs writable).
|
||||
if (!$this->isPropertyWritable($property)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$constraints = $this->constraintsOf($property);
|
||||
|
||||
// Format deja borne par Bic/Iban : longueur garantie cote contrainte.
|
||||
if ($this->hasAnyConstraint($constraints, [Assert\Bic::class, Assert\Iban::class])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$excludeKey = $shortClass.'::'.$property->getName();
|
||||
if (isset(self::EXCLUDED_LENGTH_MIRROR[$excludeKey])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$length = null;
|
||||
foreach ($constraints as $c) {
|
||||
if ($c instanceof Assert\Length) {
|
||||
$length = $c->max;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
self::assertNotNull(
|
||||
$length,
|
||||
sprintf(
|
||||
'%s::$%s est une colonne string bornee (length=%d) writable sans Assert\Length : '
|
||||
.'risque d\'erreur Postgres 500. Ajouter Assert\Length(max: %d) ou whitelister. Cf. ERP-107.',
|
||||
$shortClass,
|
||||
$property->getName(),
|
||||
$column->length,
|
||||
$column->length,
|
||||
),
|
||||
);
|
||||
self::assertSame(
|
||||
$column->length,
|
||||
$length,
|
||||
sprintf(
|
||||
'Derive Assert\Length.max (%s) != ORM length (%d) sur %s::$%s. '
|
||||
.'Le max doit refleter le length de la colonne (anti-derive ERP-107).',
|
||||
(string) $length,
|
||||
$column->length,
|
||||
$shortClass,
|
||||
$property->getName(),
|
||||
),
|
||||
);
|
||||
++$checked;
|
||||
}
|
||||
|
||||
self::assertGreaterThan(0, $checked, 'Aucune colonne string bornee verifiee : scan casse ?');
|
||||
}
|
||||
|
||||
/**
|
||||
* Itere (classe courte, ReflectionProperty) sur toutes les entites metier
|
||||
* sous src/Module/<m>/Domain/Entity/.
|
||||
*
|
||||
* @return iterable<array{0: string, 1: ReflectionProperty}>
|
||||
*/
|
||||
private function entityProperties(): iterable
|
||||
{
|
||||
$finder = new Finder()
|
||||
->files()
|
||||
->in(__DIR__.'/../../src/Module')
|
||||
->path('Domain/Entity')
|
||||
->name('*.php')
|
||||
;
|
||||
|
||||
self::assertNotEmpty(iterator_to_array($finder), 'Aucune entite scannee : chemin src/Module invalide ?');
|
||||
|
||||
foreach ($finder as $file) {
|
||||
$fqcn = $this->extractFqcn($file->getRealPath());
|
||||
if (null === $fqcn) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$reflection = new ReflectionClass($fqcn);
|
||||
if ($reflection->isAbstract()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($reflection->getProperties() as $property) {
|
||||
yield [$reflection->getShortName(), $property];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Liste des proprietes de message a verifier pour une contrainte donnee, ou
|
||||
* null si la contrainte n'est pas geree (le test echoue alors explicitement).
|
||||
*
|
||||
* @return list<string>|null
|
||||
*/
|
||||
private function messagePropertiesFor(Constraint $constraint): ?array
|
||||
{
|
||||
if ($constraint instanceof Assert\Length) {
|
||||
$props = [];
|
||||
if (null !== $constraint->min) {
|
||||
$props[] = 'minMessage';
|
||||
}
|
||||
if (null !== $constraint->max) {
|
||||
$props[] = 'maxMessage';
|
||||
}
|
||||
|
||||
return $props;
|
||||
}
|
||||
|
||||
if ($constraint instanceof Assert\Count) {
|
||||
$props = [];
|
||||
if (null !== $constraint->min) {
|
||||
$props[] = 'minMessage';
|
||||
}
|
||||
if (null !== $constraint->max) {
|
||||
$props[] = 'maxMessage';
|
||||
}
|
||||
|
||||
return $props;
|
||||
}
|
||||
|
||||
if (in_array($constraint::class, self::SIMPLE_MESSAGE_CONSTRAINTS, true)) {
|
||||
return ['message'];
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Message par defaut d'une contrainte (instance « nue ») pour la propriete
|
||||
* demandee. Sert de reference pour detecter un message laisse au defaut.
|
||||
*/
|
||||
private function defaultMessageFor(string $class, string $prop): ?string
|
||||
{
|
||||
$bare = match ($class) {
|
||||
Assert\Length::class => new Assert\Length(max: 1),
|
||||
Assert\Count::class => new Assert\Count(min: 1),
|
||||
Assert\Regex::class => new Assert\Regex(pattern: '/^x$/'),
|
||||
default => new $class(),
|
||||
};
|
||||
|
||||
$value = $bare->{$prop} ?? null;
|
||||
|
||||
return is_string($value) ? $value : null;
|
||||
}
|
||||
|
||||
private function ormColumn(ReflectionProperty $property): ?Column
|
||||
{
|
||||
$attrs = $property->getAttributes(Column::class);
|
||||
|
||||
return [] === $attrs ? null : $attrs[0]->newInstance();
|
||||
}
|
||||
|
||||
/** @return list<Constraint> */
|
||||
private function constraintsOf(ReflectionProperty $property): array
|
||||
{
|
||||
$out = [];
|
||||
foreach ($property->getAttributes() as $attribute) {
|
||||
if (is_subclass_of($attribute->getName(), Constraint::class)) {
|
||||
$out[] = $attribute->newInstance();
|
||||
}
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<Constraint> $constraints
|
||||
* @param list<class-string<Constraint>> $classes
|
||||
*/
|
||||
private function hasAnyConstraint(array $constraints, array $classes): bool
|
||||
{
|
||||
foreach ($constraints as $c) {
|
||||
if (in_array($c::class, $classes, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function isPropertyWritable(ReflectionProperty $property): bool
|
||||
{
|
||||
$attrs = $property->getAttributes(Groups::class);
|
||||
if ([] === $attrs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var Groups $groups */
|
||||
$groups = $attrs[0]->newInstance();
|
||||
foreach ($groups->groups as $group) {
|
||||
if (is_string($group) && str_contains($group, 'write')) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private function extractFqcn(string $path): ?string
|
||||
{
|
||||
$source = file_get_contents($path);
|
||||
if (false === $source) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (
|
||||
1 !== preg_match('/^namespace\s+([^;]+);/m', $source, $nsMatch)
|
||||
|| 1 !== preg_match('/^(?:final\s+|abstract\s+|readonly\s+)*class\s+(\w+)/m', $source, $classMatch)
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return trim($nsMatch[1]).'\\'.$classMatch[1];
|
||||
}
|
||||
}
|
||||
@@ -66,6 +66,34 @@ final class ClientSubResourceApiTest extends AbstractCommercialApiTestCase
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
}
|
||||
|
||||
/**
|
||||
* ERP-107 : une violation de contrainte sort avec un message FR explicite ET
|
||||
* un `propertyPath` rattache au champ (consommable par useFormErrors /
|
||||
* mapViolationsToRecord cote front, ERP-101). On verifie le JSON 422 reel.
|
||||
*/
|
||||
public function testPostContactInvalidEmailReturns422WithFrenchMessageOnField(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
$seed = $this->seedClient('Contact Bad Email');
|
||||
|
||||
$response = $client->request('POST', '/api/clients/'.$seed->getId().'/contacts', [
|
||||
'headers' => ['Content-Type' => self::LD, 'Accept' => self::LD],
|
||||
'json' => [
|
||||
'firstName' => 'Jean',
|
||||
'email' => 'pas-un-email',
|
||||
],
|
||||
]);
|
||||
|
||||
self::assertResponseStatusCodeSame(422);
|
||||
$byPath = [];
|
||||
foreach ($response->toArray(false)['violations'] ?? [] as $v) {
|
||||
$byPath[$v['propertyPath']] = $v['message'];
|
||||
}
|
||||
|
||||
self::assertArrayHasKey('email', $byPath, 'La violation email doit porter propertyPath=email (mapping front).');
|
||||
self::assertSame('L\'adresse email n\'est pas valide.', $byPath['email']);
|
||||
}
|
||||
|
||||
public function testPatchContactNormalizes(): void
|
||||
{
|
||||
$client = $this->createAdminClient();
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# Les traductions natives FR viennent du vendor (validators.fr.xlf).
|
||||
# Ce dossier accueille les overrides applicatifs eventuels.
|
||||
Reference in New Issue
Block a user