Code is Poetry.

WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.



git-svn-id: https://develop.svn.wordpress.org/trunk@42343 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2017-11-30 23:09:33 +00:00
parent ec6a089f98
commit 8f95800d52
1103 changed files with 105978 additions and 78184 deletions
+154 -87
View File
@@ -9,12 +9,14 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Query log
*
* @var array
*/
protected $_queries = array();
/**
* Our special WPDB
*
* @var resource
*/
protected static $_wpdb;
@@ -43,6 +45,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Log each query
*
* @param string $sql
* @return string
*/
@@ -53,6 +56,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that WPDB will reconnect when the DB link dies
*
* @ticket 5932
*/
public function test_db_reconnect() {
@@ -74,6 +78,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that floats formatted as "0,700" get sanitized properly by wpdb
*
* @global mixed $wpdb
*
* @ticket 19861
@@ -86,8 +91,9 @@ class Tests_DB extends WP_UnitTestCase {
// Switch to Russian
$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 )
if ( false === $flag ) {
$this->markTestSkipped( 'No European languages available for testing' );
}
// Try an update query
$wpdb->suppress_errors( true );
@@ -104,15 +110,16 @@ class Tests_DB extends WP_UnitTestCase {
$this->assertContains( '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 );
$sql = $wpdb->prepare( 'UPDATE test_table SET float_column = %f AND meta_id = %d', 0.7, 5 );
$this->assertContains( '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 ) )
if ( defined( $category ) ) {
setlocale( constant( $category ), $locale );
}
} else {
setlocale( LC_ALL, $locale_setting );
}
@@ -125,7 +132,7 @@ class Tests_DB extends WP_UnitTestCase {
function test_esc_like() {
global $wpdb;
$inputs = array(
$inputs = array(
'howdy%', //Single Percent
'howdy_', //Single Underscore
'howdy\\', //Single slash
@@ -140,8 +147,8 @@ class Tests_DB extends WP_UnitTestCase {
'howdy\'"[[]*#[^howdy]!+)(*&$#@!~|}{=--`/.,<>?',
);
foreach ($inputs as $key => $input) {
$this->assertEquals($expected[$key], $wpdb->esc_like($input));
foreach ( $inputs as $key => $input ) {
$this->assertEquals( $expected[ $key ], $wpdb->esc_like( $input ) );
}
}
@@ -156,11 +163,11 @@ class Tests_DB extends WP_UnitTestCase {
* @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
* @param $result string The expected comparison result; '1' = true, '0' = false
*/
function test_like_query( $data, $like, $result ) {
global $wpdb;
return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( "SELECT %s LIKE %s", $data, $wpdb->esc_like( $like ) ) ) );
return $this->assertEquals( $result, $wpdb->get_var( $wpdb->prepare( 'SELECT %s LIKE %s', $data, $wpdb->esc_like( $like ) ) ) );
}
function data_like_query() {
@@ -242,7 +249,7 @@ class Tests_DB extends WP_UnitTestCase {
$new_meta = "HAHA I HOPE THIS DOESN'T WORK";
$col_meta = $wpdb->col_meta;
$col_meta = $wpdb->col_meta;
$wpdb->col_meta = $new_meta;
$this->assertNotEquals( $col_meta, $new_meta );
@@ -265,6 +272,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that an escaped %%f is not altered
*
* @ticket 19861
*/
public function test_double_escaped_placeholders() {
@@ -279,6 +287,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that SQL modes are set correctly
*
* @ticket 26847
*/
function test_set_sql_mode() {
@@ -298,6 +307,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that incompatible SQL modes are blocked
*
* @ticket 26847
*/
function test_set_incompatible_sql_mode() {
@@ -315,6 +325,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* Test that incompatible SQL modes can be changed
*
* @ticket 26847
*/
function test_set_allowed_incompatible_sql_mode() {
@@ -338,7 +349,7 @@ class Tests_DB extends WP_UnitTestCase {
$pos = array_search( 'ONLY_FULL_GROUP_BY', $modes );
$this->assertGreaterThanOrEqual( 0, $pos );
if ( FALSE === $pos ) {
if ( false === $pos ) {
return $modes;
}
@@ -361,7 +372,7 @@ class Tests_DB extends WP_UnitTestCase {
function test_prepare_sprintf() {
global $wpdb;
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, "admin" );
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, 'admin' );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
}
@@ -371,17 +382,17 @@ class Tests_DB extends WP_UnitTestCase {
function test_prepare_sprintf_invalid_args() {
global $wpdb;
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( "admin" ) );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", 1, array( 'admin' ) );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), "admin" );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1 ), 'admin' );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
}
function test_prepare_vsprintf() {
global $wpdb;
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, "admin" ) );
$prepared = $wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, 'admin' ) );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = 'admin'", $prepared );
}
@@ -391,10 +402,10 @@ class Tests_DB extends WP_UnitTestCase {
function test_prepare_vsprintf_invalid_args() {
global $wpdb;
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( "admin" ) ) );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( 1, array( 'admin' ) ) );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 1 AND user_login = ''", $prepared );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), "admin" ) );
$prepared = @$wpdb->prepare( "SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s", array( array( 1 ), 'admin' ) );
$this->assertEquals( "SELECT * FROM $wpdb->users WHERE id = 0 AND user_login = 'admin'", $prepared );
}
@@ -419,7 +430,7 @@ class Tests_DB extends WP_UnitTestCase {
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
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(
@@ -429,37 +440,37 @@ class Tests_DB extends WP_UnitTestCase {
),
array(
"SELECT * FROM $wpdb->users WHERE id = %d AND user_login = %s",
array( array( 1, "admin", "extra-arg" ) ),
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" ),
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" ),
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" ),
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( 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( 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" ),
array( 1, 'admin', 'extra-arg' ),
'',
),
);
@@ -473,8 +484,8 @@ class Tests_DB extends WP_UnitTestCase {
function test_get_caller() {
global $wpdb;
$str = $wpdb->get_caller();
$calls = explode( ', ', $str );
$str = $wpdb->get_caller();
$calls = explode( ', ', $str );
$called = join( '->', array( __CLASS__, __FUNCTION__ ) );
$this->assertEquals( $called, end( $calls ) );
}
@@ -566,7 +577,12 @@ class Tests_DB extends WP_UnitTestCase {
$this->assertNotEmpty( $wpdb->insert_id );
$last = $wpdb->insert_id;
$rows2 = $wpdb->replace( $wpdb->users, array( 'ID' => $last, 'display_name' => 'Walter Replace Sobchak' ) );
$rows2 = $wpdb->replace(
$wpdb->users, array(
'ID' => $last,
'display_name' => 'Walter Replace Sobchak',
)
);
$this->assertEquals( 2, $rows2 );
$this->assertNotEmpty( $wpdb->insert_id );
@@ -612,9 +628,11 @@ class Tests_DB extends WP_UnitTestCase {
$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
$wpdb->query(
'CREATE PROCEDURE `test_mysqli_flush_sync_procedure`() BEGIN
SELECT ID FROM `' . $wpdb->posts . '` LIMIT 1;
END' );
END'
);
if ( count( $wpdb->get_results( 'SHOW CREATE PROCEDURE `test_mysqli_flush_sync_procedure`' ) ) < 1 ) {
$wpdb->suppress_errors( $suppress );
@@ -637,7 +655,7 @@ class Tests_DB extends WP_UnitTestCase {
* @ticket 21212
*/
function data_get_table_from_query() {
$table = 'a_test_table_name';
$table = 'a_test_table_name';
$more_tables = array(
// table_name => expected_value
'`a_test_db`.`another_test_table`' => 'a_test_db.another_test_table',
@@ -741,7 +759,7 @@ class Tests_DB extends WP_UnitTestCase {
"SHOW INDEX FROM $table",
// @ticket 32763
"SELECT " . str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery",
'SELECT ' . str_repeat( 'a', 10000 ) . " FROM (SELECT * FROM $table) as subquery",
);
$querycount = count( $queries );
@@ -786,15 +804,15 @@ class Tests_DB extends WP_UnitTestCase {
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' ),
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_' ),
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_' ),
);
}
@@ -811,49 +829,98 @@ class Tests_DB extends WP_UnitTestCase {
*/
function data_process_field_formats() {
$core_db_fields_no_format_specified = array(
array( 'post_content' => 'foo', 'post_parent' => 0 ),
array(
'post_content' => 'foo',
'post_parent' => 0,
),
null,
array(
'post_content' => array( 'value' => 'foo', 'format' => '%s' ),
'post_parent' => array( 'value' => 0, 'format' => '%d' ),
)
'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(
'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' ),
)
'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 ),
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' ),
)
'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(
'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' ),
)
'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(
'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' ),
'this_is_not_either' => array( 'value' => 's', 'format' => '%s' ),
'nor_this' => array( 'value' => 1, 'format' => '%d' ),
)
'this_is_not_a_core_field' => array(
'value' => 0,
'format' => '%d',
),
'this_is_not_either' => array(
'value' => 's',
'format' => '%s',
),
'nor_this' => array(
'value' => 1,
'format' => '%d',
),
),
);
$vars = get_defined_vars();
@@ -886,17 +953,17 @@ class Tests_DB extends WP_UnitTestCase {
}
if ( ! in_array( $expected_charset, array( 'utf8', 'utf8mb4', 'latin1' ) ) ) {
$this->markTestSkipped( "This test only works with utf8, utf8mb4 or latin1 character sets" );
$this->markTestSkipped( 'This test only works with utf8, utf8mb4 or latin1 character sets' );
}
$data = array( 'post_content' => '¡foo foo foo!' );
$data = array( 'post_content' => '¡foo foo foo!' );
$expected = array(
'post_content' => array(
'value' => '¡foo foo foo!',
'format' => '%s',
'value' => '¡foo foo foo!',
'format' => '%s',
'charset' => $expected_charset,
'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ),
)
'length' => $wpdb->get_col_length( $wpdb->posts, 'post_content' ),
),
);
$this->assertSame( $expected, self::$_wpdb->process_fields( $wpdb->posts, $data, null ) );
@@ -952,8 +1019,8 @@ class Tests_DB extends WP_UnitTestCase {
$wpdb->insert(
$wpdb->postmeta,
array(
'meta_key' => $key,
'meta_value' => NULL
'meta_key' => $key,
'meta_value' => null,
),
array( '%s', '%s' )
);
@@ -969,14 +1036,14 @@ class Tests_DB extends WP_UnitTestCase {
function test_null_update_value() {
global $wpdb;
$key = 'null_update_value_key';
$key = 'null_update_value_key';
$value = 'null_update_value_key';
$wpdb->insert(
$wpdb->postmeta,
array(
'meta_key' => $key,
'meta_value' => $value
'meta_key' => $key,
'meta_value' => $value,
),
array( '%s', '%s' )
);
@@ -987,10 +1054,10 @@ class Tests_DB extends WP_UnitTestCase {
$wpdb->update(
$wpdb->postmeta,
array( 'meta_value' => NULL ),
array( 'meta_value' => null ),
array(
'meta_key' => $key,
'meta_value' => $value
'meta_key' => $key,
'meta_value' => $value,
),
array( '%s' ),
array( '%s', '%s' )
@@ -1007,14 +1074,14 @@ class Tests_DB extends WP_UnitTestCase {
function test_null_update_where() {
global $wpdb;
$key = 'null_update_where_key';
$key = 'null_update_where_key';
$value = 'null_update_where_key';
$wpdb->insert(
$wpdb->postmeta,
array(
'meta_key' => $key,
'meta_value' => NULL
'meta_key' => $key,
'meta_value' => null,
),
array( '%s', '%s' )
);
@@ -1027,8 +1094,8 @@ class Tests_DB extends WP_UnitTestCase {
$wpdb->postmeta,
array( 'meta_value' => $value ),
array(
'meta_key' => $key,
'meta_value' => NULL
'meta_key' => $key,
'meta_value' => null,
),
array( '%s' ),
array( '%s', '%s' )
@@ -1045,14 +1112,14 @@ class Tests_DB extends WP_UnitTestCase {
function test_null_delete() {
global $wpdb;
$key = 'null_update_where_key';
$key = 'null_update_where_key';
$value = 'null_update_where_key';
$wpdb->insert(
$wpdb->postmeta,
array(
'meta_key' => $key,
'meta_value' => NULL
'meta_key' => $key,
'meta_value' => null,
),
array( '%s', '%s' )
);
@@ -1064,8 +1131,8 @@ class Tests_DB extends WP_UnitTestCase {
$wpdb->delete(
$wpdb->postmeta,
array(
'meta_key' => $key,
'meta_value' => NULL
'meta_key' => $key,
'meta_value' => null,
),
array( '%s', '%s' )
);
@@ -1190,7 +1257,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* @dataProvider data_prepare_with_placeholders
*/
function test_prepare_with_placeholders_and_individual_args( $sql, $values, $incorrect_usage, $expected) {
function test_prepare_with_placeholders_and_individual_args( $sql, $values, $incorrect_usage, $expected ) {
global $wpdb;
if ( $incorrect_usage ) {
@@ -1210,7 +1277,7 @@ class Tests_DB extends WP_UnitTestCase {
/**
* @dataProvider data_prepare_with_placeholders
*/
function test_prepare_with_placeholders_and_array_args( $sql, $values, $incorrect_usage, $expected) {
function test_prepare_with_placeholders_and_array_args( $sql, $values, $incorrect_usage, $expected ) {
global $wpdb;
if ( $incorrect_usage ) {
@@ -1305,7 +1372,7 @@ class Tests_DB extends WP_UnitTestCase {
'%d %2$s',
array( 1, 'hello' ),
false,
"1 hello",
'1 hello',
),
array(
"'%s'",
@@ -1371,7 +1438,7 @@ class Tests_DB extends WP_UnitTestCase {
'WHERE second=%2$d AND first=%1$d',
array( 1, 2 ),
false,
"WHERE second=2 AND first=1",
'WHERE second=2 AND first=1',
),
array(
"'%'%%s",