From 15d2d438c409a81cb8e7e4dd3620f0937c711b54 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Mon, 11 Nov 2024 17:11:02 -0800 Subject: [PATCH 1/2] Ensure runtime environment is set up before testing Universal_Runtime_Preparation. --- .../Universal_Runtime_Preparation_Tests.php | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/phpunit/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php index c805b0e21..0cd416c96 100644 --- a/tests/phpunit/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php +++ b/tests/phpunit/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -9,16 +9,43 @@ use WordPress\Plugin_Check\Checker\Check_Context; use WordPress\Plugin_Check\Checker\Preparations\Universal_Runtime_Preparation; +use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; use WP_UnitTestCase; class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase { + /** + * Storage for preparation cleanups that need to be run after a test. + * + * @var array + */ + private $cleanups = array(); + + public function tear_down() { + if ( count( $this->cleanups ) > 0 ) { + $this->cleanups = array_reverse( $this->cleanups ); + foreach ( $this->cleanups as $cleanup ) { + $cleanup(); + } + $this->cleanups = array(); + } + parent::tear_down(); + } + public function test_prepare() { $check_context = new Check_Context( plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ) ); + // The runtime environment must be prepared manually before regular runtime preparations. + $runtime = new Runtime_Environment_Setup(); + $runtime->set_up(); + $this->cleanups[] = function () use ( $runtime ) { + $runtime->clean_up(); + }; + $universal_runtime_preparation = new Universal_Runtime_Preparation( $check_context ); - $cleanup = $universal_runtime_preparation->prepare(); + $cleanup = $universal_runtime_preparation->prepare(); + $this->cleanups[] = $cleanup; $this->assertTrue( has_filter( 'option_active_plugins' ) ); $this->assertTrue( has_filter( 'default_option_active_plugins' ) ); @@ -32,6 +59,9 @@ public function test_prepare() { $cleanup(); + // If this is reached, the universal runtime preparation cleanup was already done, so we can remove it again. + array_pop( $this->cleanups ); + $this->assertFalse( has_filter( 'option_active_plugins' ) ); $this->assertFalse( has_filter( 'default_option_active_plugins' ) ); $this->assertFalse( has_filter( 'stylesheet' ) ); From 9703245dc5109f47747f36de8564f1605f962638 Mon Sep 17 00:00:00 2001 From: Felix Arntz Date: Tue, 12 Nov 2024 14:02:39 -0800 Subject: [PATCH 2/2] Fix another instance of missing Runtime_Environment_Setup. --- .../Plugin_Request_Utility_Tests.php | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php b/tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php index fc760ca26..80a36513d 100644 --- a/tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php +++ b/tests/phpunit/tests/Utilities/Plugin_Request_Utility_Tests.php @@ -11,6 +11,7 @@ use WordPress\Plugin_Check\Checker\Checks; use WordPress\Plugin_Check\Checker\Checks\General\I18n_Usage_Check; use WordPress\Plugin_Check\Checker\CLI_Runner; +use WordPress\Plugin_Check\Checker\Runtime_Environment_Setup; use WordPress\Plugin_Check\Test_Data\Runtime_Check; use WordPress\Plugin_Check\Test_Utils\Traits\With_Mock_Filesystem; use WordPress\Plugin_Check\Utilities\Plugin_Request_Utility; @@ -90,6 +91,16 @@ public function test_initialize_runner_with_ajax() { $_REQUEST['action'] = 'plugin_check_run_checks'; $_REQUEST['plugin'] = 'plugin-check'; + /* + * The runtime environment must be prepared manually before regular runtime preparations. + * This is necessary because in reality it happens in a separate AJAX request before. + */ + $runtime = new Runtime_Environment_Setup(); + $runtime->set_up(); + $this->cleanups[] = function () use ( $runtime ) { + $runtime->clean_up(); + }; + Plugin_Request_Utility::initialize_runner(); $this->cleanups[] = function () { Plugin_Request_Utility::destroy_runner(); @@ -109,7 +120,7 @@ public function test_initialize_runner_with_ajax() { public function test_destroy_runner_with_cli() { define( 'WP_CLI', true ); - global $wpdb, $table_prefix, $wp_actions; + global $wp_actions; $this->set_up_mock_filesystem(); @@ -151,7 +162,6 @@ function ( $checks ) { unset( $_SERVER['argv'] ); $wp_actions['muplugins_loaded'] = $muplugins_loaded; - $wpdb->set_prefix( $table_prefix ); $this->assertTrue( $prepared ); $this->assertTrue( $cleanup ); @@ -159,7 +169,7 @@ function ( $checks ) { } public function test_destroy_runner_with_ajax() { - global $wpdb, $table_prefix, $wp_actions; + global $wp_actions; $this->set_up_mock_filesystem(); @@ -168,6 +178,16 @@ public function test_destroy_runner_with_ajax() { $_REQUEST['plugin'] = 'plugin-check'; $_REQUEST['checks'] = array( 'runtime_check' ); + /* + * The runtime environment must be prepared manually before regular runtime preparations. + * This is necessary because in reality it happens in a separate AJAX request before. + */ + $runtime = new Runtime_Environment_Setup(); + $runtime->set_up(); + $this->cleanups[] = function () use ( $runtime ) { + $runtime->clean_up(); + }; + add_filter( 'wp_plugin_check_checks', function ( $checks ) { @@ -196,7 +216,6 @@ function ( $checks ) { $cleanup = ! has_filter( 'option_active_plugins' ); $runner = Plugin_Request_Utility::get_runner(); - $wpdb->set_prefix( $table_prefix ); $wp_actions['muplugins_loaded'] = $muplugins_loaded; $this->assertTrue( $prepared );