function test_other_attrs() {
$expectation = new ContainsTagWithAttribute('span', 'class', 'error');
- $this->assertTrue($expectation->test('<span otherattr="thingy" class = "error" otherattr="thingy">message</span>'));
+ $this->assertTrue($expectation->test('<span oneattr="thingy" class = "error" otherattr="thingy">message</span>'));
}
function test_fails() {
}
function test_link() {
+ $html = '<a href="http://www.test.com">Click Here</a>';
$expectation = new ContainsTagWithAttribute('a', 'href', 'http://www.test.com');
- $this->assertTrue($expectation->test('<a href="http://www.test.com">Click Here</a>'));
+ $this->assertTrue($expectation->test($html));
+ $this->assert(new ContainsTagWithContents('a', 'Click Here'), $html);
+ }
+
+ function test_garbage() {
+ $expectation = new ContainsTagWithAttribute('a', 'href', '!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~');
+ $this->assertTrue($expectation->test('<a href="!#@*%@_-)(*#0-735\\fdf//fdfg235-0970}$@}{#:~">Click Here</a>'));
+
+ }
+
+ function test_inline_js() {
+ $html = '<a title="Popup window" href="http://otheraddress.com" class="link" onclick="this.target=\'my_popup\';">Click here</a>';
+ $this->assert(new ContainsTagWithAttribute('a', 'href', 'http://otheraddress.com'), $html);
}
}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ContainsTagWithAttribute extends SimpleExpectation {
- const ATTRSREGEX = '(?:\s+\w+\s*=\s*["\'][^"\'>]*["\'])*';
-
protected $tag;
protected $attribute;
protected $value;
}
function test($html) {
- $regex = '/<' . preg_quote($this->tag, '/') . self::ATTRSREGEX .
- '(?:\s+' . preg_quote($this->attribute, '/') . '\s*=\s*["\']' . preg_quote($this->value, '/') . '["\'])' .
- self::ATTRSREGEX . '\s*>/';
- return preg_match($regex, $html);
+ $parser = new DOMDocument();
+ $parser->validateOnParse = true;
+ $parser->loadHTML($html);
+ $list = $parser->getElementsByTagName($this->tag);
+
+ foreach ($list as $node) {
+ if ($node->attributes->getNamedItem($this->attribute)->nodeValue == $this->value) {
+ return true;
+ }
+ }
+ return false;
}
function testMessage($html) {
}
}
+/**
+ * An Expectation that looks to see whether some HMTL contains a tag with an array of attributes.
+ * All attributes must be present and their values must match the expected values.
+ *
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class ContainsTagWithAttributes extends SimpleExpectation {
+ protected $tag;
+ protected $attributes = array();
+
+ function __construct($tag, $attributes, $message = '%s') {
+ $this->SimpleExpectation($message);
+ $this->tag = $tag;
+ $this->attributes = $attributes;
+ }
+
+ function test($html) {
+ $parser = new DOMDocument();
+ $parser->validateOnParse = true;
+ $parser->loadHTML($html);
+ $list = $parser->getElementsByTagName($this->tag);
+
+ // Iterating through inputs
+ foreach ($list as $node) {
+ if (empty($node->attributes) || !is_a($node->attributes, 'DOMNamedNodeMap')) {
+ continue;
+ }
+
+ $result = true;
+
+ foreach ($this->attributes as $attribute => $expectedvalue) {
+ if (!$node->attributes->getNamedItem($attribute)) {
+ break 2;
+ }
+
+ if ($node->attributes->getNamedItem($attribute)->value != $expectedvalue) {
+ $result = false;
+ }
+ }
+
+ if ($result) {
+ return true;
+ }
+
+ }
+ return false;
+ }
+
+ function testMessage($html) {
+ $output = 'Content [' . $html . '] does not contain the tag [' . $this->tag . '] with attributes [';
+ foreach ($this->attributes as $var => $val) {
+ $output .= "$var=\"$val\" ";
+ }
+ $output = rtrim($output);
+ $output .= ']';
+ return $output;
+ }
+}
/**
* An Expectation that looks to see whether some HMTL contains a tag with a certain text inside it.
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class ContainsTagWithContents extends SimpleExpectation {
- const ATTRSREGEX = '(?:\s+\w+\s*=\s*["\'][^"\'>]*["\'])*';
-
protected $tag;
protected $content;
}
function test($html) {
- $regex = '/<' . preg_quote($this->tag, '/') . self::ATTRSREGEX . '\s*>' . preg_quote($this->content, '/') .
- '<\/' . preg_quote($this->tag, '/') . '>/';
- return preg_match($regex, $html);
+ $parser = new DOMDocument();
+ $parser->loadHTML($html);
+ $list = $parser->getElementsByTagName($this->tag);
+
+ foreach ($list as $node) {
+ if ($node->textContent == $this->content) {
+ return true;
+ }
+ }
+
+ return false;
}
function testMessage($html) {
}
}
+/**
+ * An Expectation that looks to see whether some HMTL contains an empty tag of a specific type.
+ *
+ * @copyright 2009 Nicolas Connault
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class ContainsEmptyTag extends SimpleExpectation {
+ protected $tag;
+
+ function __construct($tag, $message = '%s') {
+ $this->SimpleExpectation($message);
+ $this->tag = $tag;
+ }
+
+ function test($html) {
+ $parser = new DOMDocument();
+ $parser->loadHTML($html);
+ $list = $parser->getElementsByTagName($this->tag);
+
+ foreach ($list as $node) {
+ if (!$node->hasAttributes() && !$node->hasChildNodes()) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ function testMessage($html) {
+ return 'Content ['.$html.'] does not contain the empty tag ['.$this->tag.'].';
+ }
+}
+
/**
* This class lets you write unit tests that access a separate set of test