Browse Source

Make h() return all non string scalars unchanged.

Earlier floats were converted to string.
ADmad 8 years ago
parent
commit
8a56d7bf32
2 changed files with 28 additions and 1 deletions
  1. 1 1
      src/Core/functions.php
  2. 27 0
      tests/TestCase/Core/FunctionsTest.php

+ 1 - 1
src/Core/functions.php

@@ -51,7 +51,7 @@ if (!function_exists('h')) {
             } else {
                 $text = '(object)' . get_class($text);
             }
-        } elseif ($text === null || is_bool($text) || is_int($text)) {
+        } elseif ($text === null || is_scalar($text)) {
             return $text;
         }
 

+ 27 - 0
tests/TestCase/Core/FunctionsTest.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Test\TestCase\Core;
 
+use Cake\Http\Response;
 use Cake\TestSuite\TestCase;
 
 /**
@@ -44,6 +45,32 @@ class FunctionsTest extends TestCase
     }
 
     /**
+     * Test cases for h()
+     *
+     * @return void
+     * @dataProvider hInputProvider
+     */
+    public function testH($value, $expected)
+    {
+        $result = h($value);
+        $this->assertSame($expected, $result);
+    }
+
+    public function hInputProvider()
+    {
+        return [
+            ['i am clean', 'i am clean'],
+            ['i "need" escaping', 'i "need" escaping'],
+            [null, null],
+            [1, 1],
+            [1.1, 1.1],
+            [new \stdClass, '(object)stdClass'],
+            [new Response(), ''],
+            [['clean', '"clean-me'], ['clean', '"clean-me']],
+        ];
+    }
+
+    /**
      * Test error messages coming out when deprecated level is on, manually setting the stack frame
      *
      * @expectedException PHPUnit\Framework\Error\Deprecated