Skip to content

Commit 35ee6cd

Browse files
committed
Update _mb_substr tests and deprecate $set parameter of _wp_can_use_pcre_u()
1 parent 3a0f14d commit 35ee6cd

File tree

2 files changed

+48
-92
lines changed

2 files changed

+48
-92
lines changed

src/wp-includes/compat.php

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,42 @@ function _( $message ) {
3333
*
3434
* @ignore
3535
* @since 4.2.2
36+
* @since 6.9.0 Deprecated the `$set` argument.
3637
* @access private
3738
*
38-
* @param bool $set - Used for testing only
39-
* null : default - get PCRE/u capability
40-
* false : Used for testing - return false for future calls to this function
41-
* 'reset': Used for testing - restore default behavior of this function
39+
* @param bool $set Deprecated. This argument is no longer used for testing purposes.
4240
*/
4341
function _wp_can_use_pcre_u( $set = null ) {
44-
static $utf8_pcre = 'reset';
42+
static $utf8_pcre = null;
4543

46-
if ( null !== $set ) {
47-
$utf8_pcre = $set;
44+
if ( isset( $set ) ) {
45+
_deprecated_argument( __FUNCTION__, '6.9.0' );
4846
}
4947

50-
if ( 'reset' === $utf8_pcre ) {
51-
$utf8_pcre = true;
52-
53-
set_error_handler(
54-
function ( $errno, $errstr ) use ( &$utf8_pcre ) {
55-
if ( str_starts_with( $errstr, 'preg_match():' ) ) {
56-
$utf8_pcre = false;
57-
return true;
58-
}
48+
if ( isset( $utf8_pcre ) ) {
49+
return $utf8_pcre;
50+
}
5951

60-
return false;
61-
},
62-
E_WARNING
63-
);
52+
$utf8_pcre = true;
53+
set_error_handler(
54+
function ( $errno, $errstr ) use ( &$utf8_pcre ) {
55+
if ( str_starts_with( $errstr, 'preg_match():' ) ) {
56+
$utf8_pcre = false;
57+
return true;
58+
}
6459

65-
/*
66-
* Attempt to compile a PCRE pattern with the PCRE_UTF8 flag. For
67-
* systems lacking Unicode support this will trigger a warning
68-
* during compilation, which the error handler will intercept.
69-
*/
70-
preg_match( '//u', '' );
60+
return false;
61+
},
62+
E_WARNING
63+
);
7164

72-
restore_error_handler();
73-
}
65+
/*
66+
* Attempt to compile a PCRE pattern with the PCRE_UTF8 flag. For
67+
* systems lacking Unicode support this will trigger a warning
68+
* during compilation, which the error handler will intercept.
69+
*/
70+
preg_match( '//u', '' );
71+
restore_error_handler();
7472

7573
return $utf8_pcre;
7674
}

tests/phpunit/tests/compat/mbSubstr.php

Lines changed: 22 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,88 +13,46 @@ class Tests_Compat_mbSubstr extends WP_UnitTestCase {
1313
* Test that mb_substr() is always available (either from PHP or WP).
1414
*/
1515
public function test_mb_substr_availability() {
16-
$this->assertTrue( function_exists( 'mb_substr' ) );
17-
}
18-
19-
/**
20-
* @dataProvider data_utf8_substrings
21-
*/
22-
public function test_mb_substr( $input_string, $start, $length, $expected_character_substring ) {
23-
$this->assertSame( $expected_character_substring, _mb_substr( $input_string, $start, $length, 'UTF-8' ) );
16+
$this->assertTrue(
17+
in_array( 'mb_substr', get_defined_functions()['internal'], true ),
18+
'Test runner should have `mbstring` extension active but doesn’t.'
19+
);
2420
}
2521

2622
/**
2723
* @dataProvider data_utf8_substrings
2824
*/
29-
public function test_mb_substr_via_regex( $input_string, $start, $length, $expected_character_substring ) {
30-
_wp_can_use_pcre_u( false );
31-
$this->assertSame( $expected_character_substring, _mb_substr( $input_string, $start, $length, 'UTF-8' ) );
32-
_wp_can_use_pcre_u( 'reset' );
25+
public function test_mb_substr( $input_string, $start, $length ) {
26+
$this->assertSame(
27+
mb_substr( $input_string, $start, $length, 'UTF-8' ),
28+
_mb_substr( $input_string, $start, $length, 'UTF-8' )
29+
);
3330
}
3431

3532
/**
3633
* @dataProvider data_utf8_substrings
3734
*/
38-
public function test_8bit_mb_substr( $input_string, $start, $length, $expected_character_substring, $expected_byte_substring ) {
39-
$this->assertSame( $expected_byte_substring, _mb_substr( $input_string, $start, $length, '8bit' ) );
35+
public function test_8bit_mb_substr( $input_string, $start, $length ) {
36+
$this->assertSame(
37+
mb_substr( $input_string, $start, $length, '8bit' ),
38+
_mb_substr( $input_string, $start, $length, '8bit' )
39+
);
4040
}
4141

4242
/**
4343
* Data provider.
4444
*
45-
* @return array
45+
* @return array[]
4646
*/
4747
public function data_utf8_substrings() {
4848
return array(
49-
array(
50-
'input_string' => 'баба',
51-
'start' => 0,
52-
'length' => 3,
53-
'expected_character_substring' => 'баб',
54-
'expected_byte_substring' => "б\xD0",
55-
),
56-
array(
57-
'input_string' => 'баба',
58-
'start' => 0,
59-
'length' => -1,
60-
'expected_character_substring' => 'баб',
61-
'expected_byte_substring' => "баб\xD0",
62-
),
63-
array(
64-
'input_string' => 'баба',
65-
'start' => 1,
66-
'length' => null,
67-
'expected_character_substring' => 'аба',
68-
'expected_byte_substring' => "\xB1аба",
69-
),
70-
array(
71-
'input_string' => 'баба',
72-
'start' => -3,
73-
'length' => null,
74-
'expected_character_substring' => 'аба',
75-
'expected_byte_substring' => "\xB1а",
76-
),
77-
array(
78-
'input_string' => 'баба',
79-
'start' => -3,
80-
'length' => 2,
81-
'expected_character_substring' => 'аб',
82-
'expected_byte_substring' => "\xB1\xD0",
83-
),
84-
array(
85-
'input_string' => 'баба',
86-
'start' => -1,
87-
'length' => 2,
88-
'expected_character_substring' => 'а',
89-
'expected_byte_substring' => "\xB0",
90-
),
91-
array(
92-
'input_string' => 'I am your баба',
93-
'start' => 0,
94-
'length' => 11,
95-
'expected_character_substring' => 'I am your б',
96-
'expected_byte_substring' => "I am your \xD0",
97-
),
49+
'баба' => array( 'баба', 0, 3 ),
50+
'баба' => array( 'баба', 0, -1 ),
51+
'баба' => array( 'баба', 1, null ),
52+
'баба' => array( 'баба', -3, null ),
53+
'баба' => array( 'баба', -3, 2 ),
54+
'баба' => array( 'баба', -2, 1 ),
55+
'I am your баба' => array( 'I am your баба', 0, 11 ),
9856
);
9957
}
10058

0 commit comments

Comments
 (0)