Skip to content

Commit

Permalink
Invalid XML string for xml() function throws an exception
Browse files Browse the repository at this point in the history
  • Loading branch information
zzgab committed Feb 21, 2017
1 parent a8bf481 commit bfa0a4b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
24 changes: 17 additions & 7 deletions src/figdice/classes/functions/Function_xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace figdice\classes\functions;

use figdice\exceptions\XMLParsingException;
use \figdice\FigFunction;
use \figdice\classes\ViewElementTag;

Expand All @@ -41,20 +42,29 @@
*/
class Function_xml implements FigFunction {

/**
* @param {@link ViewElement} $viewElement
* @param integer $arity
* @param array $arguments
*/
/**
* @param ViewElementTag $viewElement
* @param integer $arity
* @param array $arguments
*
* @return \DOMXPath
* @throws XMLParsingException
*/
public function evaluate(ViewElementTag $viewElement, $arity, $arguments) {
$xmlString = $arguments[0];
$xml = new \DomDocument();
$xml = new \DOMDocument();
$successParse = @ $xml->loadXML($xmlString, LIBXML_NOENT);
if (! $successParse) {
$explicitRoot = $arity >= 2 ? $arguments[1] : 'xml';
$xmlString = '<'.$explicitRoot.'>' . $arguments[0] . '</'.$explicitRoot.'>';
// This time we let a warning be fired in case of invalid xml.
$xml->loadXML($xmlString, LIBXML_NOENT);
$successParse = @ $xml->loadXML($xmlString, LIBXML_NOENT);
if (! $successParse) {
$xmlError = libxml_get_last_error();
throw new XMLParsingException('xml() function: ' . $xmlError->message,
$viewElement->getCurrentFile()->getFilename(),
$viewElement->getLineNumber());
}
}
$xpath = new \DOMXPath($xml);
return $xpath;
Expand Down
6 changes: 3 additions & 3 deletions test/FunctionXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
class FunctionXmlTest extends PHPUnit_Framework_TestCase
{
/**
* @expectedException PHPUnit_Framework_Error_Warning
* @expectedException \figdice\exceptions\XMLParsingException
*/
public function testFunctionXmlForInvalidXmlIssuesWarning()
public function testFunctionXmlForInvalidXmlIssuesException()
{
// Deliberately supplying invalid XML island,
// but because we're inside a Fig template which must be valid XML,
Expand All @@ -35,7 +35,7 @@ public function testFunctionXmlForInvalidXmlIssuesWarning()
$view->loadString($xml);
$view->render();

$this->assertTrue(false, 'Should never occur: a Warning was caught before.');
$this->assertTrue(false, 'Should never occur: an exception was caught before.');
}

public function testFunctionXmlWithoutRootNodeDefaultsToXmlRootNode()
Expand Down

0 comments on commit bfa0a4b

Please sign in to comment.