Permalink
Browse files

MDL-34171 qformat_gift: Fix edge case with special character escaping.

We need to escape \ on export, because it is un-escaped on import.
  • Loading branch information...
1 parent f2867a8 commit 69a600a067d52af195fb1045ed6c22262af88739 @timhunt timhunt committed Jul 4, 2012
Showing with 71 additions and 2 deletions.
  1. +2 −2 question/format/gift/format.php
  2. +69 −0 question/format/gift/tests/giftformat_test.php
@@ -553,8 +553,8 @@ protected function add_blank_combined_feedback($question) {
protected function repchar($text, $notused = 0) {
// Escapes 'reserved' characters # = ~ {) :
// Removes new lines
- $reserved = array( '#', '=', '~', '{', '}', ':', "\n", "\r");
- $escaped = array('\#','\=','\~','\{','\}','\:', '\n', '' );
+ $reserved = array( '\\', '#', '=', '~', '{', '}', ':', "\n", "\r");
+ $escaped = array('\\\\', '\#','\=','\~','\{','\}','\:', '\n', '' );
$newtext = str_replace($reserved, $escaped, $text);
return $newtext;
@@ -885,4 +885,73 @@ public function test_export_truefalse() {
$this->assert_same_gift($expectedgift, $gift);
}
+
+ public function test_export_backslash() {
+ // There was a bug (MDL-34171) where \\ was getting exported as \\, not
+ // \\\\, and on import, \\ in converted to \.
+ // We need \\\\ in the test code, because of PHPs string escaping rules.
+ $qdata = (object) array(
+ 'id' => 666 ,
+ 'name' => 'backslash',
+ 'questiontext' => 'A \\ B \\\\ C',
+ 'questiontextformat' => FORMAT_MOODLE,
+ 'generalfeedback' => '',
+ 'generalfeedbackformat' => FORMAT_MOODLE,
+ 'defaultmark' => 1,
+ 'penalty' => 0.3333333,
+ 'length' => 1,
+ 'qtype' => 'essay',
+ 'options' => (object) array(
+ 'responseformat' => 'editor',
+ 'responsefieldlines' => 15,
+ 'attachments' => 0,
+ 'graderinfo' => '',
+ 'graderinfoformat' => FORMAT_HTML,
+ ),
+ );
+
+ $exporter = new qformat_gift();
+ $gift = $exporter->writequestion($qdata);
+
+ $expectedgift = "// question: 666 name: backslash
+::backslash::A \\\\ B \\\\\\\\ C{}
+
+";
+
+ $this->assert_same_gift($expectedgift, $gift);
+ }
+
+ public function test_import_backslash() {
+ // There was a bug (MDL-34171) where \\ in the import was getting changed
+ // to \. This test checks for that.
+ // We need \\\\ in the test code, because of PHPs string escaping rules.
+ $gift = '
+// essay
+::double backslash:: A \\\\ B \\\\\\\\ C{}';
+ $lines = preg_split('/[\\n\\r]/', str_replace("\r\n", "\n", $gift));
+
+ $importer = new qformat_gift();
+ $q = $importer->readquestion($lines);
+
+ $expectedq = (object) array(
+ 'name' => 'double backslash',
+ 'questiontext' => 'A \\ B \\\\ C',
+ 'questiontextformat' => FORMAT_MOODLE,
+ 'generalfeedback' => '',
+ 'generalfeedbackformat' => FORMAT_MOODLE,
+ 'qtype' => 'essay',
+ 'defaultmark' => 1,
+ 'penalty' => 0.3333333,
+ 'length' => 1,
+ 'responseformat' => 'editor',
+ 'responsefieldlines' => 15,
+ 'attachments' => 0,
+ 'graderinfo' => array(
+ 'text' => '',
+ 'format' => FORMAT_HTML,
+ 'files' => array()),
+ );
+
+ $this->assert(new question_check_specified_fields_expectation($expectedq), $q);
+ }
}

0 comments on commit 69a600a

Please sign in to comment.