ZF-12106: DomQuery fails to evaluate XPath when there are warnings in loaded Zend_Dom.

Description

I couldn't get assertQuery from Zend_Test_PHPUnit working. Then I tried to hack in the ZF code and I realized that XPath does not work when there are warnings in the DOMDocument:


$doc = new DOMDocument();
$doc->loadXML('Fine');
$xpath = new DOMXPath($doc);
echo 'Count: ' . $xpath->evaluate('count(/root/node)') . "\n"; //1

$doc = new DOMDocument();
$doc->loadXML('Unknown entity ©');
$xpath = new DOMXPath($doc);
echo 'Count: ' . $xpath->evaluate('count(/root/node)') . "\n"; //0

When the Zend_Test_PHPUnit_Constraint_DomQuery instances Zend_Dom_Query which loads the document (with unknown entities), it does not produce any warnings (although, there are populated in the documentErrors propery).

The assert just fails as if the XPath returned no results.

Comments

If the document is malformed, why would it return results? Or are you suggesting we throw an exception?

It should do something. Either try to recover and ignore that unknown entity along with issuing a warning or fail completly and throw "malformed document" exception. Only thing it SHOULD NOT do is to fail silently IMO.

Thanks for the quick answer!

The first problem is that is fails with misleading error.

I have a test that does assertQuery() on response (using PHPUnit and Zend_Test), and when the returned webpage is not valid XML (although it is returned as valid xhtml from w3.org validator), it fails the assertion (failed asserting that sth is present). I would have expected to get info saying "Document is malformed, couldn't evaluate the XPath expression.".

Another problem is, that the document type detection perfomed in Zend_Dom_Query is based on document content, which resolves to xml, despite the fact that webpage returns header "Content-Type:text/html". (It parses fine when I hack the ZF code to force the parsing as HTML. Also the XPath works fine in this case).

Attaching patch, which changes the regex added in ZF-11376 (+test)

It fixes this issue and does not break any tests for Zend_Dom

Adam, can you please review the patch? (Assigning you, because you have worked on the closely related isssue ZF-11376)

Fixed in trunk r24820. Thanks, Martin!