8 | | class Tests_DB extends WP_UnitTestCase { |
9 | | |
10 | | /** |
11 | | * Query log |
12 | | * @var array |
13 | | */ |
14 | | protected $_queries = array(); |
15 | | |
16 | | /** |
17 | | * Our special WPDB |
18 | | * @var resource |
19 | | */ |
20 | | protected static $_wpdb; |
21 | | |
22 | | public static function setUpBeforeClass() { |
23 | | parent::setUpBeforeClass(); |
24 | | self::$_wpdb = new wpdb_exposed_methods_for_testing(); |
25 | | } |
26 | | |
27 | | /** |
28 | | * Set up the test fixture |
29 | | */ |
30 | | public function setUp() { |
31 | | parent::setUp(); |
32 | | $this->_queries = array(); |
33 | | add_filter( 'query', array( $this, 'query_filter' ) ); |
34 | | } |
35 | | |
36 | | /** |
37 | | * Tear down the test fixture |
38 | | */ |
39 | | public function tearDown() { |
40 | | remove_filter( 'query', array( $this, 'query_filter' ) ); |
41 | | parent::tearDown(); |
42 | | } |
43 | | |
44 | | /** |
45 | | * Log each query |
46 | | * @param string $sql |
47 | | * @return string |
48 | | */ |
49 | | public function query_filter( $sql ) { |
50 | | $this->_queries[] = $sql; |
51 | | return $sql; |
52 | | } |
53 | | |
54 | | /** |
55 | | * Test that WPDB will reconnect when the DB link dies |
56 | | * @ticket 5932 |
57 | | */ |
58 | | public function test_db_reconnect() { |
59 | | global $wpdb; |
60 | | |
61 | | $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" ); |
62 | | $this->assertGreaterThan( 0, $var ); |
63 | | |
64 | | $wpdb->close(); |
65 | | |
66 | | $var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" ); |
67 | | |
68 | | // Ensure all database handles have been properly reconnected after this test. |
69 | | $wpdb->db_connect(); |
70 | | self::$_wpdb->db_connect(); |
71 | | |
72 | | $this->assertGreaterThan( 0, $var ); |
73 | | } |
74 | | |
75 | | /** |
76 | | * Test that floats formatted as "0,700" get sanitized properly by wpdb |
77 | | * @global mixed $wpdb |
78 | | * |
79 | | * @ticket 19861 |
80 | | */ |
81 | | public function test_locale_floats() { |
82 | | global $wpdb; |
83 | | |
84 | | // Save the current locale settings |
85 | | $current_locales = explode( ';', setlocale( LC_ALL, 0 ) ); |
86 | | |
87 | | // Switch to Russian |
88 | | $flag = setlocale( LC_ALL, 'ru_RU.utf8', 'rus', 'fr_FR.utf8', 'fr_FR', 'de_DE.utf8', 'de_DE', 'es_ES.utf8', 'es_ES' ); |
89 | | if ( false === $flag ) |
90 | | $this->markTestSkipped( 'No European languages available for testing' ); |
91 | | |
92 | | // Try an update query |
93 | | $wpdb->suppress_errors( true ); |
94 | | $wpdb->update( |
95 | | 'test_table', |
96 | | array( 'float_column' => 0.7 ), |
97 | | array( 'meta_id' => 5 ), |
98 | | array( '%f' ), |
99 | | array( '%d' ) |
100 | | ); |
101 | | $wpdb->suppress_errors( false ); |
102 | | |
103 | | // Ensure the float isn't 0,700 |
104 | | $this->assertContains( '0.700', array_pop( $this->_queries ) ); |
105 | | |
106 | | // Try a prepare |
107 | | $sql = $wpdb->prepare( "UPDATE test_table SET float_column = %f AND meta_id = %d", 0.7, 5 ); |
108 | | $this->assertContains( '0.700', $sql ); |
109 | | |
110 | | // Restore locale settings |
111 | | foreach ( $current_locales as $locale_setting ) { |
112 | | if ( false !== strpos( $locale_setting, '=' ) ) { |
113 | | list( $category, $locale ) = explode( '=', $locale_setting ); |
114 | | if ( defined( $category ) ) |
115 | | setlocale( constant( $category ), $locale ); |
116 | | } else { |
117 | | setlocale( LC_ALL, $locale_setting ); |
118 | | } |
119 | | } |
120 | | } |
121 | | |
122 | | /** |
123 | | * @ticket 10041 |
124 | | */ |
125 | | function test_esc_like() { |
126 | | global $wpdb; |
127 | | |
128 | | $inputs = array( |
129 | | 'howdy%', //Single Percent |
130 | | 'howdy_', //Single Underscore |
131 | | 'howdy\\', //Single slash |
132 | | 'howdy\\howdy%howdy_', //The works |
133 | | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', //Plain text |
134 | | ); |
135 | | $expected = array( |
136 | | 'howdy\\%', |
137 | | 'howdy\\_', |
138 | | 'howdy\\\\', |
139 | | 'howdy\\\\howdy\\%howdy\\_', |
140 | | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
141 | | ); |
142 | | |
143 | | foreach ($inputs as $key => $input) { |
144 | | $this->assertEquals($expected[$key], $wpdb->esc_like($input)); |
145 | | } |
146 | | } |
147 | | |
148 | | /** |
149 | | * Test LIKE Queries |
150 | | * |
151 | | * Make sure $wpdb is fully compatible with esc_like() by testing the identity of various strings. |
152 | | * When escaped properly, a string literal is always LIKE itself (1) |
153 | | * and never LIKE any other string literal (0) no matter how crazy the SQL looks. |
154 | | * |
155 | | * @ticket 10041 |
156 | | * @dataProvider data_like_query |
157 | | * @param $data string The haystack, raw. |
158 | | * @param $like string The like phrase, raw. |
| 8 | class Tests_DB extends WP_UnitTestCase |
| 9 | { |
| 10 | |
| 11 | /** |
| 12 | * Query log |
| 13 | * @var array |
| 14 | */ |
| 15 | protected $_queries = array(); |
| 16 | |
| 17 | /** |
| 18 | * Our special WPDB |
| 19 | * @var resource |
| 20 | */ |
| 21 | protected static $_wpdb; |
| 22 | |
| 23 | public static function setUpBeforeClass() |
| 24 | { |
| 25 | parent::setUpBeforeClass(); |
| 26 | self::$_wpdb = new wpdb_exposed_methods_for_testing(); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Set up the test fixture |
| 31 | */ |
| 32 | public function setUp() |
| 33 | { |
| 34 | parent::setUp(); |
| 35 | $this->_queries = array(); |
| 36 | add_filter('query', array( $this, 'query_filter' )); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Tear down the test fixture |
| 41 | */ |
| 42 | public function tearDown() |
| 43 | { |
| 44 | remove_filter('query', array( $this, 'query_filter' )); |
| 45 | parent::tearDown(); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Log each query |
| 50 | * @param string $sql |
| 51 | * @return string |
| 52 | */ |
| 53 | public function query_filter($sql) |
| 54 | { |
| 55 | $this->_queries[] = $sql; |
| 56 | return $sql; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Test that WPDB will reconnect when the DB link dies |
| 61 | * @ticket 5932 |
| 62 | */ |
| 63 | public function test_db_reconnect() |
| 64 | { |
| 65 | global $wpdb; |
| 66 | |
| 67 | $var = $wpdb->get_var("SELECT ID FROM $wpdb->users LIMIT 1"); |
| 68 | $this->assertGreaterThan(0, $var); |
| 69 | |
| 70 | $wpdb->close(); |
| 71 | |
| 72 | $var = $wpdb->get_var("SELECT ID FROM $wpdb->users LIMIT 1"); |
| 73 | |
| 74 | // Ensure all database handles have been properly reconnected after this test. |
| 75 | $wpdb->db_connect(); |
| 76 | self::$_wpdb->db_connect(); |
| 77 | |
| 78 | $this->assertGreaterThan(0, $var); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Test that floats formatted as "0,700" get sanitized properly by wpdb |
| 83 | * @global mixed $wpdb |
| 84 | * |
| 85 | * @ticket 19861 |
| 86 | */ |
| 87 | public function test_locale_floats() |
| 88 | { |
| 89 | global $wpdb; |
| 90 | |
| 91 | // Save the current locale settings |
| 92 | $current_locales = explode(';', setlocale(LC_ALL, 0)); |
| 93 | |
| 94 | // Switch to Russian |
| 95 | $flag = setlocale(LC_ALL, 'ru_RU.utf8', 'rus', 'fr_FR.utf8', 'fr_FR', 'de_DE.utf8', 'de_DE', 'es_ES.utf8', 'es_ES'); |
| 96 | if (false === $flag) { |
| 97 | $this->markTestSkipped('No European languages available for testing'); |
| 98 | } |
| 99 | |
| 100 | // Try an update query |
| 101 | $wpdb->suppress_errors(true); |
| 102 | $wpdb->update( |
| 103 | 'test_table', |
| 104 | array( 'float_column' => 0.7 ), |
| 105 | array( 'meta_id' => 5 ), |
| 106 | array( '%f' ), |
| 107 | array( '%d' ) |
| 108 | ); |
| 109 | $wpdb->suppress_errors(false); |
| 110 | |
| 111 | // Ensure the float isn't 0,700 |
| 112 | $this->assertContains('0.700', array_pop($this->_queries)); |
| 113 | |
| 114 | // Try a prepare |
| 115 | $sql = $wpdb->prepare("UPDATE test_table SET float_column = %f AND meta_id = %d", 0.7, 5); |
| 116 | $this->assertContains('0.700', $sql); |
| 117 | |
| 118 | // Restore locale settings |
| 119 | foreach ($current_locales as $locale_setting) { |
| 120 | if (false !== strpos($locale_setting, '=')) { |
| 121 | list($category, $locale) = explode('=', $locale_setting); |
| 122 | if (defined($category)) { |
| 123 | setlocale(constant($category), $locale); |
| 124 | } |
| 125 | } else { |
| 126 | setlocale(LC_ALL, $locale_setting); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * @ticket 10041 |
| 133 | */ |
| 134 | public function test_esc_like() |
| 135 | { |
| 136 | global $wpdb; |
| 137 | |
| 138 | $inputs = array( |
| 139 | 'howdy%', //Single Percent |
| 140 | 'howdy_', //Single Underscore |
| 141 | 'howdy\\', //Single slash |
| 142 | 'howdy\\howdy%howdy_', //The works |
| 143 | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', //Plain text |
| 144 | ); |
| 145 | $expected = array( |
| 146 | 'howdy\\%', |
| 147 | 'howdy\\_', |
| 148 | 'howdy\\\\', |
| 149 | 'howdy\\\\howdy\\%howdy\\_', |
| 150 | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
| 151 | ); |
| 152 | |
| 153 | foreach ($inputs as $key => $input) { |
| 154 | $this->assertEquals($expected[$key], $wpdb->esc_like($input)); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Test LIKE Queries |
| 160 | * |
| 161 | * Make sure $wpdb is fully compatible with esc_like() by testing the identity of various strings. |
| 162 | * When escaped properly, a string literal is always LIKE itself (1) |
| 163 | * and never LIKE any other string literal (0) no matter how crazy the SQL looks. |
| 164 | * |
| 165 | * @ticket 10041 |
| 166 | * @dataProvider data_like_query |
| 167 | * @param $data string The haystack, raw. |
| 168 | * @param $like string The like phrase, raw. |
160 | | */ |
161 | | function test_like_query( $data, $like, $result ) { |
162 | | global $wpdb; |
163 | | return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( "SELECT %s LIKE %s", $data, $wpdb->esc_like( $like ) ) ) ); |
164 | | } |
165 | | |
166 | | function data_like_query() { |
167 | | return array( |
168 | | array( |
169 | | 'aaa', |
170 | | 'aaa', |
171 | | '1', |
172 | | ), |
173 | | array( |
174 | | 'a\\aa', // SELECT 'a\\aa' # This represents a\aa in both languages. |
175 | | 'a\\aa', // LIKE 'a\\\\aa' |
176 | | '1', |
177 | | ), |
178 | | array( |
179 | | 'a%aa', |
180 | | 'a%aa', |
181 | | '1', |
182 | | ), |
183 | | array( |
184 | | 'aaaa', |
185 | | 'a%aa', |
186 | | '0', |
187 | | ), |
188 | | array( |
189 | | 'a\\%aa', // SELECT 'a\\%aa' |
190 | | 'a\\%aa', // LIKE 'a\\\\\\%aa' # The PHP literal would be "LIKE 'a\\\\\\\\\\\\%aa'". This is why we need reliable escape functions! |
191 | | '1', |
192 | | ), |
193 | | array( |
194 | | 'a%aa', |
195 | | 'a\\%aa', |
196 | | '0', |
197 | | ), |
198 | | array( |
199 | | 'a\\%aa', |
200 | | 'a%aa', |
201 | | '0', |
202 | | ), |
203 | | array( |
204 | | 'a_aa', |
205 | | 'a_aa', |
206 | | '1', |
207 | | ), |
208 | | array( |
209 | | 'aaaa', |
210 | | 'a_aa', |
211 | | '0', |
212 | | ), |
213 | | array( |
214 | | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
215 | | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
216 | | '1', |
217 | | ), |
218 | | ); |
219 | | } |
220 | | |
221 | | /** |
222 | | * @ticket 18510 |
223 | | */ |
224 | | function test_wpdb_supposedly_protected_properties() { |
225 | | global $wpdb; |
226 | | |
227 | | $this->assertNotEmpty( $wpdb->dbh ); |
228 | | $dbh = $wpdb->dbh; |
229 | | $this->assertNotEmpty( $dbh ); |
230 | | $this->assertTrue( isset( $wpdb->dbh ) ); // Test __isset() |
231 | | unset( $wpdb->dbh ); |
232 | | $this->assertTrue( empty( $wpdb->dbh ) ); |
233 | | $wpdb->dbh = $dbh; |
234 | | $this->assertNotEmpty( $wpdb->dbh ); |
235 | | } |
236 | | |
237 | | /** |
238 | | * @ticket 21212 |
239 | | */ |
240 | | function test_wpdb_actually_protected_properties() { |
241 | | global $wpdb; |
242 | | |
243 | | $new_meta = "HAHA I HOPE THIS DOESN'T WORK"; |
244 | | |
245 | | $col_meta = $wpdb->col_meta; |
246 | | $wpdb->col_meta = $new_meta; |
247 | | |
248 | | $this->assertNotEquals( $col_meta, $new_meta ); |
249 | | $this->assertEquals( $col_meta, $wpdb->col_meta ); |
250 | | } |
251 | | |
252 | | /** |
253 | | * @ticket 18510 |
254 | | */ |
255 | | function test_wpdb_nonexistent_properties() { |
256 | | global $wpdb; |
257 | | |
258 | | $this->assertTrue( empty( $wpdb->nonexistent_property ) ); |
259 | | $wpdb->nonexistent_property = true; |
260 | | $this->assertTrue( $wpdb->nonexistent_property ); |
261 | | $this->assertTrue( isset( $wpdb->nonexistent_property ) ); |
262 | | unset( $wpdb->nonexistent_property ); |
263 | | $this->assertTrue( empty( $wpdb->nonexistent_property ) ); |
264 | | } |
265 | | |
266 | | /** |
267 | | * Test that an escaped %%f is not altered |
268 | | * @ticket 19861 |
269 | | */ |
270 | | public function test_double_escaped_placeholders() { |
271 | | global $wpdb; |
272 | | $sql = $wpdb->prepare( "UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4' ); |
273 | | $this->assertEquals( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql ); |
274 | | } |
275 | | |
276 | | |
277 | | /** |
278 | | * Test that SQL modes are set correctly |
279 | | * @ticket 26847 |
280 | | */ |
281 | | function test_set_sql_mode() { |
282 | | global $wpdb; |
283 | | |
284 | | $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
285 | | |
286 | | $new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' ); |
287 | | |
288 | | $wpdb->set_sql_mode( $new_modes ); |
289 | | |
290 | | $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
291 | | $this->assertEqualSets( $new_modes, explode( ',', $check_new_modes ) ); |
292 | | |
293 | | $wpdb->set_sql_mode( explode( ',', $current_modes ) ); |
294 | | } |
295 | | |
296 | | /** |
297 | | * Test that incompatible SQL modes are blocked |
298 | | * @ticket 26847 |
299 | | */ |
300 | | function test_set_incompatible_sql_mode() { |
301 | | global $wpdb; |
302 | | |
303 | | $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
304 | | |
305 | | $new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' ); |
306 | | $wpdb->set_sql_mode( $new_modes ); |
307 | | $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
308 | | $this->assertNotContains( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) ); |
309 | | |
310 | | $wpdb->set_sql_mode( explode( ',', $current_modes ) ); |
311 | | } |
312 | | |
313 | | /** |
314 | | * Test that incompatible SQL modes can be changed |
315 | | * @ticket 26847 |
316 | | */ |
317 | | function test_set_allowed_incompatible_sql_mode() { |
318 | | global $wpdb; |
319 | | |
320 | | $current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
321 | | |
322 | | $new_modes = array( 'IGNORE_SPACE', 'ONLY_FULL_GROUP_BY', 'NO_AUTO_CREATE_USER' ); |
323 | | |
324 | | add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 ); |
325 | | $wpdb->set_sql_mode( $new_modes ); |
326 | | remove_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1 ); |
327 | | |
328 | | $check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' ); |
329 | | $this->assertContains( 'ONLY_FULL_GROUP_BY', explode( ',', $check_new_modes ) ); |
330 | | |
331 | | $wpdb->set_sql_mode( explode( ',', $current_modes ) ); |
332 | | } |
333 | | |
334 | | public function filter_allowed_incompatible_sql_mode( $modes ) { |
335 | | $pos = array_search( 'ONLY_FULL_GROUP_BY', $modes ); |
336 | | $this->assertGreaterThanOrEqual( 0, $pos ); |
337 | | |
338 | | if ( FALSE === $pos ) { |
339 | | return $modes; |
340 | | } |
341 | | |
342 | | unset( $modes[ $pos ] ); |
343 | | return $modes; |
344 | | } |
345 | | |
346 | | /** |
347 | | * @ticket 25604 |
348 | | * @expectedIncorrectUsage wpdb::prepare |
349 | | */ |
350 | | function test_prepare_without_arguments() { |
351 | | global $wpdb; |
352 | | $id = 0; |
353 | | // This, obviously, is an incorrect prepare. |
354 | | $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = $id", $id ); |
355 | | $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0", $prepared ); |
356 | | } |
357 | | |
358 | | function test_prepare_sprintf() { |
359 | | global $wpdb; |
360 | | |
361 | | $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin" ); |
362 | | $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared ); |
363 | | } |
364 | | |
365 | | /** |
366 | | * @expectedIncorrectUsage wpdb::prepare |
367 | | */ |
368 | | function test_prepare_sprintf_invalid_args() { |
369 | | global $wpdb; |
| 170 | */ |
| 171 | public function test_like_query($data, $like, $result) |
| 172 | { |
| 173 | global $wpdb; |
| 174 | return $this->assertEquals($result, $wpdb->get_var($wpdb->prepare("SELECT %s LIKE %s", $data, $wpdb->esc_like($like)))); |
| 175 | } |
| 176 | |
| 177 | public function data_like_query() |
| 178 | { |
| 179 | return array( |
| 180 | array( |
| 181 | 'aaa', |
| 182 | 'aaa', |
| 183 | '1', |
| 184 | ), |
| 185 | array( |
| 186 | 'a\\aa', // SELECT 'a\\aa' # This represents a\aa in both languages. |
| 187 | 'a\\aa', // LIKE 'a\\\\aa' |
| 188 | '1', |
| 189 | ), |
| 190 | array( |
| 191 | 'a%aa', |
| 192 | 'a%aa', |
| 193 | '1', |
| 194 | ), |
| 195 | array( |
| 196 | 'aaaa', |
| 197 | 'a%aa', |
| 198 | '0', |
| 199 | ), |
| 200 | array( |
| 201 | 'a\\%aa', // SELECT 'a\\%aa' |
| 202 | 'a\\%aa', // LIKE 'a\\\\\\%aa' # The PHP literal would be "LIKE 'a\\\\\\\\\\\\%aa'". This is why we need reliable escape functions! |
| 203 | '1', |
| 204 | ), |
| 205 | array( |
| 206 | 'a%aa', |
| 207 | 'a\\%aa', |
| 208 | '0', |
| 209 | ), |
| 210 | array( |
| 211 | 'a\\%aa', |
| 212 | 'a%aa', |
| 213 | '0', |
| 214 | ), |
| 215 | array( |
| 216 | 'a_aa', |
| 217 | 'a_aa', |
| 218 | '1', |
| 219 | ), |
| 220 | array( |
| 221 | 'aaaa', |
| 222 | 'a_aa', |
| 223 | '0', |
| 224 | ), |
| 225 | array( |
| 226 | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
| 227 | 'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', |
| 228 | '1', |
| 229 | ), |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * @ticket 18510 |
| 235 | */ |
| 236 | public function test_wpdb_supposedly_protected_properties() |
| 237 | { |
| 238 | global $wpdb; |
| 239 | |
| 240 | $this->assertNotEmpty($wpdb->dbh); |
| 241 | $dbh = $wpdb->dbh; |
| 242 | $this->assertNotEmpty($dbh); |
| 243 | $this->assertTrue(isset($wpdb->dbh)); // Test __isset() |
| 244 | unset($wpdb->dbh); |
| 245 | $this->assertTrue(empty($wpdb->dbh)); |
| 246 | $wpdb->dbh = $dbh; |
| 247 | $this->assertNotEmpty($wpdb->dbh); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * @ticket 21212 |
| 252 | */ |
| 253 | public function test_wpdb_actually_protected_properties() |
| 254 | { |
| 255 | global $wpdb; |
| 256 | |
| 257 | $new_meta = "HAHA I HOPE THIS DOESN'T WORK"; |
| 258 | |
| 259 | $col_meta = $wpdb->col_meta; |
| 260 | $wpdb->col_meta = $new_meta; |
| 261 | |
| 262 | $this->assertNotEquals($col_meta, $new_meta); |
| 263 | $this->assertEquals($col_meta, $wpdb->col_meta); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @ticket 18510 |
| 268 | */ |
| 269 | public function test_wpdb_nonexistent_properties() |
| 270 | { |
| 271 | global $wpdb; |
| 272 | |
| 273 | $this->assertTrue(empty($wpdb->nonexistent_property)); |
| 274 | $wpdb->nonexistent_property = true; |
| 275 | $this->assertTrue($wpdb->nonexistent_property); |
| 276 | $this->assertTrue(isset($wpdb->nonexistent_property)); |
| 277 | unset($wpdb->nonexistent_property); |
| 278 | $this->assertTrue(empty($wpdb->nonexistent_property)); |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Test that an escaped %%f is not altered |
| 283 | * @ticket 19861 |
| 284 | */ |
| 285 | public function test_double_escaped_placeholders() |
| 286 | { |
| 287 | global $wpdb; |
| 288 | $sql = $wpdb->prepare("UPDATE test_table SET string_column = '%%f is a float, %%d is an int %d, %%s is a string', field = %s", 3, '4'); |
| 289 | $this->assertEquals("UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql); |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Test that SQL modes are set correctly |
| 295 | * @ticket 26847 |
| 296 | */ |
| 297 | public function test_set_sql_mode() |
| 298 | { |
| 299 | global $wpdb; |
| 300 | |
| 301 | $current_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 302 | |
| 303 | $new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_CREATE_USER' ); |
| 304 | |
| 305 | $wpdb->set_sql_mode($new_modes); |
| 306 | |
| 307 | $check_new_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 308 | $this->assertEqualSets($new_modes, explode(',', $check_new_modes)); |
| 309 | |
| 310 | $wpdb->set_sql_mode(explode(',', $current_modes)); |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Test that incompatible SQL modes are blocked |
| 315 | * @ticket 26847 |
| 316 | */ |
| 317 | public function test_set_incompatible_sql_mode() |
| 318 | { |
| 319 | global $wpdb; |
| 320 | |
| 321 | $current_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 322 | |
| 323 | $new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_CREATE_USER' ); |
| 324 | $wpdb->set_sql_mode($new_modes); |
| 325 | $check_new_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 326 | $this->assertNotContains('NO_ZERO_DATE', explode(',', $check_new_modes)); |
| 327 | |
| 328 | $wpdb->set_sql_mode(explode(',', $current_modes)); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Test that incompatible SQL modes can be changed |
| 333 | * @ticket 26847 |
| 334 | */ |
| 335 | public function test_set_allowed_incompatible_sql_mode() |
| 336 | { |
| 337 | global $wpdb; |
| 338 | |
| 339 | $current_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 340 | |
| 341 | $new_modes = array( 'IGNORE_SPACE', 'ONLY_FULL_GROUP_BY', 'NO_AUTO_CREATE_USER' ); |
| 342 | |
| 343 | add_filter('incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1); |
| 344 | $wpdb->set_sql_mode($new_modes); |
| 345 | remove_filter('incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1); |
| 346 | |
| 347 | $check_new_modes = $wpdb->get_var('SELECT @@SESSION.sql_mode;'); |
| 348 | $this->assertContains('ONLY_FULL_GROUP_BY', explode(',', $check_new_modes)); |
| 349 | |
| 350 | $wpdb->set_sql_mode(explode(',', $current_modes)); |
| 351 | } |
| 352 | |
| 353 | public function filter_allowed_incompatible_sql_mode($modes) |
| 354 | { |
| 355 | $pos = array_search('ONLY_FULL_GROUP_BY', $modes); |
| 356 | $this->assertGreaterThanOrEqual(0, $pos); |
| 357 | |
| 358 | if (false === $pos) { |
| 359 | return $modes; |
| 360 | } |
| 361 | |
| 362 | unset($modes[ $pos ]); |
| 363 | return $modes; |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * @ticket 25604 |
| 368 | * @expectedIncorrectUsage wpdb::prepare |
| 369 | */ |
| 370 | public function test_prepare_without_arguments() |
| 371 | { |
| 372 | global $wpdb; |
| 373 | $id = 0; |
| 374 | // This, obviously, is an incorrect prepare. |
| 375 | $prepared = $wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = $id", $id); |
| 376 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 0", $prepared); |
| 377 | } |
385 | | /** |
386 | | * @expectedIncorrectUsage wpdb::prepare |
387 | | */ |
388 | | function test_prepare_vsprintf_invalid_args() { |
389 | | global $wpdb; |
390 | | |
391 | | $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( "admin" ) ) ); |
392 | | $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared ); |
393 | | |
394 | | $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), "admin" ) ); |
395 | | $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared ); |
| 397 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), "admin"); |
| 398 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared); |
| 399 | } |
| 400 | |
| 401 | public function test_prepare_vsprintf() |
| 402 | { |
| 403 | global $wpdb; |
| 404 | |
| 405 | $prepared = $wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, "admin" )); |
| 406 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared); |
| 407 | } |
| 408 | |
| 409 | /** |
| 410 | * @expectedIncorrectUsage wpdb::prepare |
| 411 | */ |
| 412 | public function test_prepare_vsprintf_invalid_args() |
| 413 | { |
| 414 | global $wpdb; |
| 415 | |
| 416 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( "admin" ) )); |
| 417 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared); |
| 418 | |
| 419 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), "admin" )); |
| 420 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * @ticket 42040 |
| 425 | * @expectedIncorrectUsage wpdb::prepare |
| 426 | */ |
| 427 | public function test_prepare_invalid_args_count() |
| 428 | { |
| 429 | global $wpdb; |
| 430 | |
| 431 | /* |
| 432 | * cases with more number of argument than the patterns found |
| 433 | */ |
| 434 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin", "extra-arg"); |
| 435 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared); |
| 436 | |
| 437 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1,"admin", "extra-arg")); |
| 438 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared); |
| 439 | |
| 440 | |
| 441 | //case with double IncorrectUsage |
| 442 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), "admin", "extra-arg"); |
| 443 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared); |
| 444 | |
| 445 | //random case to check if it ignores %% |
| 446 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND %% AND user_login = %s", 1, "admin", "extra-arg"); |
| 447 | $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND % AND user_login = 'admin'", $prepared); |
| 448 | |
| 449 | //case with multiple patterns clubbed together and also Testing %F pattern detection |
| 450 | // Note: Floats getting converted to 6 decimal place after - which is not 42040 patch is about |
| 451 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %%%d AND %F AND %f AND user_login = %s", 1, 2.3, "4.5", "admin", "extra-arg"); |
| 452 | $this->assertEquals("SELECT * FROM wptests_users WHERE id = %1 AND 2.300000 AND 4.500000 AND user_login = 'admin'", $prepared); |
| 453 | |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | * cases with less number of argument than the patterns found |
| 458 | */ |
| 459 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status =%d and user_login = %s", 1, "admin", 0); |
| 460 | $this->assertEquals("", $prepared); |
| 461 | |
| 462 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status =%d and user_login = %s", array( 1,"admin", 0 )); |
| 463 | $this->assertEquals("", $prepared); |
| 464 | |
| 465 | //case with double notices |
| 466 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status =%d and user_login = %s", array( 1 ), "admin", 0); |
| 467 | $this->assertEquals("", $prepared); |
| 468 | |
| 469 | //random case to check if it ignores %% |
| 470 | $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d and %% and user_login = %s and user_status =%d and user_login = %s", 1, "admin", "extra-arg"); |
| 471 | $this->assertEquals("", $prepared); |
| 472 | |
| 473 | // $prepared = @$wpdb->prepare("SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin"); |
| 474 | // $this->assertEquals("SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared); |
| 475 | } |
| 476 | |
| 477 | |
| 478 | |
| 479 | |
| 480 | |
| 481 | public function test_db_version() |
| 482 | { |
| 483 | global $wpdb; |
| 484 | |
| 485 | $this->assertTrue(version_compare($wpdb->db_version(), '5.0', '>=')); |
| 486 | } |
| 487 | |
| 488 | public function test_get_caller() |
| 489 | { |
| 490 | global $wpdb; |
| 491 | $str = $wpdb->get_caller(); |
| 492 | $calls = explode(', ', $str); |
| 493 | $called = join('->', array( __CLASS__, __FUNCTION__ )); |
| 494 | $this->assertEquals($called, end($calls)); |
| 495 | } |
| 496 | |
| 497 | public function test_has_cap() |
| 498 | { |
| 499 | global $wpdb; |
| 500 | $this->assertTrue($wpdb->has_cap('collation')); |
| 501 | $this->assertTrue($wpdb->has_cap('group_concat')); |
| 502 | $this->assertTrue($wpdb->has_cap('subqueries')); |
| 503 | $this->assertTrue($wpdb->has_cap('COLLATION')); |
| 504 | $this->assertTrue($wpdb->has_cap('GROUP_CONCAT')); |
| 505 | $this->assertTrue($wpdb->has_cap('SUBQUERIES')); |
| 506 | $this->assertEquals( |
| 507 | version_compare($wpdb->db_version(), '5.0.7', '>='), |
| 508 | $wpdb->has_cap('set_charset') |
| 509 | ); |
| 510 | $this->assertEquals( |
| 511 | version_compare($wpdb->db_version(), '5.0.7', '>='), |
| 512 | $wpdb->has_cap('SET_CHARSET') |
| 513 | ); |
| 514 | } |
| 515 | |
| 516 | /** |
| 517 | * @expectedDeprecated supports_collation |
| 518 | */ |
| 519 | public function test_supports_collation() |
| 520 | { |
| 521 | global $wpdb; |
| 522 | $this->assertTrue($wpdb->supports_collation()); |
| 523 | } |
| 524 | |
| 525 | public function test_check_database_version() |
| 526 | { |
| 527 | global $wpdb; |
| 528 | $this->assertEmpty($wpdb->check_database_version()); |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * @expectedException WPDieException |
| 533 | */ |
| 534 | public function test_bail() |
| 535 | { |
| 536 | global $wpdb; |
| 537 | $wpdb->bail('Database is dead.'); |
| 538 | } |
| 539 | |
| 540 | public function test_timers() |
| 541 | { |
| 542 | global $wpdb; |
| 543 | |
| 544 | $wpdb->timer_start(); |
| 545 | usleep(5); |
| 546 | $stop = $wpdb->timer_stop(); |
| 547 | |
| 548 | $this->assertNotEquals($wpdb->time_start, $stop); |
| 549 | $this->assertGreaterThan($stop, $wpdb->time_start); |
| 550 | } |
| 551 | |
| 552 | public function test_get_col_info() |
| 553 | { |
| 554 | global $wpdb; |
| 555 | |
| 556 | $wpdb->get_results("SELECT ID FROM $wpdb->users"); |
| 557 | |
| 558 | $this->assertEquals(array( 'ID' ), $wpdb->get_col_info()); |
| 559 | $this->assertEquals(array( $wpdb->users ), $wpdb->get_col_info('table')); |
| 560 | $this->assertEquals($wpdb->users, $wpdb->get_col_info('table', 0)); |
| 561 | } |
| 562 | |
| 563 | public function test_query_and_delete() |
| 564 | { |
| 565 | global $wpdb; |
| 566 | $rows = $wpdb->query("INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')"); |
| 567 | $this->assertEquals(1, $rows); |
| 568 | $this->assertNotEmpty($wpdb->insert_id); |
| 569 | $d_rows = $wpdb->delete($wpdb->users, array( 'ID' => $wpdb->insert_id )); |
| 570 | $this->assertEquals(1, $d_rows); |
| 571 | } |
| 572 | |
| 573 | public function test_get_row() |
| 574 | { |
| 575 | global $wpdb; |
| 576 | $rows = $wpdb->query("INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')"); |
| 577 | $this->assertEquals(1, $rows); |
| 578 | $this->assertNotEmpty($wpdb->insert_id); |
| 579 | |
| 580 | $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d", $wpdb->insert_id)); |
| 581 | $this->assertInternalType('object', $row); |
| 582 | $this->assertEquals('Walter Sobchak', $row->display_name); |
| 583 | } |
| 584 | |
| 585 | public function test_replace() |
| 586 | { |
| 587 | global $wpdb; |
| 588 | $rows1 = $wpdb->insert($wpdb->users, array( 'display_name' => 'Walter Sobchak' )); |
| 589 | $this->assertEquals(1, $rows1); |
| 590 | $this->assertNotEmpty($wpdb->insert_id); |
| 591 | $last = $wpdb->insert_id; |
| 592 | |
| 593 | $rows2 = $wpdb->replace($wpdb->users, array( 'ID' => $last, 'display_name' => 'Walter Replace Sobchak' )); |
| 594 | $this->assertEquals(2, $rows2); |
| 595 | $this->assertNotEmpty($wpdb->insert_id); |
| 596 | |
| 597 | $this->assertEquals($last, $wpdb->insert_id); |
| 598 | |
| 599 | $row = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE ID = %d", $last)); |
| 600 | $this->assertEquals('Walter Replace Sobchak', $row->display_name); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * wpdb::update() requires a WHERE condition. |
| 605 | * |
| 606 | * @ticket 26106 |
| 607 | */ |
| 608 | public function test_empty_where_on_update() |
| 609 | { |
| 610 | global $wpdb; |
| 611 | $suppress = $wpdb->suppress_errors(true); |
| 612 | $wpdb->update($wpdb->posts, array( 'post_name' => 'burrito' ), array()); |
| 613 | |
| 614 | $expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE "; |
| 615 | $this->assertNotEmpty($wpdb->last_error); |
| 616 | $this->assertEquals($expected1, $wpdb->last_query); |
| 617 | |
| 618 | $wpdb->update($wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' )); |
| 619 | |
| 620 | $expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'"; |
| 621 | $this->assertEmpty($wpdb->last_error); |
| 622 | $this->assertEquals($expected2, $wpdb->last_query); |
| 623 | $wpdb->suppress_errors($suppress); |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * mysqli_ incorrect flush and further sync issues. |
| 628 | * |
| 629 | * @ticket 28155 |
| 630 | */ |
| 631 | public function test_mysqli_flush_sync() |
| 632 | { |
| 633 | global $wpdb; |
| 634 | if (! $wpdb->use_mysqli) { |
| 635 | $this->markTestSkipped('mysqli not being used'); |
547 | | END' ); |
548 | | |
549 | | if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) { |
550 | | $wpdb->suppress_errors( $suppress ); |
551 | | $this->fail( 'procedure could not be created (missing privileges?)' ); |
552 | | } |
553 | | |
554 | | $post_id = self::factory()->post->create(); |
555 | | |
556 | | $this->assertNotEmpty( $wpdb->get_results( 'CALL `test_mysqli_flush_sync_procedure`' ) ); |
557 | | $this->assertNotEmpty( $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` LIMIT 1" ) ); |
558 | | |
559 | | // DROP PROCEDURE will cause a COMMIT, so we delete the post manually before that happens. |
560 | | wp_delete_post( $post_id, true ); |
561 | | |
562 | | $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' ); |
563 | | $wpdb->suppress_errors( $suppress ); |
564 | | } |
565 | | |
566 | | /** |
567 | | * @ticket 21212 |
568 | | */ |
569 | | function data_get_table_from_query() { |
570 | | $table = 'a_test_table_name'; |
571 | | $more_tables = array( |
572 | | // table_name => expected_value |
573 | | '`a_test_db`.`another_test_table`' => 'a_test_db.another_test_table', |
574 | | 'a-test-with-dashes' => 'a-test-with-dashes', |
575 | | ); |
576 | | |
577 | | $queries = array( |
578 | | // Basic |
579 | | "SELECT * FROM $table", |
580 | | "SELECT * FROM `$table`", |
581 | | |
582 | | "SELECT * FROM (SELECT * FROM $table) as subquery", |
583 | | |
584 | | "INSERT $table", |
585 | | "INSERT IGNORE $table", |
586 | | "INSERT IGNORE INTO $table", |
587 | | "INSERT INTO $table", |
588 | | "INSERT LOW_PRIORITY $table", |
589 | | "INSERT DELAYED $table", |
590 | | "INSERT HIGH_PRIORITY $table", |
591 | | "INSERT LOW_PRIORITY IGNORE $table", |
592 | | "INSERT LOW_PRIORITY INTO $table", |
593 | | "INSERT LOW_PRIORITY IGNORE INTO $table", |
594 | | |
595 | | "REPLACE $table", |
596 | | "REPLACE INTO $table", |
597 | | "REPLACE LOW_PRIORITY $table", |
598 | | "REPLACE DELAYED $table", |
599 | | "REPLACE LOW_PRIORITY INTO $table", |
600 | | |
601 | | "UPDATE LOW_PRIORITY $table", |
602 | | "UPDATE LOW_PRIORITY IGNORE $table", |
603 | | |
604 | | "DELETE $table", |
605 | | "DELETE IGNORE $table", |
606 | | "DELETE IGNORE FROM $table", |
607 | | "DELETE FROM $table", |
608 | | "DELETE LOW_PRIORITY $table", |
609 | | "DELETE QUICK $table", |
610 | | "DELETE IGNORE $table", |
611 | | "DELETE LOW_PRIORITY FROM $table", |
612 | | "DELETE a FROM $table a", |
613 | | "DELETE `a` FROM $table a", |
614 | | |
615 | | // Extended |
616 | | "EXPLAIN SELECT * FROM $table", |
617 | | "EXPLAIN EXTENDED SELECT * FROM $table", |
618 | | "EXPLAIN EXTENDED SELECT * FROM `$table`", |
619 | | |
620 | | "DESCRIBE $table", |
621 | | "DESC $table", |
622 | | "EXPLAIN $table", |
623 | | "HANDLER $table", |
624 | | |
625 | | "LOCK TABLE $table", |
626 | | "LOCK TABLES $table", |
627 | | "UNLOCK TABLE $table", |
628 | | |
629 | | "RENAME TABLE $table", |
630 | | "OPTIMIZE TABLE $table", |
631 | | "BACKUP TABLE $table", |
632 | | "RESTORE TABLE $table", |
633 | | "CHECK TABLE $table", |
634 | | "CHECKSUM TABLE $table", |
635 | | "ANALYZE TABLE $table", |
636 | | "REPAIR TABLE $table", |
637 | | |
638 | | "TRUNCATE $table", |
639 | | "TRUNCATE TABLE $table", |
640 | | |
641 | | "CREATE TABLE $table", |
642 | | "CREATE TEMPORARY TABLE $table", |
643 | | "CREATE TABLE IF NOT EXISTS $table", |
644 | | |
645 | | "ALTER TABLE $table", |
646 | | "ALTER IGNORE TABLE $table", |
647 | | |
648 | | "DROP TABLE $table", |
649 | | "DROP TABLE IF EXISTS $table", |
650 | | |
651 | | "CREATE INDEX foo(bar(20)) ON $table", |
652 | | "CREATE UNIQUE INDEX foo(bar(20)) ON $table", |
653 | | "CREATE FULLTEXT INDEX foo(bar(20)) ON $table", |
654 | | "CREATE SPATIAL INDEX foo(bar(20)) ON $table", |
655 | | |
656 | | "DROP INDEX foo ON $table", |
657 | | |
658 | | "LOAD DATA INFILE 'wp.txt' INTO TABLE $table", |
659 | | "LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table", |
660 | | "LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table", |
661 | | "LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table", |
662 | | "LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table", |
663 | | "LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table", |
664 | | |
665 | | "GRANT ALL ON TABLE $table", |
666 | | "REVOKE ALL ON TABLE $table", |
667 | | |
668 | | "SHOW COLUMNS FROM $table", |
669 | | "SHOW FULL COLUMNS FROM $table", |
670 | | "SHOW CREATE TABLE $table", |
671 | | "SHOW INDEX FROM $table", |
672 | | |
673 | | // @ticket 32763 |
674 | | "SELECT " . str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery", |
675 | | ); |
676 | | |
677 | | $querycount = count( $queries ); |
678 | | for ( $ii = 0; $ii < $querycount; $ii++ ) { |
679 | | foreach ( $more_tables as $name => $expected_name ) { |
680 | | $new_query = str_replace( $table, $name, $queries[ $ii ] ); |
681 | | $queries[] = array( $new_query, $expected_name ); |
682 | | } |
683 | | |
684 | | $queries[ $ii ] = array( $queries[ $ii ], $table ); |
685 | | } |
686 | | return $queries; |
687 | | } |
688 | | |
689 | | /** |
690 | | * @dataProvider data_get_table_from_query |
691 | | * @ticket 21212 |
692 | | */ |
693 | | function test_get_table_from_query( $query, $table ) { |
694 | | $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) ); |
695 | | } |
696 | | |
697 | | function data_get_table_from_query_false() { |
698 | | $table = 'a_test_table_name'; |
699 | | return array( |
700 | | array( "LOL THIS ISN'T EVEN A QUERY $table" ), |
701 | | ); |
702 | | } |
703 | | |
704 | | /** |
705 | | * @dataProvider data_get_table_from_query_false |
706 | | * @ticket 21212 |
707 | | */ |
708 | | function test_get_table_from_query_false( $query ) { |
709 | | $this->assertFalse( self::$_wpdb->get_table_from_query( $query ) ); |
710 | | } |
711 | | |
712 | | /** |
713 | | * @ticket 38751 |
714 | | */ |
715 | | function data_get_escaped_table_from_show_query() { |
716 | | return array( |
717 | | // Equality |
718 | | array( "SHOW TABLE STATUS WHERE Name = 'test_name'", 'test_name' ), |
719 | | array( "SHOW TABLE STATUS WHERE NAME=\"test_name\"", 'test_name' ), |
720 | | array( "SHOW TABLES WHERE Name = \"test_name\"", 'test_name' ), |
721 | | array( "SHOW FULL TABLES WHERE Name='test_name'", 'test_name' ), |
722 | | |
723 | | // LIKE |
724 | | array( "SHOW TABLE STATUS LIKE 'test\_prefix\_%'", 'test_prefix_' ), |
725 | | array( "SHOW TABLE STATUS LIKE \"test\_prefix\_%\"", 'test_prefix_' ), |
726 | | array( "SHOW TABLES LIKE 'test\_prefix\_%'", 'test_prefix_' ), |
727 | | array( "SHOW FULL TABLES LIKE \"test\_prefix\_%\"", 'test_prefix_' ), |
728 | | ); |
729 | | } |
730 | | |
731 | | /** |
732 | | * @dataProvider data_get_escaped_table_from_show_query |
733 | | * @ticket 38751 |
734 | | */ |
735 | | function test_get_escaped_table_from_show_query( $query, $table ) { |
736 | | $this->assertEquals( $table, self::$_wpdb->get_table_from_query( $query ) ); |
737 | | } |
738 | | |
739 | | /** |
740 | | * @ticket 21212 |
741 | | */ |
742 | | function data_process_field_formats() { |
743 | | $core_db_fields_no_format_specified = array( |
744 | | array( 'post_content' => 'foo', 'post_parent' => 0 ), |
745 | | null, |
746 | | array( |
747 | | 'post_content' => array( 'value' => 'foo', 'format' => '%s' ), |
748 | | 'post_parent' => array( 'value' => 0, 'format' => '%d' ), |
749 | | ) |
750 | | ); |
751 | | |
752 | | $core_db_fields_formats_specified = array( |
753 | | array( 'post_content' => 'foo', 'post_parent' => 0 ), |
754 | | array( '%d', '%s' ), // These override core field_types |
755 | | array( |
756 | | 'post_content' => array( 'value' => 'foo', 'format' => '%d' ), |
757 | | 'post_parent' => array( 'value' => 0, 'format' => '%s' ), |
758 | | ) |
759 | | ); |
760 | | |
761 | | $misc_fields_no_format_specified = array( |
762 | | array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ), |
763 | | null, |
764 | | array( |
765 | | 'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ), |
766 | | 'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ), |
767 | | ) |
768 | | ); |
769 | | |
770 | | $misc_fields_formats_specified = array( |
771 | | array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ), |
772 | | array( '%d', '%f' ), |
773 | | array( |
774 | | 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), |
775 | | 'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ), |
776 | | ) |
777 | | ); |
778 | | |
779 | | $misc_fields_insufficient_formats_specified = array( |
780 | | array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ), |
781 | | array( '%d', '%s' ), // The first format is used for the third |
782 | | array( |
783 | | 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), |
784 | | 'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ), |
785 | | 'nor_this' => array( 'value' => 1, 'format' => '%d' ), |
786 | | ) |
787 | | ); |
788 | | |
789 | | $vars = get_defined_vars(); |
790 | | // Push the variable name onto the end for assertSame $message |
791 | | foreach ( $vars as $var_name => $var ) { |
792 | | $vars[ $var_name ][] = $var_name; |
793 | | } |
794 | | return array_values( $vars ); |
795 | | } |
796 | | |
797 | | /** |
798 | | * @dataProvider data_process_field_formats |
799 | | * @ticket 21212 |
800 | | */ |
801 | | function test_process_field_formats( $data, $format, $expected, $message ) { |
802 | | $actual = self::$_wpdb->process_field_formats( $data, $format ); |
803 | | $this->assertSame( $expected, $actual, $message ); |
804 | | } |
805 | | |
806 | | /** |
807 | | * @ticket 21212 |
808 | | */ |
809 | | function test_process_fields() { |
810 | | global $wpdb; |
811 | | |
812 | | if ( $wpdb->charset ) { |
813 | | $expected_charset = $wpdb->charset; |
814 | | } else { |
815 | | $expected_charset = $wpdb->get_col_charset( $wpdb->posts, 'post_content' ); |
816 | | } |
817 | | |
818 | | if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) { |
819 | | $this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets" ); |
820 | | } |
821 | | |
822 | | $data = array( 'post_content' => '¡foo foo foo!' ); |
823 | | $expected = array( |
824 | | 'post_content' => array( |
825 | | 'value' => '¡foo foo foo!', |
826 | | 'format' => '%s', |
827 | | 'charset' => $expected_charset, |
828 | | 'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ), |
829 | | ) |
830 | | ); |
831 | | |
832 | | $this->assertSame( $expected, self::$_wpdb->process_fields( $wpdb->posts, $data, null ) ); |
833 | | } |
834 | | |
835 | | /** |
836 | | * @ticket 21212 |
837 | | * @depends test_process_fields |
838 | | */ |
839 | | function test_process_fields_on_nonexistent_table( $data ) { |
840 | | self::$_wpdb->suppress_errors( true ); |
841 | | $data = array( 'post_content' => '¡foo foo foo!' ); |
842 | | $this->assertFalse( self::$_wpdb->process_fields( 'nonexistent_table', $data, null ) ); |
843 | | self::$_wpdb->suppress_errors( false ); |
844 | | } |
845 | | |
846 | | /** |
847 | | * @ticket 21212 |
848 | | */ |
849 | | function test_pre_get_table_charset_filter() { |
850 | | add_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10, 2 ); |
851 | | $charset = self::$_wpdb->get_table_charset( 'some_table' ); |
852 | | remove_filter( 'pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10 ); |
853 | | |
854 | | $this->assertEquals( $charset, 'fake_charset' ); |
855 | | } |
856 | | function filter_pre_get_table_charset( $charset, $table ) { |
857 | | return 'fake_charset'; |
858 | | } |
859 | | |
860 | | /** |
861 | | * @ticket 21212 |
862 | | */ |
863 | | function test_pre_get_col_charset_filter() { |
864 | | add_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10, 3 ); |
865 | | $charset = self::$_wpdb->get_col_charset( 'some_table', 'some_col' ); |
866 | | remove_filter( 'pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10 ); |
867 | | |
868 | | $this->assertEquals( $charset, 'fake_col_charset' ); |
869 | | } |
870 | | function filter_pre_get_col_charset( $charset, $table, $column ) { |
871 | | return 'fake_col_charset'; |
872 | | } |
873 | | |
874 | | /** |
875 | | * @ticket 15158 |
876 | | */ |
877 | | function test_null_insert() { |
878 | | global $wpdb; |
879 | | |
880 | | $key = 'null_insert_key'; |
881 | | |
882 | | $wpdb->insert( |
883 | | $wpdb->postmeta, |
884 | | array( |
885 | | 'meta_key' => $key, |
886 | | 'meta_value' => NULL |
887 | | ), |
888 | | array( '%s', '%s' ) |
889 | | ); |
890 | | |
891 | | $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" ); |
892 | | |
893 | | $this->assertNull( $row->meta_value ); |
894 | | } |
895 | | |
896 | | /** |
897 | | * @ticket 15158 |
898 | | */ |
899 | | function test_null_update_value() { |
900 | | global $wpdb; |
901 | | |
902 | | $key = 'null_update_value_key'; |
903 | | $value = 'null_update_value_key'; |
904 | | |
905 | | $wpdb->insert( |
906 | | $wpdb->postmeta, |
907 | | array( |
908 | | 'meta_key' => $key, |
909 | | 'meta_value' => $value |
910 | | ), |
911 | | array( '%s', '%s' ) |
912 | | ); |
913 | | |
914 | | $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" ); |
915 | | |
916 | | $this->assertSame( $value, $row->meta_value ); |
917 | | |
918 | | $wpdb->update( |
919 | | $wpdb->postmeta, |
920 | | array( 'meta_value' => NULL ), |
921 | | array( |
922 | | 'meta_key' => $key, |
923 | | 'meta_value' => $value |
924 | | ), |
925 | | array( '%s' ), |
926 | | array( '%s', '%s' ) |
927 | | ); |
928 | | |
929 | | $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" ); |
930 | | |
931 | | $this->assertNull( $row->meta_value ); |
932 | | } |
933 | | |
934 | | /** |
935 | | * @ticket 15158 |
936 | | */ |
937 | | function test_null_update_where() { |
938 | | global $wpdb; |
939 | | |
940 | | $key = 'null_update_where_key'; |
941 | | $value = 'null_update_where_key'; |
942 | | |
943 | | $wpdb->insert( |
944 | | $wpdb->postmeta, |
945 | | array( |
946 | | 'meta_key' => $key, |
947 | | 'meta_value' => NULL |
948 | | ), |
949 | | array( '%s', '%s' ) |
950 | | ); |
951 | | |
952 | | $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" ); |
953 | | |
954 | | $this->assertNull( $row->meta_value ); |
955 | | |
956 | | $wpdb->update( |
957 | | $wpdb->postmeta, |
958 | | array( 'meta_value' => $value ), |
959 | | array( |
960 | | 'meta_key' => $key, |
961 | | 'meta_value' => NULL |
962 | | ), |
963 | | array( '%s' ), |
964 | | array( '%s', '%s' ) |
965 | | ); |
966 | | |
967 | | $row = $wpdb->get_row( "SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'" ); |
968 | | |
969 | | $this->assertSame( $value, $row->meta_value ); |
970 | | } |
971 | | |
972 | | /** |
973 | | * @ticket 15158 |
974 | | */ |
975 | | function test_null_delete() { |
976 | | global $wpdb; |
977 | | |
978 | | $key = 'null_update_where_key'; |
979 | | $value = 'null_update_where_key'; |
980 | | |
981 | | $wpdb->insert( |
982 | | $wpdb->postmeta, |
983 | | array( |
984 | | 'meta_key' => $key, |
985 | | 'meta_value' => NULL |
986 | | ), |
987 | | array( '%s', '%s' ) |
988 | | ); |
| 643 | END'); |
| 644 | |
| 645 | if (count($wpdb->get_results('SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`')) < 1) { |
| 646 | $wpdb->suppress_errors($suppress); |
| 647 | $this->fail('procedure could not be created (missing privileges?)'); |
| 648 | } |
| 649 | |
| 650 | $post_id = self::factory()->post->create(); |
| 651 | |
| 652 | $this->assertNotEmpty($wpdb->get_results('CALL `test_mysqli_flush_sync_procedure`')); |
| 653 | $this->assertNotEmpty($wpdb->get_results("SELECT ID FROM `{$wpdb->posts}` LIMIT 1")); |
| 654 | |
| 655 | // DROP PROCEDURE will cause a COMMIT, so we delete the post manually before that happens. |
| 656 | wp_delete_post($post_id, true); |
| 657 | |
| 658 | $wpdb->query('DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`'); |
| 659 | $wpdb->suppress_errors($suppress); |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * @ticket 21212 |
| 664 | */ |
| 665 | public function data_get_table_from_query() |
| 666 | { |
| 667 | $table = 'a_test_table_name'; |
| 668 | $more_tables = array( |
| 669 | // table_name => expected_value |
| 670 | '`a_test_db`.`another_test_table`' => 'a_test_db.another_test_table', |
| 671 | 'a-test-with-dashes' => 'a-test-with-dashes', |
| 672 | ); |
| 673 | |
| 674 | $queries = array( |
| 675 | // Basic |
| 676 | "SELECT * FROM $table", |
| 677 | "SELECT * FROM `$table`", |
| 678 | |
| 679 | "SELECT * FROM (SELECT * FROM $table) as subquery", |
| 680 | |
| 681 | "INSERT $table", |
| 682 | "INSERT IGNORE $table", |
| 683 | "INSERT IGNORE INTO $table", |
| 684 | "INSERT INTO $table", |
| 685 | "INSERT LOW_PRIORITY $table", |
| 686 | "INSERT DELAYED $table", |
| 687 | "INSERT HIGH_PRIORITY $table", |
| 688 | "INSERT LOW_PRIORITY IGNORE $table", |
| 689 | "INSERT LOW_PRIORITY INTO $table", |
| 690 | "INSERT LOW_PRIORITY IGNORE INTO $table", |
| 691 | |
| 692 | "REPLACE $table", |
| 693 | "REPLACE INTO $table", |
| 694 | "REPLACE LOW_PRIORITY $table", |
| 695 | "REPLACE DELAYED $table", |
| 696 | "REPLACE LOW_PRIORITY INTO $table", |
| 697 | |
| 698 | "UPDATE LOW_PRIORITY $table", |
| 699 | "UPDATE LOW_PRIORITY IGNORE $table", |
| 700 | |
| 701 | "DELETE $table", |
| 702 | "DELETE IGNORE $table", |
| 703 | "DELETE IGNORE FROM $table", |
| 704 | "DELETE FROM $table", |
| 705 | "DELETE LOW_PRIORITY $table", |
| 706 | "DELETE QUICK $table", |
| 707 | "DELETE IGNORE $table", |
| 708 | "DELETE LOW_PRIORITY FROM $table", |
| 709 | "DELETE a FROM $table a", |
| 710 | "DELETE `a` FROM $table a", |
| 711 | |
| 712 | // Extended |
| 713 | "EXPLAIN SELECT * FROM $table", |
| 714 | "EXPLAIN EXTENDED SELECT * FROM $table", |
| 715 | "EXPLAIN EXTENDED SELECT * FROM `$table`", |
| 716 | |
| 717 | "DESCRIBE $table", |
| 718 | "DESC $table", |
| 719 | "EXPLAIN $table", |
| 720 | "HANDLER $table", |
| 721 | |
| 722 | "LOCK TABLE $table", |
| 723 | "LOCK TABLES $table", |
| 724 | "UNLOCK TABLE $table", |
| 725 | |
| 726 | "RENAME TABLE $table", |
| 727 | "OPTIMIZE TABLE $table", |
| 728 | "BACKUP TABLE $table", |
| 729 | "RESTORE TABLE $table", |
| 730 | "CHECK TABLE $table", |
| 731 | "CHECKSUM TABLE $table", |
| 732 | "ANALYZE TABLE $table", |
| 733 | "REPAIR TABLE $table", |
| 734 | |
| 735 | "TRUNCATE $table", |
| 736 | "TRUNCATE TABLE $table", |
| 737 | |
| 738 | "CREATE TABLE $table", |
| 739 | "CREATE TEMPORARY TABLE $table", |
| 740 | "CREATE TABLE IF NOT EXISTS $table", |
| 741 | |
| 742 | "ALTER TABLE $table", |
| 743 | "ALTER IGNORE TABLE $table", |
| 744 | |
| 745 | "DROP TABLE $table", |
| 746 | "DROP TABLE IF EXISTS $table", |
| 747 | |
| 748 | "CREATE INDEX foo(bar(20)) ON $table", |
| 749 | "CREATE UNIQUE INDEX foo(bar(20)) ON $table", |
| 750 | "CREATE FULLTEXT INDEX foo(bar(20)) ON $table", |
| 751 | "CREATE SPATIAL INDEX foo(bar(20)) ON $table", |
| 752 | |
| 753 | "DROP INDEX foo ON $table", |
| 754 | |
| 755 | "LOAD DATA INFILE 'wp.txt' INTO TABLE $table", |
| 756 | "LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table", |
| 757 | "LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table", |
| 758 | "LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table", |
| 759 | "LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table", |
| 760 | "LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table", |
| 761 | |
| 762 | "GRANT ALL ON TABLE $table", |
| 763 | "REVOKE ALL ON TABLE $table", |
| 764 | |
| 765 | "SHOW COLUMNS FROM $table", |
| 766 | "SHOW FULL COLUMNS FROM $table", |
| 767 | "SHOW CREATE TABLE $table", |
| 768 | "SHOW INDEX FROM $table", |
| 769 | |
| 770 | // @ticket 32763 |
| 771 | "SELECT " . str_repeat('a', 10000) . " FROM (SELECT * FROM $table) as subquery", |
| 772 | ); |
| 773 | |
| 774 | $querycount = count($queries); |
| 775 | for ($ii = 0; $ii < $querycount; $ii++) { |
| 776 | foreach ($more_tables as $name => $expected_name) { |
| 777 | $new_query = str_replace($table, $name, $queries[ $ii ]); |
| 778 | $queries[] = array( $new_query, $expected_name ); |
| 779 | } |
| 780 | |
| 781 | $queries[ $ii ] = array( $queries[ $ii ], $table ); |
| 782 | } |
| 783 | return $queries; |
| 784 | } |
| 785 | |
| 786 | /** |
| 787 | * @dataProvider data_get_table_from_query |
| 788 | * @ticket 21212 |
| 789 | */ |
| 790 | public function test_get_table_from_query($query, $table) |
| 791 | { |
| 792 | $this->assertEquals($table, self::$_wpdb->get_table_from_query($query)); |
| 793 | } |
| 794 | |
| 795 | public function data_get_table_from_query_false() |
| 796 | { |
| 797 | $table = 'a_test_table_name'; |
| 798 | return array( |
| 799 | array( "LOL THIS ISN'T EVEN A QUERY $table" ), |
| 800 | ); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @dataProvider data_get_table_from_query_false |
| 805 | * @ticket 21212 |
| 806 | */ |
| 807 | public function test_get_table_from_query_false($query) |
| 808 | { |
| 809 | $this->assertFalse(self::$_wpdb->get_table_from_query($query)); |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * @ticket 38751 |
| 814 | */ |
| 815 | public function data_get_escaped_table_from_show_query() |
| 816 | { |
| 817 | return array( |
| 818 | // Equality |
| 819 | array( "SHOW TABLE STATUS WHERE Name = 'test_name'", 'test_name' ), |
| 820 | array( "SHOW TABLE STATUS WHERE NAME=\"test_name\"", 'test_name' ), |
| 821 | array( "SHOW TABLES WHERE Name = \"test_name\"", 'test_name' ), |
| 822 | array( "SHOW FULL TABLES WHERE Name='test_name'", 'test_name' ), |
| 823 | |
| 824 | // LIKE |
| 825 | array( "SHOW TABLE STATUS LIKE 'test\_prefix\_%'", 'test_prefix_' ), |
| 826 | array( "SHOW TABLE STATUS LIKE \"test\_prefix\_%\"", 'test_prefix_' ), |
| 827 | array( "SHOW TABLES LIKE 'test\_prefix\_%'", 'test_prefix_' ), |
| 828 | array( "SHOW FULL TABLES LIKE \"test\_prefix\_%\"", 'test_prefix_' ), |
| 829 | ); |
| 830 | } |
| 831 | |
| 832 | /** |
| 833 | * @dataProvider data_get_escaped_table_from_show_query |
| 834 | * @ticket 38751 |
| 835 | */ |
| 836 | public function test_get_escaped_table_from_show_query($query, $table) |
| 837 | { |
| 838 | $this->assertEquals($table, self::$_wpdb->get_table_from_query($query)); |
| 839 | } |
| 840 | |
| 841 | /** |
| 842 | * @ticket 21212 |
| 843 | */ |
| 844 | public function data_process_field_formats() |
| 845 | { |
| 846 | $core_db_fields_no_format_specified = array( |
| 847 | array( 'post_content' => 'foo', 'post_parent' => 0 ), |
| 848 | null, |
| 849 | array( |
| 850 | 'post_content' => array( 'value' => 'foo', 'format' => '%s' ), |
| 851 | 'post_parent' => array( 'value' => 0, 'format' => '%d' ), |
| 852 | ) |
| 853 | ); |
| 854 | |
| 855 | $core_db_fields_formats_specified = array( |
| 856 | array( 'post_content' => 'foo', 'post_parent' => 0 ), |
| 857 | array( '%d', '%s' ), // These override core field_types |
| 858 | array( |
| 859 | 'post_content' => array( 'value' => 'foo', 'format' => '%d' ), |
| 860 | 'post_parent' => array( 'value' => 0, 'format' => '%s' ), |
| 861 | ) |
| 862 | ); |
| 863 | |
| 864 | $misc_fields_no_format_specified = array( |
| 865 | array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ), |
| 866 | null, |
| 867 | array( |
| 868 | 'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ), |
| 869 | 'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ), |
| 870 | ) |
| 871 | ); |
| 872 | |
| 873 | $misc_fields_formats_specified = array( |
| 874 | array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ), |
| 875 | array( '%d', '%f' ), |
| 876 | array( |
| 877 | 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), |
| 878 | 'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ), |
| 879 | ) |
| 880 | ); |
| 881 | |
| 882 | $misc_fields_insufficient_formats_specified = array( |
| 883 | array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ), |
| 884 | array( '%d', '%s' ), // The first format is used for the third |
| 885 | array( |
| 886 | 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), |
| 887 | 'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ), |
| 888 | 'nor_this' => array( 'value' => 1, 'format' => '%d' ), |
| 889 | ) |
| 890 | ); |
| 891 | |
| 892 | $vars = get_defined_vars(); |
| 893 | // Push the variable name onto the end for assertSame $message |
| 894 | foreach ($vars as $var_name => $var) { |
| 895 | $vars[ $var_name ][] = $var_name; |
| 896 | } |
| 897 | return array_values($vars); |
| 898 | } |
| 899 | |
| 900 | /** |
| 901 | * @dataProvider data_process_field_formats |
| 902 | * @ticket 21212 |
| 903 | */ |
| 904 | public function test_process_field_formats($data, $format, $expected, $message) |
| 905 | { |
| 906 | $actual = self::$_wpdb->process_field_formats($data, $format); |
| 907 | $this->assertSame($expected, $actual, $message); |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @ticket 21212 |
| 912 | */ |
| 913 | public function test_process_fields() |
| 914 | { |
| 915 | global $wpdb; |
| 916 | |
| 917 | if ($wpdb->charset) { |
| 918 | $expected_charset = $wpdb->charset; |
| 919 | } else { |
| 920 | $expected_charset = $wpdb->get_col_charset($wpdb->posts, 'post_content'); |
| 921 | } |
| 922 | |
| 923 | if (! in_array($expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ))) { |
| 924 | $this->markTestSkipped("This test only works with utf8, utf8mb4 or latin1 character sets"); |
| 925 | } |
| 926 | |
| 927 | $data = array( 'post_content' => '¡foo foo foo!' ); |
| 928 | $expected = array( |
| 929 | 'post_content' => array( |
| 930 | 'value' => '¡foo foo foo!', |
| 931 | 'format' => '%s', |
| 932 | 'charset' => $expected_charset, |
| 933 | 'length' => $wpdb->get_col_length($wpdb->posts, 'post_content'), |
| 934 | ) |
| 935 | ); |
| 936 | |
| 937 | $this->assertSame($expected, self::$_wpdb->process_fields($wpdb->posts, $data, null)); |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * @ticket 21212 |
| 942 | * @depends test_process_fields |
| 943 | */ |
| 944 | public function test_process_fields_on_nonexistent_table($data) |
| 945 | { |
| 946 | self::$_wpdb->suppress_errors(true); |
| 947 | $data = array( 'post_content' => '¡foo foo foo!' ); |
| 948 | $this->assertFalse(self::$_wpdb->process_fields('nonexistent_table', $data, null)); |
| 949 | self::$_wpdb->suppress_errors(false); |
| 950 | } |
| 951 | |
| 952 | /** |
| 953 | * @ticket 21212 |
| 954 | */ |
| 955 | public function test_pre_get_table_charset_filter() |
| 956 | { |
| 957 | add_filter('pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10, 2); |
| 958 | $charset = self::$_wpdb->get_table_charset('some_table'); |
| 959 | remove_filter('pre_get_table_charset', array( $this, 'filter_pre_get_table_charset' ), 10); |
| 960 | |
| 961 | $this->assertEquals($charset, 'fake_charset'); |
| 962 | } |
| 963 | public function filter_pre_get_table_charset($charset, $table) |
| 964 | { |
| 965 | return 'fake_charset'; |
| 966 | } |
| 967 | |
| 968 | /** |
| 969 | * @ticket 21212 |
| 970 | */ |
| 971 | public function test_pre_get_col_charset_filter() |
| 972 | { |
| 973 | add_filter('pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10, 3); |
| 974 | $charset = self::$_wpdb->get_col_charset('some_table', 'some_col'); |
| 975 | remove_filter('pre_get_col_charset', array( $this, 'filter_pre_get_col_charset' ), 10); |
| 976 | |
| 977 | $this->assertEquals($charset, 'fake_col_charset'); |
| 978 | } |
| 979 | public function filter_pre_get_col_charset($charset, $table, $column) |
| 980 | { |
| 981 | return 'fake_col_charset'; |
| 982 | } |
| 983 | |
| 984 | /** |
| 985 | * @ticket 15158 |
| 986 | */ |
| 987 | public function test_null_insert() |
| 988 | { |
| 989 | global $wpdb; |
| 990 | |
| 991 | $key = 'null_insert_key'; |
| 992 | |
| 993 | $wpdb->insert( |
| 994 | $wpdb->postmeta, |
| 995 | array( |
| 996 | 'meta_key' => $key, |
| 997 | 'meta_value' => null |
| 998 | ), |
| 999 | array( '%s', '%s' ) |
| 1000 | ); |
| 1001 | |
| 1002 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1003 | |
| 1004 | $this->assertNull($row->meta_value); |
| 1005 | } |
| 1006 | |
| 1007 | /** |
| 1008 | * @ticket 15158 |
| 1009 | */ |
| 1010 | public function test_null_update_value() |
| 1011 | { |
| 1012 | global $wpdb; |
| 1013 | |
| 1014 | $key = 'null_update_value_key'; |
| 1015 | $value = 'null_update_value_key'; |
| 1016 | |
| 1017 | $wpdb->insert( |
| 1018 | $wpdb->postmeta, |
| 1019 | array( |
| 1020 | 'meta_key' => $key, |
| 1021 | 'meta_value' => $value |
| 1022 | ), |
| 1023 | array( '%s', '%s' ) |
| 1024 | ); |
| 1025 | |
| 1026 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1027 | |
| 1028 | $this->assertSame($value, $row->meta_value); |
| 1029 | |
| 1030 | $wpdb->update( |
| 1031 | $wpdb->postmeta, |
| 1032 | array( 'meta_value' => null ), |
| 1033 | array( |
| 1034 | 'meta_key' => $key, |
| 1035 | 'meta_value' => $value |
| 1036 | ), |
| 1037 | array( '%s' ), |
| 1038 | array( '%s', '%s' ) |
| 1039 | ); |
| 1040 | |
| 1041 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1042 | |
| 1043 | $this->assertNull($row->meta_value); |
| 1044 | } |
| 1045 | |
| 1046 | /** |
| 1047 | * @ticket 15158 |
| 1048 | */ |
| 1049 | public function test_null_update_where() |
| 1050 | { |
| 1051 | global $wpdb; |
| 1052 | |
| 1053 | $key = 'null_update_where_key'; |
| 1054 | $value = 'null_update_where_key'; |
| 1055 | |
| 1056 | $wpdb->insert( |
| 1057 | $wpdb->postmeta, |
| 1058 | array( |
| 1059 | 'meta_key' => $key, |
| 1060 | 'meta_value' => null |
| 1061 | ), |
| 1062 | array( '%s', '%s' ) |
| 1063 | ); |
| 1064 | |
| 1065 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1066 | |
| 1067 | $this->assertNull($row->meta_value); |
| 1068 | |
| 1069 | $wpdb->update( |
| 1070 | $wpdb->postmeta, |
| 1071 | array( 'meta_value' => $value ), |
| 1072 | array( |
| 1073 | 'meta_key' => $key, |
| 1074 | 'meta_value' => null |
| 1075 | ), |
| 1076 | array( '%s' ), |
| 1077 | array( '%s', '%s' ) |
| 1078 | ); |
| 1079 | |
| 1080 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1081 | |
| 1082 | $this->assertSame($value, $row->meta_value); |
| 1083 | } |
| 1084 | |
| 1085 | /** |
| 1086 | * @ticket 15158 |
| 1087 | */ |
| 1088 | public function test_null_delete() |
| 1089 | { |
| 1090 | global $wpdb; |
| 1091 | |
| 1092 | $key = 'null_update_where_key'; |
| 1093 | $value = 'null_update_where_key'; |
| 1094 | |
| 1095 | $wpdb->insert( |
| 1096 | $wpdb->postmeta, |
| 1097 | array( |
| 1098 | 'meta_key' => $key, |
| 1099 | 'meta_value' => null |
| 1100 | ), |
| 1101 | array( '%s', '%s' ) |
| 1102 | ); |
| 1103 | |
| 1104 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1105 | |
| 1106 | $this->assertNull($row->meta_value); |
| 1107 | |
| 1108 | $wpdb->delete( |
| 1109 | $wpdb->postmeta, |
| 1110 | array( |
| 1111 | 'meta_key' => $key, |
| 1112 | 'meta_value' => null |
| 1113 | ), |
| 1114 | array( '%s', '%s' ) |
| 1115 | ); |
| 1116 | |
| 1117 | $row = $wpdb->get_row("SELECT * FROM $wpdb->postmeta WHERE meta_key='$key'"); |
| 1118 | |
| 1119 | $this->assertNull($row); |
| 1120 | } |
| 1121 | |
| 1122 | /** |
| 1123 | * @ticket 34903 |
| 1124 | */ |
| 1125 | public function test_close() |
| 1126 | { |
| 1127 | global $wpdb; |
| 1128 | |
| 1129 | $this->assertTrue($wpdb->close()); |
| 1130 | $this->assertFalse($wpdb->close()); |
| 1131 | |
| 1132 | $this->assertFalse($wpdb->ready); |
| 1133 | $this->assertFalse($wpdb->has_connected); |
| 1134 | |
| 1135 | $wpdb->check_connection(); |
| 1136 | |
| 1137 | $this->assertTrue($wpdb->close()); |
| 1138 | |
| 1139 | $wpdb->check_connection(); |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * @ticket 36917 |
| 1144 | */ |
| 1145 | public function test_charset_not_determined_when_disconnected() |
| 1146 | { |
| 1147 | global $wpdb; |
| 1148 | |
| 1149 | $charset = 'utf8'; |
| 1150 | $collate = 'this_isnt_a_collation'; |
| 1151 | |
| 1152 | $wpdb->close(); |
| 1153 | |
| 1154 | $result = $wpdb->determine_charset($charset, $collate); |
| 1155 | |
| 1156 | $this->assertSame(compact('charset', 'collate'), $result); |
| 1157 | |
| 1158 | $wpdb->check_connection(); |
| 1159 | } |
1051 | | if ( ! $wpdb->has_cap( 'utf8mb4' ) ) { |
1052 | | $this->markTestSkipped( 'This test requires utf8mb4 support.' ); |
1053 | | } |
1054 | | |
1055 | | $charset = 'utf8'; |
1056 | | $collate = 'utf8_general_ci'; |
1057 | | |
1058 | | $result = $wpdb->determine_charset( $charset, $collate ); |
1059 | | |
1060 | | $this->assertSame( 'utf8mb4', $result['charset'] ); |
1061 | | } |
1062 | | |
1063 | | /** |
1064 | | * @ticket 32105 |
1065 | | * @ticket 36917 |
1066 | | */ |
1067 | | function test_collate_switched_to_utf8mb4_520() { |
1068 | | global $wpdb; |
1069 | | |
1070 | | if ( ! $wpdb->has_cap( 'utf8mb4_520' ) ) { |
1071 | | $this->markTestSkipped( 'This test requires utf8mb4_520 support.' ); |
1072 | | } |
1073 | | |
1074 | | $charset = 'utf8'; |
1075 | | $collate = 'utf8_general_ci'; |
1076 | | |
1077 | | $result = $wpdb->determine_charset( $charset, $collate ); |
1078 | | |
1079 | | $this->assertSame( 'utf8mb4_unicode_520_ci', $result['collate'] ); |
1080 | | } |
1081 | | |
1082 | | /** |
1083 | | * @ticket 32405 |
1084 | | * @ticket 36917 |
1085 | | */ |
1086 | | function test_non_unicode_collations() { |
1087 | | global $wpdb; |
1088 | | |
1089 | | if ( ! $wpdb->has_cap( 'utf8mb4' ) ) { |
1090 | | $this->markTestSkipped( 'This test requires utf8mb4 support.' ); |
1091 | | } |
1092 | | |
1093 | | $charset = 'utf8'; |
1094 | | $collate = 'utf8_swedish_ci'; |
1095 | | |
1096 | | $result = $wpdb->determine_charset( $charset, $collate ); |
1097 | | |
1098 | | $this->assertSame( 'utf8mb4_swedish_ci', $result['collate'] ); |
1099 | | } |
1100 | | |
1101 | | /** |
1102 | | * @ticket 37982 |
1103 | | */ |
1104 | | function test_charset_switched_to_utf8() { |
1105 | | global $wpdb; |
1106 | | |
1107 | | if ( $wpdb->has_cap( 'utf8mb4' ) ) { |
1108 | | $this->markTestSkipped( 'This test requires utf8mb4 to not be supported.' ); |
1109 | | } |
1110 | | |
1111 | | $charset = 'utf8mb4'; |
1112 | | $collate = 'utf8mb4_general_ci'; |
1113 | | |
1114 | | $result = $wpdb->determine_charset( $charset, $collate ); |
1115 | | |
1116 | | $this->assertSame( 'utf8', $result['charset'] ); |
1117 | | $this->assertSame( 'utf8_general_ci', $result['collate'] ); |
1118 | | } |
1119 | | |
1120 | | /** |
1121 | | * |
1122 | | */ |
1123 | | function test_prepare_with_unescaped_percents() { |
1124 | | global $wpdb; |
1125 | | |
1126 | | $sql = $wpdb->prepare( '%d %1$d %%% %', 1 ); |
1127 | | $this->assertEquals( '1 %1$d %% %', $sql ); |
1128 | | } |
1129 | | |
1130 | | /** |
1131 | | * @dataProvider parse_db_host_data_provider |
1132 | | * @ticket 41722 |
1133 | | */ |
1134 | | public function test_parse_db_host( $host_string, $expect_bail, $host, $port, $socket, $is_ipv6 ) { |
1135 | | global $wpdb; |
1136 | | $data = $wpdb->parse_db_host( $host_string ); |
1137 | | if ( $expect_bail ) { |
1138 | | $this->assertFalse( $data ); |
1139 | | } else { |
1140 | | $this->assertInternalType( 'array', $data ); |
1141 | | |
1142 | | list( $parsed_host, $parsed_port, $parsed_socket, $parsed_is_ipv6 ) = $data; |
1143 | | |
1144 | | $this->assertEquals( $host, $parsed_host ); |
1145 | | $this->assertEquals( $port, $parsed_port ); |
1146 | | $this->assertEquals( $socket, $parsed_socket ); |
1147 | | $this->assertEquals( $is_ipv6, $parsed_is_ipv6 ); |
1148 | | } |
1149 | | } |
1150 | | |
1151 | | public function parse_db_host_data_provider() { |
1152 | | return array( |
1153 | | array( |
1154 | | '', // DB_HOST |
1155 | | false, // Expect parse_db_host to bail for this hostname |
1156 | | null, // Parsed host |
1157 | | null, // Parsed port |
1158 | | null, // Parsed socket |
1159 | | false, // is_ipv6 |
1160 | | ), |
1161 | | array( |
1162 | | ':3306', |
1163 | | false, |
1164 | | null, |
1165 | | '3306', |
1166 | | null, |
1167 | | false, |
1168 | | ), |
1169 | | array( |
1170 | | ':/tmp/mysql.sock', |
1171 | | false, |
1172 | | null, |
1173 | | null, |
1174 | | '/tmp/mysql.sock', |
1175 | | false, |
1176 | | ), |
1177 | | array( |
1178 | | '127.0.0.1', |
1179 | | false, |
1180 | | '127.0.0.1', |
1181 | | null, |
1182 | | null, |
1183 | | false, |
1184 | | ), |
1185 | | array( |
1186 | | '127.0.0.1:3306', |
1187 | | false, |
1188 | | '127.0.0.1', |
1189 | | '3306', |
1190 | | null, |
1191 | | false, |
1192 | | ), |
1193 | | array( |
1194 | | 'example.com', |
1195 | | false, |
1196 | | 'example.com', |
1197 | | null, |
1198 | | null, |
1199 | | false, |
1200 | | ), |
1201 | | array( |
1202 | | 'example.com:3306', |
1203 | | false, |
1204 | | 'example.com', |
1205 | | '3306', |
1206 | | null, |
1207 | | false, |
1208 | | ), |
1209 | | array( |
1210 | | 'localhost', |
1211 | | false, |
1212 | | 'localhost', |
1213 | | null, |
1214 | | null, |
1215 | | false, |
1216 | | ), |
1217 | | array( |
1218 | | 'localhost:/tmp/mysql.sock', |
1219 | | false, |
1220 | | 'localhost', |
1221 | | null, |
1222 | | '/tmp/mysql.sock', |
1223 | | false, |
1224 | | ), |
1225 | | array( |
1226 | | '0000:0000:0000:0000:0000:0000:0000:0001', |
1227 | | false, |
1228 | | '0000:0000:0000:0000:0000:0000:0000:0001', |
1229 | | null, |
1230 | | null, |
1231 | | true, |
1232 | | ), |
1233 | | array( |
1234 | | '::1', |
1235 | | false, |
1236 | | '::1', |
1237 | | null, |
1238 | | null, |
1239 | | true, |
1240 | | ), |
1241 | | array( |
1242 | | '[::1]', |
1243 | | false, |
1244 | | '::1', |
1245 | | null, |
1246 | | null, |
1247 | | true, |
1248 | | ), |
1249 | | array( |
1250 | | '[::1]:3306', |
1251 | | false, |
1252 | | '::1', |
1253 | | '3306', |
1254 | | null, |
1255 | | true, |
1256 | | ), |
1257 | | array( |
1258 | | '2001:0db8:0000:0000:0000:ff00:0042:8329', |
1259 | | false, |
1260 | | '2001:0db8:0000:0000:0000:ff00:0042:8329', |
1261 | | null, |
1262 | | null, |
1263 | | true, |
1264 | | ), |
1265 | | array( |
1266 | | '2001:db8:0:0:0:ff00:42:8329', |
1267 | | false, |
1268 | | '2001:db8:0:0:0:ff00:42:8329', |
1269 | | null, |
1270 | | null, |
1271 | | true, |
1272 | | ), |
1273 | | array( |
1274 | | '2001:db8::ff00:42:8329', |
1275 | | false, |
1276 | | '2001:db8::ff00:42:8329', |
1277 | | null, |
1278 | | null, |
1279 | | true, |
1280 | | ), |
1281 | | array( |
1282 | | '?::', |
1283 | | true, |
1284 | | null, |
1285 | | null, |
1286 | | null, |
1287 | | false, |
1288 | | ), |
1289 | | ); |
1290 | | } |
| 1231 | $charset = 'utf8mb4'; |
| 1232 | $collate = 'utf8mb4_general_ci'; |
| 1233 | |
| 1234 | $result = $wpdb->determine_charset($charset, $collate); |
| 1235 | |
| 1236 | $this->assertSame('utf8', $result['charset']); |
| 1237 | $this->assertSame('utf8_general_ci', $result['collate']); |
| 1238 | } |
| 1239 | |
| 1240 | /** |
| 1241 | * |
| 1242 | */ |
| 1243 | public function test_prepare_with_unescaped_percents() |
| 1244 | { |
| 1245 | global $wpdb; |
| 1246 | |
| 1247 | $sql = $wpdb->prepare('%d %1$d %%% %', 1); |
| 1248 | $this->assertEquals('1 %1$d %% %', $sql); |
| 1249 | } |
| 1250 | |
| 1251 | /** |
| 1252 | * @dataProvider parse_db_host_data_provider |
| 1253 | * @ticket 41722 |
| 1254 | */ |
| 1255 | public function test_parse_db_host($host_string, $expect_bail, $host, $port, $socket, $is_ipv6) |
| 1256 | { |
| 1257 | global $wpdb; |
| 1258 | $data = $wpdb->parse_db_host($host_string); |
| 1259 | if ($expect_bail) { |
| 1260 | $this->assertFalse($data); |
| 1261 | } else { |
| 1262 | $this->assertInternalType('array', $data); |
| 1263 | |
| 1264 | list($parsed_host, $parsed_port, $parsed_socket, $parsed_is_ipv6) = $data; |
| 1265 | |
| 1266 | $this->assertEquals($host, $parsed_host); |
| 1267 | $this->assertEquals($port, $parsed_port); |
| 1268 | $this->assertEquals($socket, $parsed_socket); |
| 1269 | $this->assertEquals($is_ipv6, $parsed_is_ipv6); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | public function parse_db_host_data_provider() |
| 1274 | { |
| 1275 | return array( |
| 1276 | array( |
| 1277 | '', // DB_HOST |
| 1278 | false, // Expect parse_db_host to bail for this hostname |
| 1279 | null, // Parsed host |
| 1280 | null, // Parsed port |
| 1281 | null, // Parsed socket |
| 1282 | false, // is_ipv6 |
| 1283 | ), |
| 1284 | array( |
| 1285 | ':3306', |
| 1286 | false, |
| 1287 | null, |
| 1288 | '3306', |
| 1289 | null, |
| 1290 | false, |
| 1291 | ), |
| 1292 | array( |
| 1293 | ':/tmp/mysql.sock', |
| 1294 | false, |
| 1295 | null, |
| 1296 | null, |
| 1297 | '/tmp/mysql.sock', |
| 1298 | false, |
| 1299 | ), |
| 1300 | array( |
| 1301 | '127.0.0.1', |
| 1302 | false, |
| 1303 | '127.0.0.1', |
| 1304 | null, |
| 1305 | null, |
| 1306 | false, |
| 1307 | ), |
| 1308 | array( |
| 1309 | '127.0.0.1:3306', |
| 1310 | false, |
| 1311 | '127.0.0.1', |
| 1312 | '3306', |
| 1313 | null, |
| 1314 | false, |
| 1315 | ), |
| 1316 | array( |
| 1317 | 'example.com', |
| 1318 | false, |
| 1319 | 'example.com', |
| 1320 | null, |
| 1321 | null, |
| 1322 | false, |
| 1323 | ), |
| 1324 | array( |
| 1325 | 'example.com:3306', |
| 1326 | false, |
| 1327 | 'example.com', |
| 1328 | '3306', |
| 1329 | null, |
| 1330 | false, |
| 1331 | ), |
| 1332 | array( |
| 1333 | 'localhost', |
| 1334 | false, |
| 1335 | 'localhost', |
| 1336 | null, |
| 1337 | null, |
| 1338 | false, |
| 1339 | ), |
| 1340 | array( |
| 1341 | 'localhost:/tmp/mysql.sock', |
| 1342 | false, |
| 1343 | 'localhost', |
| 1344 | null, |
| 1345 | '/tmp/mysql.sock', |
| 1346 | false, |
| 1347 | ), |
| 1348 | array( |
| 1349 | '0000:0000:0000:0000:0000:0000:0000:0001', |
| 1350 | false, |
| 1351 | '0000:0000:0000:0000:0000:0000:0000:0001', |
| 1352 | null, |
| 1353 | null, |
| 1354 | true, |
| 1355 | ), |
| 1356 | array( |
| 1357 | '::1', |
| 1358 | false, |
| 1359 | '::1', |
| 1360 | null, |
| 1361 | null, |
| 1362 | true, |
| 1363 | ), |
| 1364 | array( |
| 1365 | '[::1]', |
| 1366 | false, |
| 1367 | '::1', |
| 1368 | null, |
| 1369 | null, |
| 1370 | true, |
| 1371 | ), |
| 1372 | array( |
| 1373 | '[::1]:3306', |
| 1374 | false, |
| 1375 | '::1', |
| 1376 | '3306', |
| 1377 | null, |
| 1378 | true, |
| 1379 | ), |
| 1380 | array( |
| 1381 | '2001:0db8:0000:0000:0000:ff00:0042:8329', |
| 1382 | false, |
| 1383 | '2001:0db8:0000:0000:0000:ff00:0042:8329', |
| 1384 | null, |
| 1385 | null, |
| 1386 | true, |
| 1387 | ), |
| 1388 | array( |
| 1389 | '2001:db8:0:0:0:ff00:42:8329', |
| 1390 | false, |
| 1391 | '2001:db8:0:0:0:ff00:42:8329', |
| 1392 | null, |
| 1393 | null, |
| 1394 | true, |
| 1395 | ), |
| 1396 | array( |
| 1397 | '2001:db8::ff00:42:8329', |
| 1398 | false, |
| 1399 | '2001:db8::ff00:42:8329', |
| 1400 | null, |
| 1401 | null, |
| 1402 | true, |
| 1403 | ), |
| 1404 | array( |
| 1405 | '?::', |
| 1406 | true, |
| 1407 | null, |
| 1408 | null, |
| 1409 | null, |
| 1410 | false, |
| 1411 | ), |
| 1412 | ); |
| 1413 | } |