Skip to content

Commit

Permalink
fix(field): avoid html entitization of accented chars
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Nov 15, 2017
1 parent ececfe3 commit a973f7b
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 11 deletions.
2 changes: 0 additions & 2 deletions inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,10 @@ public function prepareQuestionInputForSave($input) {
return [];
} else {
$input['values'] = $this->trimValue($input['values']);
$input['values'] = addslashes($input['values']);
}
}
if (isset($input['default_values'])) {
$input['default_values'] = $this->trimValue($input['default_values']);
$input['default_values'] = addslashes($input['default_values']);
}
return $input;
}
Expand Down
4 changes: 2 additions & 2 deletions inc/fields/multiselectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function displayField($canEdit = true) {
} else {
$answer = $this->getAnswer();
echo '<div class="form_field">';
echo empty($answer) ? '' : implode('<br />', json_decode($answer));
echo empty($answer) ? '' : implode('<br />', $answer);
echo '</div>';
}
}
Expand All @@ -64,7 +64,7 @@ public function getAnswer() {
$return[] = $value;
}
}
return json_encode($return);
return $return;
}

public static function getName() {
Expand Down
2 changes: 0 additions & 2 deletions inc/fields/radiosfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,11 @@ public function prepareQuestionInputForSave($input) {
} else {
// trim values
$input['values'] = $this->trimValue($input['values']);
$input['values'] = addslashes($input['values']);
}
}
if (isset($input['default_values'])) {
// trim values
$input['default_values'] = $this->trimValue($input['default_values']);
$input['default_values'] = addslashes($input['default_values']);
}
return $input;
}
Expand Down
2 changes: 0 additions & 2 deletions inc/fields/selectfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@ public function prepareQuestionInputForSave($input) {
} else {
// trim values
$input['values'] = $this->trimValue($input['values']);
$input['values'] = addslashes($input['values']);
}
}
if (isset($input['default_values'])) {
// trim values
$input['default_values'] = $this->trimValue($input['default_values']);
$input['default_values'] = addslashes($input['default_values']);
}
return $input;
}
Expand Down
13 changes: 11 additions & 2 deletions inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,19 @@ public function prepareInputForUpdate($input) {
}

