Skip to content

Commit

Permalink
Merge branch 'MDL-42519' of git://github.com/timhunt/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewnicols committed Jan 5, 2015
2 parents 1f8cc2e + 46e4c35 commit e2b9458
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions question/format/xml/format.php
Expand Up @@ -1055,6 +1055,10 @@ public function get_single($id) {
*/
public function xml_escape($string) {
if (!empty($string) && htmlspecialchars($string) != $string) {
// If the string contains something that looks like the end
// of a CDATA section, then we need to avoid errors by splitting
// the string between two CDATA sections.
$string = str_replace(']]>', ']]]]><![CDATA[>', $string);
return "<![CDATA[{$string}]]>";
} else {
return $string;
Expand Down
34 changes: 34 additions & 0 deletions question/format/xml/tests/xmlformat_test.php
Expand Up @@ -111,6 +111,40 @@ protected function itemid_to_files($var) {
return $newvar;
}

public function test_xml_escape_simple_input_not_escaped() {
$exporter = new qformat_xml();
$string = 'Nothing funny here. Even if we go to a café or to 日本.';
$this->assertEquals($string, $exporter->xml_escape($string));
}

public function test_xml_escape_html_wrapped_in_cdata() {
$exporter = new qformat_xml();
$string = '<p>Nothing <b>funny<b> here. Even if we go to a café or to 日本.</p>';
$this->assertEquals('<![CDATA[' . $string . ']]>', $exporter->xml_escape($string));
}

public function test_xml_escape_script_tag_handled_ok() {
$exporter = new qformat_xml();
$input = '<script><![CDATA[alert(1<2);]]></script>';
$expected = '<![CDATA[<script><![CDATA[alert(1<2);]]]]><![CDATA[></script>]]>';
$this->assertEquals($expected, $exporter->xml_escape($input));

// Check that parsing the expected result does give the input again.
$parsed = simplexml_load_string('<div>' . $expected . '</div>');
$this->assertEquals($input, $parsed->xpath('//div')[0]);
}

public function test_xml_escape_code_that_looks_like_cdata_end_ok() {
$exporter = new qformat_xml();
$input = "if (x[[0]]>a) print('hah');";
$expected = "<![CDATA[if (x[[0]]]]><![CDATA[>a) print('hah');]]>";
$this->assertEquals($expected, $exporter->xml_escape($input));

// Check that parsing the expected result does give the input again.
$parsed = simplexml_load_string('<div>' . $expected . '</div>');
$this->assertEquals($input, $parsed->xpath('//div')[0]);
}

public function test_write_hint_basic() {
$q = $this->make_test_question();
$q->name = 'Short answer question';
Expand Down

0 comments on commit e2b9458

Please sign in to comment.