From d93c7e02f174d8788331ffc6277394c5173361ac Mon Sep 17 00:00:00 2001 From: Vitalii Shtykhno Date: Wed, 6 Nov 2024 09:54:57 +0100 Subject: [PATCH] fix: parse special symbols on XML --- src/qtism/data/storage/xml/Utils.php | 23 +++++++++- .../data/storage/xml/XmlUtilsTest.php | 46 +++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/qtism/data/storage/xml/Utils.php b/src/qtism/data/storage/xml/Utils.php index 8930aa811..0a76850b8 100644 --- a/src/qtism/data/storage/xml/Utils.php +++ b/src/qtism/data/storage/xml/Utils.php @@ -1,5 +1,7 @@ '"', + "'" => ''', + '&' => '&', + '<' => '<', + '>' => '>', + "\t" => ' ', + "\r" => ' ', + "\x08" => '�', + ]; + + $string = strtr($string, $symbolMap); + + return htmlspecialchars($string, ENT_QUOTES | ENT_XML1, 'UTF-8'); + } + /** * Get the child elements of a given element by tag name. This method does * not behave like DOMElement::getElementsByTagName. It only returns the direct diff --git a/test/qtismtest/data/storage/xml/XmlUtilsTest.php b/test/qtismtest/data/storage/xml/XmlUtilsTest.php index 3e81f84cf..f79fa0bb2 100644 --- a/test/qtismtest/data/storage/xml/XmlUtilsTest.php +++ b/test/qtismtest/data/storage/xml/XmlUtilsTest.php @@ -391,6 +391,52 @@ public function testFindCustomNamespaces(): void ); } + public function testValueAsStringReplaceSpecialSymbols(): void + { + $xml = ('160°'); + $this->assertEquals('&lt;value&gt;160°&#xFFFD;&lt;/value&gt;', Utils::valueAsString($xml)); + } + + public function testProcessSpecialCharsetWithoutError(): void + { + $xml = (' + + + + + + + 1 + + + + + PT22S + + + + completed + + + 0 + + + 1 + + + + %s + + + + +'); + $this->assertNotNull(Utils::findExternalNamespaces(sprintf($xml, Utils::valueAsString('160°')))); + } + + public function testremoveAllButFirstOccurrence(): void { $subject = 'abc 12 abc 345abc678abc';