MDL-61619 phpunit: phpunit test confirming bug in new xmlize code
authorJake Dallimore <jake@moodle.com>
Mon, 12 Mar 2018 02:55:51 +0000 (10:55 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 12 Mar 2018 03:11:58 +0000 (11:11 +0800)
lib/tests/sample_questions_with_old_image_tag.ser [new file with mode: 0644]
lib/tests/sample_questions_with_old_image_tag.xml [new file with mode: 0644]
lib/tests/xmlize_test.php

diff --git a/lib/tests/sample_questions_with_old_image_tag.ser b/lib/tests/sample_questions_with_old_image_tag.ser
new file mode 100644 (file)
index 0000000..959d302
--- /dev/null
@@ -0,0 +1,13 @@
+a:1:{s:4:"quiz";a:2:{s:1:"@";a:0:{}s:1:"#";a:1:{s:8:"question";a:3:{i:0;a:2:{s:1:"@";a:1:{s:4:"type";s:8:"category";}s:1:"#";a:1:{s:8:"category";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:42:"$course$/Default for Test images in backup";}}}}}}}i:1;a:2:{s:1:"@";a:1:{s:4:"type";s:5:"cloze";}s:1:"#";a:6:{s:4:"name";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:25:"cloze question with image";}}}}}s:12:"questiontext";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:650:"This is a multianswer question with an image in the old &quot;image&quot; field that was used in moodle 1.9.<br />Match the following cities with the correct state:<br />* San Francisco: {1:MULTICHOICE:=California#OK~Arizona#Wrong}<br />* Tucson: {1:MULTICHOICE:California#Wrong~%100%Arizona#OK}<br />* Los Angeles: {1:MULTICHOICE:=California#OK~Arizona#Wrong}<br />* Phoenix: {1:MULTICHOICE:%0%California#Wrong~=Arizona#OK}<br />The capital of France is {1:SHORTANSWER:%100%Paris#Congratulations!<br />~%50%Marseille#No, that is the second largest city in France (after<br />Paris).~*#Wrong answer. The capital of France is Paris, of course.}.<br />";}}}}}s:5:"image";a:1:{i:0;a:1:{s:1:"#";s:10:"moodle.png";}}s:12:"image_base64";a:1:{i:0;a:1:{s:1:"#";s:815:"
+iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAHzSURBVDhPxZFbSJNhGIDXERsVGbKawWJaoyAYGeqNYgdl6zenrhhUlB0I6iJEbUl0WF1UUK2DYhDkGghu7Z+HraisPLAsu+0ygi66kCioqwg2t6f///YhzAVBXfTc/HzP9/J8L/wG/pG8QDw+TE+Xn/O+dva07MW5vZayys3yNp/ZwPPRBPuOHqGisordDc00Ki42bqhAjQ3Jid8jAvoL9vIt4qs463G73VRtrcFiseDvuSYG+TwB7y/zfdzHpzeDWachAjeuXyWkPqZ21zZ2Nrioa/JQarOjlK8jes7Eh/s18GwHRMxkgka+3lvEi1ubcgM63Zea6W5v5ESLB4fDwRVvNam+eRBey3TAxqObNl76rXwLFvAlsIyhC2W5gZ+JJmYixQS9q8RZZzpghwEr/R2LpYGPIYWZqJnYSWNugCkPPDDx1JddTyeZqCetasMdS6WB9OQB7Xet5kmbITeQntoP/YWEfSZxFoy5SA2YCXeukAJ+vD5MMl7Cw9YF2UAkGhIXmYSbZLgI9WKxOOukx52kVAvq6eXS6HOHyAyuRz0lN5hlwg19hcTOWqXQ0FxaXZPnGC4V7s+BsToRUM8YpdA2eHswG/DO2eDV7WpGWg30dpZIA5Nddkba5nP3+Epp4F2vwqh3CXeOLZyzwV/wvwPwC58bSNEdAkhaAAAAAElFTkSuQmCC
+
+    ";}}s:15:"generalfeedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}s:14:"shuffleanswers";a:1:{i:0;a:1:{s:1:"#";s:1:"0";}}}}i:2;a:2:{s:1:"@";a:1:{s:4:"type";s:11:"multichoice";}s:1:"#";a:15:{s:4:"name";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:14:"mcq with image";}}}}}s:12:"questiontext";a:1:{i:0;a:2:{s:1:"@";a:1:{s:6:"format";s:4:"html";}s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:107:"This is a multichoice question with an image in the old &quot;image&quot; field that was used in Moodle 1.9";}}}}}s:5:"image";a:1:{i:0;a:1:{s:1:"#";s:10:"moodle.png";}}s:12:"image_base64";a:1:{i:0;a:1:{s:1:"#";s:815:"
+iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAHzSURBVDhPxZFbSJNhGIDXERsVGbKawWJaoyAYGeqNYgdl6zenrhhUlB0I6iJEbUl0WF1UUK2DYhDkGghu7Z+HraisPLAsu+0ygi66kCioqwg2t6f///YhzAVBXfTc/HzP9/J8L/wG/pG8QDw+TE+Xn/O+dva07MW5vZayys3yNp/ZwPPRBPuOHqGisordDc00Ki42bqhAjQ3Jid8jAvoL9vIt4qs463G73VRtrcFiseDvuSYG+TwB7y/zfdzHpzeDWachAjeuXyWkPqZ21zZ2Nrioa/JQarOjlK8jes7Eh/s18GwHRMxkgka+3lvEi1ubcgM63Zea6W5v5ESLB4fDwRVvNam+eRBey3TAxqObNl76rXwLFvAlsIyhC2W5gZ+JJmYixQS9q8RZZzpghwEr/R2LpYGPIYWZqJnYSWNugCkPPDDx1JddTyeZqCetasMdS6WB9OQB7Xet5kmbITeQntoP/YWEfSZxFoy5SA2YCXeukAJ+vD5MMl7Cw9YF2UAkGhIXmYSbZLgI9WKxOOukx52kVAvq6eXS6HOHyAyuRz0lN5hlwg19hcTOWqXQ0FxaXZPnGC4V7s+BsToRUM8YpdA2eHswG/DO2eDV7WpGWg30dpZIA5Nddkba5nP3+Epp4F2vwqh3CXeOLZyzwV/wvwPwC58bSNEdAkhaAAAAAElFTkSuQmCC
+
+    ";}}s:15:"generalfeedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}s:12:"defaultgrade";a:1:{i:0;a:1:{s:1:"#";s:1:"1";}}s:7:"penalty";a:1:{i:0;a:1:{s:1:"#";s:3:"0.1";}}s:6:"hidden";a:1:{i:0;a:1:{s:1:"#";s:1:"0";}}s:14:"shuffleanswers";a:2:{i:0;a:1:{s:1:"#";s:1:"1";}i:1;a:1:{s:1:"#";s:4:"true";}}s:6:"single";a:1:{i:0;a:1:{s:1:"#";s:4:"true";}}s:15:"correctfeedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}s:24:"partiallycorrectfeedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}s:17:"incorrectfeedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}s:15:"answernumbering";a:1:{i:0;a:1:{s:1:"#";s:3:"abc";}}s:6:"answer";a:3:{i:0;a:2:{s:1:"@";a:1:{s:8:"fraction";s:1:"0";}s:1:"#";a:2:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:22:"
+wrong answer
+        ";}}s:8:"feedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}}}i:1;a:2:{s:1:"@";a:1:{s:8:"fraction";s:1:"0";}s:1:"#";a:2:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:30:"
+another wrong answer
+        ";}}s:8:"feedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}}}i:2;a:2:{s:1:"@";a:1:{s:8:"fraction";s:3:"100";}s:1:"#";a:2:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:22:"
+right answer
+        ";}}s:8:"feedback";a:1:{i:0;a:1:{s:1:"#";a:1:{s:4:"text";a:1:{i:0;a:1:{s:1:"#";s:0:"";}}}}}}}}}}}}}}
\ No newline at end of file
diff --git a/lib/tests/sample_questions_with_old_image_tag.xml b/lib/tests/sample_questions_with_old_image_tag.xml
new file mode 100644 (file)
index 0000000..852e5bc
--- /dev/null
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<quiz>
+
+
+<!-- question: 0  -->
+  <question type="category">
+    <category>
+        <text>$course$/Default for Test images in backup</text>
+
+    </category>
+  </question>
+
+
+
+<!-- question: 5  -->
+  <question type="cloze">
+    <name><text>cloze question with image</text>
+</name>
+    <questiontext>
+<text><![CDATA[This is a multianswer question with an image in the old &quot;image&quot; field that was used in moodle 1.9.<br />Match the following cities with the correct state:<br />* San Francisco: {1:MULTICHOICE:=California#OK~Arizona#Wrong}<br />* Tucson: {1:MULTICHOICE:California#Wrong~%100%Arizona#OK}<br />* Los Angeles: {1:MULTICHOICE:=California#OK~Arizona#Wrong}<br />* Phoenix: {1:MULTICHOICE:%0%California#Wrong~=Arizona#OK}<br />The capital of France is {1:SHORTANSWER:%100%Paris#Congratulations!<br />~%50%Marseille#No, that is the second largest city in France (after<br />Paris).~*#Wrong answer. The capital of France is Paris, of course.}.<br />]]></text>
+    </questiontext>
+<image>moodle.png</image>
+    <image_base64>
+iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAHzSURBVDhPxZFbSJNhGIDXERsVGbKawWJaoyAYGeqNYgdl6zenrhhUlB0I6iJEbUl0WF1UUK2DYhDkGghu7Z+HraisPLAsu+0ygi66kCioqwg2t6f///YhzAVBXfTc/HzP9/J8L/wG/pG8QDw+TE+Xn/O+dva07MW5vZayys3yNp/ZwPPRBPuOHqGisordDc00Ki42bqhAjQ3Jid8jAvoL9vIt4qs463G73VRtrcFiseDvuSYG+TwB7y/zfdzHpzeDWachAjeuXyWkPqZ21zZ2Nrioa/JQarOjlK8jes7Eh/s18GwHRMxkgka+3lvEi1ubcgM63Zea6W5v5ESLB4fDwRVvNam+eRBey3TAxqObNl76rXwLFvAlsIyhC2W5gZ+JJmYixQS9q8RZZzpghwEr/R2LpYGPIYWZqJnYSWNugCkPPDDx1JddTyeZqCetasMdS6WB9OQB7Xet5kmbITeQntoP/YWEfSZxFoy5SA2YCXeukAJ+vD5MMl7Cw9YF2UAkGhIXmYSbZLgI9WKxOOukx52kVAvq6eXS6HOHyAyuRz0lN5hlwg19hcTOWqXQ0FxaXZPnGC4V7s+BsToRUM8YpdA2eHswG/DO2eDV7WpGWg30dpZIA5Nddkba5nP3+Epp4F2vwqh3CXeOLZyzwV/wvwPwC58bSNEdAkhaAAAAAElFTkSuQmCC
+
+    </image_base64>
+    <generalfeedback>
+<text></text>
+    </generalfeedback>
+    <shuffleanswers>0</shuffleanswers>
+</question>
+
+
+
+<!-- question: 4  -->
+  <question type="multichoice">
+    <name><text>mcq with image</text>
+</name>
+    <questiontext format="html">
+<text><![CDATA[This is a multichoice question with an image in the old &quot;image&quot; field that was used in Moodle 1.9]]></text>
+    </questiontext>
+    <image>moodle.png</image>
+    <image_base64>
+iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAHzSURBVDhPxZFbSJNhGIDXERsVGbKawWJaoyAYGeqNYgdl6zenrhhUlB0I6iJEbUl0WF1UUK2DYhDkGghu7Z+HraisPLAsu+0ygi66kCioqwg2t6f///YhzAVBXfTc/HzP9/J8L/wG/pG8QDw+TE+Xn/O+dva07MW5vZayys3yNp/ZwPPRBPuOHqGisordDc00Ki42bqhAjQ3Jid8jAvoL9vIt4qs463G73VRtrcFiseDvuSYG+TwB7y/zfdzHpzeDWachAjeuXyWkPqZ21zZ2Nrioa/JQarOjlK8jes7Eh/s18GwHRMxkgka+3lvEi1ubcgM63Zea6W5v5ESLB4fDwRVvNam+eRBey3TAxqObNl76rXwLFvAlsIyhC2W5gZ+JJmYixQS9q8RZZzpghwEr/R2LpYGPIYWZqJnYSWNugCkPPDDx1JddTyeZqCetasMdS6WB9OQB7Xet5kmbITeQntoP/YWEfSZxFoy5SA2YCXeukAJ+vD5MMl7Cw9YF2UAkGhIXmYSbZLgI9WKxOOukx52kVAvq6eXS6HOHyAyuRz0lN5hlwg19hcTOWqXQ0FxaXZPnGC4V7s+BsToRUM8YpdA2eHswG/DO2eDV7WpGWg30dpZIA5Nddkba5nP3+Epp4F2vwqh3CXeOLZyzwV/wvwPwC58bSNEdAkhaAAAAAElFTkSuQmCC
+
+    </image_base64>
+    <generalfeedback>
+<text></text>
+    </generalfeedback>
+    <defaultgrade>1</defaultgrade>
+    <penalty>0.1</penalty>
+    <hidden>0</hidden>
+    <shuffleanswers>1</shuffleanswers>
+    <single>true</single>
+    <shuffleanswers>true</shuffleanswers>
+    <correctfeedback>      <text></text>
+</correctfeedback>
+    <partiallycorrectfeedback>      <text></text>
+</partiallycorrectfeedback>
+    <incorrectfeedback>      <text></text>
+</incorrectfeedback>
+    <answernumbering>abc</answernumbering>
+      <answer fraction="0">
+        <text>
+wrong answer
+        </text>
+      <feedback>
+          <text>
+
+          </text>
+      </feedback>
+    </answer>
+      <answer fraction="0">
+        <text>
+another wrong answer
+        </text>
+      <feedback>
+          <text>
+
+          </text>
+      </feedback>
+    </answer>
+      <answer fraction="100">
+        <text>
+right answer
+        </text>
+      <feedback>
+          <text>
+
+          </text>
+      </feedback>
+    </answer>
+</question>
+
+
+</quiz>
\ No newline at end of file
index f6dceb4..554f165 100644 (file)
@@ -56,4 +56,16 @@ class core_xmlize_testcase extends basic_testcase {
         $this->expectExceptionMessage('Error parsing XML: Mismatched tag at line 18, char 23');
         $xmlnew = xmlize($xml, 1, "UTF-8", true);
     }
+
+    /**
+     * Test an XML import using legacy question data with old image tag.
+     */
+    public function test_xmlimport_of_sample_question_with_old_image_tag() {
+        $xml = file_get_contents(__DIR__ . '/sample_questions_with_old_image_tag.xml');
+        $serialised = file_get_contents(__DIR__ . '/sample_questions_with_old_image_tag.ser');
+
+        // Compare the legacy representation in its serialized state and after unserialization.
+        $this->assertEquals($serialised, serialize(xmlize($xml)));
+        $this->assertEquals(unserialize($serialised), xmlize($xml));
+    }
 }