From 0eb7d3de5b14efca6a8feceff24ba54cf107734a Mon Sep 17 00:00:00 2001 From: Ilie Pandia Date: Wed, 25 Apr 2018 10:47:58 +0300 Subject: [PATCH] Ref #11957 - diagnosis would be easier if the underling error had been exposed. --- src/Utility/Xml.php | 18 ++++++++---------- tests/TestCase/Utility/XmlTest.php | 13 +++++++++++++ 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/Utility/Xml.php b/src/Utility/Xml.php index eb9bcd69b6f..92816077b91 100644 --- a/src/Utility/Xml.php +++ b/src/Utility/Xml.php @@ -157,18 +157,16 @@ protected static function _loadXml($input, $options) $xml = new DOMDocument(); $xml->loadXML($input, $flags); } + return $xml; } catch (Exception $e) { - $xml = null; - } - if ($hasDisable && !$options['loadEntities']) { - libxml_disable_entity_loader(false); - } - libxml_use_internal_errors($internalErrors); - if ($xml === null) { - throw new XmlException('Xml cannot be read.'); + throw new XmlException('Xml cannot be read. ' . $e->getMessage(), null, $e); + } finally { + //CakePHP requires PHP 5.6 so we can safely use finally to restore error handling + if ($hasDisable && !$options['loadEntities']) { + libxml_disable_entity_loader(false); + } + libxml_use_internal_errors($internalErrors); } - - return $xml; } /** diff --git a/tests/TestCase/Utility/XmlTest.php b/tests/TestCase/Utility/XmlTest.php index e643c723f89..a86d8e41ccb 100644 --- a/tests/TestCase/Utility/XmlTest.php +++ b/tests/TestCase/Utility/XmlTest.php @@ -18,6 +18,7 @@ use Cake\Core\Configure; use Cake\ORM\Entity; use Cake\TestSuite\TestCase; +use Cake\Utility\Exception\XmlException; use Cake\Utility\Xml; /** @@ -64,6 +65,18 @@ public function tearDown() Configure::write('App.encoding', $this->_appEncoding); } + public function testExceptionChainingForInvalidInput(){ + try { + $value = "invalid-xml-input<<"; + Xml::build($value); //should throw an XmlException + $this->assertFalse(true,'This line should not be executed because of exception above.'); + }catch(XmlException $exception){ + $cause = $exception->getPrevious(); + $this->assertNotNull($cause); + $this->assertInstanceOf(\Exception::class, $cause ); + } + } + /** * testBuild method *