@@ -177,6 +177,60 @@ public function testFieldsQuestionSubmitEmptyDropdown(): void
177177 ]);
178178 }
179179
180+ public function testFieldDeletionWhenUsedInForm (): void
181+ {
182+ $ this ->login ();
183+
184+ // Arrange: create form with Field question
185+ $ builder = new FormBuilder ("My form " );
186+ $ builder ->addQuestion (
187+ "My question " ,
188+ PluginFieldsQuestionType::class,
189+ extra_data: json_encode ($ this ->getFieldExtraDataConfig ('glpi_item ' )),
190+ );
191+ $ this ->createForm ($ builder );
192+
193+ // Act: try to delete field
194+ $ response = $ this ->fields ['glpi_item ' ]->delete ($ this ->fields ['glpi_item ' ]->fields );
195+
196+ // Assert: deletion is blocked with appropriate message
197+ $ this ->assertFalse ($ response );
198+ $ this ->hasSessionMessageThatContains ('The field "GLPI Item" cannot be deleted because it is used in a form question ' , ERROR );
199+ }
200+
201+ public function testFieldDeletionWhenNotUsedInForm (): void
202+ {
203+ $ this ->login ();
204+
205+ // Act: try to delete field
206+ $ response = $ this ->fields ['glpi_item ' ]->delete ($ this ->fields ['glpi_item ' ]->fields );
207+
208+ // Assert: deletion is successful
209+ $ this ->assertTrue ($ response );
210+ $ this ->hasNoSessionMessage (ERROR );
211+ }
212+
213+ public function testFieldDeletionWhenAnotherFieldUsedInForm (): void
214+ {
215+ $ this ->login ();
216+
217+ // Arrange: create form with Field question using dropdown field
218+ $ builder = new FormBuilder ("My form " );
219+ $ builder ->addQuestion (
220+ "My question " ,
221+ PluginFieldsQuestionType::class,
222+ extra_data: json_encode ($ this ->getFieldExtraDataConfig ('dropdown ' )),
223+ );
224+ $ this ->createForm ($ builder );
225+
226+ // Act: try to delete glpi_item field which isn't used in form but exists
227+ $ response = $ this ->fields ['glpi_item ' ]->delete ($ this ->fields ['glpi_item ' ]->fields );
228+
229+ // Assert: deletion is successful and not blocked by the fact that another field is used in form
230+ $ this ->assertTrue ($ response );
231+ $ this ->hasNoSessionMessage (ERROR );
232+ }
233+
180234 private function getFieldExtraDataConfig (string $ field_name ): PluginFieldsQuestionTypeExtraDataConfig
181235 {
182236 if (!$ this ->block instanceof PluginFieldsContainer || !$ this ->fields [$ field_name ] instanceof PluginFieldsField) {
0 commit comments