diff --git a/libraries/src/Feed/Parser/AtomParser.php b/libraries/src/Feed/Parser/AtomParser.php index 57def5e92bb74..57a259830e37f 100644 --- a/libraries/src/Feed/Parser/AtomParser.php +++ b/libraries/src/Feed/Parser/AtomParser.php @@ -190,10 +190,9 @@ protected function handleUpdated(Feed $feed, \SimpleXMLElement $el) */ protected function initialise() { - // We want to move forward to the first XML Element after the xml doc type declaration - $this->moveToNextElement(); - + // We are on the first XML Element after the xml doc type declaration $this->version = ($this->stream->getAttribute('version') == '0.3') ? '0.3' : '1.0'; + $this->moveToNextElement(); } /** diff --git a/libraries/src/Feed/Parser/RssParser.php b/libraries/src/Feed/Parser/RssParser.php index 51f74a3182252..803e0c58b967d 100644 --- a/libraries/src/Feed/Parser/RssParser.php +++ b/libraries/src/Feed/Parser/RssParser.php @@ -347,10 +347,7 @@ protected function handleWebmaster(Feed $feed, \SimpleXMLElement $el) */ protected function initialise() { - // We want to move forward to the first XML Element after the xml doc type declaration - $this->moveToNextElement(); - - // Read the version attribute. + // We are on the first XML Element after the xml doc type declaration $this->version = $this->stream->getAttribute('version'); // We want to move forward to the first element after the element. diff --git a/tests/Unit/Libraries/Cms/Feed/Parser/AtomParserTest.php b/tests/Unit/Libraries/Cms/Feed/Parser/AtomParserTest.php index d8a32f4ad920e..0eaa47ac68c5c 100644 --- a/tests/Unit/Libraries/Cms/Feed/Parser/AtomParserTest.php +++ b/tests/Unit/Libraries/Cms/Feed/Parser/AtomParserTest.php @@ -351,9 +351,26 @@ public function testInitialiseSetsDefaultVersionWithXmlDocType() */ public function testInitialiseSetsDefaultVersion() { - $dummyXml = ''; + $dummyXml = ' + + +Joomla! Unit test +'; $reader = \XMLReader::XML($dummyXml); $atomParser = new AtomParser($reader); + + // same logic as FeedFactory.php : skip head record + try { + // Skip ahead to the root node. + while ($reader->read()) { + if ($reader->nodeType == \XMLReader::ELEMENT) { + break; + } + } + } catch (\Exception $e) { + throw new \RuntimeException('Error reading feed.', $e->getCode(), $e); + } + $atomParser->parse(); // Use reflection to check the value @@ -373,9 +390,26 @@ public function testInitialiseSetsDefaultVersion() */ public function testInitialiseSetsOldVersion() { - $dummyXml = ''; + $dummyXml = ' + + +Joomla! Unit test +'; $reader = \XMLReader::XML($dummyXml); $atomParser = new AtomParser($reader); + + // same logic as FeedFactory.php : skip head record + try { + // Skip ahead to the root node. + while ($reader->read()) { + if ($reader->nodeType == \XMLReader::ELEMENT) { + break; + } + } + } catch (\Exception $e) { + throw new \RuntimeException('Error reading feed.', $e->getCode(), $e); + } + $atomParser->parse(); // Use reflection to check the value diff --git a/tests/Unit/Libraries/Cms/Feed/Parser/RssParserTest.php b/tests/Unit/Libraries/Cms/Feed/Parser/RssParserTest.php index aac629fda7050..f31997c92312d 100644 --- a/tests/Unit/Libraries/Cms/Feed/Parser/RssParserTest.php +++ b/tests/Unit/Libraries/Cms/Feed/Parser/RssParserTest.php @@ -582,7 +582,8 @@ public function testHandleWebmaster() */ public function testParseSetsVersion() { - $dummyXml = ' + $dummyXml = ' + Test Channel @@ -590,13 +591,26 @@ public function testParseSetsVersion() '; $reader = \XMLReader::XML($dummyXml); $rssParser = new RssParser($reader); + + // same logic as FeedFactory.php : skip head record + try { + // Skip ahead to the root node. + while ($reader->read()) { + if ($reader->nodeType == \XMLReader::ELEMENT) { + break; + } + } + } catch (\Exception $e) { + throw new \RuntimeException('Error reading feed.', $e->getCode(), $e); + } + $rssParser->parse(); // Use reflection to check the value $reflectionClass = new ReflectionClass($rssParser); $attribute = $reflectionClass->getProperty('version'); - $attribute->setAccessible(true); + $attribute->setAccessible(true); $this->assertEquals('2.0', $attribute->getValue($rssParser)); }