From 1c696321a132a9840a99409d7abe415186f806d5 Mon Sep 17 00:00:00 2001 From: Thierry Bugier Date: Tue, 28 Jul 2020 09:19:40 +0200 Subject: [PATCH] fix(formanswer): do not render section title if invisible Signed-off-by: Thierry Bugier --- inc/formanswer.class.php | 16 ++- .../PluginFormcreatorFormAnswer.php | 111 +++++++++++++++--- 2 files changed, 108 insertions(+), 19 deletions(-) diff --git a/inc/formanswer.class.php b/inc/formanswer.class.php index 1ba2542b7..89198e6a5 100644 --- a/inc/formanswer.class.php +++ b/inc/formanswer.class.php @@ -1051,7 +1051,7 @@ public function getFullForm($richText = false) { $questions = $DB->request([ 'SELECT' => [ $sectionTable => ['name as section_name'], - $questionTable => ['id', 'fieldtype'], + $questionTable => ['id', 'fieldtype', $sectionFk], ], 'FROM' => [ $questionTable, @@ -1066,7 +1066,7 @@ public function getFullForm($richText = false) { ], 'WHERE' => [ 'AND' => [ - "$sectionTable.$formFk" => $this->fields['plugin_formcreator_forms_id'], + "$sectionTable.$formFk" => $this->fields[$formFk], ], ], 'GROUPBY' => [ @@ -1079,17 +1079,23 @@ public function getFullForm($richText = false) { "$questionTable.col *ASC", ], ]); - $last_section = ""; + $last_section = -1; while ($question_line = $questions->next()) { // Get and display current section if needed - if ($last_section != $question_line['section_name']) { + if ($last_section != $question_line[$sectionFk]) { + $currentSection = new PluginFormcreatorSection(); + $currentSection->getFromDB($question_line[$sectionFk]); + if (!PluginFormcreatorFields::isVisible($currentSection, $fields)) { + // The section is not visible, skip it as well all its questions + continue; + } if ($richText) { $output .= '

' . $question_line['section_name'] . '

'; } else { $output .= $eol . $question_line['section_name'] . $eol; $output .= '---------------------------------' . $eol; } - $last_section = $question_line['section_name']; + $last_section = $question_line[$sectionFk]; } // Don't save tags in "full form" diff --git a/tests/suite-unit/PluginFormcreatorFormAnswer.php b/tests/suite-unit/PluginFormcreatorFormAnswer.php index ce434ff2e..b480a4e9c 100644 --- a/tests/suite-unit/PluginFormcreatorFormAnswer.php +++ b/tests/suite-unit/PluginFormcreatorFormAnswer.php @@ -38,32 +38,115 @@ public function beforeTestMethod($method) { case 'testSaveForm': case 'testGetFullForm': case 'testCanValidate': - self::login('glpi', 'glpi'); + $this->login('glpi', 'glpi'); } } - public function testGetFullForm() { + public function providerGetFullForm() { $form = $this->getForm(); - $section = $this->getSection([ + $section1 = $this->getSection([ \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), - 'name' => \Toolbox::addslashes_deep("section '1'"), + 'name' => \Toolbox::addslashes_deep("section 1"), ]); - $question = $this->getQuestion([ - \PluginFormcreatorSection::getForeignKeyField() => $section->getID(), - 'name' => \Toolbox::addslashes_deep("question '1'"), + $question1 = $this->getQuestion([ + \PluginFormcreatorSection::getForeignKeyField() => $section1->getID(), + 'name' => \Toolbox::addslashes_deep("radios for section"), + 'fieldtype' => 'radios', + 'values' => "yes\r\nno", ]); - - $instance = $this->newTestedInstance(); - $instance->add([ + $question2 = $this->getQuestion([ + \PluginFormcreatorSection::getForeignKeyField() => $section1->getID(), + 'name' => \Toolbox::addslashes_deep("radios for question"), + 'fieldtype' => 'radios', + 'values' => "yes\r\nno", + ]); + $section2 = $this->getSection([ \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), - 'formcreator_field_' . $question->getID() => '' + 'name' => \Toolbox::addslashes_deep("section 2"), + 'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_HIDDEN, + '_conditions' => [ + 'plugin_formcreator_questions_id' => [$question1->getID()], + 'show_condition' => [\PluginFormcreatorCondition::SHOW_CONDITION_EQ], + 'show_value' => ['yes'], + 'show_logic' => [\PluginFormcreatorCondition::SHOW_LOGIC_AND], + ] ]); + $question3 = $this->getQuestion([ + \PluginFormcreatorSection::getForeignKeyField() => $section2->getID(), + 'name' => \Toolbox::addslashes_deep("text"), + 'fieldtype' => 'text', + 'values' => 'hello', + 'show_rule' => \PluginFormcreatorCondition::SHOW_RULE_HIDDEN, + '_conditions' => [ + 'plugin_formcreator_questions_id' => [$question2->getID()], + 'show_condition' => [\PluginFormcreatorCondition::SHOW_CONDITION_EQ], + 'show_value' => ['yes'], + 'show_logic' => [\PluginFormcreatorCondition::SHOW_LOGIC_AND], + ] + ]); + + return [ + // fullForm matches all question and section names + [ + 'answer' => [ + \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), + 'formcreator_field_' . $question1->getID() => 'yes', + 'formcreator_field_' . $question2->getID() => 'yes', + 'formcreator_field_' . $question3->getID() => 'foo', + ], + 'expected' => function($output) use($section1, $section2, $question1, $question2, $question3) { + $this->string($output)->contains($section1->fields['name']); + $this->string($output)->contains('##question_' . $question1->getID() . '##'); + $this->string($output)->contains('##question_' . $question2->getID() . '##'); + $this->string($output)->contains($section2->fields['name']); + $this->string($output)->contains('##question_' . $question3->getID() . '##'); + } + ], + // fullForm matches only visible section names + [ + 'answer' => [ + \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), + 'formcreator_field_' . $question1->getID() => 'no', + 'formcreator_field_' . $question2->getID() => 'yes', + 'formcreator_field_' . $question3->getID() => 'foo', + ], + 'expected' => function($output) use($section1, $section2, $question1, $question2, $question3) { + $this->string($output)->contains($section1->fields['name']); + $this->string($output)->contains('##question_' . $question1->getID() . '##'); + $this->string($output)->contains('##question_' . $question2->getID() . '##'); + $this->string($output)->notContains($section2->fields['name']); + $this->string($output)->notContains('##question_' . $question3->getID() . '##'); + } + ], + // fullForm matches only visible question names + [ + 'answer' => [ + \PluginFormcreatorForm::getForeignKeyField() => $form->getID(), + 'formcreator_field_' . $question1->getID() => 'yes', + 'formcreator_field_' . $question2->getID() => 'no', + 'formcreator_field_' . $question3->getID() => 'foo', + ], + 'expected' => function($output) use($section1, $section2, $question1, $question2, $question3) { + $this->string($output)->contains($section1->fields['name']); + $this->string($output)->contains('##question_' . $question1->getID() . '##'); + $this->string($output)->contains('##question_' . $question2->getID() . '##'); + $this->string($output)->contains($section2->fields['name']); + $this->string($output)->notContains('##question_' . $question3->getID() . '##'); + } + ], + ]; + } - $questionId = $question->getID(); + /** + * @dataProvider providerGetFullForm + */ + public function testGetFullForm($answers, $expected) { + $instance = $this->newTestedInstance(); + $output = $instance->add($answers); + $this->boolean($instance->isNewItem())->isFalse(); $output = $instance->getFullForm(true); - $this->string($output)->contains("section '1'"); - $this->string($output)->contains("##question_$questionId##"); + $expected($output); } public function testSaveForm() {