WordPress.org

Make WordPress Core


Ignore:
Timestamp:
06/01/2016 02:37:20 AM (4 years ago)
Author:
pento
Message:

Database: Split the logic of wpdb::init_charset() into a separate method.

The logic for determining the appropriate character set and collation to use is becoming more complex, particularly with the recent additions of [37522] and [37523]. As init_charset() has side effects, and makes use of constants instead of parameters, it's not possible to unit test this logic.

This commit splits the logic part of init_charset() out into a new method, wpdb::determine_charset(), along with appropriate unit tests.

See #32105, #37522.

Fixes #36917.

File:
1 edited

Legend:

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

    r37522 r37601  
    956956        $wpdb->check_connection();
    957957    }
     958
     959    /**
     960     * @ticket 36917
     961     */
     962    function test_charset_not_determined_when_disconnected() {
     963        global $wpdb;
     964
     965        $charset = 'utf8';
     966        $collate = 'this_isnt_a_collation';
     967
     968        $wpdb->close();
     969
     970        $result = $wpdb->determine_charset( $charset, $collate );
     971
     972        $this->assertSame( compact( 'charset', 'collate' ), $result );
     973
     974        $wpdb->check_connection();
     975    }
     976
     977    /**
     978     * @ticket 36917
     979     */
     980    function test_charset_switched_to_utf8mb4() {
     981        global $wpdb;
     982
     983        if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
     984            $this->markTestSkipped( 'This test requires utf8mb4 support.' );
     985        }
     986
     987        $charset = 'utf8';
     988        $collate = 'utf8_general_ci';
     989
     990        $result = $wpdb->determine_charset( $charset, $collate );
     991
     992        $this->assertSame( 'utf8mb4', $result['charset'] );
     993    }
     994
     995    /**
     996     * @ticket 32105
     997     * @ticket 36917
     998     */
     999    function test_collate_switched_to_utf8mb4_520() {
     1000        global $wpdb;
     1001
     1002        if ( ! $wpdb->has_cap( 'utf8mb4_520' ) ) {
     1003            $this->markTestSkipped( 'This test requires utf8mb4_520 support.' );
     1004        }
     1005
     1006        $charset = 'utf8';
     1007        $collate = 'utf8_general_ci';
     1008
     1009        $result = $wpdb->determine_charset( $charset, $collate );
     1010
     1011        $this->assertSame( 'utf8mb4_unicode_520_ci', $result['collate'] );
     1012    }
     1013
     1014    /**
     1015     * @ticket 36917
     1016     * @ticket 37522
     1017     */
     1018    function test_non_unicode_collations() {
     1019        global $wpdb;
     1020
     1021        if ( ! $wpdb->has_cap( 'utf8mb4' ) ) {
     1022            $this->markTestSkipped( 'This test requires utf8mb4 support.' );
     1023        }
     1024
     1025        $charset = 'utf8';
     1026        $collate = 'utf8_swedish_ci';
     1027
     1028        $result = $wpdb->determine_charset( $charset, $collate );
     1029
     1030        $this->assertSame( 'utf8mb4_swedish_ci', $result['collate'] );
     1031    }
    9581032}
Note: See TracChangeset for help on using the changeset viewer.