From 69a600a067d52af195fb1045ed6c22262af88739 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Wed, 4 Jul 2012 10:55:52 +0100 Subject: [PATCH] MDL-34171 qformat_gift: Fix edge case with special character escaping. We need to escape \ on export, because it is un-escaped on import. --- question/format/gift/format.php | 4 +- .../format/gift/tests/giftformat_test.php | 69 +++++++++++++++++++ 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/question/format/gift/format.php b/question/format/gift/format.php index 7aa95f2b712..06bcebc564a 100644 --- a/question/format/gift/format.php +++ b/question/format/gift/format.php @@ -553,8 +553,8 @@ class qformat_gift extends qformat_default { 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; diff --git a/question/format/gift/tests/giftformat_test.php b/question/format/gift/tests/giftformat_test.php index 73a8a6ea9f5..e06a12344af 100644 --- a/question/format/gift/tests/giftformat_test.php +++ b/question/format/gift/tests/giftformat_test.php @@ -885,4 +885,73 @@ FALSE#42 is the Ultimate Answer.#You gave the right answer.}"; $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); + } } -- 2.43.0