Skip to content

Commit ac5ed2d

Browse files
committed
Update tests
1 parent 39c63fd commit ac5ed2d

File tree

2 files changed

+111
-56
lines changed

2 files changed

+111
-56
lines changed

features/plugin-dependencies.feature

Lines changed: 88 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,52 @@ Feature: Plugin dependencies support
33
Background:
44
Given an empty cache
55

6+
@less-than-wp-6.5
7+
Scenario: Install plugin with dependencies using --with-dependencies flag
8+
Given a WP install
9+
10+
When I try `wp plugin install --with-dependencies bp-classic`
11+
Then STDERR should contain:
12+
"""
13+
Installing plugins with dependencies requires WordPress 6.5 or greater.
14+
"""
15+
616
@require-wp-6.5
717
Scenario: Install plugin with dependencies using --with-dependencies flag
818
Given a WP install
919

10-
When I run `wp plugin install akismet`
20+
When I run `wp plugin install --with-dependencies bp-classic`
1121
Then STDOUT should contain:
1222
"""
13-
Plugin installed successfully.
23+
Installing BuddyPress
24+
"""
25+
And STDOUT should contain:
26+
"""
27+
Installing BP Classic
28+
"""
29+
And STDOUT should contain:
30+
"""
31+
Success: Installed 2 of 2 plugins.
1432
"""
1533

16-
# Create a test plugin with dependencies
17-
And a wp-content/plugins/test-plugin/test-plugin.php file:
34+
When I run `wp plugin list --fields=name,status --format=csv`
35+
Then STDOUT should contain:
1836
"""
19-
<?php
20-
/**
21-
* Plugin Name: Test Plugin
22-
* Requires Plugins: akismet
23-
*/
37+
buddypress,inactive
38+
"""
39+
And STDOUT should contain:
40+
"""
41+
bp-classic,inactive
2442
"""
2543

26-
When I run `wp plugin delete akismet --quiet`
27-
Then STDOUT should be empty
44+
@less-than-wp-6.5
45+
Scenario: Install dependencies of an installed plugin
46+
Given a WP install
2847

29-
# Note: Testing with actual WP.org plugins that have dependencies would be better
30-
# but we'll test with a local plugin that declares dependencies
31-
When I run `wp plugin get test-plugin --field=requires_plugins`
32-
Then STDOUT should be:
48+
When I try `wp plugin install-dependencies akismet`
49+
Then STDERR should contain:
3350
"""
34-
akismet
51+
Installing plugin dependencies requires WordPress 6.5 or greater.
3552
"""
3653

3754
@require-wp-6.5
@@ -44,7 +61,7 @@ Feature: Plugin dependencies support
4461
<?php
4562
/**
4663
* Plugin Name: Test Plugin
47-
* Requires Plugins: akismet, hello
64+
* Requires Plugins: duplicate-post, debug-bar
4865
*/
4966
"""
5067

@@ -80,31 +97,76 @@ Feature: Plugin dependencies support
8097
<?php
8198
/**
8299
* Plugin Name: Test Plugin
83-
* Requires Plugins: hello
100+
* Requires Plugins: akismet, buddypress
84101
*/
85102
"""
86103

87-
When I run `wp plugin install-dependencies test-plugin --activate`
104+
When I try `wp plugin install-dependencies test-plugin --activate`
88105
Then STDOUT should contain:
89106
"""
90-
Installing 1 dependency for 'test-plugin'
107+
Installing 2 dependencies for 'test-plugin'
91108
"""
92109
And STDOUT should contain:
93110
"""
94-
Success:
111+
Plugin 'buddypress' activated
112+
"""
113+
And STDOUT should contain:
114+
"""
115+
Success: Installed 1 of 2 plugins.
116+
"""
117+
And STDERR should contain:
118+
"""
119+
Warning: akismet: Plugin already installed.
95120
"""
96121

97-
When I run `wp plugin list --name=hello --field=status`
98-
Then STDOUT should be:
122+
When I run `wp plugin list --fields=name,status --format=csv`
123+
Then STDOUT should contain:
124+
"""
125+
buddypress,active
126+
"""
127+
And STDOUT should contain:
128+
"""
129+
akismet,active
130+
"""
131+
# Only the dependencies are activated, not the plugin itself.
132+
And STDOUT should contain:
133+
"""
134+
test-plugin,inactive
135+
"""
136+
137+
@require-wp-6.5
138+
Scenario: Force install dependencies
139+
Given a WP install
140+
141+
# Create a test plugin with dependencies
142+
And a wp-content/plugins/test-plugin/test-plugin.php file:
143+
"""
144+
<?php
145+
/**
146+
* Plugin Name: Test Plugin
147+
* Requires Plugins: akismet
148+
*/
149+
"""
150+
151+
When I run `wp plugin install-dependencies test-plugin --force`
152+
Then STDOUT should contain:
153+
"""
154+
Installing 1 dependency for 'test-plugin'
155+
"""
156+
And STDOUT should contain:
157+
"""
158+
Installing Akismet
159+
"""
160+
And STDOUT should contain:
99161
"""
100-
active
162+
Success: Installed 1 of 1 plugins.
101163
"""
164+
And STDERR should be empty
102165

