From fc2bec7a5e64a6cd2f65409adda7a87d32a08a9f Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Mon, 24 Nov 2014 01:55:42 +0000 Subject: [PATCH] `json_encode()` returns different results for non UTF-8 strings in PHP 5.5+, versus earlier versions of PHP. This fixes the unit tests that fail in earlier versions, see #30471 for fixing this globally in `wp_json_encode()`. git-svn-id: https://develop.svn.wordpress.org/trunk@30534 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/functions.php | 42 +++++++++++++++++++++++++++---- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 40c001067c..7b2870c6e5 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -543,10 +543,6 @@ class Tests_Functions extends WP_UnitTestCase { * @ticket 28786 */ function test_wp_json_encode_non_utf8() { - if ( version_compare( PHP_VERSION, '5.4', '<' ) ) { - $this->markTestSkipped( 'EUC-JP character set added in PHP 5.4' ); - }; - $old_charsets = $charsets = mb_detect_order(); if ( ! in_array( 'EUC-JP', $charsets ) ) { $charsets[] = 'EUC-JP'; @@ -558,7 +554,43 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals( 'aあb', $utf8 ); - $this->assertEquals( wp_json_encode( $eucjp ), '"a\u3042b"' ); + // json_encode() returns different things in different PHP versions. + // See: https://core.trac.wordpress.org/ticket/30471 + if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { + $expected = '"a\u3042b"'; + } else { + $expected = 'null'; + } + + $this->assertEquals( $expected, wp_json_encode( $eucjp ) ); + + mb_detect_order( $old_charsets ); + } + + /** + * @ticket 28786 + */ + function test_wp_json_encode_non_utf8_in_array() { + $old_charsets = $charsets = mb_detect_order(); + if ( ! in_array( 'EUC-JP', $charsets ) ) { + $charsets[] = 'EUC-JP'; + mb_detect_order( $charsets ); + } + + $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); + $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); + + $this->assertEquals( 'aあb', $utf8 ); + + // json_encode() returns different things in different PHP versions. + // See: https://core.trac.wordpress.org/ticket/30471 + if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { + $expected = '["c","a\u3042b"]'; + } else { + $expected = '["c",null]'; + } + + $this->assertEquals( $expected, wp_json_encode( array( 'c', $eucjp ) ) ); mb_detect_order( $old_charsets ); }