Make WordPress Core


Ignore:
Timestamp:
05/06/2015 03:29:01 AM (9 years ago)
Author:
pento
Message:

WPDB: When checking that a string can be sent to MySQL, we shouldn't use mb_convert_encoding(), as it behaves differently to MySQL's character encoding conversion.

Merge of [32364] to the 4.2 branch.

Props mdawaffe, pento, nbachiyski, jorbin, johnjamesjacoby, jeremyfelt.

See #32165.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/4.2/tests/phpunit/tests/compat.php

    r25002 r32367  
    33/**
    44 * @group compat
     5 * @group security-153
    56 */
    67class Tests_Compat extends WP_UnitTestCase {
    7     function test_mb_substr() {
    8         $this->assertEquals('баб', _mb_substr('баба', 0, 3));
    9         $this->assertEquals('баб', _mb_substr('баба', 0, -1));
    10         $this->assertEquals('баб', _mb_substr('баба', 0, -1));
    11         $this->assertEquals('I am your б', _mb_substr('I am your баба', 0, 11));
     8    function utf8_string_lengths() {
     9        return array(
     10            //                     string, character_length, byte_length
     11            array(                 'баба',                4,           8 ),
     12            array(                  'баб',                3,           6 ),
     13            array(          'I am your б',               11,          12 ),
     14            array(           '1111111111',               10,          10 ),
     15            array(           '²²²²²²²²²²',               10,          20 ),
     16            array( '3333333333',               10,          30 ),
     17            array(           '𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜𝟜',               10,          40 ),
     18            array(      '1²3𝟜1²3𝟜1²3𝟜',               12,          30 ),
     19        );
     20    }
     21
     22    function utf8_substrings() {
     23        return array(
     24            //               string, start, length, character_substring,   byte_substring
     25            array(           'баба',     0,      3,               'баб',          "б\xD0" ),
     26            array(           'баба',     0,     -1,               'баб',        "баб\xD0" ),
     27            array(           'баба',     1,   null,               'аба',        "\xB1аба" ),
     28            array(           'баба',    -3,   null,               'аба',          "\xB1а" ),
     29            array(           'баба',    -3,      2,                'аб',       "\xB1\xD0" ),
     30            array(           'баба',    -1,      2,                 'а',           "\xB0" ),
     31            array( 'I am your баба',     0,     11,       'I am your б', "I am your \xD0" ),
     32        );
     33    }
     34
     35    /**
     36     * @dataProvider utf8_string_lengths
     37     */
     38    function test_mb_strlen( $string, $expected_character_length ) {
     39        $this->assertEquals( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
     40    }
     41
     42    /**
     43     * @dataProvider utf8_string_lengths
     44     */
     45    function test_mb_strlen_via_regex( $string, $expected_character_length ) {
     46        _wp_can_use_pcre_u( false );
     47        $this->assertEquals( $expected_character_length, _mb_strlen( $string, 'UTF-8' ) );
     48        _wp_can_use_pcre_u( 'reset' );
     49    }
     50
     51    /**
     52     * @dataProvider utf8_string_lengths
     53     */
     54    function test_8bit_mb_strlen( $string, $expected_character_length, $expected_byte_length ) {
     55        $this->assertEquals( $expected_byte_length, _mb_strlen( $string, '8bit' ) );
     56    }
     57
     58    /**
     59     * @dataProvider utf8_substrings
     60     */
     61    function test_mb_substr( $string, $start, $length, $expected_character_substring ) {
     62        $this->assertEquals( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
     63    }
     64
     65    /**
     66     * @dataProvider utf8_substrings
     67     */
     68    function test_mb_substr_via_regex( $string, $start, $length, $expected_character_substring ) {
     69        _wp_can_use_pcre_u( false );
     70        $this->assertEquals( $expected_character_substring, _mb_substr( $string, $start, $length, 'UTF-8' ) );
     71        _wp_can_use_pcre_u( 'reset' );
     72    }
     73
     74    /**
     75     * @dataProvider utf8_substrings
     76     */
     77    function test_8bit_mb_substr( $string, $start, $length, $expected_character_substring, $expected_byte_substring ) {
     78        $this->assertEquals( $expected_byte_substring, _mb_substr( $string, $start, $length, '8bit' ) );
     79    }
     80
     81    function test_mb_substr_phpcore(){
     82        /* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_basic.phpt */
     83        $string_ascii = 'ABCDEF';
     84        $string_mb = base64_decode('5pel5pys6Kqe44OG44Kt44K544OI44Gn44GZ44CCMDEyMzTvvJXvvJbvvJfvvJjvvJnjgII=');
     85
     86        $this->assertEquals( 'DEF', _mb_substr($string_ascii, 3) );
     87        $this->assertEquals( 'DEF', _mb_substr($string_ascii, 3, 5, 'ISO-8859-1') );
     88
     89        // specific latin-1 as that is the default the core php test opporates under   
     90        $this->assertEquals( 'peacrOiqng==' , base64_encode( _mb_substr($string_mb, 2, 7, 'latin-1' ) ) );
     91        $this->assertEquals( '6Kqe44OG44Kt44K544OI44Gn44GZ', base64_encode( _mb_substr($string_mb, 2, 7, 'utf-8') ) );
     92
     93        /* https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt */
     94        $start = 0;
     95        $length = 5;
     96        $unset_var = 10;
     97        unset ($unset_var);
     98        $heredoc = <<<EOT
     99hello world
     100EOT;
     101        $inputs = array(
     102        /*1*/  0,
     103               1,
     104               12345,
     105               -2345,
     106               // float data
     107        /*5*/  10.5,
     108               -10.5,
     109               12.3456789000e10,
     110               12.3456789000E-10,
     111               .5,
     112               // null data
     113        /*10*/ NULL,
     114               null,
     115               // boolean data
     116        /*12*/ true,
     117               false,
     118               TRUE,
     119               FALSE,
     120               // empty data
     121        /*16*/ "",
     122               '',
     123               // string data
     124        /*18*/ "string",
     125               'string',
     126               $heredoc,
     127               // object data
     128        /*21*/ new classA(),
     129               // undefined data
     130        /*22*/ @$undefined_var,
     131               // unset data
     132        /*23*/ @$unset_var,
     133        );
     134        $outputs = array(
     135            "0",
     136            "1",
     137            "12345",
     138            "-2345",
     139            "10.5",
     140            "-10.5",
     141            "12345",
     142            "1.234",
     143            "0.5",
     144            "",
     145            "",
     146            "1",
     147            "",
     148            "1",
     149            "",
     150            "",
     151            "",
     152            "strin",
     153            "strin",
     154            "hello",
     155            "Class",
     156            "",
     157            "",
     158        );
     159        $iterator = 0;
     160        foreach($inputs as $input) {
     161            $this->assertEquals( $outputs[$iterator] ,  _mb_substr($input, $start, $length) );
     162            $iterator++;
     163        }
     164
    12165    }
    13166
     
    35188    }
    36189}
     190
     191/* used in test_mb_substr_phpcore */
     192class classA {
     193    public function __toString() {
     194        return "Class A object";
     195    }
     196}
Note: See TracChangeset for help on using the changeset viewer.