From 17714f80c9166c8a5e98d4e746218705bc29ab33 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Wed, 1 Feb 2023 18:19:30 +0530 Subject: [PATCH 1/5] Add Universal_Runtime_Preparation class --- .../Universal_Runtime_Preparation.php | 69 +++++++++++++++++++ .../Universal_Runtime_Preparation_Tests.php | 51 ++++++++++++++ tests/bootstrap.php | 3 + 3 files changed, 123 insertions(+) create mode 100644 includes/Checker/Preparations/Universal_Runtime_Preparation.php create mode 100644 tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php diff --git a/includes/Checker/Preparations/Universal_Runtime_Preparation.php b/includes/Checker/Preparations/Universal_Runtime_Preparation.php new file mode 100644 index 000000000..0d53f3306 --- /dev/null +++ b/includes/Checker/Preparations/Universal_Runtime_Preparation.php @@ -0,0 +1,69 @@ +check_context = $check_context; + } + + /** + * Runs the preparations for the theme and plugin. And returns a cleanup function. + * + * @since n.e.x.t + * + * @return callable Cleanup function to revert changes made by theme and plugin preparation classes. + * + * @throws Exception Thrown when preparation fails. + */ + public function prepare() { + + $cleanup_functions = array(); + + $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' ); + $cleanup_functions[] = $use_minimal_theme_preparation->prepare(); + + $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( 'plugin-check/plugin-check.php' ); + $cleanup_functions[] = $force_single_plugin_preparation->prepare(); + + // Return the cleanup function. + return function () use ( $cleanup_functions ) { + + foreach ( $cleanup_functions as $cleanup_function ) { + + callback( $cleanup_function ); + } + }; + } +} diff --git a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php new file mode 100644 index 000000000..5733ada6f --- /dev/null +++ b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -0,0 +1,51 @@ +prepare(); + + $cleanup(); + + $this->assertIsCallable( $cleanup ); + + $this->assertEquals( 'wp-empty-theme', get_option( 'template' ) ); + + $active_plugins = get_option( 'active_plugins' ); + + $this->assertContains( 'plugin-check/plugin-check.php', $active_plugins ); + + $this->assertEquals( + array( + 'plugin-check/plugin-check.php', + 'plugin-check/plugin-check.php', + ), + $active_plugins + ); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 8857a0030..23e70ff70 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -26,3 +26,6 @@ // Start up the WP testing environment. require $_test_root . '/includes/bootstrap.php'; + +// Load the plugin checker plugin, so the important constants and functions can be used in unit test. +require_once TESTS_PLUGIN_DIR . '/plugin-check.php'; From 7cce8894a9d041300a3a6d48fbc769a79114a7a2 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Mon, 6 Feb 2023 11:13:41 +0530 Subject: [PATCH 2/5] Fix unit test --- .../Universal_Runtime_Preparation_Tests.php | 24 +++++++++---------- tests/bootstrap.php | 3 --- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php index 5733ada6f..0a205b4d9 100644 --- a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php +++ b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -13,7 +13,17 @@ class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase { + protected $plugin_basename_file; + + public function set_up() { + parent::set_up(); + + $this->plugin_basename_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); + } + public function test_prepare() { + // Remove the WP tests active plugins filter which interfers with this test. + remove_filter( 'pre_option_active_plugins', 'wp_tests_options' ); $check_context = new Check_Context( 'test-plugin/test-plugin.php' ); @@ -21,7 +31,7 @@ public function test_prepare() { $plugins = array( 'akismet/akismet.php', - 'plugin-check/plugin-check.php', + $this->plugin_basename_file, 'wp-reset/wp-reset.php', ); @@ -31,21 +41,11 @@ public function test_prepare() { $cleanup(); - $this->assertIsCallable( $cleanup ); - $this->assertEquals( 'wp-empty-theme', get_option( 'template' ) ); $active_plugins = get_option( 'active_plugins' ); - $this->assertContains( 'plugin-check/plugin-check.php', $active_plugins ); - - $this->assertEquals( - array( - 'plugin-check/plugin-check.php', - 'plugin-check/plugin-check.php', - ), - $active_plugins - ); + $this->assertSame( array( $this->plugin_basename_file ), $active_plugins ); } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index de257675a..59daf57ba 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -31,6 +31,3 @@ // Start up the WP testing environment. require $_test_root . '/includes/bootstrap.php'; - -// Load the plugin checker plugin, so the important constants and functions can be used in unit test. -require_once TESTS_PLUGIN_DIR . '/plugin-check.php'; From 600f8a6cc8c635236cbf20505674336ec851c49f Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Mon, 6 Feb 2023 11:18:32 +0530 Subject: [PATCH 3/5] Remove unwanted code --- .../Universal_Runtime_Preparation_Tests.php | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php index 0a205b4d9..1612cc6a1 100644 --- a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php +++ b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -13,15 +13,10 @@ class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase { - protected $plugin_basename_file; - - public function set_up() { - parent::set_up(); + public function test_prepare() { - $this->plugin_basename_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); - } + $plugin_basename_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); - public function test_prepare() { // Remove the WP tests active plugins filter which interfers with this test. remove_filter( 'pre_option_active_plugins', 'wp_tests_options' ); @@ -31,7 +26,7 @@ public function test_prepare() { $plugins = array( 'akismet/akismet.php', - $this->plugin_basename_file, + $plugin_basename_file, 'wp-reset/wp-reset.php', ); @@ -45,7 +40,7 @@ public function test_prepare() { $active_plugins = get_option( 'active_plugins' ); - $this->assertSame( array( $this->plugin_basename_file ), $active_plugins ); + $this->assertSame( array( $plugin_basename_file ), $active_plugins ); } } From 1e9047e3551359e43e4301a9c1d5e7111e775132 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Tue, 7 Feb 2023 10:59:40 +0530 Subject: [PATCH 4/5] Address feedbacks --- .../Preparations/Universal_Runtime_Preparation.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/includes/Checker/Preparations/Universal_Runtime_Preparation.php b/includes/Checker/Preparations/Universal_Runtime_Preparation.php index 0d53f3306..e615ff747 100644 --- a/includes/Checker/Preparations/Universal_Runtime_Preparation.php +++ b/includes/Checker/Preparations/Universal_Runtime_Preparation.php @@ -10,7 +10,6 @@ use WordPress\Plugin_Check\Checker\Check_Context; use WordPress\Plugin_Check\Checker\Preparation; use Exception; -use function PHPUnit\Framework\callback; /** * Class handle all preparations required for when at least one `Runtime_Check` is being run. @@ -39,7 +38,11 @@ public function __construct( Check_Context $check_context ) { } /** - * Runs the preparations for the theme and plugin. And returns a cleanup function. + * Runs preparation step for the environment by modifying the plugins and theme to use, + * and returns a closure as a cleanup function. + * + * This preparation needs to be called very early in the WordPress lifecycle, before + * plugins are loaded, e.g. from a drop-in like `object-cache.php`. * * @since n.e.x.t * @@ -61,8 +64,7 @@ public function prepare() { return function () use ( $cleanup_functions ) { foreach ( $cleanup_functions as $cleanup_function ) { - - callback( $cleanup_function ); + $cleanup_function(); } }; } From b27add4cc49b17f6a1ea5fdef9d7f5e0c3ba5308 Mon Sep 17 00:00:00 2001 From: vishalkakadiya Date: Tue, 7 Feb 2023 11:35:06 +0530 Subject: [PATCH 5/5] Address feedbacks --- .../Universal_Runtime_Preparation.php | 2 +- .../Universal_Runtime_Preparation_Tests.php | 41 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/includes/Checker/Preparations/Universal_Runtime_Preparation.php b/includes/Checker/Preparations/Universal_Runtime_Preparation.php index e615ff747..cd4ab8793 100644 --- a/includes/Checker/Preparations/Universal_Runtime_Preparation.php +++ b/includes/Checker/Preparations/Universal_Runtime_Preparation.php @@ -57,7 +57,7 @@ public function prepare() { $use_minimal_theme_preparation = new Use_Minimal_Theme_Preparation( 'wp-empty-theme', WP_PLUGIN_CHECK_PLUGIN_DIR_PATH . '/test-content/themes' ); $cleanup_functions[] = $use_minimal_theme_preparation->prepare(); - $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( 'plugin-check/plugin-check.php' ); + $force_single_plugin_preparation = new Force_Single_Plugin_Preparation( $this->check_context->basename() ); $cleanup_functions[] = $force_single_plugin_preparation->prepare(); // Return the cleanup function. diff --git a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php index 1612cc6a1..c805b0e21 100644 --- a/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php +++ b/tests/Checker/Preparations/Universal_Runtime_Preparation_Tests.php @@ -14,33 +14,32 @@ class Universal_Runtime_Preparation_Tests extends WP_UnitTestCase { public function test_prepare() { - - $plugin_basename_file = plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ); - - // Remove the WP tests active plugins filter which interfers with this test. - remove_filter( 'pre_option_active_plugins', 'wp_tests_options' ); - - $check_context = new Check_Context( 'test-plugin/test-plugin.php' ); + $check_context = new Check_Context( plugin_basename( WP_PLUGIN_CHECK_MAIN_FILE ) ); $universal_runtime_preparation = new Universal_Runtime_Preparation( $check_context ); - $plugins = array( - 'akismet/akismet.php', - $plugin_basename_file, - 'wp-reset/wp-reset.php', - ); - - update_option( 'active_plugins', $plugins ); - $cleanup = $universal_runtime_preparation->prepare(); - $cleanup(); - - $this->assertEquals( 'wp-empty-theme', get_option( 'template' ) ); + $this->assertTrue( has_filter( 'option_active_plugins' ) ); + $this->assertTrue( has_filter( 'default_option_active_plugins' ) ); + $this->assertTrue( has_filter( 'stylesheet' ) ); + $this->assertTrue( has_filter( 'template' ) ); + $this->assertTrue( has_filter( 'pre_option_template' ) ); + $this->assertTrue( has_filter( 'pre_option_stylesheet' ) ); + $this->assertTrue( has_filter( 'pre_option_current_theme' ) ); + $this->assertTrue( has_filter( 'pre_option_template_root' ) ); + $this->assertTrue( has_filter( 'pre_option_stylesheet_root' ) ); - $active_plugins = get_option( 'active_plugins' ); + $cleanup(); - $this->assertSame( array( $plugin_basename_file ), $active_plugins ); + $this->assertFalse( has_filter( 'option_active_plugins' ) ); + $this->assertFalse( has_filter( 'default_option_active_plugins' ) ); + $this->assertFalse( has_filter( 'stylesheet' ) ); + $this->assertFalse( has_filter( 'template' ) ); + $this->assertFalse( has_filter( 'pre_option_template' ) ); + $this->assertFalse( has_filter( 'pre_option_stylesheet' ) ); + $this->assertFalse( has_filter( 'pre_option_current_theme' ) ); + $this->assertFalse( has_filter( 'pre_option_template_root' ) ); + $this->assertFalse( has_filter( 'pre_option_stylesheet_root' ) ); } - }