Skip to content

Commit

Permalink
fix(formanswer): do not render section title if invisible
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Jul 28, 2020
1 parent 5801968 commit 6bb6be3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 19 deletions.
16 changes: 11 additions & 5 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,7 @@ public function getFullForm($richText = false) {
$questions = $DB->request([
'SELECT' => [
$sectionTable => ['name as section_name'],
$questionTable => ['id', 'fieldtype'],
$questionTable => ['id', 'fieldtype', $sectionFk],
],
'FROM' => [
$questionTable,
Expand All @@ -1081,7 +1081,7 @@ public function getFullForm($richText = false) {
],
'WHERE' => [
'AND' => [
"$sectionTable.$formFk" => $this->fields['plugin_formcreator_forms_id'],
"$sectionTable.$formFk" => $this->fields[$formFk],
],
],
'GROUPBY' => [
Expand All @@ -1093,17 +1093,23 @@ public function getFullForm($richText = false) {
"$questionTable.order *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 .= '<h2>' . $question_line['section_name'] . '</h2>';
} 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"
Expand Down
111 changes: 97 additions & 14 deletions tests/suite-unit/PluginFormcreatorFormAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,115 @@ public function beforeTestMethod($method) {
parent::beforeTestMethod($method);
switch ($method) {
case 'testSaveForm':
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() {
Expand Down

0 comments on commit 6bb6be3

Please sign in to comment.