MDL-81581 phpunit: Create the normalise_line_endings() method

And apply it to all the obvious places related with the issue.

Note that surely there are way more in code base, but it's
out of scope for this issue.
This commit is contained in:
Eloy Lafuente (stronk7)
2024-04-25 18:52:25 +02:00
parent 894ee17f53
commit c9d48f6f25
4 changed files with 18 additions and 4 deletions
+10
View File
@@ -990,6 +990,16 @@ class phpunit_util extends testing_util {
return str_repeat(" ", $level * 2) . "{$string}\n";
}
/**
* Normalise any text to always use unix line endings (line-feeds).
*
* @param string $text The text to normalize
* @return string
*/
public static function normalise_line_endings(string $text): string {
return str_replace(["\r\n", "\r"], "\n", $text);
}
/**
* Get the coverage config for the supplied includelist and excludelist configuration.
*
+2 -2
View File
@@ -611,8 +611,8 @@ abstract class question_testcase extends advanced_testcase {
*/
public function assert_same_xml($expectedxml, $xml) {
$this->assertEquals(
str_replace("\r\n", "\n", $expectedxml),
str_replace("\r\n", "\n", $xml)
phpunit_util::normalise_line_endings($expectedxml),
phpunit_util::normalise_line_endings($xml)
);
}
}
@@ -78,7 +78,7 @@ class qformat_xml_import_export_test extends advanced_testcase {
*/
protected function normalise_xml($xml) {
// Normalise line endings.
$xml = str_replace("\r\n", "\n", $xml);
$xml = phpunit_util::normalise_line_endings($xml);
$xml = preg_replace("~\n$~", "", $xml); // Strip final newline in file.
// Replace all numbers in question id comments with 0.
@@ -17,6 +17,7 @@
namespace qtype_ordering;
use core_question_generator;
use phpunit_util;
use qtype_ordering;
use qtype_ordering_test_helper;
use qtype_ordering_edit_form;
@@ -315,6 +316,9 @@ final class questiontype_test extends \question_testcase {
$expectedgift = file_get_contents(__DIR__ . '/fixtures/testexport.gift.txt');
$this->assertEquals($expectedgift, $gift);
$this->assertEquals(
phpunit_util::normalise_line_endings($expectedgift),
phpunit_util::normalise_line_endings($gift)
);
}
}