Zend Framework

assertQueryContentContains() crashes PHP on attribute value query

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: None
  • Fix Version/s: 1.9.3
  • Component/s: Zend_Test_PHPUnit
  • Labels:
    None

Description

Using Zend_Test_PHPUnit I'm trying to test if an input-field contains a desired value.
PHP crashes completely on my call to $this->assertQueryContentContains('input#fax-0 @value', '(1234) 567890');.
I've tried to reproduce this behaviour and managed to cut the example down to:

$string='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>Bugtest</title>
    </head>
    <body>
        <form>
            <fieldset id="fieldset-fax"><legend>Faxnummern</legend>
                <ol>
                    <li><input type="text" name="fax[0]" id="fax-0" value="(1234) 567890" /></li>
                    <li><input type="text" name="fax[1]" id="fax-1" value="(0987) 654321" /></li>
                    <li><input type="text" name="fax[2]" id="fax-2" value="" /></li>
                </ol>
            </fieldset>
        </form>
    </body>
</html>';
$query=new Zend_Dom_Query($string);
$result=$query->query('input#fax-0 @value');
var_dump($result);
foreach ($result as $r) {
    var_dump($r->value);
}

$assertion=new Zend_Test_PHPUnit_Constraint_DomQuery('input#fax-0 @value');
$result=$assertion->evaluate($string, Zend_Test_PHPUnit_Constraint_DomQuery::ASSERT_CONTENT_CONTAINS, '(1234) 567890');
var_dump($result);

Running this script I get:

object(Zend_Dom_Query_Result)#3 (7) {
  ["_count:protected"]=>
  NULL
  ["_cssQuery:protected"]=>
  string(18) "input#fax-0 @value"
  ["_document:protected"]=>
  object(DOMDocument)#2 (0) {
  }
  ["_nodeList:protected"]=>
  object(DOMNodeList)#5 (0) {
  }
  ["_position:protected"]=>
  int(0)
  ["_xpath:protected"]=>
  NULL
  ["_xpathQuery:protected"]=>
  string(28) "//input[@id='fax-0']//@value"
}
string(13) "(1234) 567890"

and then PHP crashes completely.

So I'd think that Zend_Dom_Query is working correctly and that the problem is the way Zend_Test_PHPUnit_Constraint_DomQuery tries to get the value out of the nodes returned by the Zend_Dom_Query. The relevant method is Zend_Test_PHPUnit_Constraint_DomQuery::_getNodeContent(DOMNode $node) which reads

$doc     = $node->ownerDocument;
$content = $doc->saveXML($node);
$tag     = $node->nodeName;
$regex   = '|</?' . $tag . '[^>]*>|';
return preg_replace($regex, '', $content);

Perhaps there should be a test if the given DOMNode $node is actually a DOMAttr whose value could easily be retrieved by using $node->value.

Activity

Hide
Matthew Weier O'Phinney added a comment -

The problem isusing @value – this syntax is not supported by Zend_Dom_Query. You can query for the value of an arbitrary attribute using some alternate CSS style notation; please see http://framework.zend.com/manual/en/zend.dom.query.html#zend.dom.query.operation for more information.

Currently, there is no support for finding simply the existence of an arbitrary attribute, only for testing against the value of an arbitrary attribute. If you would like the former functionality (existence of arbitrary attribute), please open a separate feature request.

Show
Matthew Weier O'Phinney added a comment - The problem isusing @value – this syntax is not supported by Zend_Dom_Query. You can query for the value of an arbitrary attribute using some alternate CSS style notation; please see http://framework.zend.com/manual/en/zend.dom.query.html#zend.dom.query.operation for more information. Currently, there is no support for finding simply the existence of an arbitrary attribute, only for testing against the value of an arbitrary attribute. If you would like the former functionality (existence of arbitrary attribute), please open a separate feature request.
Hide
Stefan Gehrig added a comment -

Hi Matthew,

sorry, but I can't follow your comment... I'm trying to test against the value of an attribute (the value-Attribute of an <input/>-Tag).

The problem obviously is not Zend_Dom_Query as the following works as expected:

$query=new Zend_Dom_Query($string); // $string from example above
$result=$query->query('input#fax-0 @value');
var_dump($result);
foreach ($result as $r) {
    var_dump($r->value);
}

Zend_Dom_Query translates this into {{//input[@id='fax-0']//@value}} and gives the following output

object(Zend_Dom_Query_Result)#3 (7) {
  ["_count:protected"]=>
  NULL
  ["_cssQuery:protected"]=>
  string(18) "input#fax-0 @value"
  ["_document:protected"]=>
  object(DOMDocument)#2 (0) {
  }
  ["_nodeList:protected"]=>
  object(DOMNodeList)#5 (0) {
  }
  ["_position:protected"]=>
  int(0)
  ["_xpath:protected"]=>
  NULL
  ["_xpathQuery:protected"]=>
  string(28) "//input[@id='fax-0']//@value"
}
string(13) "(1234) 567890"

This is exactly what I want. But when using the query from above in a Zend_Test_PHPUnit_Constraint_DomQuery PHP crashes most obviously due to the code of Zend_Test_PHPUnit_Constraint_DomQuery::_getNodeContent(DOMNode $node) which reads:

$doc     = $node->ownerDocument;
$content = $doc->saveXML($node);
$tag     = $node->nodeName;
$regex   = '|</?' . $tag . '[^>]*>|';
return preg_replace($regex, '', $content);

This doesn't work id $node is a DOMAttr.

Show
Stefan Gehrig added a comment - Hi Matthew, sorry, but I can't follow your comment... I'm trying to test against the value of an attribute (the value-Attribute of an <input/>-Tag). The problem obviously is not Zend_Dom_Query as the following works as expected:
$query=new Zend_Dom_Query($string); // $string from example above
$result=$query->query('input#fax-0 @value');
var_dump($result);
foreach ($result as $r) {
    var_dump($r->value);
}
Zend_Dom_Query translates this into {{//input[@id='fax-0']//@value}} and gives the following output
object(Zend_Dom_Query_Result)#3 (7) {
  ["_count:protected"]=>
  NULL
  ["_cssQuery:protected"]=>
  string(18) "input#fax-0 @value"
  ["_document:protected"]=>
  object(DOMDocument)#2 (0) {
  }
  ["_nodeList:protected"]=>
  object(DOMNodeList)#5 (0) {
  }
  ["_position:protected"]=>
  int(0)
  ["_xpath:protected"]=>
  NULL
  ["_xpathQuery:protected"]=>
  string(28) "//input[@id='fax-0']//@value"
}
string(13) "(1234) 567890"
This is exactly what I want. But when using the query from above in a Zend_Test_PHPUnit_Constraint_DomQuery PHP crashes most obviously due to the code of Zend_Test_PHPUnit_Constraint_DomQuery::_getNodeContent(DOMNode $node) which reads:
$doc     = $node->ownerDocument;
$content = $doc->saveXML($node);
$tag     = $node->nodeName;
$regex   = '|</?' . $tag . '[^>]*>|';
return preg_replace($regex, '', $content);
This doesn't work id $node is a DOMAttr.
Hide
Stefan Gehrig added a comment -

Fixed in r18224.
Zend_Test_PHPUnit_Constraint_DomQuery allows matching of attribute values.
Although the original issue seems to be related to the PHP version used, the present fix is PHP version independent.

Show
Stefan Gehrig added a comment - Fixed in r18224. Zend_Test_PHPUnit_Constraint_DomQuery allows matching of attribute values. Although the original issue seems to be related to the PHP version used, the present fix is PHP version independent.

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: