Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
1868 lines (1613 loc) · 45.2 KB

File metadata and controls

1868 lines (1613 loc) · 45.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Test WPDB methods
*
* @group wpdb
*/
class Tests_DB extends WP_UnitTestCase {
/**
* Query log
*
* @var array
*/
protected $_queries = array();
/**
* Our special WPDB
*
* @var resource
*/
protected static $_wpdb;
public static function set_up_before_class() {
parent::set_up_before_class();
self::$_wpdb = new WpdbExposedMethodsForTesting();
}
/**
* Set up the test fixture
*/
public function set_up() {
parent::set_up();
$this->_queries = array();
add_filter( 'query', array( $this, 'query_filter' ) );
}
/**
* Log each query
*
* @param string $sql
* @return string
*/
public function query_filter( $sql ) {
$this->_queries[] = $sql;
return $sql;
}
/**
* Test that WPDB will reconnect when the DB link dies
*
* @ticket 5932
*/
public function test_db_reconnect() {
global $wpdb;
$var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
$this->assertGreaterThan( 0, $var );
$wpdb->close();
$var = $wpdb->get_var( "SELECT ID FROM $wpdb->users LIMIT 1" );
// Ensure all database handles have been properly reconnected after this test.
$wpdb->db_connect();
self::$_wpdb->db_connect();
$this->assertGreaterThan( 0, $var );
}
/**
* Test that floats formatted as "0,700" get sanitized properly by wpdb
*
* @global mixed $wpdb
*
* @ticket 19861
*/
public function test_locale_floats() {
global $wpdb;
// Save the current locale settings.
$current_locales = explode( ';', setlocale( LC_ALL, 0 ) );
// Switch to a locale using comma as a decimal point separator.
$flag = setlocale( LC_ALL, 'ru_RU.utf8', 'rus', 'fr_FR.utf8', 'fr_FR', 'de_DE.utf8', 'de_DE', 'es_ES.utf8', 'es_ES' );
if ( false === $flag ) {
$this->markTestSkipped( 'No European locales available for testing.' );
}
// Try an update query.
$wpdb->suppress_errors( true );
$wpdb->update(
'test_table',
array( 'float_column' => 0.7 ),
array( 'meta_id' => 5 ),
array( '%f' ),
array( '%d' )
);
$wpdb->suppress_errors( false );
// Ensure the float isn't 0,700.
$this->assertStringContainsString( '0.700', array_pop( $this->_queries ) );
// Try a prepare.
$sql = $wpdb->prepare( 'UPDATE test_table SET float_column = %f AND meta_id = %d', 0.7, 5 );
$this->assertStringContainsString( '0.700', $sql );
// Restore locale settings.
foreach ( $current_locales as $locale_setting ) {
if ( false !== strpos( $locale_setting, '=' ) ) {
list( $category, $locale ) = explode( '=', $locale_setting );
if ( defined( $category ) ) {
setlocale( constant( $category ), $locale );
}
} else {
setlocale( LC_ALL, $locale_setting );
}
}
}
/**
* @ticket 10041
*/
public function test_esc_like() {
global $wpdb;
$inputs = array(
'howdy%', // Single percent.
'howdy_', // Single underscore.
'howdy\\', // Single slash.
'howdy\\howdy%howdy_', // The works.
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?', // Plain text.
);
$expected = array(
'howdy\\%',
'howdy\\_',
'howdy\\\\',
'howdy\\\\howdy\\%howdy\\_',
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
);
foreach ( $inputs as $key => $input ) {
$this->assertSame( $expected[ $key ], $wpdb->esc_like( $input ) );
}
}
/**
* Test LIKE Queries
*
* Make sure $wpdb is fully compatible with esc_like() by testing the identity of various strings.
* When escaped properly, a string literal is always LIKE itself (1)
* and never LIKE any other string literal (0) no matter how crazy the SQL looks.
*
* @ticket 10041
* @dataProvider data_like_query
* @param $data string The haystack, raw.
* @param $like string The like phrase, raw.
* @param $result string The expected comparison result; '1' = true, '0' = false
*/
public function test_like_query( $data, $like, $result ) {
global $wpdb;
return $this->assertSame( $result, $wpdb->get_var( $wpdb->prepare( 'SELECT %s LIKE %s', $data, $wpdb->esc_like( $like ) ) ) );
}
public function data_like_query() {
return array(
array(
'aaa',
'aaa',
'1',
),
array(
'a\\aa', // SELECT 'a\\aa' # This represents a\aa in both languages.
'a\\aa', // LIKE 'a\\\\aa'
'1',
),
array(
'a%aa',
'a%aa',
'1',
),
array(
'aaaa',
'a%aa',
'0',
),
array(
'a\\%aa', // SELECT 'a\\%aa'
'a\\%aa', // LIKE 'a\\\\\\%aa' # The PHP literal would be "LIKE 'a\\\\\\\\\\\\%aa'". This is why we need reliable escape functions!
'1',
),
array(
'a%aa',
'a\\%aa',
'0',
),
array(
'a\\%aa',
'a%aa',
'0',
),
array(
'a_aa',
'a_aa',
'1',
),
array(
'aaaa',
'a_aa',
'0',
),
array(
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
'1',
),
);
}
/**
* @ticket 18510
*/
public function test_wpdb_supposedly_protected_properties() {
global $wpdb;
$this->assertNotEmpty( $wpdb->dbh );
$dbh = $wpdb->dbh;
$this->assertNotEmpty( $dbh );
$this->assertTrue( isset( $wpdb->dbh ) ); // Test __isset().
unset( $wpdb->dbh );
$this->assertTrue( empty( $wpdb->dbh ) );
$wpdb->dbh = $dbh;
$this->assertNotEmpty( $wpdb->dbh );
}
/**
* @ticket 21212
*/
public function test_wpdb_actually_protected_properties() {
global $wpdb;
$new_meta = "HAHA I HOPE THIS DOESN'T WORK";
$col_meta = $wpdb->col_meta;
$wpdb->col_meta = $new_meta;
$this->assertNotEquals( $col_meta, $new_meta );
$this->assertSame( $col_meta, $wpdb->col_meta );
}
/**
* @ticket 18510
*/
public function test_wpdb_nonexistent_properties() {
global $wpdb;
$this->assertTrue( empty( $wpdb->nonexistent_property ) );
$wpdb->nonexistent_property = true;
$this->assertTrue( $wpdb->nonexistent_property );
$this->assertTrue( isset( $wpdb->nonexistent_property ) );
unset( $wpdb->nonexistent_property );
$this->assertTrue( empty( $wpdb->nonexistent_property ) );
}
/**
* Test that an escaped %%f is not altered
*
* @ticket 19861
*/
public function test_double_escaped_placeholders() {
global $wpdb;
$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' );
$this->assertStringContainsString( $wpdb->placeholder_escape(), $sql );
$sql = $wpdb->remove_placeholder_escape( $sql );
$this->assertSame( "UPDATE test_table SET string_column = '%f is a float, %d is an int 3, %s is a string', field = '4'", $sql );
}
/**
* Test that SQL modes are set correctly
*
* @ticket 26847
*/
public function test_set_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'NO_AUTO_VALUE_ON_ZERO' );
$wpdb->set_sql_mode( $new_modes );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertSameSets( $new_modes, explode( ',', $check_new_modes ) );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
/**
* Test that incompatible SQL modes are blocked
*
* @ticket 26847
*/
public function test_set_incompatible_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'NO_ZERO_DATE', 'NO_AUTO_VALUE_ON_ZERO' );
$wpdb->set_sql_mode( $new_modes );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertNotContains( 'NO_ZERO_DATE', explode( ',', $check_new_modes ) );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
/**
* Test that incompatible SQL modes can be changed
*
* @ticket 26847
*/
public function test_set_allowed_incompatible_sql_mode() {
global $wpdb;
$current_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$new_modes = array( 'IGNORE_SPACE', 'ONLY_FULL_GROUP_BY', 'NO_AUTO_VALUE_ON_ZERO' );
add_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1, 1 );
$wpdb->set_sql_mode( $new_modes );
remove_filter( 'incompatible_sql_modes', array( $this, 'filter_allowed_incompatible_sql_mode' ), 1 );
$check_new_modes = $wpdb->get_var( 'SELECT @@SESSION.sql_mode;' );
$this->assertContains( 'ONLY_FULL_GROUP_BY', explode( ',', $check_new_modes ) );
$wpdb->set_sql_mode( explode( ',', $current_modes ) );
}
public function filter_allowed_incompatible_sql_mode( $modes ) {
$pos = array_search( 'ONLY_FULL_GROUP_BY', $modes, true );
$this->assertGreaterThanOrEqual( 0, $pos );
if ( false === $pos ) {
return $modes;
}
unset( $modes[ $pos ] );
return $modes;
}
/**
* @ticket 25604
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_without_arguments() {
global $wpdb;
$id = 0;
// This, obviously, is an incorrect prepare.
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = $id", $id );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0", $prepared );
}
public function test_prepare_sprintf() {
global $wpdb;
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, 'admin' );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
}
/**
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_sprintf_invalid_args() {
global $wpdb;
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( 'admin' ) );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), 'admin' );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
}
public function test_prepare_vsprintf() {
global $wpdb;
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, 'admin' ) );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
}
/**
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_vsprintf_invalid_args() {
global $wpdb;
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( 'admin' ) ) );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), 'admin' ) );
$this->assertSame( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
}
/**
* @ticket 42040
* @dataProvider data_prepare_incorrect_arg_count
* @expectedIncorrectUsage wpdb::prepare
*/
public function test_prepare_incorrect_arg_count( $query, $args, $expected ) {
global $wpdb;
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged,WordPress.DB.PreparedSQL
$prepared = @$wpdb->prepare( $query, ...$args );
$this->assertSame( $expected, $prepared );
}
public function data_prepare_incorrect_arg_count() {
global $wpdb;
return array(
array(
"SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", // Query.
array( 1, 'admin', 'extra-arg' ), // ::prepare() args, to be passed via call_user_func_array().
"SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", // Expected output.
),
array(
"SELECT * FROM $wpdb->users WHERE id = %%%d AND user_login = %s",
array( 1 ),
'',
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s",
array( array( 1, 'admin', 'extra-arg' ) ),
"SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'",
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d AND %% AND user_login = %s",
array( 1, 'admin', 'extra-arg' ),
"SELECT * FROM $wpdb->users WHERE id = 1 AND {$wpdb->placeholder_escape()} AND user_login = 'admin'",
),
array(
"SELECT * FROM $wpdb->users WHERE id = %%%d AND %F AND %f AND user_login = %s",
array( 1, 2.3, '4.5', 'admin', 'extra-arg' ),
"SELECT * FROM $wpdb->users WHERE id = {$wpdb->placeholder_escape()}1 AND 2.300000 AND 4.500000 AND user_login = 'admin'",
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s",
array( array( 1 ), 'admin', 'extra-arg' ),
"SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'",
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s",
array( 1, 'admin', 0 ),
'',
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s",
array( array( 1, 'admin', 0 ) ),
'',
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d and %% and user_login = %s and user_status = %d and user_login = %s",
array( 1, 'admin', 'extra-arg' ),
'',
),
);
}
public function test_db_version() {
global $wpdb;
$this->assertTrue( version_compare( $wpdb->db_version(), '5.0', '>=' ) );
}
public function test_get_caller() {
global $wpdb;
$str = $wpdb->get_caller();
$calls = explode( ', ', $str );
$called = implode( '->', array( __CLASS__, __FUNCTION__ ) );
$this->assertSame( $called, end( $calls ) );
}
public function test_has_cap() {
global $wpdb;
$this->assertTrue( $wpdb->has_cap( 'collation' ) );
$this->assertTrue( $wpdb->has_cap( 'group_concat' ) );
$this->assertTrue( $wpdb->has_cap( 'subqueries' ) );
$this->assertTrue( $wpdb->has_cap( 'COLLATION' ) );
$this->assertTrue( $wpdb->has_cap( 'GROUP_CONCAT' ) );
$this->assertTrue( $wpdb->has_cap( 'SUBQUERIES' ) );
$this->assertSame(
version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
$wpdb->has_cap( 'set_charset' )
);
$this->assertSame(
version_compare( $wpdb->db_version(), '5.0.7', '>=' ),
$wpdb->has_cap( 'SET_CHARSET' )
);
}
/**
* @expectedDeprecated supports_collation
*/
public function test_supports_collation() {
global $wpdb;
$this->assertTrue( $wpdb->supports_collation() );
}
public function test_check_database_version() {
global $wpdb;
$this->assertEmpty( $wpdb->check_database_version() );
}
public function test_bail() {
global $wpdb;
$this->expectException( 'WPDieException' );
$wpdb->bail( 'Database is dead.' );
}
public function test_timers() {
global $wpdb;
$wpdb->timer_start();
usleep( 5 );
$stop = $wpdb->timer_stop();
$this->assertNotEquals( $wpdb->time_start, $stop );
$this->assertGreaterThan( $stop, $wpdb->time_start );
}
public function test_get_col_info() {
global $wpdb;
$wpdb->get_results( "SELECT ID FROM $wpdb->users" );
$this->assertSame( array( 'ID' ), $wpdb->get_col_info() );
$this->assertSame( array( $wpdb->users ), $wpdb->get_col_info( 'table' ) );
$this->assertSame( $wpdb->users, $wpdb->get_col_info( 'table', 0 ) );
}
public function test_query_and_delete() {
global $wpdb;
$rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
$this->assertSame( 1, $rows );
$this->assertNotEmpty( $wpdb->insert_id );
$d_rows = $wpdb->delete( $wpdb->users, array( 'ID' => $wpdb->insert_id ) );
$this->assertSame( 1, $d_rows );
}
public function test_get_row() {
global $wpdb;
$rows = $wpdb->query( "INSERT INTO $wpdb->users (display_name) VALUES ('Walter Sobchak')" );
$this->assertSame( 1, $rows );
$this->assertNotEmpty( $wpdb->insert_id );
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $wpdb->insert_id ) );
$this->assertIsObject( $row );
$this->assertSame( 'Walter Sobchak', $row->display_name );
}
/**
* Test the `get_col()` method.
*
* @param string|null $query The query to run.
* @param string|array $expected The expected resulting value.
* @param arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
* @param int|string $column The column index to retrieve.
*
* @dataProvider data_test_get_col
*
* @ticket 45299
*/
public function test_get_col( $query, $expected, $last_result, $column ) {
global $wpdb;
$wpdb->last_result = $last_result;
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
$result = $wpdb->get_col( $query, $column );
if ( $query ) {
$this->assertSame( $query, $wpdb->last_query );
}
if ( is_array( $expected ) ) {
$this->assertSame( $expected, $result );
} else {
$this->assertContains( $expected, $result );
}
}
/**
* Data provider for testing `get_col()`.
*
* @return array {
* Arguments for testing `get_col()`.
*
* @type string|null $query The query to run.
* @type string|array $expected The resulting expected value.
* @type arrray|string|null $last_result The value to assign to `$wpdb->last_result`.
* @type int|string $column The column index to retrieve.
*/
public function data_test_get_col() {
global $wpdb;
return array(
array(
"SELECT display_name FROM $wpdb->users",
'admin',
array(),
0,
),
array(
"SELECT user_login, user_email FROM $wpdb->users",
'admin',
array(),
0,
),
array(
"SELECT user_login, user_email FROM $wpdb->users",
'admin@example.org',
array(),
1,
),
array(
"SELECT user_login, user_email FROM $wpdb->users",
'admin@example.org',
array(),
'1',
),
array(
"SELECT user_login, user_email FROM $wpdb->users",
array( null ),
array(),
3,
),
array(
'',
array(),
null,
0,
),
array(
null,
array(),
'',
0,
),
);
}
public function test_replace() {
global $wpdb;
$rows1 = $wpdb->insert( $wpdb->users, array( 'display_name' => 'Walter Sobchak' ) );
$this->assertSame( 1, $rows1 );
$this->assertNotEmpty( $wpdb->insert_id );
$last = $wpdb->insert_id;
$rows2 = $wpdb->replace(
$wpdb->users,
array(
'ID' => $last,
'display_name' => 'Walter Replace Sobchak',
)
);
$this->assertSame( 2, $rows2 );
$this->assertNotEmpty( $wpdb->insert_id );
$this->assertSame( $last, $wpdb->insert_id );
$row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE ID = %d", $last ) );
$this->assertSame( 'Walter Replace Sobchak', $row->display_name );
}
/**
* wpdb::update() requires a WHERE condition.
*
* @ticket 26106
*/
public function test_empty_where_on_update() {
global $wpdb;
$suppress = $wpdb->suppress_errors( true );
$wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array() );
$expected1 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE ";
$this->assertNotEmpty( $wpdb->last_error );
$this->assertSame( $expected1, $wpdb->last_query );
$wpdb->update( $wpdb->posts, array( 'post_name' => 'burrito' ), array( 'post_status' => 'taco' ) );
$expected2 = "UPDATE `{$wpdb->posts}` SET `post_name` = 'burrito' WHERE `post_status` = 'taco'";
$this->assertEmpty( $wpdb->last_error );
$this->assertSame( $expected2, $wpdb->last_query );
$wpdb->suppress_errors( $suppress );
}
/**
* mysqli_ incorrect flush and further sync issues.
*
* @ticket 28155
*/
public function test_mysqli_flush_sync() {
global $wpdb;
if ( ! $wpdb->use_mysqli ) {
$this->markTestSkipped( 'mysqli not being used.' );
}
$suppress = $wpdb->suppress_errors( true );
$wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' );
$wpdb->query(
'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN
SELECT ID FROM `' . $wpdb->posts . '` LIMIT 1;
END'
);
if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) {
$wpdb->suppress_errors( $suppress );
$this->fail( 'Procedure could not be created (missing privileges?)' );
}
$post_id = self::factory()->post->create();
$this->assertNotEmpty( $wpdb->get_results( 'CALL `test_mysqli_flush_sync_procedure`' ) );
$this->assertNotEmpty( $wpdb->get_results( "SELECT ID FROM `{$wpdb->posts}` LIMIT 1" ) );
// DROP PROCEDURE will cause a COMMIT, so we delete the post manually before that happens.
wp_delete_post( $post_id, true );
$wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' );
$wpdb->suppress_errors( $suppress );
}
/**
* @ticket 21212
* @ticket 32763
*/
public function data_get_table_from_query() {
$table = 'a_test_table_name';
$more_tables = array(
// table_name => expected_value
'`a_test_db`.`another_test_table`' => 'a_test_db.another_test_table',
'a-test-with-dashes' => 'a-test-with-dashes',
);
$queries = array(
// Basic.
"SELECT * FROM $table",
"SELECT * FROM `$table`",
"SELECT * FROM (SELECT * FROM $table) as subquery",
"INSERT $table",
"INSERT IGNORE $table",
"INSERT IGNORE INTO $table",
"INSERT INTO $table",
"INSERT LOW_PRIORITY $table",
"INSERT DELAYED $table",
"INSERT HIGH_PRIORITY $table",
"INSERT LOW_PRIORITY IGNORE $table",
"INSERT LOW_PRIORITY INTO $table",
"INSERT LOW_PRIORITY IGNORE INTO $table",
"REPLACE $table",
"REPLACE INTO $table",
"REPLACE LOW_PRIORITY $table",
"REPLACE DELAYED $table",
"REPLACE LOW_PRIORITY INTO $table",
"UPDATE LOW_PRIORITY $table",
"UPDATE LOW_PRIORITY IGNORE $table",
"DELETE $table",
"DELETE IGNORE $table",
"DELETE IGNORE FROM $table",
"DELETE FROM $table",
"DELETE LOW_PRIORITY $table",
"DELETE QUICK $table",
"DELETE IGNORE $table",
"DELETE LOW_PRIORITY FROM $table",
"DELETE a FROM $table a",
"DELETE `a` FROM $table a",
// Extended.
"EXPLAIN SELECT * FROM $table",
"EXPLAIN EXTENDED SELECT * FROM $table",
"EXPLAIN EXTENDED SELECT * FROM `$table`",
"DESCRIBE $table",
"DESC $table",
"EXPLAIN $table",
"HANDLER $table",
"LOCK TABLE $table",
"LOCK TABLES $table",
"UNLOCK TABLE $table",
"RENAME TABLE $table",
"OPTIMIZE TABLE $table",
"BACKUP TABLE $table",
"RESTORE TABLE $table",
"CHECK TABLE $table",
"CHECKSUM TABLE $table",
"ANALYZE TABLE $table",
"REPAIR TABLE $table",
"TRUNCATE $table",
"TRUNCATE TABLE $table",
"CREATE TABLE $table",
"CREATE TEMPORARY TABLE $table",
"CREATE TABLE IF NOT EXISTS $table",
"ALTER TABLE $table",
"ALTER IGNORE TABLE $table",
"DROP TABLE $table",
"DROP TABLE IF EXISTS $table",
"CREATE INDEX foo(bar(20)) ON $table",
"CREATE UNIQUE INDEX foo(bar(20)) ON $table",
"CREATE FULLTEXT INDEX foo(bar(20)) ON $table",
"CREATE SPATIAL INDEX foo(bar(20)) ON $table",
"DROP INDEX foo ON $table",
"LOAD DATA INFILE 'wp.txt' INTO TABLE $table",
"LOAD DATA LOW_PRIORITY INFILE 'wp.txt' INTO TABLE $table",
"LOAD DATA CONCURRENT INFILE 'wp.txt' INTO TABLE $table",
"LOAD DATA LOW_PRIORITY LOCAL INFILE 'wp.txt' INTO TABLE $table",
"LOAD DATA INFILE 'wp.txt' REPLACE INTO TABLE $table",
"LOAD DATA INFILE 'wp.txt' IGNORE INTO TABLE $table",
"GRANT ALL ON TABLE $table",
"REVOKE ALL ON TABLE $table",
"SHOW COLUMNS FROM $table",
"SHOW FULL COLUMNS FROM $table",
"SHOW CREATE TABLE $table",
"SHOW INDEX FROM $table",
// @ticket 32763
'SELECT ' . str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery",
);
$querycount = count( $queries );
for ( $ii = 0; $ii < $querycount; $ii++ ) {
foreach ( $more_tables as $name => $expected_name ) {
$new_query = str_replace( $table, $name, $queries[ $ii ] );
$queries[] = array( $new_query, $expected_name );
}
$queries[ $ii ] = array( $queries[ $ii ], $table );
}
return $queries;
}
/**
* @dataProvider data_get_table_from_query
* @ticket 21212
*/
public function test_get_table_from_query( $query, $table ) {
$this->assertSame( $table, self::$_wpdb->get_table_from_query( $query ) );
}
public function data_get_table_from_query_false() {
$table = 'a_test_table_name';
return array(
array( "LOL THIS ISN'T EVEN A QUERY $table" ),
);
}
/**
* @dataProvider data_get_table_from_query_false
* @ticket 21212
*/
public function test_get_table_from_query_false( $query ) {
$this->assertFalse( self::$_wpdb->get_table_from_query( $query ) );
}
/**
* @ticket 38751
*/
public function data_get_escaped_table_from_show_query() {
return array(
// Equality.
array( "SHOW TABLE STATUS WHERE Name = 'test_name'", 'test_name' ),
array( 'SHOW TABLE STATUS WHERE NAME="test_name"', 'test_name' ),
array( 'SHOW TABLES WHERE Name = "test_name"', 'test_name' ),
array( "SHOW FULL TABLES WHERE Name='test_name'", 'test_name' ),
// LIKE.
array( "SHOW TABLE STATUS LIKE 'test\_prefix\_%'", 'test_prefix_' ),
array( 'SHOW TABLE STATUS LIKE "test\_prefix\_%"', 'test_prefix_' ),
array( "SHOW TABLES LIKE 'test\_prefix\_%'", 'test_prefix_' ),
array( 'SHOW FULL TABLES LIKE "test\_prefix\_%"', 'test_prefix_' ),
);
}
/**
* @dataProvider data_get_escaped_table_from_show_query
* @ticket 38751
*/
public function test_get_escaped_table_from_show_query( $query, $table ) {
$this->assertSame( $table, self::$_wpdb->get_table_from_query( $query ) );
}
/**
* @ticket 21212
*/
public function data_process_field_formats() {
$core_db_fields_no_format_specified = array(
array(
'post_content' => 'foo',
'post_parent' => 0,
),
null,
array(
'post_content' => array(
'value' => 'foo',
'format' => '%s',
),
'post_parent' => array(
'value' => 0,
'format' => '%d',
),
),
);
$core_db_fields_formats_specified = array(
array(
'post_content' => 'foo',
'post_parent' => 0,
),
array( '%d', '%s' ), // These override core field_types.
array(
'post_content' => array(
'value' => 'foo',
'format' => '%d',
),
'post_parent' => array(
'value' => 0,
'format' => '%s',
),
),
);
$misc_fields_no_format_specified = array(
array(
'this_is_not_a_core_field' => 'foo',
'this_is_not_either' => 0,
),
null,
array(
'this_is_not_a_core_field' => array(
'value' => 'foo',
'format' => '%s',
),
'this_is_not_either' => array(
'value' => 0,
'format' => '%s',
),
),
);
$misc_fields_formats_specified = array(
array(
'this_is_not_a_core_field' => 0,
'this_is_not_either' => 1.2,
),
array( '%d', '%f' ),
array(
'this_is_not_a_core_field' => array(
'value' => 0,
'format' => '%d',
),
'this_is_not_either' => array(
'value' => 1.2,
'format' => '%f',
),
),
);
$misc_fields_insufficient_formats_specified = array(
array(
'this_is_not_a_core_field' => 0,
'this_is_not_either' => 's',
'nor_this' => 1,
),
array( '%d', '%s' ), // The first format is used for the third.
array(
'this_is_not_a_core_field' => array(
'value' => 0,
'format' => '%d',
Morty Proxy This is a proxified and sanitized view of the page, visit original site.