From d990c6ed44afa8e670f9029dde17b8117db1f501 Mon Sep 17 00:00:00 2001 From: Mark Story Date: Sun, 4 Apr 2010 21:43:29 -0400 Subject: [PATCH] Adding tests for single quoted attributes to assertTags(). Because of the __array_permute works ensuring quotes match is a non-trivial problem. Fixing single quoted attributes not being matchable with assertTags(). Fixes #539 --- cake/tests/cases/libs/cake_test_case.test.php | 35 +++++++++++++++++-- cake/tests/lib/cake_test_case.php | 9 +++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cake/tests/cases/libs/cake_test_case.test.php b/cake/tests/cases/libs/cake_test_case.test.php index b138d7a76..dd11366f2 100644 --- a/cake/tests/cases/libs/cake_test_case.test.php +++ b/cake/tests/cases/libs/cake_test_case.test.php @@ -138,7 +138,7 @@ class CakeTestCaseTest extends CakeTestCase { 'My link', '/a' ); - $this->assertTrue($this->Case->assertTags($input, $pattern)); + $this->assertTrue($this->Case->assertTags($input, $pattern), 'Attributes in wrong order. %s'); $input = "\tMy link"; $pattern = array( @@ -148,7 +148,7 @@ class CakeTestCaseTest extends CakeTestCase { '/span', '/a' ); - $this->assertTrue($this->Case->assertTags($input, $pattern)); + $this->assertTrue($this->Case->assertTags($input, $pattern), 'Whitespace consumption %s'); $input = '

My link

'; $pattern = array( @@ -163,6 +163,37 @@ class CakeTestCaseTest extends CakeTestCase { $this->assertTrue($this->Case->assertTags($input, $pattern)); } +/** + * test that assertTags knows how to handle correct quoting. + * + * @return void + */ + function testAssertTagsQuotes() { + $input = 'My link'; + $pattern = array( + 'a' => array('href' => '/test.html', 'class' => 'active'), + 'My link', + '/a' + ); + $this->assertTrue($this->Case->assertTags($input, $pattern), 'Double quoted attributes %s'); + + $input = "My link"; + $pattern = array( + 'a' => array('href' => '/test.html', 'class' => 'active'), + 'My link', + '/a' + ); + $this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s'); + + $input = "My link"; + $pattern = array( + 'a' => array('href' => 'preg:/.*\.html/', 'class' => 'active'), + 'My link', + '/a' + ); + $this->assertTrue($this->Case->assertTags($input, $pattern), 'Single quoted attributes %s'); + } + /** * testNumericValuesInExpectationForAssertTags * diff --git a/cake/tests/lib/cake_test_case.php b/cake/tests/lib/cake_test_case.php index b27636db2..5f9341387 100644 --- a/cake/tests/lib/cake_test_case.php +++ b/cake/tests/lib/cake_test_case.php @@ -631,30 +631,33 @@ class CakeTestCase extends UnitTestCase { } $attrs = array(); $explanations = array(); + $i = 1; foreach ($attributes as $attr => $val) { if (is_numeric($attr) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) { $attrs[] = $matches[1]; $explanations[] = sprintf('Regex "%s" matches', $matches[1]); continue; } else { - $quotes = '"'; + $quotes = '["\']'; if (is_numeric($attr)) { $attr = $val; $val = '.+?'; $explanations[] = sprintf('Attribute "%s" present', $attr); } elseif (!empty($val) && preg_match('/^preg\:\/(.+)\/$/i', $val, $matches)) { - $quotes = '"?'; + $quotes = '["\']?'; $val = $matches[1]; $explanations[] = sprintf('Attribute "%s" matches "%s"', $attr, $val); } else { $explanations[] = sprintf('Attribute "%s" == "%s"', $attr, $val); $val = preg_quote($val, '/'); } - $attrs[] = '[\s]+'.preg_quote($attr, '/').'='.$quotes.$val.$quotes; + $attrs[] = '[\s]+' . preg_quote($attr, '/') . '=' . $quotes . $val . $quotes; } + $i++; } if ($attrs) { $permutations = $this->__array_permute($attrs); + $permutationTokens = array(); foreach ($permutations as $permutation) { $permutationTokens[] = implode('', $permutation);