// Decode (if already encoded) and encode strings to avoid problems with quotes
// The if() {} structures here will grow until the call to plugin_formcreator_encode
// becomes obsolete
foreach ($input as $key => $value) {
if ($input['fieldtype'] != 'dropdown'
|| $input['fieldtype'] != 'dropdown' && $key != 'values') {
$input[$key] = plugin_formcreator_encode($value);
|| $input['fieldtype'] != 'dropdown' && $key != 'values' && $key != 'default_values') {
if (!($input['fieldtype'] == 'select' && ($key == 'values' || $key == 'default_values'))
&& !($input['fieldtype'] == 'checkboxes' && ($key == 'values' || $key == 'default_values'))
&& !($input['fieldtype'] == 'radios' && ($key == 'values' || $key == 'default_values'))
&& !($input['fieldtype'] == 'multiselect' && ($key == 'values' || $key == 'default_values'))) {
$input[$key] = plugin_formcreator_encode($value);
} else {
$input[$key] = str_replace('\r\n', "\r\n", $input[$key]);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
class CheckboxFieldTest extends SuperAdminTestCase {
class CheckboxesFieldTest extends SuperAdminTestCase {

public function provider() {

Expand Down Expand Up @@ -138,4 +138,45 @@ public function testFieldIsValid($fields, $data, $expectedValue, $expectedValidi
$isValid = $fieldInstance->isValid($values);
$this->assertEquals($expectedValidity, $isValid);
}

public function testPrepareInputForSave() {
$fields = array(
'fieldtype' => 'checkboxes',
'name' => 'question',
'required' => '0',
'default_values' => "1\r\n2\r\n3\r\n5\r\n6",
'values' => "1\r\n2\r\n3\r\n4\r\n5\r\n6",
'order' => '1',
'show_rule' => 'always',
'range_min' => 3,
'range_max' => 4,
);
$fieldInstance = new PluginFormcreatorCheckboxesField($fields);

// Test a value is mandatory
$input = [
'values' => "",
'name' => 'foo',
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals(0, count($out));

// Test accented chars are kept
$input = [
'values' => "éè\r\nsomething else",
'default_values' => "éè",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals("éè\r\nsomething else", $out['values']);
$this->assertEquals("éè", $out['default_values']);

// Test values are trimmed
$input = [
'values' => ' something \r\n something else ',
'default_values' => " something ",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals('something\r\nsomething else', $out['values']);
$this->assertEquals("something", $out['default_values']);
}
}
40 changes: 40 additions & 0 deletions tests/0005_Unit/MultiselectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,44 @@ public function testFieldIsValid($fields, $data, $expectedValue, $expectedValidi
$this->assertEquals($expectedValidity, $isValid);
}

public function testPrepareInputForSave() {
$fields = array(
'fieldtype' => 'multiselect',
'name' => 'question',
'required' => '0',
'default_values' => "1\r\n2\r\n3\r\n5\r\n6",
'values' => "1\r\n2\r\n3\r\n4\r\n5\r\n6",
'order' => '1',
'show_rule' => 'always',
'range_min' => 3,
'range_max' => 4,
);
$fieldInstance = new PluginFormcreatorMultiselectField($fields);

// Test a value is mandatory
$input = [
'values' => "",
'name' => 'foo',
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals(0, count($out));

// Test accented chars are kept
$input = [
'values' => "éè\r\nsomething else",
'default_values' => "éè",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals("éè\r\nsomething else", $out['values']);
$this->assertEquals("éè", $out['default_values']);

// Test values are trimmed
$input = [
'values' => ' something \r\n something else ',
'default_values' => " something ",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals('something\r\nsomething else', $out['values']);
$this->assertEquals("something", $out['default_values']);
}
}
43 changes: 43 additions & 0 deletions tests/0005_Unit/RadiosFieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
class RadiossFieldTest extends SuperAdminTestCase {
public function testPrepareInputForSave() {
$fields = array(
'fieldtype' => 'radios',
'name' => 'question',
'required' => '0',
'default_values' => "1\r\n2\r\n3\r\n5\r\n6",
'values' => "1\r\n2\r\n3\r\n4\r\n5\r\n6",
'order' => '1',
'show_rule' => 'always',
'range_min' => 3,
'range_max' => 4,
);
$fieldInstance = new PluginFormcreatorRadiosField($fields);

// Test a value is mandatory
$input = [
'values' => "",
'name' => 'foo',
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals(0, count($out));

// Test accented chars are kept
$input = [
'values' => "éè\r\nsomething else",
'default_values' => "éè",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals("éè\r\nsomething else", $out['values']);
$this->assertEquals("éè", $out['default_values']);

// Test values are trimmed
$input = [
'values' => ' something \r\n something else ',
'default_values' => " something ",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals('something\r\nsomething else', $out['values']);
$this->assertEquals("something", $out['default_values']);
}
}
40 changes: 40 additions & 0 deletions tests/0005_Unit/SelectFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,44 @@ public function testFieldIsValid($fields, $data, $expectedValue, $expectedValidi
$this->assertEquals($expectedValidity, $isValid);
}

public function testPrepareInputForSave() {
$fields = array(
'fieldtype' => 'select',
'name' => 'question',
'required' => '0',
'default_values' => "1\r\n2\r\n3\r\n5\r\n6",
'values' => "1\r\n2\r\n3\r\n4\r\n5\r\n6",
'order' => '1',
'show_rule' => 'always',
'range_min' => 3,
'range_max' => 4,
);
$fieldInstance = new PluginFormcreatorSelectField($fields);

// Test a value is mandatory
$input = [
'values' => "",
'name' => 'foo',
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals(0, count($out));

// Test accented chars are kept
$input = [
'values' => "éè\r\nsomething else",
'default_values' => "éè",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals("éè\r\nsomething else", $out['values']);
$this->assertEquals("éè", $out['default_values']);

// Test values are trimmed
$input = [
'values' => ' something \r\n something else ',
'default_values' => " something ",
];
$out = $fieldInstance->prepareQuestionInputForSave($input);
$this->assertEquals('something\r\nsomething else', $out['values']);
$this->assertEquals("something", $out['default_values']);
}
}

0 comments on commit a973f7b

Please sign in to comment.