Skip to content

Commit d1cd1ee

Browse files
committed
fix: Fixed field deletion blocked when at least one form question use another field
1 parent 02ec6b4 commit d1cd1ee

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

inc/field.class.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,10 @@ public function pre_deleteItem()
355355
$question = new Question();
356356
$found = $question->find([
357357
'type' => PluginFieldsQuestionType::class,
358-
$this->fields['id'] => new QueryExpression(sprintf(
359-
"JSON_VALUE(%s, '$.field_id')",
358+
new QueryExpression(sprintf(
359+
"JSON_VALUE(%s, '$.field_id') = %s",
360360
DBmysql::quoteName('extra_data'),
361+
DBmysql::quoteValue((int) $this->fields['id']),
361362
)),
362363
]);
363364
if (!empty($found)) {

tests/Units/FieldQuestionTypeTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)