diff --git a/question/format/xml/format.php b/question/format/xml/format.php index 4b433f50f87..93ab228df88 100644 --- a/question/format/xml/format.php +++ b/question/format/xml/format.php @@ -1189,10 +1189,10 @@ class qformat_xml extends qformat_default { $infoformat = $this->format($question->infoformat); $expout .= " \n"; $expout .= " \n"; - $expout .= " {$categorypath}"; + $expout .= " {$categorypath}"; $expout .= " \n"; $expout .= " \n"; - $expout .= " {$categoryinfo}"; + $expout .= " {$categoryinfo}"; $expout .= " \n"; $expout .= " \n"; return $expout; diff --git a/question/format/xml/tests/fixtures/export_category.xml b/question/format/xml/tests/fixtures/export_category.xml index 66e6584459d..fcc3474885c 100644 --- a/question/format/xml/tests/fixtures/export_category.xml +++ b/question/format/xml/tests/fixtures/export_category.xml @@ -3,11 +3,10 @@ - $course$/Alpha + $course$/Alpha - - This is Alpha category for test + This is Alpha category for test @@ -38,4 +37,5 @@ + diff --git a/question/format/xml/tests/fixtures/nested_categories.xml b/question/format/xml/tests/fixtures/nested_categories.xml index bf4df7d41dc..836e06f07a0 100644 --- a/question/format/xml/tests/fixtures/nested_categories.xml +++ b/question/format/xml/tests/fixtures/nested_categories.xml @@ -3,30 +3,30 @@ - $course$/Delta + $course$/Delta - This is Delta category for test + This is Delta category for test - $course$/Delta/Epsilon + $course$/Delta/Epsilon - This is Epsilon category for test + This is Epsilon category for test - $course$/Delta/Epsilon/Zeta + $course$/Delta/Epsilon/Zeta - This is Zeta category for test + This is Zeta category for test @@ -57,4 +57,5 @@ + diff --git a/question/format/xml/tests/fixtures/nested_categories_with_questions.xml b/question/format/xml/tests/fixtures/nested_categories_with_questions.xml index 80920e3bda4..b4abe7bf4bc 100644 --- a/question/format/xml/tests/fixtures/nested_categories_with_questions.xml +++ b/question/format/xml/tests/fixtures/nested_categories_with_questions.xml @@ -3,10 +3,10 @@ - $course$/Iota + $course$/Iota - This is Iota category for test + This is Iota category for test @@ -41,14 +41,14 @@ - $course$/Iota/Kappa + $course$/Iota/Kappa - This is Kappa category for test + This is Kappa category for test - + Kappa Essay Question @@ -106,10 +106,10 @@ - $course$/Iota/Kappa/Lambda + $course$/Iota/Kappa/Lambda - This is Lambda category for test + This is Lambda category for test @@ -141,7 +141,7 @@ - + $course$/Iota/Mu @@ -151,7 +151,7 @@ - + Mu Question @@ -178,4 +178,5 @@ + diff --git a/question/format/xml/tests/fixtures/truefalse.xml b/question/format/xml/tests/fixtures/truefalse.xml index f0275302a24..97b099a42fa 100644 --- a/question/format/xml/tests/fixtures/truefalse.xml +++ b/question/format/xml/tests/fixtures/truefalse.xml @@ -65,4 +65,4 @@ - \ No newline at end of file + diff --git a/question/format/xml/tests/qformat_xml_import_export_test.php b/question/format/xml/tests/qformat_xml_import_export_test.php index e8890d284e9..a80c4333300 100644 --- a/question/format/xml/tests/qformat_xml_import_export_test.php +++ b/question/format/xml/tests/qformat_xml_import_export_test.php @@ -61,33 +61,36 @@ class qformat_xml_import_export_test extends advanced_testcase { return $qformat; } + /** * Check xml for compliance. * @param string $expectedxml with correct string. * @param string $xml you want to check. */ public function assert_same_xml($expectedxml, $xml) { - $this->assertEquals(preg_replace('/( +)/', "", str_replace("\n", "", - str_replace("\r\n", "\n", str_replace("\t", "\n", $expectedxml)))), - preg_replace('/( +)/', "", str_replace("\n", "", - str_replace( "\r\n", "\n", str_replace( "\t", "\n", $xml))))); + $this->assertEquals($this->normalise_xml($expectedxml), + $this->normalise_xml($xml)); } /** - * Check xml for compliance. - * @param string $expectedxml with correct string. - * @param string $xml you want to check. + * Clean up some XML to remove irrelevant differences, before it is compared. + * @param string $xml some XML. + * @return string cleaned-up XML. */ - public function assert_same_xml_random_category($expectedxml, $xml) { - $str1 = preg_replace('/( +)/', "", - str_replace("\n", "", str_replace("\r\n", "\n", - str_replace("\t", "\n", $expectedxml)))); + protected function normalise_xml($xml) { + // Normalise line endings. + $xml = str_replace("\r\n", "\n", $xml); + $xml = preg_replace("~\n$~", "", $xml); // Strip final newline in file. - $str2 = preg_replace('/( +)/', "", str_replace("\n", "", - str_replace( "\r\n", "\n", str_replace( "\t", "\n", $xml)))); + // Replace all numbers in question id comments with 0. + $xml = preg_replace('~(?<=)~', '0', $xml); - $str1 = str_replace("unknownhost+" + '/[0-9]+/' + "+", "", $str1); - $this->assertEquals($str1, $str2); + // Deal with how different databases output numbers. Only match when only thing in a tag. + $xml = preg_replace("~>.0000000<~", '>0<', $xml); // How Oracle outputs 0.0000000. + $xml = preg_replace("~(\.(:?[0-9]*[1-9])?)0*<~", '$1<', $xml); // Other cases of trailing 0s + $xml = preg_replace("~([0-9]).<~", '$1<', $xml); // Stray . in 1. after last step. + + return $xml; } /** @@ -248,8 +251,8 @@ class qformat_xml_import_export_test extends advanced_testcase { 'sortorder' => '999']); $question = $generator->create_question('truefalse', null, [ 'category' => $category->id, - 'name' => 'AlphaQuestion', - 'questiontext' => ['format' => '1', 'text' => '

TestingAlphaQuestion

'], + 'name' => 'Alpha Question', + 'questiontext' => ['format' => '1', 'text' => '

Testing Alpha Question

'], 'generalfeedback' => ['format' => '1', 'text' => ''], 'correctanswer' => '1', 'feedbacktrue' => ['format' => '1', 'text' => ''], @@ -257,10 +260,8 @@ class qformat_xml_import_export_test extends advanced_testcase { 'penalty' => '1']); $qformat->setCategory($category); - $xml = preg_replace('/()/', '', $qformat->exportprocess()); - $file = preg_replace('/()/', '', - file_get_contents(__DIR__ . '/fixtures/export_category.xml')); - $this->assert_same_xml($file, $xml); + $expectedxml = file_get_contents(__DIR__ . '/fixtures/export_category.xml'); + $this->assert_same_xml($expectedxml, $qformat->exportprocess()); } /** @@ -312,10 +313,8 @@ class qformat_xml_import_export_test extends advanced_testcase { $qformat->setCategory($categoryepsilon); $qformat->setCategory($categoryzeta); - $xml = preg_replace('/()/', '', $qformat->exportprocess()); - $file = preg_replace('/()/', '', - file_get_contents(__DIR__ . '/fixtures/nested_categories.xml')); - $this->assert_same_xml($file, $xml); + $expectedxml = file_get_contents(__DIR__ . '/fixtures/nested_categories.xml'); + $this->assert_same_xml($expectedxml, $qformat->exportprocess()); } /** @@ -419,9 +418,7 @@ class qformat_xml_import_export_test extends advanced_testcase { 'penalty' => '1']); $qformat->setCategory($categoryiota); - $xml = preg_replace('/()/', '', $qformat->exportprocess()); - $file = preg_replace('/()/', '', - file_get_contents(__DIR__ . '/fixtures/nested_categories_with_questions.xml')); - $this->assert_same_xml($file, $xml); + $expectedxml = file_get_contents(__DIR__ . '/fixtures/nested_categories_with_questions.xml'); + $this->assert_same_xml($expectedxml, $qformat->exportprocess()); } }