Make WordPress Core


Ignore:
Timestamp:
11/24/2014 01:55:42 AM (12 years ago)
Author:
pento
Message:

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().

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/functions.php

    r30533 r30534  
    544544         */
    545545        function test_wp_json_encode_non_utf8() {
    546                 if ( version_compare( PHP_VERSION, '5.4', '<' ) ) {
    547                         $this->markTestSkipped( 'EUC-JP character set added in PHP 5.4' );
    548                 };
    549 
    550546                $old_charsets = $charsets = mb_detect_order();
    551547                if ( ! in_array( 'EUC-JP', $charsets ) ) {
    … …  
    559555                $this->assertEquals( 'aあb', $utf8 );
    560556
    561                 $this->assertEquals( wp_json_encode( $eucjp ), '"a\u3042b"' );
     557                // json_encode() returns different things in different PHP versions.
     558                // See: https://core.trac.wordpress.org/ticket/30471
     559                if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
     560                        $expected = '"a\u3042b"';
     561                } else {
     562                        $expected = 'null';
     563                }
     564
     565                $this->assertEquals( $expected, wp_json_encode( $eucjp ) );
     566
     567                mb_detect_order( $old_charsets );
     568        }
     569
     570        /**
     571         * @ticket 28786
     572         */
     573        function test_wp_json_encode_non_utf8_in_array() {
     574                $old_charsets = $charsets = mb_detect_order();
     575                if ( ! in_array( 'EUC-JP', $charsets ) ) {
     576                        $charsets[] = 'EUC-JP';
     577                        mb_detect_order( $charsets );
     578                }
     579
     580                $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
     581                $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
     582
     583                $this->assertEquals( 'aあb', $utf8 );
     584
     585                // json_encode() returns different things in different PHP versions.
     586                // See: https://core.trac.wordpress.org/ticket/30471
     587                if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
     588                        $expected = '["c","a\u3042b"]';
     589                } else {
     590                        $expected = '["c",null]';
     591                }
     592
     593                $this->assertEquals( $expected, wp_json_encode( array( 'c', $eucjp ) ) );
    562594
    563595                mb_detect_order( $old_charsets );
Note: See TracChangeset for help on using the changeset viewer.