diff --git a/inc/section.class.php b/inc/section.class.php index d45489c85..dba2f9812 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -194,8 +194,9 @@ public function post_purgeItem() { $formFk => $this->fields[$formFk] ], ]); - $section = new self(); foreach ($rows as $row) { + /** @var PluginFormcreatorSection $section */ + $section = self::getById($row['id']); $section->update([ 'id' => $row['id'], 'order' => $section->fields['order'] - 1, diff --git a/tests/3-unit/PluginFormcreatorSection.php b/tests/3-unit/PluginFormcreatorSection.php index d0ec0e0ab..fdca95456 100644 --- a/tests/3-unit/PluginFormcreatorSection.php +++ b/tests/3-unit/PluginFormcreatorSection.php @@ -451,4 +451,51 @@ public function testGetTranslatableStrings() { ], ]); } + + public function post_purgeItem() { + $sections = []; + $sections[1] = $this->getSection(); + $this->boolean($sections[1]->isNewItem())->isFalse(); + $this->integer((int) $sections[1]->fields['order'])->isEqualTo(1); + + /** @var PluginFormcreatorForm $form */ + $form = \PluginFormcreatorForm::getById($sections[1]->fields['plugin_formcreator_forms_id']); + $this->boolean($form->isNewItem())->isFalse(); + + $sections[2] = $this->getSection([ + 'plugin_formcreator_forms_id' => $form->getID(), + ]); + $this->boolean($sections[2]->isNewItem())->isFalse(); + $this->integer((int) $sections[2]->fields['order'])->isEqualTo(2); + $sections[3] = $this->getSection([ + 'plugin_formcreator_forms_id' => $form->getID(), + ]); + $this->boolean($sections[3]->isNewItem())->isFalse(); + $this->integer((int) $sections[3]->fields['order'])->isEqualTo(3); + + $questions = []; + $questions[1] = $this->getQuestion([ + 'plugin_formcreator_sections_id' => $sections[1]->getID(), + ]); + $this->boolean($questions[1]->isNewItem())->isFalse(); + $questions[1] = $this->getQuestion([ + 'plugin_formcreator_sections_id' => $sections[1]->getID(), + ]); + $this->boolean($questions[2]->isNewItem())->isFalse(); + + // A form with 3 sections, the first sections has 2 questions + + $sections[1]->post_purgeItem(); + + // check the sections 2 and 3 decreased their position + $sections[2]->getFromDB($sections[2]->getID()); + $sections[3]->getFromDB($sections[3]->getID()); + + $this->integer((int) $sections[2]->fields['order'])->isEqualTo(1); + $this->integer((int) $sections[3]->fields['order'])->isEqualTo(2); + + // check the questions are deleted + $this->boolean(\PluginFormcreatorQuestion::getById($questions[1]->getID()))->isFalse(); + $this->boolean(\PluginFormcreatorQuestion::getById($questions[2]->getID()))->isFalse(); + } }