From 121e557dbfeccf7d11bd6f6be53e1c4b30acb575 Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 17 Apr 2024 11:30:52 +0545 Subject: [PATCH 1/2] Correct plugin file in bootstrap when scaffolding plugin test --- src/Scaffold_Command.php | 24 ++++++++++++++++++++++-- templates/plugin-bootstrap.mustache | 2 +- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/Scaffold_Command.php b/src/Scaffold_Command.php index c418a011..24ce5729 100644 --- a/src/Scaffold_Command.php +++ b/src/Scaffold_Command.php @@ -869,9 +869,29 @@ private function scaffold_plugin_theme_tests( $args, $assoc_args, $type ) { $wp_versions_to_test[] = 'latest'; $wp_versions_to_test[] = 'trunk'; + $main_file = ''; + + if ( 'plugin' === $type ) { + $all_plugins = get_plugins(); + + if ( ! empty( $all_plugins ) ) { + $filtered = array_filter( + array_keys( $all_plugins ), + static function ( $item ) use ( $slug ) { + return ( false !== strpos( $item, "{$slug}/" ) ); + } + ); + + if ( ! empty( $filtered ) ) { + $main_file = basename( reset( $filtered ) ); + } + } + } + $template_data = [ - "{$type}_slug" => $slug, - "{$type}_package" => $package, + "{$type}_slug" => $slug, + "{$type}_package" => $package, + "{$type}_main_file" => $main_file, ]; $force = Utils\get_flag_value( $assoc_args, 'force' ); diff --git a/templates/plugin-bootstrap.mustache b/templates/plugin-bootstrap.mustache index efaaa314..e35163a9 100644 --- a/templates/plugin-bootstrap.mustache +++ b/templates/plugin-bootstrap.mustache @@ -29,7 +29,7 @@ require_once "{$_tests_dir}/includes/functions.php"; * Manually load the plugin being tested. */ function _manually_load_plugin() { - require dirname( dirname( __FILE__ ) ) . '/{{plugin_slug}}.php'; + require dirname( dirname( __FILE__ ) ) . '/{{plugin_main_file}}'; } tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); From f9a0de9c846542917d83cc6cf54f3843d7721b1b Mon Sep 17 00:00:00 2001 From: Nilambar Sharma Date: Wed, 17 Apr 2024 11:57:22 +0545 Subject: [PATCH 2/2] Add feature test for update scaffolded plugin test bootstrap --- features/scaffold-plugin-tests.feature | 25 +++++++++++++++++++++++++ src/Scaffold_Command.php | 6 +++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/features/scaffold-plugin-tests.feature b/features/scaffold-plugin-tests.feature index da516dfa..4b91ae05 100644 --- a/features/scaffold-plugin-tests.feature +++ b/features/scaffold-plugin-tests.feature @@ -284,3 +284,28 @@ Feature: Scaffold plugin unit tests """ bootstrap.php """ + + Scenario: Scaffold plugin tests with custom main file + Given a WP install + And a wp-content/plugins/foo/bar.php file: + """ +