Skip to content

Commit

Permalink
Fix issues with getting Xml as SimpleXmlElement and invalid Xml.
Browse files Browse the repository at this point in the history
Fixes #3855
  • Loading branch information
markstory committed May 27, 2013
1 parent 41e0c52 commit 7334643
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 12 additions & 1 deletion lib/Cake/Test/Case/Utility/XmlTest.php
Expand Up @@ -187,12 +187,23 @@ public static function invalidDataProvider() {
*
* @dataProvider invalidDataProvider
* @expectedException XmlException
* return void
* @return void
*/
public function testBuildInvalidData($value) {
Xml::build($value);
}

/**
* Test that building SimpleXmlElement with invalid XML causes the right exception.
*
* @expectedException XmlException
* @return void
*/
public function testBuildInvalidDataSimpleXml() {
$input = '<derp';
$xml = Xml::build($input, array('return' => 'simplexml'));
}

/**
* test build with a single empty tag
*
Expand Down
17 changes: 12 additions & 5 deletions lib/Cake/Utility/Xml.php
Expand Up @@ -131,16 +131,23 @@ protected static function _loadXml($input, $options) {
if ($hasDisable && !$options['loadEntities']) {
libxml_disable_entity_loader(true);
}
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
$xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
} else {
$xml = new DOMDocument();
$xml->loadXML($input);
try {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
$xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
} else {
$xml = new DOMDocument();
$xml->loadXML($input);
}
} 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(__d('cake_dev', 'Xml cannot be read.'));
}
return $xml;
}

Expand Down

0 comments on commit 7334643

Please sign in to comment.