Changeset 42343 for trunk/tests/phpunit/tests/db.php
- Timestamp:
- 11/30/2017 11:09:33 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/db.php
r42226 r42343 10 10 /** 11 11 * Query log 12 * 12 13 * @var array 13 14 */ … … 16 17 /** 17 18 * Our special WPDB 19 * 18 20 * @var resource 19 21 */ … … 44 46 /** 45 47 * Log each query 48 * 46 49 * @param string $sql 47 50 * @return string … … 54 57 /** 55 58 * Test that WPDB will reconnect when the DB link dies 59 * 56 60 * @ticket 5932 57 61 */ … … 75 79 /** 76 80 * Test that floats formatted as "0,700" get sanitized properly by wpdb 81 * 77 82 * @global mixed $wpdb 78 83 * … … 87 92 // Switch to Russian 88 93 $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 ) 94 if ( false === $flag ) { 90 95 $this->markTestSkipped( 'No European languages available for testing' ); 96 } 91 97 92 98 // Try an update query … … 105 111 106 112 // Try a prepare 107 $sql = $wpdb->prepare( "UPDATE test_table SET float_column = %f AND meta_id = %d", 0.7, 5 );113 $sql = $wpdb->prepare( 'UPDATE test_table SET float_column = %f AND meta_id = %d', 0.7, 5 ); 108 114 $this->assertContains( '0.700', $sql ); 109 115 … … 112 118 if ( false !== strpos( $locale_setting, '=' ) ) { 113 119 list( $category, $locale ) = explode( '=', $locale_setting ); 114 if ( defined( $category ) ) 120 if ( defined( $category ) ) { 115 121 setlocale( constant( $category ), $locale ); 122 } 116 123 } else { 117 124 setlocale( LC_ALL, $locale_setting ); … … 126 133 global $wpdb; 127 134 128 $inputs = array(135 $inputs = array( 129 136 'howdy%', //Single Percent 130 137 'howdy_', //Single Underscore … … 141 148 ); 142 149 143 foreach ( $inputs as $key => $input) {144 $this->assertEquals( $expected[$key], $wpdb->esc_like($input));150 foreach ( $inputs as $key => $input ) { 151 $this->assertEquals( $expected[ $key ], $wpdb->esc_like( $input ) ); 145 152 } 146 153 } … … 157 164 * @param $data string The haystack, raw. 158 165 * @param $like string The like phrase, raw. 159 166 * @param $result string The expected comparison result; '1' = true, '0' = false 160 167 */ 161 168 function test_like_query( $data, $like, $result ) { 162 169 global $wpdb; 163 return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( "SELECT %s LIKE %s", $data, $wpdb->esc_like( $like ) ) ) );170 return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( 'SELECT %s LIKE %s', $data, $wpdb->esc_like( $like ) ) ) ); 164 171 } 165 172 … … 243 250 $new_meta = "HAHA I HOPE THIS DOESN'T WORK"; 244 251 245 $col_meta = $wpdb->col_meta;252 $col_meta = $wpdb->col_meta; 246 253 $wpdb->col_meta = $new_meta; 247 254 … … 266 273 /** 267 274 * Test that an escaped %%f is not altered 275 * 268 276 * @ticket 19861 269 277 */ … … 280 288 /** 281 289 * Test that SQL modes are set correctly 290 * 282 291 * @ticket 26847 283 292 */ … … 299 308 /** 300 309 * Test that incompatible SQL modes are blocked 310 * 301 311 * @ticket 26847 302 312 */ … … 316 326 /** 317 327 * Test that incompatible SQL modes can be changed 328 * 318 329 * @ticket 26847 319 330 */ … … 339 350 $this->assertGreaterThanOrEqual( 0, $pos ); 340 351 341 if ( FALSE=== $pos ) {352 if ( false === $pos ) { 342 353 return $modes; 343 354 } … … 362 373 global $wpdb; 363 374 364 $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin");375 $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, 'admin' ); 365 376 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared ); 366 377 } … … 372 383 global $wpdb; 373 384 374 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( "admin") );385 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( 'admin' ) ); 375 386 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared ); 376 387 377 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), "admin");388 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), 'admin' ); 378 389 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared ); 379 390 } … … 382 393 global $wpdb; 383 394 384 $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, "admin") );395 $prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, 'admin' ) ); 385 396 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared ); 386 397 } … … 392 403 global $wpdb; 393 404 394 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( "admin") ) );405 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( 'admin' ) ) ); 395 406 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared ); 396 407 397 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), "admin") );408 $prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), 'admin' ) ); 398 409 $this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared ); 399 410 } … … 420 431 array( 421 432 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", // Query 422 array( 1, "admin", "extra-arg"), // ::prepare() args, to be passed via call_user_func_array433 array( 1, 'admin', 'extra-arg' ), // ::prepare() args, to be passed via call_user_func_array 423 434 "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", // Expected output 424 435 ), … … 430 441 array( 431 442 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 432 array( array( 1, "admin", "extra-arg") ),443 array( array( 1, 'admin', 'extra-arg' ) ), 433 444 "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", 434 445 ), 435 446 array( 436 447 "SELECT * FROM $wpdb->users WHERE id = %d AND %% AND user_login = %s", 437 array( 1, "admin", "extra-arg"),448 array( 1, 'admin', 'extra-arg' ), 438 449 "SELECT * FROM $wpdb->users WHERE id = 1 AND {$wpdb->placeholder_escape()} AND user_login = 'admin'", 439 450 ), 440 451 array( 441 452 "SELECT * FROM $wpdb->users WHERE id = %%%d AND %F AND %f AND user_login = %s", 442 array( 1, 2.3, "4.5", "admin", "extra-arg"),453 array( 1, 2.3, '4.5', 'admin', 'extra-arg' ), 443 454 "SELECT * FROM $wpdb->users WHERE id = {$wpdb->placeholder_escape()}1 AND 2.300000 AND 4.500000 AND user_login = 'admin'", 444 455 ), 445 456 array( 446 457 "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 447 array( array( 1 ), "admin", "extra-arg"),458 array( array( 1 ), 'admin', 'extra-arg' ), 448 459 "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", 449 460 ), 450 461 array( 451 462 "SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s", 452 array( 1, "admin", 0 ),463 array( 1, 'admin', 0 ), 453 464 '', 454 465 ), 455 466 array( 456 467 "SELECT * FROM $wpdb->users WHERE id = %d and user_nicename = %s and user_status = %d and user_login = %s", 457 array( array( 1, "admin", 0 ) ),468 array( array( 1, 'admin', 0 ) ), 458 469 '', 459 470 ), 460 471 array( 461 472 "SELECT * FROM $wpdb->users WHERE id = %d and %% and user_login = %s and user_status = %d and user_login = %s", 462 array( 1, "admin", "extra-arg"),473 array( 1, 'admin', 'extra-arg' ), 463 474 '', 464 475 ), … … 474 485 function test_get_caller() { 475 486 global $wpdb; 476 $str = $wpdb->get_caller();477 $calls = explode( ', ', $str );487 $str = $wpdb->get_caller(); 488 $calls = explode( ', ', $str ); 478 489 $called = join( '->', array( __CLASS__, __FUNCTION__ ) ); 479 490 $this->assertEquals( $called, end( $calls ) ); … … 567 578 $last = $wpdb->insert_id; 568 579 569 $rows2 = $wpdb->replace( $wpdb->users, array( 'ID' => $last, 'display_name' => 'Walter Replace Sobchak' ) ); 580 $rows2 = $wpdb->replace( 581 $wpdb->users, array( 582 'ID' => $last, 583 'display_name' => 'Walter Replace Sobchak', 584 ) 585 ); 570 586 $this->assertEquals( 2, $rows2 ); 571 587 $this->assertNotEmpty( $wpdb->insert_id ); … … 613 629 614 630 $wpdb->query( 'DROP PROCEDURE IF EXISTS `test_mysqli_flush_sync_procedure`' ); 615 $wpdb->query( 'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN 631 $wpdb->query( 632 'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN 616 633 SELECT ID FROM `' . $wpdb->posts . '` LIMIT 1; 617 END' ); 634 END' 635 ); 618 636 619 637 if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) { … … 638 656 */ 639 657 function data_get_table_from_query() { 640 $table = 'a_test_table_name';658 $table = 'a_test_table_name'; 641 659 $more_tables = array( 642 660 // table_name => expected_value … … 742 760 743 761 // @ticket 32763 744 "SELECT ". str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery",762 'SELECT ' . str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery", 745 763 ); 746 764 … … 787 805 // Equality 788 806 array( "SHOW TABLE STATUS WHERE Name = 'test_name'", 'test_name' ), 789 array( "SHOW TABLE STATUS WHERE NAME=\"test_name\"", 'test_name' ),790 array( "SHOW TABLES WHERE Name = \"test_name\"",'test_name' ),791 array( "SHOW FULL TABLES WHERE Name='test_name'", 807 array( 'SHOW TABLE STATUS WHERE NAME="test_name"', 'test_name' ), 808 array( 'SHOW TABLES WHERE Name = "test_name"', 'test_name' ), 809 array( "SHOW FULL TABLES WHERE Name='test_name'", 'test_name' ), 792 810 793 811 // LIKE 794 array( "SHOW TABLE STATUS LIKE 'test\_prefix\_%'", 795 array( "SHOW TABLE STATUS LIKE \"test\_prefix\_%\"", 'test_prefix_' ),796 array( "SHOW TABLES LIKE 'test\_prefix\_%'", 797 array( "SHOW FULL TABLES LIKE \"test\_prefix\_%\"",'test_prefix_' ),812 array( "SHOW TABLE STATUS LIKE 'test\_prefix\_%'", 'test_prefix_' ), 813 array( 'SHOW TABLE STATUS LIKE "test\_prefix\_%"', 'test_prefix_' ), 814 array( "SHOW TABLES LIKE 'test\_prefix\_%'", 'test_prefix_' ), 815 array( 'SHOW FULL TABLES LIKE "test\_prefix\_%"', 'test_prefix_' ), 798 816 ); 799 817 } … … 812 830 function data_process_field_formats() { 813 831 $core_db_fields_no_format_specified = array( 814 array( 'post_content' => 'foo', 'post_parent' => 0 ), 832 array( 833 'post_content' => 'foo', 834 'post_parent' => 0, 835 ), 815 836 null, 816 837 array( 817 'post_content' => array( 'value' => 'foo', 'format' => '%s' ), 818 'post_parent' => array( 'value' => 0, 'format' => '%d' ), 819 ) 838 'post_content' => array( 839 'value' => 'foo', 840 'format' => '%s', 841 ), 842 'post_parent' => array( 843 'value' => 0, 844 'format' => '%d', 845 ), 846 ), 820 847 ); 821 848 822 849 $core_db_fields_formats_specified = array( 823 array( 'post_content' => 'foo', 'post_parent' => 0 ), 850 array( 851 'post_content' => 'foo', 852 'post_parent' => 0, 853 ), 824 854 array( '%d', '%s' ), // These override core field_types 825 855 array( 826 'post_content' => array( 'value' => 'foo', 'format' => '%d' ), 827 'post_parent' => array( 'value' => 0, 'format' => '%s' ), 828 ) 856 'post_content' => array( 857 'value' => 'foo', 858 'format' => '%d', 859 ), 860 'post_parent' => array( 861 'value' => 0, 862 'format' => '%s', 863 ), 864 ), 829 865 ); 830 866 831 867 $misc_fields_no_format_specified = array( 832 array( 'this_is_not_a_core_field' => 'foo', 'this_is_not_either' => 0 ), 868 array( 869 'this_is_not_a_core_field' => 'foo', 870 'this_is_not_either' => 0, 871 ), 833 872 null, 834 873 array( 835 'this_is_not_a_core_field' => array( 'value' => 'foo', 'format' => '%s' ), 836 'this_is_not_either' => array( 'value' => 0, 'format' => '%s' ), 837 ) 874 'this_is_not_a_core_field' => array( 875 'value' => 'foo', 876 'format' => '%s', 877 ), 878 'this_is_not_either' => array( 879 'value' => 0, 880 'format' => '%s', 881 ), 882 ), 838 883 ); 839 884 840 885 $misc_fields_formats_specified = array( 841 array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 1.2 ), 886 array( 887 'this_is_not_a_core_field' => 0, 888 'this_is_not_either' => 1.2, 889 ), 842 890 array( '%d', '%f' ), 843 891 array( 844 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), 845 'this_is_not_either' => array( 'value' => 1.2, 'format' => '%f' ), 846 ) 892 'this_is_not_a_core_field' => array( 893 'value' => 0, 894 'format' => '%d', 895 ), 896 'this_is_not_either' => array( 897 'value' => 1.2, 898 'format' => '%f', 899 ), 900 ), 847 901 ); 848 902 849 903 $misc_fields_insufficient_formats_specified = array( 850 array( 'this_is_not_a_core_field' => 0, 'this_is_not_either' => 's', 'nor_this' => 1 ), 904 array( 905 'this_is_not_a_core_field' => 0, 906 'this_is_not_either' => 's', 907 'nor_this' => 1, 908 ), 851 909 array( '%d', '%s' ), // The first format is used for the third 852 910 array( 853 'this_is_not_a_core_field' => array( 'value' => 0, 'format' => '%d' ), 854 'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ), 855 'nor_this' => array( 'value' => 1, 'format' => '%d' ), 856 ) 911 'this_is_not_a_core_field' => array( 912 'value' => 0, 913 'format' => '%d', 914 ), 915 'this_is_not_either' => array( 916 'value' => 's', 917 'format' => '%s', 918 ), 919 'nor_this' => array( 920 'value' => 1, 921 'format' => '%d', 922 ), 923 ), 857 924 ); 858 925 … … 887 954 888 955 if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) { 889 $this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets");890 } 891 892 $data = array( 'post_content' => '¡foo foo foo!' );956 $this->markTestSkipped( 'This test only works with utf8, utf8mb4 or latin1 character sets' ); 957 } 958 959 $data = array( 'post_content' => '¡foo foo foo!' ); 893 960 $expected = array( 894 961 'post_content' => array( 895 'value' => '¡foo foo foo!',896 'format' => '%s',962 'value' => '¡foo foo foo!', 963 'format' => '%s', 897 964 'charset' => $expected_charset, 898 'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ),899 ) 965 'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ), 966 ), 900 967 ); 901 968 … … 953 1020 $wpdb->postmeta, 954 1021 array( 955 'meta_key' => $key,956 'meta_value' => NULL1022 'meta_key' => $key, 1023 'meta_value' => null, 957 1024 ), 958 1025 array( '%s', '%s' ) … … 970 1037 global $wpdb; 971 1038 972 $key = 'null_update_value_key';1039 $key = 'null_update_value_key'; 973 1040 $value = 'null_update_value_key'; 974 1041 … … 976 1043 $wpdb->postmeta, 977 1044 array( 978 'meta_key' => $key,979 'meta_value' => $value 1045 'meta_key' => $key, 1046 'meta_value' => $value, 980 1047 ), 981 1048 array( '%s', '%s' ) … … 988 1055 $wpdb->update( 989 1056 $wpdb->postmeta, 990 array( 'meta_value' => NULL),991 array( 992 'meta_key' => $key,993 'meta_value' => $value 1057 array( 'meta_value' => null ), 1058 array( 1059 'meta_key' => $key, 1060 'meta_value' => $value, 994 1061 ), 995 1062 array( '%s' ), … … 1008 1075 global $wpdb; 1009 1076 1010 $key = 'null_update_where_key';1077 $key = 'null_update_where_key'; 1011 1078 $value = 'null_update_where_key'; 1012 1079 … … 1014 1081 $wpdb->postmeta, 1015 1082 array( 1016 'meta_key' => $key,1017 'meta_value' => NULL1083 'meta_key' => $key, 1084 'meta_value' => null, 1018 1085 ), 1019 1086 array( '%s', '%s' ) … … 1028 1095 array( 'meta_value' => $value ), 1029 1096 array( 1030 'meta_key' => $key,1031 'meta_value' => NULL1097 'meta_key' => $key, 1098 'meta_value' => null, 1032 1099 ), 1033 1100 array( '%s' ), … … 1046 1113 global $wpdb; 1047 1114 1048 $key = 'null_update_where_key';1115 $key = 'null_update_where_key'; 1049 1116 $value = 'null_update_where_key'; 1050 1117 … … 1052 1119 $wpdb->postmeta, 1053 1120 array( 1054 'meta_key' => $key,1055 'meta_value' => NULL1121 'meta_key' => $key, 1122 'meta_value' => null, 1056 1123 ), 1057 1124 array( '%s', '%s' ) … … 1065 1132 $wpdb->postmeta, 1066 1133 array( 1067 'meta_key' => $key,1068 'meta_value' => NULL1134 'meta_key' => $key, 1135 'meta_value' => null, 1069 1136 ), 1070 1137 array( '%s', '%s' ) … … 1191 1258 * @dataProvider data_prepare_with_placeholders 1192 1259 */ 1193 function test_prepare_with_placeholders_and_individual_args( $sql, $values, $incorrect_usage, $expected ) {1260 function test_prepare_with_placeholders_and_individual_args( $sql, $values, $incorrect_usage, $expected ) { 1194 1261 global $wpdb; 1195 1262 … … 1211 1278 * @dataProvider data_prepare_with_placeholders 1212 1279 */ 1213 function test_prepare_with_placeholders_and_array_args( $sql, $values, $incorrect_usage, $expected ) {1280 function test_prepare_with_placeholders_and_array_args( $sql, $values, $incorrect_usage, $expected ) { 1214 1281 global $wpdb; 1215 1282 … … 1306 1373 array( 1, 'hello' ), 1307 1374 false, 1308 "1 hello",1375 '1 hello', 1309 1376 ), 1310 1377 array( … … 1372 1439 array( 1, 2 ), 1373 1440 false, 1374 "WHERE second=2 AND first=1",1441 'WHERE second=2 AND first=1', 1375 1442 ), 1376 1443 array(
Note: See TracChangeset
for help on using the changeset viewer.