From 08e934be08951a13f13d5f6cf48dc5842f918a9f Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Wed, 5 Feb 2020 10:32:47 +0100 Subject: [PATCH] fix(question,section): duplication failure Signed-off-by: Thierry Bugier --- inc/condition.class.php | 21 ++++++++++++++----- inc/questiondependency.class.php | 10 +++++++-- inc/section.class.php | 18 +++++++++++++++- tests/suite-unit/PluginFormcreatorSection.php | 1 + 4 files changed, 42 insertions(+), 8 deletions(-) diff --git a/inc/condition.class.php b/inc/condition.class.php index d8f67242f..485bc7cb1 100644 --- a/inc/condition.class.php +++ b/inc/condition.class.php @@ -94,8 +94,6 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con throw new ImportFailureException('UUID or ID is mandatory'); } - //$itemtypeFk = $input['itemtype']::getForeignKeyField(); - //$questionFk = PluginFormcreatorQuestion::getForeignKeyField(); $input['items_id'] = $containerId; $item = new self(); @@ -119,10 +117,16 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con } // set ID for linked objects - $linked = $linker->getObject($input['plugin_formcreator_questions_id'], $input['itemtype']); + $linked = $linker->getObject($input['plugin_formcreator_questions_id'], PluginFormcreatorQuestion::class); if ($linked === false) { - $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); - return false; + $linked = new PluginFormcreatorQuestion(); + $linked->getFromDBByCrit([ + $idKey => $input['plugin_formcreator_questions_id'] + ]); + if ($linked->isNewItem()) { + $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); + return false; + } } $input['plugin_formcreator_questions_id'] = $linked->getID(); @@ -170,6 +174,13 @@ public function export($remove_uuid = false) { $question = new PluginFormcreatorQuestion(); $question->getFromDB($condition['plugin_formcreator_questions_id']); $condition['plugin_formcreator_questions_id'] = $question->fields['uuid']; + $containerType = $input['itemtype']; + if (!class_exists($containerType) || !is_subclass_of($containerType, PluginFormcreatorConditionnableInterface::class)) { + return false; + } + $container = new $containerType(); + $container->getFromDB($condition['items_id']); + $condition['items_id'] = $container->fields['uuid']; } unset($condition[$idToRemove]); diff --git a/inc/questiondependency.class.php b/inc/questiondependency.class.php index 926d055ec..20da5e226 100644 --- a/inc/questiondependency.class.php +++ b/inc/questiondependency.class.php @@ -171,8 +171,14 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con // set ID for linked objects $linked = $linker->getObject($input['plugin_formcreator_questions_id_2'], PluginFormcreatorQuestion::class); if ($linked === false) { - $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); - return false; + $linked = new PluginFormcreatorQuestion(); + $linked->getFromDBByCrit([ + $idKey => $input['plugin_formcreator_questions_id'] + ]); + if ($linked->isNewItem()) { + $linker->postpone($input[$idKey], $item->getType(), $input, $containerId); + return false; + } } $input['plugin_formcreator_questions_id_2'] = $linked->getID(); diff --git a/inc/section.class.php b/inc/section.class.php index 405052a3e..7df60081d 100644 --- a/inc/section.class.php +++ b/inc/section.class.php @@ -260,6 +260,7 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con // add the section to the linker $linker->addObject($originalId, $item); + // Import the questions if (isset($input['_questions'])) { // sort questions by order usort($input['_questions'], function ($a, $b) { @@ -274,6 +275,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con } } + // Import conditions + if (isset($input['_conditions'])) { + foreach ($input['_conditions'] as $condition) { + PluginFormcreatorCondition::import($linker, $condition, $itemId); + } + } + return $itemId; } @@ -284,7 +292,7 @@ public function export($remove_uuid = false) { return false; } - $section = $this->fields; + $section = $this->fields; // remove key and fk $formFk = PluginFormcreatorForm::getForeignKeyField(); @@ -306,6 +314,14 @@ public function export($remove_uuid = false) { } } + // get question conditions + $section['_conditions'] = []; + $condition = new PluginFormcreatorCondition(); + $all_conditions = $condition->getConditionsFromItem($this); + foreach ($all_conditions as $condition) { + $section['_conditions'][] = $condition->export($remove_uuid); + } + // remove ID or UUID $idToRemove = 'id'; if ($remove_uuid) { diff --git a/tests/suite-unit/PluginFormcreatorSection.php b/tests/suite-unit/PluginFormcreatorSection.php index ccba8a830..d0c387cbd 100644 --- a/tests/suite-unit/PluginFormcreatorSection.php +++ b/tests/suite-unit/PluginFormcreatorSection.php @@ -158,6 +158,7 @@ public function testExport() { ]; $extraFields = [ '_questions', + '_conditions', ]; $this->array($output) ->hasKeys($fieldsWithoutID + $extraFields + ['uuid'])