forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmbSubstr.php
More file actions
204 lines (191 loc) · 5.36 KB
/
mbSubstr.php
File metadata and controls
204 lines (191 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @group compat
* @group security-153
*
* @covers ::mb_substr
* @covers ::_mb_substr
*/
class Tests_Compat_mbSubstr extends WP_UnitTestCase {
/**
* Test that mb_substr() is always available (either from PHP or WP).
*/
public function test_mb_substr_availability() {
$this->assertTrue(
in_array( 'mb_substr', get_defined_functions()['internal'], true ),
'Test runner should have `mbstring` extension active but doesn’t.'
);
}
/**
* @dataProvider data_utf8_substrings
*/
public function test_mb_substr( $input_string, $start, $length ) {
$this->assertSame(
mb_substr( $input_string, $start, $length, 'UTF-8' ),
_mb_substr( $input_string, $start, $length, 'UTF-8' )
);
}
/**
* @dataProvider data_utf8_substrings
*/
public function test_8bit_mb_substr( $input_string, $start, $length ) {
$this->assertSame(
mb_substr( $input_string, $start, $length, '8bit' ),
_mb_substr( $input_string, $start, $length, '8bit' )
);
}
/**
* Data provider.
*
* @return array[]
*/
public function data_utf8_substrings() {
return array(
'баба' => array( 'баба', 0, 3 ),
'баба' => array( 'баба', 0, -1 ),
'баба' => array( 'баба', 1, null ),
'баба' => array( 'баба', -3, null ),
'баба' => array( 'баба', -3, 2 ),
'баба' => array( 'баба', -2, 1 ),
'баба' => array( 'баба', 30, 1 ),
'баба' => array( 'баба', 15, -30 ),
'баба' => array( 'баба', -5, -5 ),
'баба' => array( 'баба', 5, -3 ),
'баба' => array( 'баба', -3, 5 ),
'I am your баба' => array( 'I am your баба', 0, 11 ),
);
}
/**
* @link https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_basic.phpt
*/
public function test_mb_substr_phpcore_basic() {
$string_ascii = 'ABCDEF';
$string_mb = '日本語テキストです。0123456789。';
$this->assertSame(
'DEF',
_mb_substr( $string_ascii, 3 ),
'Substring does not match expected for offset 3'
);
$this->assertSame(
'DEF',
_mb_substr( $string_ascii, 3, 5, 'ISO-8859-1' ),
'Substring does not match expected for offset 3, length 5, with iso charset'
);
// Specific latin-1 as that is the default the core PHP test operates under.
$this->assertSame(
"\xA5本語",
_mb_substr( $string_mb, 2, 7, 'latin-1' ),
'Substring does not match expected for offset 2, length 7, with latin-1 charset'
);
$this->assertSame(
'語テキストです',
_mb_substr( $string_mb, 2, 7, 'utf-8' ),
'Substring does not match expected for offset 2, length 7, with utf-8 charset'
);
}
/**
* @link https://github.com/php/php-src/blob/php-5.6.8/ext/mbstring/tests/mb_substr_variation1.phpt
*
* @dataProvider data_mb_substr_phpcore_input_type_handling
*
* @param mixed $input Input to pass to the function.
* @param string $expected Expected function output.
*/
public function test_mb_substr_phpcore_input_type_handling( $input, $expected ) {
$start = 0;
$length = 5;
$this->assertSame( $expected, _mb_substr( $input, $start, $length ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_mb_substr_phpcore_input_type_handling() {
$heredoc = <<<EOT
hello world
EOT;
return array(
'integer zero' => array(
'input' => 0,
'expected' => '0',
),
'integer 1' => array(
'input' => 1,
'expected' => '1',
),
'positive integer' => array(
'input' => 12345,
'expected' => '12345',
),
'negative integer' => array(
'input' => -2345,
'expected' => '-2345',
),
// Float data.
'positive float with fraction' => array(
'input' => 10.5,
'expected' => '10.5',
),
'negative float with fraction' => array(
'input' => -10.5,
'expected' => '-10.5',
),
'float scientific whole number' => array(
'input' => 12.3456789000e10,
'expected' => '12345',
),
'float scientific with fraction' => array(
'input' => 12.3456789000E-10,
'expected' => '1.234',
),
'float, fraction only' => array(
'input' => .5,
'expected' => '0.5',
),
// Null data.
'null' => array(
'input' => null,
'expected' => '',
),
// Boolean data.
'boolean true' => array(
'input' => true,
'expected' => '1',
),
'boolean false' => array(
'input' => false,
'expected' => '',
),
// Empty data.
'empty string' => array(
'input' => '',
'expected' => '',
),
// String data.
'double quoted string' => array(
'input' => "string'",
'expected' => 'strin',
),
'single quoted string' => array(
'input' => 'string',
'expected' => 'strin',
),
'heredoc string' => array(
'input' => $heredoc,
'expected' => 'hello',
),
// Object data.
'object with __toString method' => array(
'input' => new ClassWithToStringForMbSubstr(),
'expected' => 'Class',
),
);
}
}
/* used in data_mb_substr_phpcore_input_type_handling() */
class ClassWithToStringForMbSubstr {
public function __toString() {
return 'Class object';
}
}