Browse Source

Merge pull request #12022 from cakephp/3.6-h

3.6 h()
Mark Story 8 years ago
parent
commit
9b16f360f7
3 changed files with 40 additions and 6 deletions
  1. 8 3
      src/Core/functions.php
  2. 5 3
      tests/TestCase/BasicsTest.php
  3. 27 0
      tests/TestCase/Core/FunctionsTest.php

+ 8 - 3
src/Core/functions.php

@@ -25,13 +25,14 @@ if (!function_exists('h')) {
     /**
      * Convenience method for htmlspecialchars.
      *
-     * @param string|array|object $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
+     * @param mixed $text Text to wrap through htmlspecialchars. Also works with arrays, and objects.
      *    Arrays will be mapped and have all their elements escaped. Objects will be string cast if they
      *    implement a `__toString` method. Otherwise the class name will be used.
+     *    Other scalar types will be returned unchanged.
      * @param bool $double Encode existing html entities.
      * @param string|null $charset Character set to use when escaping. Defaults to config value in `mb_internal_encoding()`
      * or 'UTF-8'.
-     * @return string|array Wrapped text.
+     * @return mixed Wrapped text.
      * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#h
      */
     function h($text, $double = true, $charset = null)
@@ -51,7 +52,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;
         }
 
@@ -63,6 +64,10 @@ if (!function_exists('h')) {
             }
         }
         if (is_string($double)) {
+            deprecationWarning(
+                'Passing charset string for 2nd argument is deprecated. ' .
+                'Use the 3rd argument instead.'
+            );
             $charset = $double;
         }
 

+ 5 - 3
tests/TestCase/BasicsTest.php

@@ -160,9 +160,11 @@ class BasicsTest extends TestCase
         $result = h($string, false);
         $this->assertEquals('<foo> &  ', $result);
 
-        $string = '<foo> & &nbsp;';
-        $result = h($string, 'UTF-8');
-        $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
+        $this->deprecated(function () {
+            $string = '<foo> & &nbsp;';
+            $result = h($string, 'UTF-8');
+            $this->assertEquals('&lt;foo&gt; &amp; &amp;nbsp;', $result);
+        });
 
         $string = "An invalid\x80string";
         $result = h($string);

+ 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 &quot;need&quot; escaping'],
+            [null, null],
+            [1, 1],
+            [1.1, 1.1],
+            [new \stdClass, '(object)stdClass'],
+            [new Response(), ''],
+            [['clean', '"clean-me'], ['clean', '&quot;clean-me']],
+        ];
+    }
+
+    /**
      * Test error messages coming out when deprecated level is on, manually setting the stack frame
      *
      * @expectedException PHPUnit\Framework\Error\Deprecated