WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/28/2014 06:34:16 PM (6 years ago)
Author:
pento
Message:

Add wp_json_encode(), a wrapper for json_encode() that ensures everything is converted to UTF-8.

Change all core calls from json_encode() to wp_json_encode().

Fixes #28786.

File:
1 edited

Legend:

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

    r29756 r30055  
    530530        $this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
    531531    }
     532
     533    /**
     534     * @ticket 28786
     535     */
     536    function test_wp_json_encode() {
     537        $this->assertEquals( wp_json_encode( 'a' ), '"a"' );
     538        $this->assertEquals( wp_json_encode( '这' ), '"\u8fd9"' );
     539
     540        $old_charsets = $charsets = mb_detect_order();
     541        if ( ! in_array( 'EUC-JP', $charsets ) ) {
     542            $charsets[] = 'EUC-JP';
     543            mb_detect_order( $charsets );
     544        }
     545
     546        $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
     547        $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
     548
     549        $this->assertEquals( 'aあb', $utf8 );
     550
     551        $this->assertEquals( wp_json_encode( $eucjp ), '"a\u3042b"' );
     552
     553        $this->assertEquals( wp_json_encode( array( 'a' ) ), '["a"]' );
     554
     555        $object = new stdClass;
     556        $object->a = 'b';
     557        $this->assertEquals( wp_json_encode( $object ), '{"a":"b"}' );
     558
     559        mb_detect_order( $old_charsets );
     560    }
     561
     562    /**
     563     * @ticket 28786
     564     */
     565    function test_wp_json_encode_depth() {
     566        $data = array( array( array( 1, 2, 3 ) ) );
     567        $json = wp_json_encode( $data, 0, 1 );
     568        $this->assertFalse( $json );
     569
     570        $data = array( 'あ', array( array( 1, 2, 3 ) ) );
     571        $json = wp_json_encode( $data, 0, 1 );
     572        $this->assertFalse( $json );
     573    }
    532574}
Note: See TracChangeset for help on using the changeset viewer.