Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/wp-admin/includes/class-plugin-upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -541,12 +541,25 @@ public function plugin_info() {
return false;
}

// Assume the requested plugin is the first in the list.
$plugin_files = array_keys( $plugin );
$plugin_files = array_keys( $plugin );
$plugin_file = $plugin_files[0];
$expected_plugin_file = $this->result['destination_name'] . '.php';

return $this->result['destination_name'] . '/' . $plugin_files[0];
}
// Prefer a root-level plugin file that matches the plugin directory name.
if ( in_array( $expected_plugin_file, $plugin_files, true ) ) {
$plugin_file = $expected_plugin_file;
} else {
// Otherwise, use the first root-level plugin file.
foreach ( $plugin_files as $candidate_plugin_file ) {
if ( ! str_contains( $candidate_plugin_file, '/' ) ) {
$plugin_file = $candidate_plugin_file;
break;
}
}
}

return $this->result['destination_name'] . '/' . $plugin_file;
}
/**
* Deactivates a plugin before it is upgraded.
*
Expand Down
105 changes: 105 additions & 0 deletions tests/phpunit/tests/admin/pluginUpgrader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* Tests the `Plugin_Upgrader` class.
*
* @group admin
* @group upgrade
*/
class Tests_Admin_PluginUpgrader extends WP_UnitTestCase {

/**
* Test plugin directories and files.
*
* @var array[]
*/
private static $test_plugins = array(
'plugin-upgrader-nested-test' => array(
'main.php' => 'Z Main Plugin',
'embedded/embedded.php' => 'A Embedded Plugin',
),
'plugin-upgrader-slug-test' => array(
'another.php' => 'A Another Plugin',
'plugin-upgrader-slug-test.php' => 'Z Slug Plugin',
),
);

/**
* Creates the test plugins before any tests run.
*/
public static function set_up_before_class() {
parent::set_up_before_class();

require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
require_once ABSPATH . 'wp-admin/includes/class-plugin-upgrader.php';

foreach ( self::$test_plugins as $directory => $files ) {
foreach ( $files as $file => $name ) {
$path = WP_PLUGIN_DIR . "/$directory/$file";

wp_mkdir_p( dirname( $path ) );
file_put_contents( $path, "<?php\n/**\n * Plugin Name: $name\n */" );
}
}
}

/**
* Removes the test plugins after all tests run.
*/
public static function tear_down_after_class() {
foreach ( self::$test_plugins as $directory => $files ) {
foreach ( $files as $file => $name ) {
unlink( WP_PLUGIN_DIR . "/$directory/$file" );
}

$subdirectories = glob( WP_PLUGIN_DIR . "/$directory/*", GLOB_ONLYDIR );
foreach ( $subdirectories as $subdirectory ) {
rmdir( $subdirectory );
}

rmdir( WP_PLUGIN_DIR . "/$directory" );
}

wp_clean_plugins_cache( false );

parent::tear_down_after_class();
}

/**
* Tests that the main plugin file is selected for the activation link.
*
* @ticket 22287
*
* @covers Plugin_Upgrader::plugin_info
*
* @dataProvider data_plugin_info
*
* @param string $destination_name Plugin destination directory name.
* @param string $expected Expected main plugin file.
*/
public function test_plugin_info_should_select_main_plugin( $destination_name, $expected ) {
wp_clean_plugins_cache( false );

$upgrader = new Plugin_Upgrader();
$upgrader->result = array( 'destination_name' => $destination_name );

$this->assertSame( $expected, $upgrader->plugin_info() );
}

/**
* Data provider.
*
* @return array[]
*/
public function data_plugin_info() {
return array(
'nested plugin' => array(
'destination_name' => 'plugin-upgrader-nested-test',
'expected' => 'plugin-upgrader-nested-test/main.php',
),
'matching slug file' => array(
'destination_name' => 'plugin-upgrader-slug-test',
'expected' => 'plugin-upgrader-slug-test/plugin-upgrader-slug-test.php',
),
);
}
}
Loading