MDL-23545 qformat_xml: don't allow irrelevant DB differences to break tests

Also slightly improve formatting of the exported file.
This commit is contained in:
Tim Hunt
2018-09-18 13:52:51 +01:00
parent 1dab8faafd
commit f19c6f60cd
6 changed files with 49 additions and 50 deletions
+2 -2
View File
@@ -1189,10 +1189,10 @@ class qformat_xml extends qformat_default {
$infoformat = $this->format($question->infoformat);
$expout .= " <question type=\"category\">\n";
$expout .= " <category>\n";
$expout .= " {$categorypath}";
$expout .= " {$categorypath}";
$expout .= " </category>\n";
$expout .= " <info {$infoformat}>\n";
$expout .= " {$categoryinfo}";
$expout .= " {$categoryinfo}";
$expout .= " </info>\n";
$expout .= " </question>\n";
return $expout;
+3 -3
View File
@@ -3,11 +3,10 @@
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Alpha</text>
<text>$course$/Alpha</text>
</category>
<info format="moodle_auto_format">
<text>This is Alpha category for test</text>
<text>This is Alpha category for test</text>
</info>
</question>
@@ -38,4 +37,5 @@
</feedback>
</answer>
</question>
</quiz>
+7 -6
View File
@@ -3,30 +3,30 @@
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Delta</text>
<text>$course$/Delta</text>
</category>
<info format="plain_text">
<text>This is Delta category for test</text>
<text>This is Delta category for test</text>
</info>
</question>
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Delta/Epsilon</text>
<text>$course$/Delta/Epsilon</text>
</category>
<info format="markdown">
<text>This is Epsilon category for test</text>
<text>This is Epsilon category for test</text>
</info>
</question>
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Delta/Epsilon/Zeta</text>
<text>$course$/Delta/Epsilon/Zeta</text>
</category>
<info format="moodle_auto_format">
<text>This is Zeta category for test</text>
<text>This is Zeta category for test</text>
</info>
</question>
@@ -57,4 +57,5 @@
</feedback>
</answer>
</question>
</quiz>
@@ -3,10 +3,10 @@
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Iota</text>
<text>$course$/Iota</text>
</category>
<info format="plain_text">
<text>This is Iota category for test</text>
<text>This is Iota category for test</text>
</info>
</question>
@@ -41,14 +41,14 @@
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Iota/Kappa</text>
<text>$course$/Iota/Kappa</text>
</category>
<info format="markdown">
<text>This is Kappa category for test</text>
<text>This is Kappa category for test</text>
</info>
</question>
<!-- question: 106 -->
<!-- question: 106 -->
<question type="essay">
<name>
<text>Kappa Essay Question</text>
@@ -106,10 +106,10 @@
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Iota/Kappa/Lambda</text>
<text>$course$/Iota/Kappa/Lambda</text>
</category>
<info format="moodle_auto_format">
<text>This is Lambda category for test</text>
<text>This is Lambda category for test</text>
</info>
</question>
@@ -141,7 +141,7 @@
</answer>
</question>
<!-- question: 0 -->
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Iota/Mu</text>
@@ -151,7 +151,7 @@
</info>
</question>
<!-- question: 99 -->
<!-- question: 99 -->
<question type="truefalse">
<name>
<text>Mu Question</text>
@@ -178,4 +178,5 @@
</feedback>
</answer>
</question>
</quiz>
+1 -1
View File
@@ -65,4 +65,4 @@
</answer>
</question>
</quiz>
</quiz>
@@ -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('~(?<=<!-- question: )([0-9]+)(?= -->)~', '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' => '<p>TestingAlphaQuestion</p>'],
'name' => 'Alpha Question',
'questiontext' => ['format' => '1', 'text' => '<p>Testing Alpha Question</p>'],
'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('/(<!-- question: )([0-9]+)( -->)/', '', $qformat->exportprocess());
$file = preg_replace('/(<!-- question: )([0-9]+)( -->)/', '',
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('/(<!-- question: )([0-9]+)( -->)/', '', $qformat->exportprocess());
$file = preg_replace('/(<!-- question: )([0-9]+)( -->)/', '',
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('/(<!-- question: )([0-9]+)( -->)/', '', $qformat->exportprocess());
$file = preg_replace('/(<!-- question: )([0-9]+)( -->)/', '',
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());
}
}