From c9d48f6f258f162b2f99472c148d117e95c0cb7e Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Wed, 17 Apr 2024 16:25:45 +0200 Subject: [PATCH] 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. --- lib/phpunit/classes/util.php | 10 ++++++++++ question/engine/tests/helpers.php | 4 ++-- .../xml/tests/qformat_xml_import_export_test.php | 2 +- question/type/ordering/tests/questiontype_test.php | 6 +++++- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/phpunit/classes/util.php b/lib/phpunit/classes/util.php index 22454da39f5..0e0bb2d1cb6 100644 --- a/lib/phpunit/classes/util.php +++ b/lib/phpunit/classes/util.php @@ -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. * diff --git a/question/engine/tests/helpers.php b/question/engine/tests/helpers.php index 1e4ec1a38d1..407374e8562 100644 --- a/question/engine/tests/helpers.php +++ b/question/engine/tests/helpers.php @@ -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) ); } } 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 975770ff4e6..11ec1a6a528 100644 --- a/question/format/xml/tests/qformat_xml_import_export_test.php +++ b/question/format/xml/tests/qformat_xml_import_export_test.php @@ -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. diff --git a/question/type/ordering/tests/questiontype_test.php b/question/type/ordering/tests/questiontype_test.php index c82f9ee8066..00981c4fe45 100644 --- a/question/type/ordering/tests/questiontype_test.php +++ b/question/type/ordering/tests/questiontype_test.php @@ -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) + ); } }