From 85380bba31225f36678a3104bf03798319c7f9fb Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Thu, 16 Feb 2023 14:03:28 +0000 Subject: [PATCH 1/8] update Plugin_Request_Utility with runner methods --- includes/Utilities/Plugin_Request_Utility.php | 53 +++++++++++++++++++ .../Plugin_Request_Utility_Tests.php | 29 ++++++++++ 2 files changed, 82 insertions(+) diff --git a/includes/Utilities/Plugin_Request_Utility.php b/includes/Utilities/Plugin_Request_Utility.php index e06162ba4..01af50513 100644 --- a/includes/Utilities/Plugin_Request_Utility.php +++ b/includes/Utilities/Plugin_Request_Utility.php @@ -8,6 +8,8 @@ namespace WordPress\Plugin_Check\Utilities; use Exception; +use WordPress\Plugin_Check\Checker\CLI_Runner; +use WordPress\Plugin_Check\Checker\AJAX_Runner; /** * Class providing utility methods to return plugin information based on the request. @@ -16,6 +18,22 @@ */ class Plugin_Request_Utility { + /** + * Instance of the current runner based on the request. + * + * @since n.e.x.t + * @var Abstract_Check_Runner + */ + protected static $runner; + + /** + * The universal runtime preparation cleanups if applied. + * + * @since n.e.x.t + * @var callable + */ + protected static $cleanup; + /** * Returns the plugin basename based on the input provided. * @@ -60,4 +78,39 @@ public static function get_plugin_basename_from_input( $plugin_slug ) { ) ); } + + /** + * Initializes the runner classes. + * + * @since n.e.x.t + */ + public static function initialize_runner() { + $runners = array( + new CLI_Runner(), + new AJAX_Runner(), + ); + + foreach ( $runners as $runner ) { + if ( $runner->is_plugin_check() ) { + static::$cleanup = $runner->prepare(); + static::$runner = $runner; + break; + } + } + } + + /** + * Get the Runner class for the current request. + * + * @since n.e.x.t + * + * @return Abstract_Check_Runner|null The Runner class for the request or null. + */ + public static function get_runner() { + if ( isset( static::$runner ) ) { + return static::$runner; + } + + return null; + } } diff --git a/tests/Utilities/Plugin_Request_Utility_Tests.php b/tests/Utilities/Plugin_Request_Utility_Tests.php index 47b677f57..a0ea70f0e 100644 --- a/tests/Utilities/Plugin_Request_Utility_Tests.php +++ b/tests/Utilities/Plugin_Request_Utility_Tests.php @@ -6,6 +6,8 @@ */ use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; +use WordPress\Plugin_Check\Checker\CLI_Runner; +use WordPress\Plugin_Check\Checker\AJAX_Runner; class Plugin_Request_Utility_Tests extends WP_UnitTestCase { @@ -28,4 +30,31 @@ public function test_get_plugin_basename_from_input_with_invalid_input() { Plugin_Request_Utility::get_plugin_basename_from_input( 'invalid' ); } + + public function test_initialize_runner_with_cli() { + $_SERVER['argv'] = array( + 'wp', + 'plugin', + 'check', + 'plugin-check', + ); + + Plugin_Request_Utility::initialize_runner(); + $runner = Plugin_Request_Utility::get_runner(); + + unset( $_SERVER['argv'] ); + + $this->assertInstanceOf( CLI_Runner::class, $runner ); + } + + public function test_initialize_runner_with_ajax() { + add_filter( 'wp_doing_ajax', '__return_true' ); + $_REQUEST['action'] = 'plugin_check_run_checks'; + $_REQUEST['plugin'] = 'plugin-check'; + + Plugin_Request_Utility::initialize_runner(); + $runner = Plugin_Request_Utility::get_runner(); + + $this->assertInstanceOf( AJAX_Runner::class, $runner ); + } } From 1cdc76736cbee1f9648f8060fa260172aa9d3406 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Thu, 16 Feb 2023 16:12:05 +0000 Subject: [PATCH 2/8] create object-cache.php file --- object-cache.copy.php | 55 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 object-cache.copy.php diff --git a/object-cache.copy.php b/object-cache.copy.php new file mode 100644 index 000000000..0ae2f01e2 --- /dev/null +++ b/object-cache.copy.php @@ -0,0 +1,55 @@ + + Date: Thu, 16 Feb 2023 17:37:22 +0000 Subject: [PATCH 3/8] create Runtime_Environment_Setup class --- .../Checker/Runtime_Environment_Setup.php | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 includes/Checker/Runtime_Environment_Setup.php diff --git a/includes/Checker/Runtime_Environment_Setup.php b/includes/Checker/Runtime_Environment_Setup.php new file mode 100644 index 000000000..f9400eb8f --- /dev/null +++ b/includes/Checker/Runtime_Environment_Setup.php @@ -0,0 +1,87 @@ +set_prefix( 'wppc_' ); + + // Create and populate the test database tables if they do not exist. + if ( 'wppc_posts' !== $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { + wp_install( + 'Plugin Check', + 'plugincheck', + 'demo@plugincheck.test', + false + ); + } + + // Create the object-cache.php file. + if ( $wp_filesystem || WP_Filesystem() ) { + // Do not replace the object-cache.php file if it already exists. + if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { + $wp_filesystem->copy( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php', WP_CONTENT_DIR . '/object-cache.php' ); + } + } + } + + /** + * Cleans up the runtime environment setup. + * + * @since n.e.x.t + * + * @return void + */ + public function cleanup() { + global $wpdb, $wp_filesystem; + + require_once ABSPATH . '/wp-admin/includes/upgrade.php'; + + $prefix = $wpdb->set_prefix( 'wppc_' ); + $tables = $wpdb->tables(); + + foreach ( $tables as $table ) { + $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared + } + + $wpdb->set_prefix( $prefix ); + + // Remove the object-cache.php file. + if ( $wp_filesystem || WP_Filesystem() ) { + if ( ! $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ) { + return; + } + + // Check the drop-in file matches the copy. + $original_content = $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ); + $copy_content = $wp_filesystem->get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ); + + if ( $original_content && $original_content === $copy_content ) { + $wp_filesystem->delete( WP_CONTENT_DIR . '/object-cache.php' ); + } + } + } +} From cc61df6375e431f81990821f27dd370e1f400555 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Thu, 16 Feb 2023 18:51:59 +0000 Subject: [PATCH 4/8] add Runtime_Environment_Setup tests --- .../Checker/Runtime_Environment_Setup.php | 4 +- .../Runtime_Environment_Setup_Tests.php | 59 ++++++++ .../WP_Filesystem_MockFilesystem.php | 141 ++++++++++++++++++ 3 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 tests/Checker/Runtime_Environment_Setup_Tests.php create mode 100644 tests/testdata/Filesystem/WP_Filesystem_MockFilesystem.php diff --git a/includes/Checker/Runtime_Environment_Setup.php b/includes/Checker/Runtime_Environment_Setup.php index f9400eb8f..8c1d32478 100644 --- a/includes/Checker/Runtime_Environment_Setup.php +++ b/includes/Checker/Runtime_Environment_Setup.php @@ -27,7 +27,7 @@ public function setup() { require_once ABSPATH . '/wp-admin/includes/upgrade.php'; // Set the new prefix. - $wpdb->set_prefix( 'wppc_' ); + $prefix = $wpdb->set_prefix( 'wppc_' ); // Create and populate the test database tables if they do not exist. if ( 'wppc_posts' !== $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { @@ -39,6 +39,8 @@ public function setup() { ); } + $wpdb->set_prefix( $prefix ); + // Create the object-cache.php file. if ( $wp_filesystem || WP_Filesystem() ) { // Do not replace the object-cache.php file if it already exists. diff --git a/tests/Checker/Runtime_Environment_Setup_Tests.php b/tests/Checker/Runtime_Environment_Setup_Tests.php new file mode 100644 index 000000000..9512d44b1 --- /dev/null +++ b/tests/Checker/Runtime_Environment_Setup_Tests.php @@ -0,0 +1,59 @@ +set_up_mock_filesystem(); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->setup(); + + $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); + $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); + $this->assertSame( file_get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ), $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); + } + + public function test_cleanup() { + global $wp_filesystem, $wpdb; + + $this->set_up_mock_filesystem(); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->setup(); + $runtime_setup->cleanup(); + + $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); + $this->assertFalse( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); + } + + private function set_up_mock_filesystem() { + global $wp_filesystem; + + add_filter( + 'filesystem_method_file', + function() { + return __DIR__ . '/../testdata/Filesystem/WP_Filesystem_MockFilesystem.php'; + } + ); + add_filter( + 'filesystem_method', + function() { + return 'MockFilesystem'; + } + ); + + WP_Filesystem(); + + // Simulate that the original object-cache.copy.php file exists. + $wp_filesystem->put_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php', file_get_contents( WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . 'object-cache.copy.php' ) ); + } +} diff --git a/tests/testdata/Filesystem/WP_Filesystem_MockFilesystem.php b/tests/testdata/Filesystem/WP_Filesystem_MockFilesystem.php new file mode 100644 index 000000000..8729c2062 --- /dev/null +++ b/tests/testdata/Filesystem/WP_Filesystem_MockFilesystem.php @@ -0,0 +1,141 @@ +file_contents[ $file ] ) ) { + return $this->file_contents[ $file ]; + } + return false; + } + + public function get_contents_array( $file ) { + if ( isset( $this->file_contents[ $file ] ) ) { + return array( $this->file_contents[ $file ] ); + } + return false; + } + + public function put_contents( $file, $contents, $mode = false ) { + $this->file_contents[ $file ] = $contents; + return true; + } + + public function cwd() { + return false; + } + + public function chdir( $dir ) { + return false; + } + + public function chgrp( $file, $group, $recursive = false ) { + return false; + } + + public function chmod( $file, $mode = false, $recursive = false ) { + return false; + } + + public function owner( $file ) { + return false; + } + + public function group( $file ) { + return false; + } + + public function copy( $source, $destination, $overwrite = false, $mode = false ) { + if ( ! isset( $this->file_contents[ $source ] ) ) { + return false; + } + if ( ! $overwrite && isset( $this->file_contents[ $destination ] ) ) { + return false; + } + $this->file_contents[ $destination ] = $this->file_contents[ $source ]; + return true; + } + + public function move( $source, $destination, $overwrite = false ) { + if ( $this->copy( $source, $destination, $overwrite, false ) ) { + return $this->delete( $source ); + } + return false; + } + + public function delete( $file, $recursive = false, $type = false ) { + if ( isset( $this->file_contents[ $file ] ) ) { + unset( $this->file_contents[ $file ] ); + } + return true; + } + + public function exists( $path ) { + return isset( $this->file_contents[ $path ] ); + } + + public function is_file( $file ) { + return isset( $this->file_contents[ $file ] ); + } + + public function is_dir( $path ) { + return false; + } + + public function is_readable( $file ) { + return isset( $this->file_contents[ $file ] ); + } + + public function is_writable( $path ) { + return true; + } + + public function atime( $file ) { + return false; + } + + public function mtime( $file ) { + return false; + } + + public function size( $file ) { + if ( isset( $this->file_contents[ $file ] ) ) { + return strlen( $this->file_contents[ $file ] ); + } + return false; + } + + public function touch( $file, $time = 0, $atime = 0 ) { + if ( ! isset( $this->file_contents[ $file ] ) ) { + $this->file_contents[ $file ] = ''; + } + return true; + } + + public function mkdir( $path, $chmod = false, $chown = false, $chgrp = false ) { + return false; + } + + public function rmdir( $path, $recursive = false ) { + return false; + } + + public function dirlist( $path, $include_hidden = true, $recursive = false ) { + return false; + } +} From 396a47eacf87626b3acfbe1822be1ec3f3ffa153 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Feb 2023 13:06:24 +0000 Subject: [PATCH 5/8] update Runtime_Environment_Setup prefix and object-cache.php --- .../Checker/Runtime_Environment_Setup.php | 26 ++++++++++++------- includes/Utilities/Plugin_Request_Utility.php | 1 + object-cache.copy.php | 14 +--------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/includes/Checker/Runtime_Environment_Setup.php b/includes/Checker/Runtime_Environment_Setup.php index 8c1d32478..1a7c90502 100644 --- a/includes/Checker/Runtime_Environment_Setup.php +++ b/includes/Checker/Runtime_Environment_Setup.php @@ -18,8 +18,6 @@ class Runtime_Environment_Setup { * Sets up the WordPress environment for runtime checks * * @since n.e.x.t - * - * @return void */ public function setup() { global $wpdb, $wp_filesystem; @@ -27,7 +25,7 @@ public function setup() { require_once ABSPATH . '/wp-admin/includes/upgrade.php'; // Set the new prefix. - $prefix = $wpdb->set_prefix( 'wppc_' ); + $old_prefix = $wpdb->set_prefix( 'wppc_' ); // Create and populate the test database tables if they do not exist. if ( 'wppc_posts' !== $wpdb->get_var( "SHOW TABLES LIKE 'wppc_posts'" ) ) { @@ -39,7 +37,13 @@ public function setup() { ); } - $wpdb->set_prefix( $prefix ); + // Restore the old prefix. + $wpdb->set_prefix( $old_prefix ); + + // Return early if the plugin check object cache already exists. + if ( defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) && WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { + return; + } // Create the object-cache.php file. if ( $wp_filesystem || WP_Filesystem() ) { @@ -54,22 +58,26 @@ public function setup() { * Cleans up the runtime environment setup. * * @since n.e.x.t - * - * @return void */ public function cleanup() { global $wpdb, $wp_filesystem; require_once ABSPATH . '/wp-admin/includes/upgrade.php'; - $prefix = $wpdb->set_prefix( 'wppc_' ); - $tables = $wpdb->tables(); + $old_prefix = $wpdb->set_prefix( 'wppc_' ); + $tables = $wpdb->tables(); foreach ( $tables as $table ) { $wpdb->query( "DROP TABLE IF EXISTS `$table`" ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared } - $wpdb->set_prefix( $prefix ); + // Restore the old prefix. + $wpdb->set_prefix( $old_prefix ); + + // Return early if the plugin check object cache does not exists. + if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { + return; + } // Remove the object-cache.php file. if ( $wp_filesystem || WP_Filesystem() ) { diff --git a/includes/Utilities/Plugin_Request_Utility.php b/includes/Utilities/Plugin_Request_Utility.php index 01af50513..10c9b5721 100644 --- a/includes/Utilities/Plugin_Request_Utility.php +++ b/includes/Utilities/Plugin_Request_Utility.php @@ -92,6 +92,7 @@ public static function initialize_runner() { foreach ( $runners as $runner ) { if ( $runner->is_plugin_check() ) { + // @TODO: Handle the cleanup function in later issue with shutdown action or method that returns cleanup functions. static::$cleanup = $runner->prepare(); static::$runner = $runner; break; diff --git a/object-cache.copy.php b/object-cache.copy.php index 0ae2f01e2..3684c7b1e 100644 --- a/object-cache.copy.php +++ b/object-cache.copy.php @@ -19,24 +19,14 @@ * This file respects any real object cache implementation the site may already * be using, and it is implemented in a way that there is no risk for breakage. * - * If you do not want the Plugin Check plugin to place this file and thus be - * limited to static checks, you can remove this file and set the following - * constant (e.g. in wp-config.php): - * - * define( 'WP_PLUGIN_CHECK_DISABLE_OBJECT_CACHE_DROPIN', true ); - * * @package plugin-check * @since n.e.x.t */ // Set constant to be able to later check for whether this file was loaded. -define( 'WP_PLUGIN_CHECK_PERFLAB_OBJECT_CACHE_DROPIN_VERSION', 1 ); +define( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION', 1 ); function plugin_check_initialize_runner() { - if ( defined( 'WP_PLUGIN_CHECK_DISABLE_OBJECT_CACHE_DROPIN' ) && WP_PLUGIN_CHECK_DISABLE_OBJECT_CACHE_DROPIN ) { - return; - } - $plugins_dir = defined( 'WP_PLUGIN_DIR' ) ? WP_PLUGIN_DIR : WP_CONTENT_DIR . '/plugins'; $plugin_dir = $plugins_dir . '/plugin-check/'; if ( ! file_exists( $plugin_dir . 'vendor/autoload.php' ) ) { @@ -48,8 +38,6 @@ function plugin_check_initialize_runner() { if ( class_exists( 'WordPress\Plugin_Check\Utilities\Plugin_Request_Utility' ) ) { // Initialize the Check Runner class based on the request. WordPress\Plugin_Check\Utilities\Plugin_Request_Utility::initialize_runner(); - - var_dump('initialize!'); } } plugin_check_initialize_runner(); From d6e71900d4e0124da37b870c4a0356aae19f287b Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Feb 2023 13:59:31 +0000 Subject: [PATCH 6/8] add tests with existing object-cache.php file --- .../Runtime_Environment_Setup_Tests.php | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/tests/Checker/Runtime_Environment_Setup_Tests.php b/tests/Checker/Runtime_Environment_Setup_Tests.php index 9512d44b1..7f44fddc3 100644 --- a/tests/Checker/Runtime_Environment_Setup_Tests.php +++ b/tests/Checker/Runtime_Environment_Setup_Tests.php @@ -29,12 +29,51 @@ public function test_cleanup() { $runtime_setup = new Runtime_Environment_Setup(); $runtime_setup->setup(); + + // Simulate file exists by setting constant found in object-cache.php. + define( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION', 1 ); + $runtime_setup->cleanup(); $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); $this->assertFalse( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); } + public function test_setup_with_existing_object_cache() { + global $wp_filesystem, $wpdb; + + $this->set_up_mock_filesystem(); + + // Simulate a different object-cache.php. + $dummy_file_content = 'put_contents( WP_CONTENT_DIR . '/object-cache.php', $dummy_file_content ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->setup(); + + $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); + $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); + $this->assertSame( $dummy_file_content, $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); + } + + public function test_cleanup_with_existing_object_cache() { + global $wp_filesystem, $wpdb; + + $this->set_up_mock_filesystem(); + + // Simulate a different object-cache.php. + $dummy_file_content = 'put_contents( WP_CONTENT_DIR . '/object-cache.php', $dummy_file_content ); + + $runtime_setup = new Runtime_Environment_Setup(); + $runtime_setup->setup(); + $runtime_setup->cleanup(); + + $this->assertTrue( 0 <= strpos( $wpdb->last_query, 'wppc_' ) ); + $this->assertTrue( $wp_filesystem->exists( WP_CONTENT_DIR . '/object-cache.php' ) ); + $this->assertSame( $dummy_file_content, $wp_filesystem->get_contents( WP_CONTENT_DIR . '/object-cache.php' ) ); + } + private function set_up_mock_filesystem() { global $wp_filesystem; From 1e5a447f8ccf300df64981787b80322416127472 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Tue, 21 Feb 2023 14:20:03 +0000 Subject: [PATCH 7/8] set database prefix in runtime preparation --- includes/Checker/Abstract_Check_Runner.php | 12 +++++++++++- tests/Checker/AJAX_Runner_Tests.php | 8 ++++++++ tests/Checker/CLI_Runner_Tests.php | 8 ++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/includes/Checker/Abstract_Check_Runner.php b/includes/Checker/Abstract_Check_Runner.php index b1762344a..b09d27a86 100644 --- a/includes/Checker/Abstract_Check_Runner.php +++ b/includes/Checker/Abstract_Check_Runner.php @@ -55,7 +55,17 @@ abstract protected function get_check_slugs_to_run(); public function prepare() { if ( $this->requires_universal_preparations( $this->get_checks_to_run() ) ) { $preparation = new Universal_Runtime_Preparation( $this->get_checks_instance()->context() ); - return $preparation->prepare(); + $cleanup = $preparation->prepare(); + + // Set the database prefix to use the demo tables. + global $wpdb; + $old_prefix = $wpdb->set_prefix( 'wppc_' ); + + return function() use ( $old_prefix, $cleanup ) { + global $wpdb; + $wpdb->set_prefix( $old_prefix ); + $cleanup(); + }; } return function() {}; diff --git a/tests/Checker/AJAX_Runner_Tests.php b/tests/Checker/AJAX_Runner_Tests.php index 52a4160c3..c70eba6d4 100644 --- a/tests/Checker/AJAX_Runner_Tests.php +++ b/tests/Checker/AJAX_Runner_Tests.php @@ -10,6 +10,14 @@ class AJAX_Runner_Tests extends WP_UnitTestCase { + public function tear_down() { + // Force reset the database prefix after runner prepare method called. + global $wpdb; + $wpdb->set_prefix( 'wp_' ); + + parent::tear_down(); + } + public function test_is_plugin_check_returns_true() { // Mock the AJAX request. add_filter( 'wp_doing_ajax', '__return_true' ); diff --git a/tests/Checker/CLI_Runner_Tests.php b/tests/Checker/CLI_Runner_Tests.php index 278f3f211..8de93f1f1 100644 --- a/tests/Checker/CLI_Runner_Tests.php +++ b/tests/Checker/CLI_Runner_Tests.php @@ -10,6 +10,14 @@ class CLI_Runner_Tests extends WP_UnitTestCase { + public function tear_down() { + // Force reset the database prefix after runner prepare method called. + global $wpdb; + $wpdb->set_prefix( 'wp_' ); + + parent::tear_down(); + } + public function test_is_plugin_check_returns_true() { $_SERVER['argv'] = array( 'wp', From 2745ff9b24f4148afbcbc6bc3e349a83bdfa1593 Mon Sep 17 00:00:00 2001 From: jjgrainger Date: Wed, 22 Feb 2023 11:30:18 +0000 Subject: [PATCH 8/8] use $table_prefix global to restore wpdb prefix --- includes/Checker/Runtime_Environment_Setup.php | 2 +- tests/Checker/AJAX_Runner_Tests.php | 5 ++--- tests/Checker/CLI_Runner_Tests.php | 5 ++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/includes/Checker/Runtime_Environment_Setup.php b/includes/Checker/Runtime_Environment_Setup.php index 1a7c90502..78acb4dc1 100644 --- a/includes/Checker/Runtime_Environment_Setup.php +++ b/includes/Checker/Runtime_Environment_Setup.php @@ -74,7 +74,7 @@ public function cleanup() { // Restore the old prefix. $wpdb->set_prefix( $old_prefix ); - // Return early if the plugin check object cache does not exists. + // Return early if the plugin check object cache does not exist. if ( ! defined( 'WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION' ) || ! WP_PLUGIN_CHECK_OBJECT_CACHE_DROPIN_VERSION ) { return; } diff --git a/tests/Checker/AJAX_Runner_Tests.php b/tests/Checker/AJAX_Runner_Tests.php index c70eba6d4..c3c82ac7a 100644 --- a/tests/Checker/AJAX_Runner_Tests.php +++ b/tests/Checker/AJAX_Runner_Tests.php @@ -12,9 +12,8 @@ class AJAX_Runner_Tests extends WP_UnitTestCase { public function tear_down() { // Force reset the database prefix after runner prepare method called. - global $wpdb; - $wpdb->set_prefix( 'wp_' ); - + global $wpdb, $table_prefix; + $wpdb->set_prefix( $table_prefix ); parent::tear_down(); } diff --git a/tests/Checker/CLI_Runner_Tests.php b/tests/Checker/CLI_Runner_Tests.php index 8de93f1f1..4b416ac16 100644 --- a/tests/Checker/CLI_Runner_Tests.php +++ b/tests/Checker/CLI_Runner_Tests.php @@ -12,9 +12,8 @@ class CLI_Runner_Tests extends WP_UnitTestCase { public function tear_down() { // Force reset the database prefix after runner prepare method called. - global $wpdb; - $wpdb->set_prefix( 'wp_' ); - + global $wpdb, $table_prefix; + $wpdb->set_prefix( $table_prefix ); parent::tear_down(); }