From d5bbc449e987932af4266f895fbcefe6463e36b2 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Tue, 12 Oct 2010 15:01:54 +0000 Subject: [PATCH] simpletestlib MDL-24637 ContainsTagWithAttribute(s) does not work reliabily with attribute values "0" and "" --- lib/simpletest/testsimpletestlib.php | 59 ++++++++++++++++++++++++++++++++++++ lib/simpletestlib.php | 8 ++--- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/lib/simpletest/testsimpletestlib.php b/lib/simpletest/testsimpletestlib.php index b6b278440f5..c87d2155da2 100644 --- a/lib/simpletest/testsimpletestlib.php +++ b/lib/simpletest/testsimpletestlib.php @@ -96,8 +96,67 @@ class ContainsTagWithAttribute_test extends UnitTestCase { $html = ''; $this->assert($expectation, $html); } + + function test_zero_attr() { + $expectation = new ContainsTagWithAttribute('span', 'class', 0); + $this->assertTrue($expectation->test('message')); + } + + function test_zero_attr_does_not_match_blank() { + $expectation = new ContainsTagWithAttribute('span', 'class', 0); + $this->assertFalse($expectation->test('message')); + } + + function test_blank_attr() { + $expectation = new ContainsTagWithAttribute('span', 'class', ''); + $this->assertTrue($expectation->test('message')); + } + + function test_blank_attr_does_not_match_zero() { + $expectation = new ContainsTagWithAttribute('span', 'class', ''); + $this->assertFalse($expectation->test('message')); + } } + +/** + * 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; + $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('message')); + } + + function test_zero_attr_does_not_match_blank() { + $expectation = new ContainsTagWithAttributes('span', array('class' => 0)); + $this->assertFalse($expectation->test('message')); + } + + function test_blank_attr() { + $expectation = new ContainsTagWithAttributes('span', array('class' => '')); + $this->assertTrue($expectation->test('message')); + } + + function test_blank_attr_does_not_match_zero() { + $expectation = new ContainsTagWithAttributes('span', array('class' => '')); + $this->assertFalse($expectation->test('message')); + } +} + + /** * Unit tests for the ContainsTagWithAttribute_test class. * diff --git a/lib/simpletestlib.php b/lib/simpletestlib.php index 76c89396807..6d4ff0614dd 100644 --- a/lib/simpletestlib.php +++ b/lib/simpletestlib.php @@ -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; } -- 2.11.4.GIT