Skip to content

Commit

Permalink
MDL-23545 qformat_xml: don't allow irrelevant DB differences to break…
Browse files Browse the repository at this point in the history
… tests

Also slightly improve formatting of the exported file.
  • Loading branch information
timhunt committed Sep 18, 2018
1 parent 1dab8fa commit f19c6f6
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 50 deletions.
4 changes: 2 additions & 2 deletions question/format/xml/format.php
Original file line number Diff line number Diff line change
Expand Up @@ -1189,10 +1189,10 @@ public function writequestion($question) {
$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;
Expand Down
6 changes: 3 additions & 3 deletions question/format/xml/tests/fixtures/export_category.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -38,4 +37,5 @@
</feedback>
</answer>
</question>

</quiz>
13 changes: 7 additions & 6 deletions question/format/xml/tests/fixtures/nested_categories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -57,4 +57,5 @@
</feedback>
</answer>
</question>

</quiz>
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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>

Expand Down Expand Up @@ -141,7 +141,7 @@
</answer>
</question>

<!-- question: 0 -->
<!-- question: 0 -->
<question type="category">
<category>
<text>$course$/Iota/Mu</text>
Expand All @@ -151,7 +151,7 @@
</info>
</question>

<!-- question: 99 -->
<!-- question: 99 -->
<question type="truefalse">
<name>
<text>Mu Question</text>
Expand All @@ -178,4 +178,5 @@
</feedback>
</answer>
</question>

</quiz>
2 changes: 1 addition & 1 deletion question/format/xml/tests/fixtures/truefalse.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
</answer>
</question>

</quiz>
</quiz>
55 changes: 26 additions & 29 deletions question/format/xml/tests/qformat_xml_import_export_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,33 +61,36 @@ public function create_qformat($filename, $course) {

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.

// Replace all numbers in question id comments with 0.
$xml = preg_replace('~(?<=<!-- question: )([0-9]+)(?= -->)~', '0', $xml);

$str2 = preg_replace('/( +)/', "", str_replace("\n", "",
str_replace( "\r\n", "\n", str_replace( "\t", "\n", $xml))));
// 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.

$str1 = str_replace("unknownhost+" + '/[0-9]+/' + "+", "", $str1);
$this->assertEquals($str1, $str2);
return $xml;
}

/**
Expand Down Expand Up @@ -248,19 +251,17 @@ public function test_export_category() {
'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' => ''],
'feedbackfalse' => ['format' => '1', 'text' => ''],
'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());
}

/**
Expand Down Expand Up @@ -312,10 +313,8 @@ public function test_export_nested_categories() {
$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());
}

/**
Expand Down Expand Up @@ -419,9 +418,7 @@ public function test_export_nested_categories_with_questions() {
'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());
}
}

0 comments on commit f19c6f6

Please sign in to comment.