Browse Source

Merge pull request #11700 from o0h/fix-hash-matches-to-string

Fix Hash::_matches() with object implements toString
Mark Story 8 years ago
parent
commit
7b183d326e
2 changed files with 18 additions and 8 deletions
  1. 2 2
      src/Utility/Hash.php
  2. 16 6
      tests/TestCase/Utility/HashTest.php

+ 2 - 2
src/Utility/Hash.php

@@ -276,8 +276,8 @@ class Hash
                 if (!preg_match($val, $prop)) {
                     return false;
                 }
-            } elseif (($op === '=' && $prop !== $val) ||
-                ($op === '!=' && $prop === $val) ||
+            } elseif (($op === '=' && $prop != $val) ||
+                ($op === '!=' && $prop == $val) ||
                 ($op === '>' && $prop <= $val) ||
                 ($op === '<' && $prop >= $val) ||
                 ($op === '>=' && $prop < $val) ||

+ 16 - 6
tests/TestCase/Utility/HashTest.php

@@ -1422,23 +1422,33 @@ class HashTest extends TestCase
     }
 
     /**
-     * Test extracting value-zero contained data based on attributes with string
+     * Test extracting attributes with string
      *
      * @return void
      */
-    public function testExtractAttributeStringWithDataContainsZero()
+    public function testExtractAttributeString()
     {
         $data = [
-            ['value' => '0'],
             ['value' => 0],
+            ['value' => 3],
             ['value' => 'string-value'],
+            ['value' => new Time('2010-01-05 01:23:45')],
         ];
 
-        $expected = [
-            ['value' => 'string-value'],
-        ];
+        // check _matches does not work as `0 == 'string-value'`
+        $expected = [$data[2]];
         $result = Hash::extract($data, '{n}[value=string-value]');
         $this->assertSame($expected, $result);
+
+        // check _matches work with object implements __toString()
+        $expected = [$data[3]];
+        $result = Hash::extract($data, sprintf('{n}[value=%s]', $data[3]['value']));
+        $this->assertSame($expected, $result);
+
+        // check _matches does not work as `3 == '3 people'`
+        $unexpected = $data[1];
+        $result = Hash::extract($data, '{n}[value=3people]');
+        $this->assertNotContains($unexpected, $result);
     }
 
     /**