From 9db4229a99eb401141185287c0e366e95e18d382 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Tue, 27 Apr 2021 22:46:36 +0200 Subject: [PATCH] fix(section): ensure unique order for duplicate Signed-off-by: Thierry Bugier --- inc/question.class.php | 4 +--- inc/section.class.php | 8 ++++++-- tests/3-unit/PluginFormcreatorSection.php | 10 ++++++++++ 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/inc/question.class.php b/inc/question.class.php index ca9cf8a81..cfc9c3563 100644 --- a/inc/question.class.php +++ b/inc/question.class.php @@ -401,12 +401,10 @@ public function prepareInputForAdd($input) { if (!isset($input['width'])) { $input['width'] = PluginFormcreatorSection::COLUMNS - $input['col']; } - $sectionFk = PluginFormcreatorSection::getForeignKeyField(); // Get next row if ($this->useAutomaticOrdering) { - $sectionFk = PluginFormcreatorSection::getForeignKeyField(); $maxRow = PluginFormcreatorCommon::getMax($this, [ - $sectionFk => $input[$sectionFk] + self::$items_id => $input[self::$items_id] ], 'row'); if ($maxRow === null) { $input['row'] = 0; diff --git a/inc/section.class.php b/inc/section.class.php index f2c7029b7..7861f263e 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -86,9 +86,8 @@ public function prepareInputForAdd($input) { // Get next order if ($this->useAutomaticOrdering) { - $formId = $input['plugin_formcreator_forms_id']; $maxOrder = PluginFormcreatorCommon::getMax($this, [ - "plugin_formcreator_forms_id" => $formId + self::$items_id => $input[self::$items_id] ], 'order'); if ($maxOrder === null) { $input['order'] = 1; @@ -180,6 +179,11 @@ public function duplicate(array $options = []) { $formFk = PluginFormcreatorForm::getForeignKeyField(); $export = $this->export(true); + $export['order'] = PluginFormcreatorCommon::getMax( + $this, + [$formFk => $this->fields[$formFk]], + 'order' + ) + 1; $newSectionId = static::import($linker, $export, $this->fields[$formFk]); if ($newSectionId === false) { diff --git a/tests/3-unit/PluginFormcreatorSection.php b/tests/3-unit/PluginFormcreatorSection.php index a55395d4c..b97543ddf 100644 --- a/tests/3-unit/PluginFormcreatorSection.php +++ b/tests/3-unit/PluginFormcreatorSection.php @@ -97,6 +97,13 @@ public function testDuplicate() { //get section $section->getFromDB($sections_id); + //get max order of sections + $max = \PluginFormcreatorCommon::getMax( + $section, + ['plugin_formcreator_forms_id' => $section->fields['plugin_formcreator_forms_id']], + 'order' + ); + //clone it $newSection_id = $section->duplicate(); $this->integer($newSection_id)->isGreaterThan(0); @@ -108,6 +115,9 @@ public function testDuplicate() { // check uuid $this->string($new_section->getField('uuid'))->isNotEqualTo($section->getField('uuid')); + // check order + $this->integer((int) $new_section->fields['order'])->isEqualTo($max + 1); + // check questions $all_questions = $DB->request([ 'SELECT' => ['uuid'],