Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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
7 changes: 6 additions & 1 deletion src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -7879,7 +7879,12 @@ function wp_delete_file( $file ) {
*/
function wp_delete_file_from_directory( $file, $directory ) {
if ( wp_is_stream( $file ) ) {
$real_file = $file;
if ( ! str_starts_with( strtolower( $file ), 'file:' ) ) {

return false;
}
Comment on lines 7881 to +7885

$real_file = $file;
$real_directory = $directory;
} else {
$real_file = realpath( wp_normalize_path( $file ) );
Expand Down
81 changes: 81 additions & 0 deletions tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* Tests for the wp_delete_file_from_directory function.
*
* @group functions.php
Comment thread
pbearne marked this conversation as resolved.
Resolved
*
* @covers ::wp_delete_file_from_directory
*/
class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase {
Comment thread
pbearne marked this conversation as resolved.
Resolved

/**
* call with file and correct directory.
*
Comment thread
pbearne marked this conversation as resolved.
Resolved
* @ticket 59781
*/
public function test_wp_delete_file_from_directory() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );
touch( $file );
Comment on lines +17 to +19

$this->assertTrue( wp_delete_file_from_directory( $file, $directory ) );
$this->assertFileDoesNotExist( $file );
}

/**
* Call with missing file.
*
* @ticket 59781
*/
public function test_wp_delete_file_from_directory_no_file() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );

$this->assertFalse( wp_delete_file_from_directory( $file, $directory ) );
$this->assertFileDoesNotExist( $file );
}

/**
* Call with streamed file.
*
* @ticket 59781
*/
public function test_wp_delete_file_from_directory_file_stream() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );
touch( $file );

$this->assertTrue( wp_delete_file_from_directory( 'file://' . $file, 'file://' . $directory ) );
Comment thread
pbearne marked this conversation as resolved.
Resolved
$this->assertFileDoesNotExist( $file );
}

/**
* Call with unsorted stream file.
*
* @ticket 59781
*/
Comment thread
pbearne marked this conversation as resolved.
Resolved
public function test_wp_delete_file_from_directory_non_file_stream() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA );
touch( $file );

$this->assertFalse( wp_delete_file_from_directory( 'php://' . $file, 'php://' . $directory ) );
Comment thread
pbearne marked this conversation as resolved.
Resolved
$this->assertFileExists( $file );
@unlink( $file );
}

/**
* Call with incorrect directory.
*
* @ticket 59781
*/
public function test_wp_delete_file_from_directory_no_folder() {
$file = realpath( DIR_TESTDATA ) . '/temp_file.txt';
$directory = realpath( DIR_TESTDATA ) . '/images';
touch( $file );

$this->assertFalse( wp_delete_file_from_directory( $file, $directory ) );
$this->assertFileExists( $file );
@unlink( $file );
}
}
Loading