|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Test WPDB methods |
|
5
|
|
|
* |
|
6
|
|
|
* @group wpdb |
|
7
|
|
|
* @group security-153 |
|
8
|
|
|
*/ |
|
9
|
|
|
class Tests_DB_Charset extends WP_UnitTestCase |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Our special WPDB |
|
14
|
|
|
* |
|
15
|
|
|
* @var resource |
|
16
|
|
|
*/ |
|
17
|
|
|
protected static $_wpdb; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The version of the MySQL server. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $server_info; |
|
25
|
|
|
|
|
26
|
|
|
public static function setUpBeforeClass() |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
|
|
include_once dirname(dirname(__FILE__)) . '/db.php'; |
|
29
|
|
|
|
|
30
|
|
|
self::$_wpdb = new wpdb_exposed_methods_for_testing(); |
|
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
if (self::$_wpdb->use_mysqli ) { |
|
33
|
|
|
self::$server_info = mysqli_get_server_info(self::$_wpdb->dbh); |
|
34
|
|
|
} else { |
|
35
|
|
|
self::$server_info = mysql_get_server_info(self::$_wpdb->dbh); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @ticket 21212 |
|
41
|
|
|
*/ |
|
42
|
|
|
function data_strip_invalid_text() |
|
43
|
|
|
{ |
|
44
|
|
|
$fields = array( |
|
45
|
|
|
'latin1' => array( |
|
46
|
|
|
// latin1. latin1 never changes. |
|
47
|
|
|
'charset' => 'latin1', |
|
48
|
|
|
'value' => "\xf0\x9f\x8e\xb7", |
|
49
|
|
|
'expected' => "\xf0\x9f\x8e\xb7", |
|
50
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
51
|
|
|
), |
|
52
|
|
|
'latin1_char_length' => array( |
|
53
|
|
|
// latin1. latin1 never changes. |
|
54
|
|
|
'charset' => 'latin1', |
|
55
|
|
|
'value' => str_repeat('A', 11), |
|
56
|
|
|
'expected' => str_repeat('A', 10), |
|
57
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
58
|
|
|
), |
|
59
|
|
|
'latin1_byte_length' => array( |
|
60
|
|
|
// latin1. latin1 never changes. |
|
61
|
|
|
'charset' => 'latin1', |
|
62
|
|
|
'value' => str_repeat('A', 11), |
|
63
|
|
|
'expected' => str_repeat('A', 10), |
|
64
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
65
|
|
|
), |
|
66
|
|
|
'ascii' => array( |
|
67
|
|
|
// ascii gets special treatment, make sure it's covered |
|
68
|
|
|
'charset' => 'ascii', |
|
69
|
|
|
'value' => 'Hello World', |
|
70
|
|
|
'expected' => 'Hello World', |
|
71
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
72
|
|
|
), |
|
73
|
|
|
'ascii_char_length' => array( |
|
74
|
|
|
// ascii gets special treatment, make sure it's covered |
|
75
|
|
|
'charset' => 'ascii', |
|
76
|
|
|
'value' => str_repeat('A', 11), |
|
77
|
|
|
'expected' => str_repeat('A', 10), |
|
78
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
79
|
|
|
), |
|
80
|
|
|
'ascii_byte_length' => array( |
|
81
|
|
|
// ascii gets special treatment, make sure it's covered |
|
82
|
|
|
'charset' => 'ascii', |
|
83
|
|
|
'value' => str_repeat('A', 11), |
|
84
|
|
|
'expected' => str_repeat('A', 10), |
|
85
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
86
|
|
|
), |
|
87
|
|
|
'utf8' => array( |
|
88
|
|
|
// utf8 only allows <= 3-byte chars |
|
89
|
|
|
'charset' => 'utf8', |
|
90
|
|
|
'value' => "H€llo\xf0\x9f\x98\x88World¢", |
|
91
|
|
|
'expected' => 'H€lloWorld¢', |
|
92
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
93
|
|
|
), |
|
94
|
|
|
'utf8_23char_length' => array( |
|
95
|
|
|
// utf8 only allows <= 3-byte chars |
|
96
|
|
|
'charset' => 'utf8', |
|
97
|
|
|
'value' => str_repeat("²3", 10), |
|
98
|
|
|
'expected' => str_repeat("²3", 5), |
|
99
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
100
|
|
|
), |
|
101
|
|
|
'utf8_23byte_length' => array( |
|
102
|
|
|
// utf8 only allows <= 3-byte chars |
|
103
|
|
|
'charset' => 'utf8', |
|
104
|
|
|
'value' => str_repeat("²3", 10), |
|
105
|
|
|
'expected' => "²3²3", |
|
106
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
107
|
|
|
), |
|
108
|
|
|
'utf8_3char_length' => array( |
|
109
|
|
|
// utf8 only allows <= 3-byte chars |
|
110
|
|
|
'charset' => 'utf8', |
|
111
|
|
|
'value' => str_repeat("3", 11), |
|
112
|
|
|
'expected' => str_repeat("3", 10), |
|
113
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
114
|
|
|
), |
|
115
|
|
|
'utf8_3byte_length' => array( |
|
116
|
|
|
// utf8 only allows <= 3-byte chars |
|
117
|
|
|
'charset' => 'utf8', |
|
118
|
|
|
'value' => str_repeat("3", 11), |
|
119
|
|
|
'expected' => "333", |
|
120
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
121
|
|
|
), |
|
122
|
|
|
'utf8mb3' => array( |
|
123
|
|
|
// utf8mb3 should behave the same an utf8 |
|
124
|
|
|
'charset' => 'utf8mb3', |
|
125
|
|
|
'value' => "H€llo\xf0\x9f\x98\x88World¢", |
|
126
|
|
|
'expected' => 'H€lloWorld¢', |
|
127
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
128
|
|
|
), |
|
129
|
|
|
'utf8mb3_23char_length' => array( |
|
130
|
|
|
// utf8mb3 should behave the same an utf8 |
|
131
|
|
|
'charset' => 'utf8mb3', |
|
132
|
|
|
'value' => str_repeat("²3", 10), |
|
133
|
|
|
'expected' => str_repeat("²3", 5), |
|
134
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
135
|
|
|
), |
|
136
|
|
|
'utf8mb3_23byte_length' => array( |
|
137
|
|
|
// utf8mb3 should behave the same an utf8 |
|
138
|
|
|
'charset' => 'utf8mb3', |
|
139
|
|
|
'value' => str_repeat("²3", 10), |
|
140
|
|
|
'expected' => "²3²3", |
|
141
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
142
|
|
|
), |
|
143
|
|
|
'utf8mb3_3char_length' => array( |
|
144
|
|
|
// utf8mb3 should behave the same an utf8 |
|
145
|
|
|
'charset' => 'utf8mb3', |
|
146
|
|
|
'value' => str_repeat("3", 11), |
|
147
|
|
|
'expected' => str_repeat("3", 10), |
|
148
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
149
|
|
|
), |
|
150
|
|
|
'utf8mb3_3byte_length' => array( |
|
151
|
|
|
// utf8mb3 should behave the same an utf8 |
|
152
|
|
|
'charset' => 'utf8mb3', |
|
153
|
|
|
'value' => str_repeat("3", 10), |
|
154
|
|
|
'expected' => "333", |
|
155
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
156
|
|
|
), |
|
157
|
|
|
'utf8mb4' => array( |
|
158
|
|
|
// utf8mb4 allows 4-byte characters, too |
|
159
|
|
|
'charset' => 'utf8mb4', |
|
160
|
|
|
'value' => "H€llo\xf0\x9f\x98\x88World¢", |
|
161
|
|
|
'expected' => "H€llo\xf0\x9f\x98\x88World¢", |
|
162
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
163
|
|
|
), |
|
164
|
|
|
'utf8mb4_234char_length' => array( |
|
165
|
|
|
// utf8mb4 allows 4-byte characters, too |
|
166
|
|
|
'charset' => 'utf8mb4', |
|
167
|
|
|
'value' => str_repeat("²3𝟜", 10), |
|
168
|
|
|
'expected' => "²3𝟜²3𝟜²3𝟜²", |
|
169
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
170
|
|
|
), |
|
171
|
|
|
'utf8mb4_234byte_length' => array( |
|
172
|
|
|
// utf8mb4 allows 4-byte characters, too |
|
173
|
|
|
'charset' => 'utf8mb4', |
|
174
|
|
|
'value' => str_repeat("²3𝟜", 10), |
|
175
|
|
|
'expected' => "²3𝟜", |
|
176
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
177
|
|
|
), |
|
178
|
|
|
'utf8mb4_4char_length' => array( |
|
179
|
|
|
// utf8mb4 allows 4-byte characters, too |
|
180
|
|
|
'charset' => 'utf8mb4', |
|
181
|
|
|
'value' => str_repeat("𝟜", 11), |
|
182
|
|
|
'expected' => str_repeat("𝟜", 10), |
|
183
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
184
|
|
|
), |
|
185
|
|
|
'utf8mb4_4byte_length' => array( |
|
186
|
|
|
// utf8mb4 allows 4-byte characters, too |
|
187
|
|
|
'charset' => 'utf8mb4', |
|
188
|
|
|
'value' => str_repeat("𝟜", 10), |
|
189
|
|
|
'expected' => "𝟜𝟜", |
|
190
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
191
|
|
|
), |
|
192
|
|
|
'koi8r' => array( |
|
193
|
|
|
'charset' => 'koi8r', |
|
194
|
|
|
'value' => "\xfdord\xf2ress", |
|
195
|
|
|
'expected' => "\xfdord\xf2ress", |
|
196
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
197
|
|
|
), |
|
198
|
|
|
'koi8r_char_length' => array( |
|
199
|
|
|
'charset' => 'koi8r', |
|
200
|
|
|
'value' => str_repeat("\xfd\xf2", 10), |
|
201
|
|
|
'expected' => str_repeat("\xfd\xf2", 5), |
|
202
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
203
|
|
|
), |
|
204
|
|
|
'koi8r_byte_length' => array( |
|
205
|
|
|
'charset' => 'koi8r', |
|
206
|
|
|
'value' => str_repeat("\xfd\xf2", 10), |
|
207
|
|
|
'expected' => str_repeat("\xfd\xf2", 5), |
|
208
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
209
|
|
|
), |
|
210
|
|
|
'hebrew' => array( |
|
211
|
|
|
'charset' => 'hebrew', |
|
212
|
|
|
'value' => "\xf9ord\xf7ress", |
|
213
|
|
|
'expected' => "\xf9ord\xf7ress", |
|
214
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
215
|
|
|
), |
|
216
|
|
|
'hebrew_char_length' => array( |
|
217
|
|
|
'charset' => 'hebrew', |
|
218
|
|
|
'value' => str_repeat("\xf9\xf7", 10), |
|
219
|
|
|
'expected' => str_repeat("\xf9\xf7", 5), |
|
220
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
221
|
|
|
), |
|
222
|
|
|
'hebrew_byte_length' => array( |
|
223
|
|
|
'charset' => 'hebrew', |
|
224
|
|
|
'value' => str_repeat("\xf9\xf7", 10), |
|
225
|
|
|
'expected' => str_repeat("\xf9\xf7", 5), |
|
226
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
227
|
|
|
), |
|
228
|
|
|
'cp1251' => array( |
|
229
|
|
|
'charset' => 'cp1251', |
|
230
|
|
|
'value' => "\xd8ord\xd0ress", |
|
231
|
|
|
'expected' => "\xd8ord\xd0ress", |
|
232
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
233
|
|
|
), |
|
234
|
|
|
'cp1251_no_length' => array( |
|
235
|
|
|
'charset' => 'cp1251', |
|
236
|
|
|
'value' => "\xd8ord\xd0ress", |
|
237
|
|
|
'expected' => "\xd8ord\xd0ress", |
|
238
|
|
|
'length' => false, |
|
239
|
|
|
), |
|
240
|
|
|
'cp1251_no_length_ascii' => array( |
|
241
|
|
|
'charset' => 'cp1251', |
|
242
|
|
|
'value' => "WordPress", |
|
243
|
|
|
'expected' => "WordPress", |
|
244
|
|
|
'length' => false, |
|
245
|
|
|
// Don't set 'ascii' => true/false. |
|
246
|
|
|
// That's a different codepath than it being unset even if |
|
247
|
|
|
// three's only only ASCII in the value. |
|
248
|
|
|
), |
|
249
|
|
|
'cp1251_char_length' => array( |
|
250
|
|
|
'charset' => 'cp1251', |
|
251
|
|
|
'value' => str_repeat("\xd8\xd0", 10), |
|
252
|
|
|
'expected' => str_repeat("\xd8\xd0", 5), |
|
253
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
254
|
|
|
), |
|
255
|
|
|
'cp1251_byte_length' => array( |
|
256
|
|
|
'charset' => 'cp1251', |
|
257
|
|
|
'value' => str_repeat("\xd8\xd0", 10), |
|
258
|
|
|
'expected' => str_repeat("\xd8\xd0", 5), |
|
259
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
260
|
|
|
), |
|
261
|
|
|
'tis620' => array( |
|
262
|
|
|
'charset' => 'tis620', |
|
263
|
|
|
'value' => "\xccord\xe3ress", |
|
264
|
|
|
'expected' => "\xccord\xe3ress", |
|
265
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
266
|
|
|
), |
|
267
|
|
|
'tis620_char_length' => array( |
|
268
|
|
|
'charset' => 'tis620', |
|
269
|
|
|
'value' => str_repeat("\xcc\xe3", 10), |
|
270
|
|
|
'expected' => str_repeat("\xcc\xe3", 5), |
|
271
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
272
|
|
|
), |
|
273
|
|
|
'tis620_byte_length' => array( |
|
274
|
|
|
'charset' => 'tis620', |
|
275
|
|
|
'value' => str_repeat("\xcc\xe3", 10), |
|
276
|
|
|
'expected' => str_repeat("\xcc\xe3", 5), |
|
277
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
278
|
|
|
), |
|
279
|
|
|
'ujis_with_utf8_connection' => array( |
|
280
|
|
|
'charset' => 'ujis', |
|
281
|
|
|
'connection_charset' => 'utf8', |
|
282
|
|
|
'value' => '自動下書き', |
|
283
|
|
|
'expected' => '自動下書き', |
|
284
|
|
|
'length' => array( 'type' => 'byte', 'length' => 100 ), |
|
285
|
|
|
), |
|
286
|
|
|
'ujis_with_utf8_connection_char_length' => array( |
|
287
|
|
|
'charset' => 'ujis', |
|
288
|
|
|
'connection_charset' => 'utf8', |
|
289
|
|
|
'value' => '自動下書き', |
|
290
|
|
|
'expected' => '自動下書', |
|
291
|
|
|
'length' => array( 'type' => 'char', 'length' => 4 ), |
|
292
|
|
|
), |
|
293
|
|
|
'ujis_with_utf8_connection_byte_length' => array( |
|
294
|
|
|
'charset' => 'ujis', |
|
295
|
|
|
'connection_charset' => 'utf8', |
|
296
|
|
|
'value' => '自動下書き', |
|
297
|
|
|
'expected' => '自動', |
|
298
|
|
|
'length' => array( 'type' => 'byte', 'length' => 6 ), |
|
299
|
|
|
), |
|
300
|
|
|
'false' => array( |
|
301
|
|
|
// false is a column with no character set (ie, a number column) |
|
302
|
|
|
'charset' => false, |
|
303
|
|
|
'value' => 100, |
|
304
|
|
|
'expected' => 100, |
|
305
|
|
|
'length' => false, |
|
306
|
|
|
), |
|
307
|
|
|
); |
|
308
|
|
|
|
|
309
|
|
|
if (function_exists('mb_convert_encoding') ) { |
|
310
|
|
|
// big5 is a non-Unicode multibyte charset |
|
311
|
|
|
$utf8 = "a\xe5\x85\xb1b"; // UTF-8 Character 20849 |
|
312
|
|
|
$big5 = mb_convert_encoding($utf8, 'BIG-5', 'UTF-8'); |
|
313
|
|
|
$conv_utf8 = mb_convert_encoding($big5, 'UTF-8', 'BIG-5'); |
|
314
|
|
|
// Make sure PHP's multibyte conversions are working correctly |
|
315
|
|
|
$this->assertNotEquals($utf8, $big5); |
|
316
|
|
|
$this->assertEquals($utf8, $conv_utf8); |
|
317
|
|
|
|
|
318
|
|
|
$fields['big5'] = array( |
|
319
|
|
|
'charset' => 'big5', |
|
320
|
|
|
'value' => $big5, |
|
321
|
|
|
'expected' => $big5, |
|
322
|
|
|
'length' => array( 'type' => 'char', 'length' => 100 ), |
|
323
|
|
|
); |
|
324
|
|
|
|
|
325
|
|
|
$fields['big5_char_length'] = array( |
|
326
|
|
|
'charset' => 'big5', |
|
327
|
|
|
'value' => str_repeat($big5, 10), |
|
328
|
|
|
'expected' => str_repeat($big5, 3) . 'a', |
|
329
|
|
|
'length' => array( 'type' => 'char', 'length' => 10 ), |
|
330
|
|
|
); |
|
331
|
|
|
|
|
332
|
|
|
$fields['big5_byte_length'] = array( |
|
333
|
|
|
'charset' => 'big5', |
|
334
|
|
|
'value' => str_repeat($big5, 10), |
|
335
|
|
|
'expected' => str_repeat($big5, 2) . 'a', |
|
336
|
|
|
'length' => array( 'type' => 'byte', 'length' => 10 ), |
|
337
|
|
|
); |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
// The data above is easy to edit. Now, prepare it for the data provider. |
|
341
|
|
|
$data_provider = $multiple = $multiple_expected = array(); |
|
342
|
|
|
foreach ( $fields as $test_case => $field ) { |
|
343
|
|
|
$expected = $field; |
|
344
|
|
|
$expected['value'] = $expected['expected']; |
|
345
|
|
|
unset($expected['expected'], $field['expected'], $expected['connection_charset']); |
|
346
|
|
|
|
|
347
|
|
|
// We're keeping track of these for our multiple-field test. |
|
348
|
|
|
$multiple[] = $field; |
|
349
|
|
|
$multiple_expected[] = $expected; |
|
350
|
|
|
|
|
351
|
|
|
// strip_invalid_text() expects an array of fields. We're testing one field at a time. |
|
352
|
|
|
$data = array( $field ); |
|
353
|
|
|
$expected = array( $expected ); |
|
354
|
|
|
|
|
355
|
|
|
// First argument is field data. Second is expected. Third is the message. |
|
356
|
|
|
$data_provider[] = array( $data, $expected, $test_case ); |
|
357
|
|
|
} |
|
358
|
|
|
|
|
359
|
|
|
return $data_provider; |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
/** |
|
363
|
|
|
* @dataProvider data_strip_invalid_text |
|
364
|
|
|
* @ticket 21212 |
|
365
|
|
|
*/ |
|
366
|
|
|
function test_strip_invalid_text( $data, $expected, $message ) |
|
367
|
|
|
{ |
|
368
|
|
|
if (version_compare(PHP_VERSION, '5.3', '<') && stristr(php_uname('s'), 'win') ) { |
|
369
|
|
|
$this->markTestSkipped('This test fails in PHP 5.2 on Windows. See https://core.trac.wordpress.org/ticket/31262'); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
$charset = self::$_wpdb->charset; |
|
373
|
|
|
if (isset($data[0]['connection_charset']) ) { |
|
374
|
|
|
$new_charset = $data[0]['connection_charset']; |
|
375
|
|
|
unset($data[0]['connection_charset']); |
|
376
|
|
|
} else { |
|
377
|
|
|
$new_charset = $data[0]['charset']; |
|
378
|
|
|
} |
|
379
|
|
|
|
|
380
|
|
|
if ('utf8mb4' === $new_charset && ! self::$_wpdb->has_cap('utf8mb4') ) { |
|
|
|
|
|
|
381
|
|
|
$this->markTestSkipped("The current MySQL server doesn't support the utf8mb4 character set."); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
if ('big5' === $new_charset && 'byte' === $data[0]['length']['type'] && false !== strpos(self::$server_info, 'MariaDB') ) { |
|
385
|
|
|
$this->markTestSkipped("MariaDB doesn't support this data set. See https://core.trac.wordpress.org/ticket/33171."); |
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
self::$_wpdb->charset = $new_charset; |
|
389
|
|
|
self::$_wpdb->set_charset(self::$_wpdb->dbh, $new_charset); |
|
|
|
|
|
|
390
|
|
|
|
|
391
|
|
|
$actual = self::$_wpdb->strip_invalid_text($data); |
|
|
|
|
|
|
392
|
|
|
|
|
393
|
|
|
self::$_wpdb->charset = $charset; |
|
394
|
|
|
self::$_wpdb->set_charset(self::$_wpdb->dbh, $charset); |
|
|
|
|
|
|
395
|
|
|
|
|
396
|
|
|
$this->assertSame($expected, $actual, $message); |
|
397
|
|
|
} |
|
398
|
|
|
|
|
399
|
|
|
/** |
|
400
|
|
|
* @ticket 21212 |
|
401
|
|
|
*/ |
|
402
|
|
|
function test_process_fields_failure() |
|
403
|
|
|
{ |
|
404
|
|
|
global $wpdb; |
|
405
|
|
|
|
|
406
|
|
|
$charset = $wpdb->get_col_charset($wpdb->posts, 'post_content'); |
|
407
|
|
|
if ('utf8' !== $charset && 'utf8mb4' !== $charset ) { |
|
408
|
|
|
$this->markTestSkipped('This test requires a utf8 character set'); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
// \xf0\xff\xff\xff is invalid in utf8 and utf8mb4. |
|
412
|
|
|
$data = array( 'post_content' => "H€llo\xf0\xff\xff\xffWorld¢" ); |
|
413
|
|
|
$this->assertFalse(self::$_wpdb->process_fields($wpdb->posts, $data, null)); |
|
|
|
|
|
|
414
|
|
|
} |
|
415
|
|
|
|
|
416
|
|
|
/** |
|
417
|
|
|
* @ticket 21212 |
|
418
|
|
|
*/ |
|
419
|
|
|
function data_process_field_charsets() |
|
420
|
|
|
{ |
|
421
|
|
|
if ($GLOBALS['wpdb']->charset ) { |
|
422
|
|
|
$charset = $GLOBALS['wpdb']->charset; |
|
423
|
|
|
} else { |
|
424
|
|
|
$charset = $GLOBALS['wpdb']->get_col_charset($GLOBALS['wpdb']->posts, 'post_content'); |
|
425
|
|
|
} |
|
426
|
|
|
|
|
427
|
|
|
// 'value' and 'format' are $data, 'charset' ends up as part of $expected |
|
428
|
|
|
|
|
429
|
|
|
$no_string_fields = array( |
|
430
|
|
|
'post_parent' => array( 'value' => 10, 'format' => '%d', 'charset' => false ), |
|
431
|
|
|
'comment_count' => array( 'value' => 0, 'format' => '%d', 'charset' => false ), |
|
432
|
|
|
); |
|
433
|
|
|
|
|
434
|
|
|
$all_ascii_fields = array( |
|
435
|
|
|
'post_content' => array( 'value' => 'foo foo foo!', 'format' => '%s', 'charset' => $charset ), |
|
436
|
|
|
'post_excerpt' => array( 'value' => 'bar bar bar!', 'format' => '%s', 'charset' => $charset ), |
|
437
|
|
|
); |
|
438
|
|
|
|
|
439
|
|
|
// This is the same data used in process_field_charsets_for_nonexistent_table() |
|
440
|
|
|
$non_ascii_string_fields = array( |
|
441
|
|
|
'post_content' => array( 'value' => '¡foo foo foo!', 'format' => '%s', 'charset' => $charset ), |
|
442
|
|
|
'post_excerpt' => array( 'value' => '¡bar bar bar!', 'format' => '%s', 'charset' => $charset ), |
|
443
|
|
|
); |
|
444
|
|
|
|
|
445
|
|
|
$vars = get_defined_vars(); |
|
446
|
|
|
unset($vars['charset']); |
|
447
|
|
|
foreach ( $vars as $var_name => $var ) { |
|
448
|
|
|
$data = $expected = $var; |
|
449
|
|
|
foreach ( $data as &$datum ) { |
|
450
|
|
|
// 'charset' and 'ascii' are part of the expected return only. |
|
451
|
|
|
unset($datum['charset'], $datum['ascii']); |
|
452
|
|
|
} |
|
453
|
|
|
|
|
454
|
|
|
$vars[ $var_name ] = array( $data, $expected, $var_name ); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
return array_values($vars); |
|
458
|
|
|
} |
|
459
|
|
|
|
|
460
|
|
|
/** |
|
461
|
|
|
* @dataProvider data_process_field_charsets |
|
462
|
|
|
* @ticket 21212 |
|
463
|
|
|
*/ |
|
464
|
|
|
function test_process_field_charsets( $data, $expected, $message ) |
|
465
|
|
|
{ |
|
466
|
|
|
$actual = self::$_wpdb->process_field_charsets($data, $GLOBALS['wpdb']->posts); |
|
|
|
|
|
|
467
|
|
|
$this->assertSame($expected, $actual, $message); |
|
468
|
|
|
} |
|
469
|
|
|
|
|
470
|
|
|
/** |
|
471
|
|
|
* The test this test depends on first verifies that this |
|
472
|
|
|
* would normally work against the posts table. |
|
473
|
|
|
* |
|
474
|
|
|
* @ticket 21212 |
|
475
|
|
|
* @depends test_process_field_charsets |
|
476
|
|
|
*/ |
|
477
|
|
|
function test_process_field_charsets_on_nonexistent_table() |
|
478
|
|
|
{ |
|
479
|
|
|
$data = array( 'post_content' => array( 'value' => '¡foo foo foo!', 'format' => '%s' ) ); |
|
480
|
|
|
self::$_wpdb->suppress_errors(true); |
|
|
|
|
|
|
481
|
|
|
$this->assertFalse(self::$_wpdb->process_field_charsets($data, 'nonexistent_table')); |
|
|
|
|
|
|
482
|
|
|
self::$_wpdb->suppress_errors(false); |
|
|
|
|
|
|
483
|
|
|
} |
|
484
|
|
|
|
|
485
|
|
|
/** |
|
486
|
|
|
* @ticket 21212 |
|
487
|
|
|
*/ |
|
488
|
|
|
function test_check_ascii() |
|
489
|
|
|
{ |
|
490
|
|
|
$ascii = "\0\t\n\r '" . '!"#$%&()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~'; |
|
491
|
|
|
$this->assertTrue(self::$_wpdb->check_ascii($ascii)); |
|
|
|
|
|
|
492
|
|
|
} |
|
493
|
|
|
|
|
494
|
|
|
/** |
|
495
|
|
|
* @ticket 21212 |
|
496
|
|
|
*/ |
|
497
|
|
|
function test_check_ascii_false() |
|
498
|
|
|
{ |
|
499
|
|
|
$this->assertFalse(self::$_wpdb->check_ascii('ABCDEFGHIJKLMNOPQRSTUVWXYZ¡©«')); |
|
|
|
|
|
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
/** |
|
503
|
|
|
* @ticket 21212 |
|
504
|
|
|
*/ |
|
505
|
|
|
function test_strip_invalid_text_for_column() |
|
506
|
|
|
{ |
|
507
|
|
|
global $wpdb; |
|
508
|
|
|
|
|
509
|
|
|
$charset = $wpdb->get_col_charset($wpdb->posts, 'post_content'); |
|
510
|
|
|
if ('utf8' !== $charset && 'utf8mb4' !== $charset ) { |
|
511
|
|
|
$this->markTestSkipped('This test requires a utf8 character set'); |
|
512
|
|
|
} |
|
513
|
|
|
|
|
514
|
|
|
// Invalid 3-byte and 4-byte sequences |
|
515
|
|
|
$value = "H€llo\xe0\x80\x80World\xf0\xff\xff\xff¢"; |
|
516
|
|
|
$expected = "H€lloWorld¢"; |
|
517
|
|
|
$actual = $wpdb->strip_invalid_text_for_column($wpdb->posts, 'post_content', $value); |
|
518
|
|
|
$this->assertEquals($expected, $actual); |
|
519
|
|
|
} |
|
520
|
|
|
|
|
521
|
|
|
/** |
|
522
|
|
|
* Set of table definitions for testing wpdb::get_table_charset and wpdb::get_column_charset |
|
523
|
|
|
* |
|
524
|
|
|
* @var array |
|
525
|
|
|
*/ |
|
526
|
|
|
protected $table_and_column_defs = array( |
|
527
|
|
|
array( |
|
528
|
|
|
'definition' => '( a INT, b FLOAT )', |
|
529
|
|
|
'table_expected' => false, |
|
530
|
|
|
'column_expected' => array( 'a' => false, 'b' => false ) |
|
531
|
|
|
), |
|
532
|
|
|
array( |
|
533
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET big5, b TEXT CHARACTER SET big5 )', |
|
534
|
|
|
'table_expected' => 'big5', |
|
535
|
|
|
'column_expected' => array( 'a' => 'big5', 'b' => 'big5' ) |
|
536
|
|
|
), |
|
537
|
|
|
array( |
|
538
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET big5, b BINARY )', |
|
539
|
|
|
'table_expected' => 'binary', |
|
540
|
|
|
'column_expected' => array( 'a' => 'big5', 'b' => false ) |
|
541
|
|
|
), |
|
542
|
|
|
array( |
|
543
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET latin1, b BLOB )', |
|
544
|
|
|
'table_expected' => 'binary', |
|
545
|
|
|
'column_expected' => array( 'a' => 'latin1', 'b' => false ) |
|
546
|
|
|
), |
|
547
|
|
|
array( |
|
548
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET latin1, b TEXT CHARACTER SET koi8r )', |
|
549
|
|
|
'table_expected' => 'koi8r', |
|
550
|
|
|
'column_expected' => array( 'a' => 'latin1', 'b' => 'koi8r' ) |
|
551
|
|
|
), |
|
552
|
|
|
array( |
|
553
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET utf8mb3, b TEXT CHARACTER SET utf8mb3 )', |
|
554
|
|
|
'table_expected' => 'utf8', |
|
555
|
|
|
'column_expected' => array( 'a' => 'utf8', 'b' => 'utf8' ) |
|
556
|
|
|
), |
|
557
|
|
|
array( |
|
558
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET utf8, b TEXT CHARACTER SET utf8mb4 )', |
|
559
|
|
|
'table_expected' => 'utf8', |
|
560
|
|
|
'column_expected' => array( 'a' => 'utf8', 'b' => 'utf8mb4' ) |
|
561
|
|
|
), |
|
562
|
|
|
array( |
|
563
|
|
|
'definition' => '( a VARCHAR(50) CHARACTER SET big5, b TEXT CHARACTER SET koi8r )', |
|
564
|
|
|
'table_expected' => 'ascii', |
|
565
|
|
|
'column_expected' => array( 'a' => 'big5', 'b' => 'koi8r' ) |
|
566
|
|
|
), |
|
567
|
|
|
); |
|
568
|
|
|
|
|
569
|
|
|
/** |
|
570
|
|
|
* @ticket 21212 |
|
571
|
|
|
*/ |
|
572
|
|
View Code Duplication |
function data_test_get_table_charset() |
|
573
|
|
|
{ |
|
574
|
|
|
$table_name = 'test_get_table_charset'; |
|
575
|
|
|
|
|
576
|
|
|
$vars = array(); |
|
577
|
|
|
foreach( $this->table_and_column_defs as $i => $value ) { |
|
|
|
|
|
|
578
|
|
|
$this_table_name = $table_name . '_' . $i; |
|
579
|
|
|
$drop = "DROP TABLE IF EXISTS $this_table_name"; |
|
580
|
|
|
$create = "CREATE TABLE $this_table_name {$value['definition']}"; |
|
581
|
|
|
$vars[] = array( $drop, $create, $this_table_name, $value['table_expected'] ); |
|
582
|
|
|
} |
|
583
|
|
|
|
|
584
|
|
|
return $vars; |
|
585
|
|
|
} |
|
586
|
|
|
|
|
587
|
|
|
/** |
|
588
|
|
|
* @dataProvider data_test_get_table_charset |
|
589
|
|
|
* @ticket 21212 |
|
590
|
|
|
*/ |
|
591
|
|
View Code Duplication |
function test_get_table_charset( $drop, $create, $table, $expected_charset ) |
|
592
|
|
|
{ |
|
593
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
594
|
|
|
|
|
595
|
|
|
if (! self::$_wpdb->has_cap('utf8mb4') && preg_match('/utf8mb[34]/i', $create) ) { |
|
|
|
|
|
|
596
|
|
|
$this->markTestSkipped("This version of MySQL doesn't support utf8mb4."); |
|
597
|
|
|
return; |
|
598
|
|
|
} |
|
599
|
|
|
|
|
600
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
601
|
|
|
|
|
602
|
|
|
$charset = self::$_wpdb->get_table_charset($table); |
|
|
|
|
|
|
603
|
|
|
$this->assertEquals($charset, $expected_charset); |
|
604
|
|
|
|
|
605
|
|
|
$charset = self::$_wpdb->get_table_charset(strtoupper($table)); |
|
|
|
|
|
|
606
|
|
|
$this->assertEquals($charset, $expected_charset); |
|
607
|
|
|
|
|
608
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
609
|
|
|
} |
|
610
|
|
|
|
|
611
|
|
|
/** |
|
612
|
|
|
* @ticket 21212 |
|
613
|
|
|
*/ |
|
614
|
|
View Code Duplication |
function data_test_get_column_charset() |
|
615
|
|
|
{ |
|
616
|
|
|
$table_name = 'test_get_column_charset'; |
|
617
|
|
|
|
|
618
|
|
|
$vars = array(); |
|
619
|
|
|
foreach( $this->table_and_column_defs as $i => $value ) { |
|
|
|
|
|
|
620
|
|
|
$this_table_name = $table_name . '_' . $i; |
|
621
|
|
|
$drop = "DROP TABLE IF EXISTS $this_table_name"; |
|
622
|
|
|
$create = "CREATE TABLE $this_table_name {$value['definition']}"; |
|
623
|
|
|
$vars[] = array( $drop, $create, $this_table_name, $value['column_expected'] ); |
|
624
|
|
|
} |
|
625
|
|
|
|
|
626
|
|
|
return $vars; |
|
627
|
|
|
} |
|
628
|
|
|
|
|
629
|
|
|
/** |
|
630
|
|
|
* @dataProvider data_test_get_column_charset |
|
631
|
|
|
* @ticket 21212 |
|
632
|
|
|
*/ |
|
633
|
|
View Code Duplication |
function test_get_column_charset( $drop, $create, $table, $expected_charset ) |
|
634
|
|
|
{ |
|
635
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
636
|
|
|
|
|
637
|
|
|
if (! self::$_wpdb->has_cap('utf8mb4') && preg_match('/utf8mb[34]/i', $create) ) { |
|
|
|
|
|
|
638
|
|
|
$this->markTestSkipped("This version of MySQL doesn't support utf8mb4."); |
|
639
|
|
|
return; |
|
640
|
|
|
} |
|
641
|
|
|
|
|
642
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
643
|
|
|
|
|
644
|
|
|
foreach ( $expected_charset as $column => $charset ) { |
|
645
|
|
|
$this->assertEquals($charset, self::$_wpdb->get_col_charset($table, $column)); |
|
|
|
|
|
|
646
|
|
|
$this->assertEquals($charset, self::$_wpdb->get_col_charset(strtoupper($table), strtoupper($column))); |
|
|
|
|
|
|
647
|
|
|
} |
|
648
|
|
|
|
|
649
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
650
|
|
|
} |
|
651
|
|
|
|
|
652
|
|
|
/** |
|
653
|
|
|
* @dataProvider data_test_get_column_charset |
|
654
|
|
|
* @ticket 21212 |
|
655
|
|
|
*/ |
|
656
|
|
View Code Duplication |
function test_get_column_charset_non_mysql( $drop, $create, $table, $columns ) |
|
657
|
|
|
{ |
|
658
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
659
|
|
|
|
|
660
|
|
|
if (! self::$_wpdb->has_cap('utf8mb4') && preg_match('/utf8mb[34]/i', $create) ) { |
|
|
|
|
|
|
661
|
|
|
$this->markTestSkipped("This version of MySQL doesn't support utf8mb4."); |
|
662
|
|
|
return; |
|
663
|
|
|
} |
|
664
|
|
|
|
|
665
|
|
|
self::$_wpdb->is_mysql = false; |
|
666
|
|
|
|
|
667
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
668
|
|
|
|
|
669
|
|
|
$columns = array_keys($columns); |
|
670
|
|
|
foreach ( $columns as $column => $charset ) { |
|
671
|
|
|
$this->assertEquals(false, self::$_wpdb->get_col_charset($table, $column)); |
|
|
|
|
|
|
672
|
|
|
} |
|
673
|
|
|
|
|
674
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
675
|
|
|
|
|
676
|
|
|
self::$_wpdb->is_mysql = true; |
|
677
|
|
|
} |
|
678
|
|
|
|
|
679
|
|
|
/** |
|
680
|
|
|
* @dataProvider data_test_get_column_charset |
|
681
|
|
|
* @ticket 33501 |
|
682
|
|
|
*/ |
|
683
|
|
View Code Duplication |
function test_get_column_charset_is_mysql_undefined( $drop, $create, $table, $columns ) |
|
684
|
|
|
{ |
|
685
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
686
|
|
|
|
|
687
|
|
|
if (! self::$_wpdb->has_cap('utf8mb4') && preg_match('/utf8mb[34]/i', $create) ) { |
|
|
|
|
|
|
688
|
|
|
$this->markTestSkipped("This version of MySQL doesn't support utf8mb4."); |
|
689
|
|
|
return; |
|
690
|
|
|
} |
|
691
|
|
|
|
|
692
|
|
|
unset(self::$_wpdb->is_mysql); |
|
693
|
|
|
|
|
694
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
695
|
|
|
|
|
696
|
|
|
$columns = array_keys($columns); |
|
697
|
|
|
foreach ( $columns as $column => $charset ) { |
|
698
|
|
|
$this->assertEquals(false, self::$_wpdb->get_col_charset($table, $column)); |
|
|
|
|
|
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
702
|
|
|
|
|
703
|
|
|
self::$_wpdb->is_mysql = true; |
|
704
|
|
|
} |
|
705
|
|
|
|
|
706
|
|
|
/** |
|
707
|
|
|
* @ticket 21212 |
|
708
|
|
|
*/ |
|
709
|
|
|
function data_strip_invalid_text_from_query() |
|
710
|
|
|
{ |
|
711
|
|
|
$table_name = 'strip_invalid_text_from_query_table'; |
|
712
|
|
|
$data = array( |
|
713
|
|
|
array( |
|
714
|
|
|
// binary tables don't get stripped |
|
715
|
|
|
"( a VARCHAR(50) CHARACTER SET utf8, b BINARY )", // create |
|
716
|
|
|
"('foo\xf0\x9f\x98\x88bar', 'foo')", // query |
|
717
|
|
|
"('foo\xf0\x9f\x98\x88bar', 'foo')" // expected result |
|
718
|
|
|
), |
|
719
|
|
|
array( |
|
720
|
|
|
// utf8/utf8mb4 tables default to utf8 |
|
721
|
|
|
"( a VARCHAR(50) CHARACTER SET utf8, b VARCHAR(50) CHARACTER SET utf8mb4 )", |
|
722
|
|
|
"('foo\xf0\x9f\x98\x88bar', 'foo')", |
|
723
|
|
|
"('foobar', 'foo')" |
|
724
|
|
|
), |
|
725
|
|
|
); |
|
726
|
|
|
|
|
727
|
|
|
foreach( $data as $i => &$value ) { |
|
|
|
|
|
|
728
|
|
|
$this_table_name = $table_name . '_' . $i; |
|
729
|
|
|
|
|
730
|
|
|
$value[0] = "CREATE TABLE $this_table_name {$value[0]}"; |
|
731
|
|
|
$value[1] = "INSERT INTO $this_table_name VALUES {$value[1]}"; |
|
732
|
|
|
$value[2] = "INSERT INTO $this_table_name VALUES {$value[2]}"; |
|
733
|
|
|
$value[3] = "DROP TABLE IF EXISTS $this_table_name"; |
|
734
|
|
|
} |
|
735
|
|
|
unset($value); |
|
736
|
|
|
|
|
737
|
|
|
return $data; |
|
738
|
|
|
} |
|
739
|
|
|
|
|
740
|
|
|
/** |
|
741
|
|
|
* @dataProvider data_strip_invalid_text_from_query |
|
742
|
|
|
* @ticket 21212 |
|
743
|
|
|
*/ |
|
744
|
|
|
function test_strip_invalid_text_from_query( $create, $query, $expected, $drop ) |
|
745
|
|
|
{ |
|
746
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
747
|
|
|
|
|
748
|
|
|
if (! self::$_wpdb->has_cap('utf8mb4') && preg_match('/utf8mb[34]/i', $create) ) { |
|
|
|
|
|
|
749
|
|
|
$this->markTestSkipped("This version of MySQL doesn't support utf8mb4."); |
|
750
|
|
|
return; |
|
751
|
|
|
} |
|
752
|
|
|
|
|
753
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
754
|
|
|
|
|
755
|
|
|
$return = self::$_wpdb->strip_invalid_text_from_query($query); |
|
|
|
|
|
|
756
|
|
|
$this->assertEquals($expected, $return); |
|
757
|
|
|
|
|
758
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
759
|
|
|
} |
|
760
|
|
|
|
|
761
|
|
|
/** |
|
762
|
|
|
* @ticket 32104 |
|
763
|
|
|
*/ |
|
764
|
|
|
function data_dont_strip_text_from_schema_queries() |
|
765
|
|
|
{ |
|
766
|
|
|
// An obviously invalid and fake table name. |
|
767
|
|
|
$table_name = "\xff\xff\xff\xff"; |
|
768
|
|
|
|
|
769
|
|
|
$queries = array( |
|
770
|
|
|
"SHOW CREATE TABLE $table_name", |
|
771
|
|
|
"DESCRIBE $table_name", |
|
772
|
|
|
"DESC $table_name", |
|
773
|
|
|
"EXPLAIN SELECT * FROM $table_name", |
|
774
|
|
|
"CREATE $table_name( a VARCHAR(100))", |
|
775
|
|
|
); |
|
776
|
|
|
|
|
777
|
|
|
foreach ( $queries as &$query ) { |
|
778
|
|
|
$query = array( $query ); |
|
779
|
|
|
} |
|
780
|
|
|
unset($query); |
|
781
|
|
|
|
|
782
|
|
|
return $queries; |
|
783
|
|
|
} |
|
784
|
|
|
|
|
785
|
|
|
/** |
|
786
|
|
|
* @dataProvider data_dont_strip_text_from_schema_queries |
|
787
|
|
|
* @ticket 32104 |
|
788
|
|
|
*/ |
|
789
|
|
|
function test_dont_strip_text_from_schema_queries( $query ) |
|
790
|
|
|
{ |
|
791
|
|
|
$return = self::$_wpdb->strip_invalid_text_from_query($query); |
|
|
|
|
|
|
792
|
|
|
$this->assertEquals($query, $return); |
|
793
|
|
|
} |
|
794
|
|
|
|
|
795
|
|
|
/** |
|
796
|
|
|
* @ticket 21212 |
|
797
|
|
|
*/ |
|
798
|
|
|
function test_invalid_characters_in_query() |
|
799
|
|
|
{ |
|
800
|
|
|
global $wpdb; |
|
801
|
|
|
|
|
802
|
|
|
$charset = $wpdb->get_col_charset($wpdb->posts, 'post_content'); |
|
803
|
|
|
if ('utf8' !== $charset && 'utf8mb4' !== $charset ) { |
|
804
|
|
|
$this->markTestSkipped('This test requires a utf8 character set'); |
|
805
|
|
|
} |
|
806
|
|
|
|
|
807
|
|
|
$this->assertFalse($wpdb->query("INSERT INTO {$wpdb->posts} (post_content) VALUES ('foo\xf0\xff\xff\xffbar')")); |
|
|
|
|
|
|
808
|
|
|
} |
|
809
|
|
|
|
|
810
|
|
|
/** |
|
811
|
|
|
* @ticket 21212 |
|
812
|
|
|
*/ |
|
813
|
|
|
function data_table_collation_check() |
|
814
|
|
|
{ |
|
815
|
|
|
$table_name = 'table_collation_check'; |
|
816
|
|
|
$data = array( |
|
817
|
|
|
array( |
|
818
|
|
|
// utf8_bin tables don't need extra sanity checking. |
|
819
|
|
|
"( a VARCHAR(50) COLLATE utf8_bin )", // create |
|
820
|
|
|
true // expected result |
|
821
|
|
|
), |
|
822
|
|
|
array( |
|
823
|
|
|
// Neither do utf8_general_ci tables. |
|
824
|
|
|
"( a VARCHAR(50) COLLATE utf8_general_ci )", |
|
825
|
|
|
true |
|
826
|
|
|
), |
|
827
|
|
|
array( |
|
828
|
|
|
// utf8_unicode_ci tables do. |
|
829
|
|
|
"( a VARCHAR(50) COLLATE utf8_unicode_ci )", |
|
830
|
|
|
false |
|
831
|
|
|
), |
|
832
|
|
|
array( |
|
833
|
|
|
// utf8_bin tables don't need extra sanity checking, |
|
834
|
|
|
// except for when they're not just utf8_bin. |
|
835
|
|
|
"( a VARCHAR(50) COLLATE utf8_bin, b VARCHAR(50) COLLATE big5_chinese_ci )", |
|
836
|
|
|
false |
|
837
|
|
|
), |
|
838
|
|
|
array( |
|
839
|
|
|
// utf8_bin tables don't need extra sanity checking |
|
840
|
|
|
// when the other columns aren't strings. |
|
841
|
|
|
"( a VARCHAR(50) COLLATE utf8_bin, b INT )", |
|
842
|
|
|
true |
|
843
|
|
|
), |
|
844
|
|
|
); |
|
845
|
|
|
|
|
846
|
|
|
foreach( $data as $i => &$value ) { |
|
|
|
|
|
|
847
|
|
|
$this_table_name = $table_name . '_' . $i; |
|
848
|
|
|
|
|
849
|
|
|
$value[0] = "CREATE TABLE $this_table_name {$value[0]}"; |
|
850
|
|
|
$value[2] = "SELECT * FROM $this_table_name WHERE a='\xf0\x9f\x98\x88'"; |
|
851
|
|
|
$value[3] = "DROP TABLE IF EXISTS $this_table_name"; |
|
852
|
|
|
$value[4] = array( |
|
853
|
|
|
"SELECT * FROM $this_table_name WHERE a='foo'", |
|
854
|
|
|
"SHOW FULL TABLES LIKE $this_table_name", |
|
855
|
|
|
"DESCRIBE $this_table_name", |
|
856
|
|
|
"DESC $this_table_name", |
|
857
|
|
|
"EXPLAIN SELECT * FROM $this_table_name", |
|
858
|
|
|
); |
|
859
|
|
|
} |
|
860
|
|
|
unset($value); |
|
861
|
|
|
|
|
862
|
|
|
return $data; |
|
863
|
|
|
} |
|
864
|
|
|
|
|
865
|
|
|
|
|
866
|
|
|
/** |
|
867
|
|
|
* @dataProvider data_table_collation_check |
|
868
|
|
|
* @ticket 21212 |
|
869
|
|
|
*/ |
|
870
|
|
|
function test_table_collation_check( $create, $expected, $query, $drop, $always_true ) |
|
871
|
|
|
{ |
|
872
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
873
|
|
|
|
|
874
|
|
|
self::$_wpdb->query($create); |
|
|
|
|
|
|
875
|
|
|
|
|
876
|
|
|
$return = self::$_wpdb->check_safe_collation($query); |
|
|
|
|
|
|
877
|
|
|
$this->assertEquals($expected, $return); |
|
878
|
|
|
|
|
879
|
|
|
foreach( $always_true as $true_query ) { |
|
|
|
|
|
|
880
|
|
|
$return = self::$_wpdb->check_safe_collation($true_query); |
|
|
|
|
|
|
881
|
|
|
$this->assertTrue($return); |
|
882
|
|
|
} |
|
883
|
|
|
|
|
884
|
|
|
self::$_wpdb->query($drop); |
|
|
|
|
|
|
885
|
|
|
} |
|
886
|
|
|
|
|
887
|
|
|
function test_strip_invalid_text_for_column_bails_if_ascii_input_too_long() |
|
888
|
|
|
{ |
|
889
|
|
|
global $wpdb; |
|
890
|
|
|
|
|
891
|
|
|
// TEXT column |
|
892
|
|
|
$stripped = $wpdb->strip_invalid_text_for_column($wpdb->comments, 'comment_content', str_repeat('A', 65536)); |
|
893
|
|
|
$this->assertEquals(65535, strlen($stripped)); |
|
894
|
|
|
|
|
895
|
|
|
// VARCHAR column |
|
896
|
|
|
$stripped = $wpdb->strip_invalid_text_for_column($wpdb->comments, 'comment_agent', str_repeat('A', 256)); |
|
897
|
|
|
$this->assertEquals(255, strlen($stripped)); |
|
898
|
|
|
} |
|
899
|
|
|
|
|
900
|
|
|
/** |
|
901
|
|
|
* @ticket 32279 |
|
902
|
|
|
*/ |
|
903
|
|
|
function test_strip_invalid_text_from_query_cp1251_is_safe() |
|
904
|
|
|
{ |
|
905
|
|
|
$tablename = 'test_cp1251_query_' . rand_str(5); |
|
906
|
|
|
if (! self::$_wpdb->query("CREATE TABLE $tablename ( a VARCHAR(50) ) DEFAULT CHARSET 'cp1251'") ) { |
|
|
|
|
|
|
907
|
|
|
$this->markTestSkipped("Test requires the 'cp1251' charset"); |
|
908
|
|
|
} |
|
909
|
|
|
|
|
910
|
|
|
$safe_query = "INSERT INTO $tablename( `a` ) VALUES( 'safe data' )"; |
|
911
|
|
|
$stripped_query = self::$_wpdb->strip_invalid_text_from_query($safe_query); |
|
|
|
|
|
|
912
|
|
|
|
|
913
|
|
|
self::$_wpdb->query("DROP TABLE $tablename"); |
|
|
|
|
|
|
914
|
|
|
|
|
915
|
|
|
$this->assertEquals($safe_query, $stripped_query); |
|
916
|
|
|
} |
|
917
|
|
|
|
|
918
|
|
|
/** |
|
919
|
|
|
* @ticket 34708 |
|
920
|
|
|
*/ |
|
921
|
|
|
function test_no_db_charset_defined() |
|
922
|
|
|
{ |
|
923
|
|
|
$tablename = 'test_cp1251_query_' . rand_str(5); |
|
924
|
|
|
if (! self::$_wpdb->query("CREATE TABLE $tablename ( a VARCHAR(50) ) DEFAULT CHARSET 'cp1251'") ) { |
|
|
|
|
|
|
925
|
|
|
$this->markTestSkipped("Test requires the 'cp1251' charset"); |
|
926
|
|
|
} |
|
927
|
|
|
|
|
928
|
|
|
$charset = self::$_wpdb->charset; |
|
929
|
|
|
self::$_wpdb->charset = ''; |
|
930
|
|
|
|
|
931
|
|
|
$safe_query = "INSERT INTO $tablename( `a` ) VALUES( 'safe data' )"; |
|
932
|
|
|
$stripped_query = self::$_wpdb->strip_invalid_text_from_query($safe_query); |
|
|
|
|
|
|
933
|
|
|
|
|
934
|
|
|
self::$_wpdb->query("DROP TABLE $tablename"); |
|
|
|
|
|
|
935
|
|
|
|
|
936
|
|
|
self::$_wpdb->charset = $charset; |
|
937
|
|
|
|
|
938
|
|
|
$this->assertEquals($safe_query, $stripped_query); |
|
939
|
|
|
} |
|
940
|
|
|
|
|
941
|
|
|
/** |
|
942
|
|
|
* @ticket 36649 |
|
943
|
|
|
*/ |
|
944
|
|
|
function test_set_charset_changes_the_connection_collation() |
|
945
|
|
|
{ |
|
946
|
|
|
self::$_wpdb->set_charset(self::$_wpdb->dbh, 'utf8', 'utf8_general_ci'); |
|
|
|
|
|
|
947
|
|
|
$results = self::$_wpdb->get_results("SHOW VARIABLES WHERE Variable_name='collation_connection'"); |
|
|
|
|
|
|
948
|
|
|
$this->assertEquals('utf8_general_ci', $results[0]->Value); |
|
949
|
|
|
|
|
950
|
|
|
self::$_wpdb->set_charset(self::$_wpdb->dbh, 'utf8mb4', 'utf8mb4_unicode_ci'); |
|
|
|
|
|
|
951
|
|
|
$results = self::$_wpdb->get_results("SHOW VARIABLES WHERE Variable_name='collation_connection'"); |
|
|
|
|
|
|
952
|
|
|
$this->assertEquals('utf8mb4_unicode_ci', $results[0]->Value); |
|
953
|
|
|
|
|
954
|
|
|
self::$_wpdb->set_charset(self::$_wpdb->dbh); |
|
|
|
|
|
|
955
|
|
|
} |
|
956
|
|
|
} |
|
957
|
|
|
|