Skip to content

Commit

Permalink
fix(section): order lost after section deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Jan 4, 2022
1 parent 2ca80e1 commit ac49573
Showing 2 changed files with 49 additions and 1 deletion.
3 changes: 2 additions & 1 deletion inc/section.class.php
Original file line number Diff line number Diff line change
@@ -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,
47 changes: 47 additions & 0 deletions tests/3-unit/PluginFormcreatorSection.php
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit ac49573

Please sign in to comment.