103166
@require-wp-6.5
104167
Scenario: Install plugin with no dependencies
105168
Given a WP install
106169

107-
# Create a test plugin without dependencies
108170
And a wp-content/plugins/test-plugin/test-plugin.php file:
109171
"""
110172
<?php
@@ -126,6 +188,6 @@ Feature: Plugin dependencies support
126188
When I try `wp plugin install-dependencies non-existent-plugin`
127189
Then STDERR should contain:
128190
"""
129-
Error:
191+
Error: The 'non-existent-plugin' plugin could not be found.
130192
"""
131193
And the return code should be 1

src/Plugin_Command.php

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,9 @@ public function install( $args, $assoc_args ) {
10951095

10961096
// If --with-dependencies is set, we need to handle dependencies
10971097
if ( Utils\get_flag_value( $assoc_args, 'with-dependencies', false ) ) {
1098+
if ( WP_CLI\Utils\wp_version_compare( '6.5', '<' ) ) {
1099+
WP_CLI::error( 'Installing plugins with dependencies requires WordPress 6.5 or greater.' );
1100+
}
10981101
$this->install_with_dependencies( $args, $assoc_args );
10991102
} else {
11001103
parent::install( $args, $assoc_args );
@@ -1167,27 +1170,21 @@ private function collect_dependencies( $slug, &$all_to_install, &$installed_trac
11671170
/**
11681171
* Gets the dependencies for a plugin.
11691172
*
1170-
* Uses WP_Plugin_Dependencies class if available (WordPress 6.5+),
1171-
* otherwise falls back to WordPress.org API.
1172-
*
11731173
* @param string $slug Plugin slug.
11741174
* @return array Array of dependency slugs.
11751175
*/
11761176
private function get_plugin_dependencies( $slug ) {
1177-
// Try to use WP_Plugin_Dependencies class if available (WordPress 6.5+)
1178-
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
1179-
// Find the plugin file for this slug
1180-
$plugins = get_plugins();
1181-
foreach ( $plugins as $plugin_file => $plugin_data ) {
1182-
$plugin_slug = dirname( $plugin_file );
1183-
if ( '.' === $plugin_slug ) {
1184-
$plugin_slug = basename( $plugin_file, '.php' );
1185-
}
1177+
// Find the plugin file for this slug
1178+
$plugins = get_plugins();
1179+
foreach ( $plugins as $plugin_file => $plugin_data ) {
1180+
$plugin_slug = dirname( $plugin_file );
1181+
if ( '.' === $plugin_slug ) {
1182+
$plugin_slug = basename( $plugin_file, '.php' );
1183+
}
11861184

1187-
if ( $plugin_slug === $slug ) {
1188-
WP_Plugin_Dependencies::initialize();
1189-
return WP_Plugin_Dependencies::get_dependencies( $plugin_file );
1190-
}
1185+
if ( $plugin_slug === $slug ) {
1186+
WP_Plugin_Dependencies::initialize();
1187+
return WP_Plugin_Dependencies::get_dependencies( $plugin_file );
11911188
}
11921189
}
11931190

@@ -1518,6 +1515,10 @@ public function is_active( $args, $assoc_args ) {
15181515
* [--activate-network]
15191516
* : If set, dependencies will be network activated immediately after install.
15201517
*
1518+
* [--force]
1519+
* : If set, the command will overwrite any installed version of the plugin, without prompting
1520+
* for confirmation.
1521+
*
15211522
* ## EXAMPLES
15221523
*
15231524
* # Install all dependencies of an installed plugin
@@ -1531,25 +1532,17 @@ public function is_active( $args, $assoc_args ) {
15311532
* @subcommand install-dependencies
15321533
*/
15331534
public function install_dependencies( $args, $assoc_args ) {
1535+
if ( WP_CLI\Utils\wp_version_compare( '6.5', '<' ) ) {
1536+
WP_CLI::error( 'Installing plugin dependencies requires WordPress 6.5 or greater.' );
1537+
}
1538+
15341539
$plugin = $this->fetcher->get_check( $args[0] );
15351540
$file = $plugin->file;
15361541

1537-
// Get dependencies using WP_Plugin_Dependencies if available (WordPress 6.5+)
15381542
$dependencies = [];
15391543

1540-
if ( class_exists( 'WP_Plugin_Dependencies' ) ) {
1541-
WP_Plugin_Dependencies::initialize();
1542-
// Initialize WP_Plugin_Dependencies
1543-
$dependencies = WP_Plugin_Dependencies::get_dependencies( $file );
1544-
} else {
1545-
// Fallback: Get dependencies from plugin header manually
1546-
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $file, false, false );
1547-
1548-
if ( ! empty( $plugin_data['RequiresPlugins'] ) ) {
1549-
// Parse the comma-separated list
1550-
$dependencies = array_map( 'trim', explode( ',', $plugin_data['RequiresPlugins'] ) );
1551-
}
1552-
}
1544+
WP_Plugin_Dependencies::initialize();
1545+
$dependencies = WP_Plugin_Dependencies::get_dependencies( $file );
15531546

15541547
if ( empty( $dependencies ) ) {
15551548
WP_CLI::success( "Plugin '{$args[0]}' has no dependencies." );

0 commit comments

Comments
 (0)