Skip to content
Draft
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
10 changes: 10 additions & 0 deletions src/wp-admin/includes/class-wp-upgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,8 @@ public function install_package( $args = array() ) {
*
* @since 2.8.0
*
* @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
*
* @param array $options {
* Array or string of arguments for upgrading/installing a package.
*
Expand All @@ -772,6 +774,7 @@ public function install_package( $args = array() ) {
* or false if unable to connect to the filesystem.
*/
public function run( $options ) {
global $wp_filesystem;

$defaults = array(
'package' => '', // Please always pass this.
Expand Down Expand Up @@ -906,6 +909,13 @@ public function run( $options ) {
)
);

if ( is_wp_error( $result ) && $options['clear_working']
&& $wp_filesystem instanceof WP_Filesystem_Base
&& $wp_filesystem->exists( $working_dir )
) {
$wp_filesystem->delete( $working_dir, true );
}

/**
* Filters the result of WP_Upgrader::install_package().
*
Expand Down
56 changes: 56 additions & 0 deletions tests/phpunit/tests/admin/wpUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -1464,6 +1464,62 @@ public function test_run_should_return_false_when_requesting_filesystem_credenti
$this->assertFalse( self::$instance->run( array() ) );
}

/**
* Tests that `WP_Upgrader::run()` removes the working directory after an
* installation failure.
*
* @ticket 44710
*
* @covers WP_Upgrader::run
*/
public function test_run_should_clear_working_directory_after_installation_failure() {
$working_directory = '/wp-content/upgrade/test-package';
$expected = new WP_Error( 'incompatible_archive' );

self::$wp_filesystem_mock
->expects( $this->once() )
->method( 'exists' )
->with( $working_directory )
->willReturn( true );

self::$wp_filesystem_mock
->expects( $this->once() )
->method( 'delete' )
->with( $working_directory, true );
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
$instance = $this->getMockBuilder( 'WP_Upgrader' )
->setConstructorArgs( array( self::$upgrader_skin_mock ) )
->setMethods( array( 'fs_connect', 'download_package', 'unpack_package', 'install_package' ) )
->getMock();

$instance
->method( 'fs_connect' )
->willReturn( true );

$instance
->method( 'download_package' )
->willReturn( '/tmp/test-package.zip' );

$instance
->method( 'unpack_package' )
->willReturn( $working_directory );

$instance
->method( 'install_package' )
->willReturn( $expected );

$actual = $instance->run(
array(
'package' => '/tmp/test-package.zip',
'destination' => WP_PLUGIN_DIR,
'clear_working' => true,
'is_multi' => true,
)
);

$this->assertSame( $expected, $actual );
}

/**
* Tests that `WP_Upgrader::maintenance_mode()` removes the `.maintenance` file.
*
Expand Down
Loading