diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php index 037c6a2a7..65c1a0e39 100644 --- a/lib/Cake/Test/Case/Utility/HashTest.php +++ b/lib/Cake/Test/Case/Utility/HashTest.php @@ -831,6 +831,39 @@ class HashTest extends CakeTestCase { $this->assertEquals(5, $result[3]['id']); } +/** + * Test extracting based on attributes with boolean values. + * + * @return void + */ + public function testExtractAttributeBoolean() { + $users = array( + array( + 'id' => 2, + 'username' => 'johndoe', + 'active' => true + ), + array( + 'id' => 5, + 'username' => 'kevin', + 'active' => true + ), + array( + 'id' => 9, + 'username' => 'samantha', + 'active' => false + ), + ); + $result = Hash::extract($users, '{n}[active=false]'); + $this->assertCount(1, $result); + $this->assertEquals($users[2], $result[0]); + + $result = Hash::extract($users, '{n}[active=true]'); + $this->assertCount(2, $result); + $this->assertEquals($users[0], $result[0]); + $this->assertEquals($users[1], $result[1]); + } + /** * Test that attribute matchers don't cause errors on scalar data. * diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php index 7c9b685f0..fa22fe8d1 100644 --- a/lib/Cake/Utility/Hash.php +++ b/lib/Cake/Utility/Hash.php @@ -192,7 +192,13 @@ class Hash { return false; } - $prop = isset($data[$attr]) ? $data[$attr] : null; + $prop = null; + if (isset($data[$attr])) { + $prop = $data[$attr]; + } + if ($prop === true || $prop === false) { + $prop = $prop ? 'true' : 'false'; + } // Pattern matches and other operators. if ($op === '=' && $val && $val[0] === '/') {