Skip to content

Commit

Permalink
fix(section): ensure unique order for duplicate
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Apr 28, 2021
1 parent f9fabb5 commit 9db4229
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
4 changes: 1 addition & 3 deletions inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions inc/section.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions tests/3-unit/PluginFormcreatorSection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'],
Expand Down

0 comments on commit 9db4229

Please sign in to comment.