diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index 7aa5df10e..dae77f257 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -555,6 +555,7 @@ private function register_checks() { 'late_escaping' => new Checks\Late_Escaping_Check(), 'plugin_updater' => new Checks\Plugin_Updater_Check(), 'plugin_review_phpcs' => new Checks\Plugin_Review_PHPCS_Check(), + 'direct_db_queries' => new Checks\Direct_DB_Queries_Check(), 'performant_wp_query_params' => new Checks\Performant_WP_Query_Params_Check(), 'enqueued_scripts_in_footer' => new Checks\Enqueued_Scripts_In_Footer_Check(), 'plugin_readme' => new Checks\Plugin_Readme_Check(), diff --git a/includes/Checker/Checks/Direct_DB_Queries_Check.php b/includes/Checker/Checks/Direct_DB_Queries_Check.php new file mode 100644 index 000000000..98639ab77 --- /dev/null +++ b/includes/Checker/Checks/Direct_DB_Queries_Check.php @@ -0,0 +1,49 @@ + 'php', + 'standard' => 'WordPress', + 'sniffs' => 'WordPress.DB.DirectDatabaseQuery', + ); + } +} diff --git a/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-with-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-with-errors/load.php new file mode 100644 index 000000000..4f25b235c --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-with-errors/load.php @@ -0,0 +1,28 @@ +get_col( 'SELECT X FROM Y WHERE Z = 1' ); + +$autoload = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option_name ) ); + +$wpdb->update( $wpdb->posts, array( 'post_title' => 'Hello World' ), array( 'ID' => 1 ) ); diff --git a/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-without-errors/load.php b/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-without-errors/load.php new file mode 100644 index 000000000..68c7e685f --- /dev/null +++ b/tests/phpunit/testdata/plugins/test-plugin-direct-db-queries-without-errors/load.php @@ -0,0 +1,20 @@ +insert_id; diff --git a/tests/phpunit/tests/Checker/Checks/Direct_DB_Queries_Check_Tests.php b/tests/phpunit/tests/Checker/Checks/Direct_DB_Queries_Check_Tests.php new file mode 100644 index 000000000..c46026b21 --- /dev/null +++ b/tests/phpunit/tests/Checker/Checks/Direct_DB_Queries_Check_Tests.php @@ -0,0 +1,43 @@ +run( $check_result ); + + $warnings = $check_result->get_warnings(); + $errors = $check_result->get_errors(); + + $this->assertNotEmpty( $warnings ); + $this->assertArrayHasKey( 'load.php', $warnings ); + $this->assertEquals( 6, $check_result->get_warning_count() ); + $this->assertEmpty( $errors ); + $this->assertEquals( 0, $check_result->get_error_count() ); + } + + public function test_run_without_errors() { + $check = new Direct_DB_Queries_Check(); + $check_context = new Check_Context( UNIT_TESTS_PLUGIN_DIR . 'test-plugin-direct-db-queries-without-errors/load.php' ); + $check_result = new Check_Result( $check_context ); + + $check->run( $check_result ); + + $this->assertEmpty( $check_result->get_warnings() ); + $this->assertEmpty( $check_result->get_errors() ); + $this->assertEquals( 0, $check_result->get_warning_count() ); + $this->assertEquals( 0, $check_result->get_error_count() ); + } +}