Skip to content

Commit 7bfe6ca

Browse files
committed
fix(section): condition rule loss after duplicate / import
1 parent bc6a790 commit 7bfe6ca

File tree

2 files changed

+81
-22
lines changed

2 files changed

+81
-22
lines changed

inc/section.class.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,10 @@ public function prepareInputForAdd($input) {
142142
}
143143
}
144144

145-
if (!$this->checkConditionSettings($input)) {
146-
$input['show_rule'] = PluginFormcreatorCondition::SHOW_RULE_ALWAYS;
145+
if (!$this->skipChecks) {
146+
if (!$this->checkConditionSettings($input)) {
147+
$input['show_rule'] = PluginFormcreatorCondition::SHOW_RULE_ALWAYS;
148+
}
147149
}
148150

149151
return $input;
@@ -411,7 +413,7 @@ public function export(bool $remove_uuid = false) : array {
411413
unset($export[$formFk]);
412414

413415
$subItems = [
414-
'_questions' => PluginFormcreatorQuestion::class,
416+
'_questions' => PluginFormcreatorQuestion::class,
415417
'_conditions' => PluginFormcreatorCondition::class,
416418
];
417419
$export = $this->exportChildrenObjects($subItems, $export, $remove_uuid);

tests/3-unit/PluginFormcreatorSection.php

Lines changed: 76 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,26 +83,50 @@ public function beforeTestMethod($method) {
8383
public function testDuplicate() {
8484
global $DB;
8585

86-
// instanciate classes
87-
$form = new PluginFormcreatorForm;
88-
$section = $this->newTestedInstance();
89-
$question = new PluginFormcreatorQuestion;
90-
9186
// create objects
92-
$forms_id = $form->add(['name' => "test clone form",
93-
'is_active' => true,
94-
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER]);
95-
$sections_id = $section->add(['name' => "test clone section",
96-
'plugin_formcreator_forms_id' => $forms_id]);
97-
$question->add(['name' => "test clone question 1",
98-
'fieldtype' => 'text',
99-
'plugin_formcreator_sections_id' => $sections_id]);
100-
$question->add(['name' => "test clone question 2",
101-
'fieldtype' => 'textarea',
102-
'plugin_formcreator_sections_id' => $sections_id]);
103-
104-
//get section
105-
$section->getFromDB($sections_id);
87+
$form = $this->getForm([
88+
'name' => "test clone form",
89+
'is_active' => true,
90+
'validation_required' => PluginFormcreatorForm_Validator::VALIDATION_USER
91+
]);
92+
$other_section = $this->getSection([
93+
'plugin_formcreator_forms_id' => $form->getID(),
94+
]);
95+
$other_question = $this->getQuestion([
96+
'plugin_formcreator_sections_id' => $other_section->getID(),
97+
]);
98+
$section = $this->getSection([
99+
'name' => "test clone section",
100+
'plugin_formcreator_forms_id' => $form->getID(),
101+
]);
102+
$condition = new PluginFormcreatorCondition();
103+
$condition->add([
104+
'itemtype' => $section::getType(),
105+
'items_id' => $section->getID(),
106+
'show_logic' => PluginFormcreatorCondition::SHOW_LOGIC_AND,
107+
'show_condition' => PluginFormcreatorCondition::SHOW_CONDITION_EQ,
108+
'show_value' => 'foo',
109+
'plugin_formcreator_questions_id' => $other_question->getID(),
110+
]);
111+
$this->boolean($condition->isNewItem())->isFalse();
112+
113+
$DB->update($section::getTable(), [
114+
'show_rule' => PluginFormcreatorCondition::SHOW_RULE_HIDDEN,
115+
], [
116+
'id' => $section->getID(),
117+
]);
118+
$section->getFromDB($section->getID()); // Refresh instance
119+
120+
$this->getQuestion([
121+
'name' => "test clone question 1",
122+
'fieldtype' => 'text',
123+
'plugin_formcreator_sections_id' => $section->getID(),
124+
]);
125+
$this->getQuestion([
126+
'name' => "test clone question 2",
127+
'fieldtype' => 'textarea',
128+
'plugin_formcreator_sections_id' => $section->getID(),
129+
]);
106130

107131
//get max order of sections
108132
$max = PluginFormcreatorCommon::getMax(
@@ -151,6 +175,39 @@ public function testDuplicate() {
151175
$new_uuids[] = $question['uuid'];
152176
}
153177
$this->integer(count(array_diff($new_uuids, $uuids)))->isEqualTo(count($new_uuids));
178+
179+
// Check conditions
180+
$all_conditions = $DB->request([
181+
'SELECT' => ['uuid'],
182+
'FROM' => PluginFormcreatorCondition::getTable(),
183+
'WHERE' => [
184+
'itemtype' => $section::getType(),
185+
'items_id' => $section->getID(),
186+
],
187+
]);
188+
189+
$all_new_conditions = $DB->request([
190+
'SELECT' => ['uuid'],
191+
'FROM' => PluginFormcreatorCondition::getTable(),
192+
'WHERE' => [
193+
'itemtype' => $new_section::getType(),
194+
'items_id' => $new_section->getID(),
195+
],
196+
]);
197+
$this->integer(count($all_new_questions))->isEqualTo(count($all_questions));
198+
199+
// check that all conditions uuid are new
200+
$uuids = $new_uuids = [];
201+
foreach ($all_conditions as $condition) {
202+
$uuids[] = $condition['uuid'];
203+
}
204+
foreach ($all_new_conditions as $condition) {
205+
$new_uuids[] = $condition['uuid'];
206+
}
207+
$this->integer(count(array_diff($new_uuids, $uuids)))->isEqualTo(count($new_uuids));
208+
209+
// Check that new section has same show rule as original
210+
$this->integer($section->fields['show_rule'])->isEqualTo($new_section->fields['show_rule']);
154211
}
155212

156213
public function testExport() {

0 commit comments

Comments
 (0)