simpletestlib MDL-24637 ContainsTagWithAttribute(s) does not work reliabily with...
authorTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 12 Oct 2010 15:01:54 +0000 (15:01 +0000)
committerTim Hunt <T.J.Hunt@open.ac.uk>
Tue, 12 Oct 2010 15:01:54 +0000 (15:01 +0000)
lib/simpletest/testsimpletestlib.php
lib/simpletestlib.php

index b6b2784..c87d215 100644 (file)
@@ -96,8 +96,67 @@ class ContainsTagWithAttribute_test extends UnitTestCase {
         $html = '<label for="html_select4ac387224bf9d">Cool menu</label><select name="mymenu" id="html_select4ac387224bf9d" class="menumymenu select"> <option value="0">Choose...</option><option value="10">ten</option><option value="c2">two</option></select>';
         $this->assert($expectation, $html);
     }
+
+    function test_zero_attr() {
+        $expectation = new ContainsTagWithAttribute('span', 'class', 0);
+        $this->assertTrue($expectation->test('<span class="0">message</span>'));
+    }
+
+    function test_zero_attr_does_not_match_blank() {
+        $expectation = new ContainsTagWithAttribute('span', 'class', 0);
+        $this->assertFalse($expectation->test('<span class="">message</span>'));
+    }
+
+    function test_blank_attr() {
+        $expectation = new ContainsTagWithAttribute('span', 'class', '');
+        $this->assertTrue($expectation->test('<span class="">message</span>'));
+    }
+
+    function test_blank_attr_does_not_match_zero() {
+        $expectation = new ContainsTagWithAttribute('span', 'class', '');
+        $this->assertFalse($expectation->test('<span class="0">message</span>'));
+    }
 }
 
+
+/**
+ * Unit tests for the ContainsTagWithAttribute class.
+ *
+ * @copyright 2009 Tim Hunt
+ * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
+class ContainsTagWithAttributes_test extends UnitTestCase {
+    function test_simple() {
+        $content = <<<END
+<input id="qIhr6wWLTt3,1_omact_gen_14" name="qIhr6wWLTt3,1_omact_gen_14" onclick="if(this.hasSubmitted) { return false; } this.hasSubmitted=true; preSubmit(this.form); return true;" type="submit" value="Check" />
+END;
+        $expectation = new ContainsTagWithAttributes('input',
+                array('type' => 'submit', 'name' => 'qIhr6wWLTt3,1_omact_gen_14', 'value' => 'Check'));
+        $this->assert($expectation, $content);
+    }
+
+    function test_zero_attr() {
+        $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
+        $this->assertTrue($expectation->test('<span class="0">message</span>'));
+    }
+
+    function test_zero_attr_does_not_match_blank() {
+        $expectation = new ContainsTagWithAttributes('span', array('class' => 0));
+        $this->assertFalse($expectation->test('<span class="">message</span>'));
+    }
+
+    function test_blank_attr() {
+        $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
+        $this->assertTrue($expectation->test('<span class="">message</span>'));
+    }
+
+    function test_blank_attr_does_not_match_zero() {
+        $expectation = new ContainsTagWithAttributes('span', array('class' => ''));
+        $this->assertFalse($expectation->test('<span class="0">message</span>'));
+    }
+}
+
+
 /**
  * Unit tests for the ContainsTagWithAttribute_test class.
  *
index 76c8939..6d4ff06 100644 (file)
@@ -229,7 +229,7 @@ class ContainsTagWithAttribute extends XMLStructureExpectation {
         $list = $parser->getElementsByTagName($this->tag);
 
         foreach ($list as $node) {
-            if ($node->attributes->getNamedItem($this->attribute)->nodeValue == $this->value) {
+            if ($node->attributes->getNamedItem($this->attribute)->nodeValue === (string) $this->value) {
                 return true;
             }
         }
@@ -291,11 +291,11 @@ class ContainsTagWithAttributes extends XMLStructureExpectation {
             $allattributesmatch = true;
 
             foreach ($this->expectedvalues as $expectedattribute => $expectedvalue) {
-                if (!$node->getAttribute($expectedattribute) && $expectedvalue != '') {
+                if ($node->getAttribute($expectedattribute) === '' && $expectedvalue !== '') {
                     $this->failurereason = 'nomatch';
                     continue 2; // Skip this tag, it doesn't have all the expected attributes
                 }
-                if ($node->getAttribute($expectedattribute) != $expectedvalue) {
+                if ($node->getAttribute($expectedattribute) !== (string) $expectedvalue) {
                     $allattributesmatch = false;
                     $this->failurereason = 'nomatch';
                 }
@@ -308,7 +308,7 @@ class ContainsTagWithAttributes extends XMLStructureExpectation {
                 $nodeattrlist = $node->attributes;
 
                 foreach ($nodeattrlist as $domattrname => $domattr) {
-                    if (array_key_exists($domattrname, $this->forbiddenvalues) && $node->getAttribute($domattrname) == $this->forbiddenvalues[$domattrname]) {
+                    if (array_key_exists($domattrname, $this->forbiddenvalues) && $node->getAttribute($domattrname) === (string) $this->forbiddenvalues[$domattrname]) {
                         $this->failurereason = "forbiddenmatch:$domattrname:" . $node->getAttribute($domattrname);
                         $foundamatch = false;
                     }