From 6e88963033596e7ef6428a8ab0d1b9d28dd46fe6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Tue, 31 Oct 2023 19:45:24 -0400 Subject: [PATCH 1/5] test for wp_delete_file_from_directory and update to Function to stop stream URL failing --- src/wp-includes/functions.php | 7 ++- .../functions/wpDeleteFileFromDirectory.php | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index cb490ee1764fa..5194faf102349 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -7560,7 +7560,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; + } + + $real_file = $file; $real_directory = $directory; } else { $real_file = realpath( wp_normalize_path( $file ) ); diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php new file mode 100644 index 0000000000000..02fe8a2468030 --- /dev/null +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -0,0 +1,59 @@ +assertTrue( wp_delete_file_from_directory( $file, $directory ) ); + $this->assertFileDoesNotExist( $file ); + } + + public function test_wp_delete_file_from_directory_no_file() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); +// touch( $file ); + + $this->assertFalse( wp_delete_file_from_directory( $file, $directory ) ); + $this->assertFileDoesNotExist( $file ); + } + + public function test_wp_delete_file_from_directory_file_steam() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); + touch( $file ); + + $this->assertTrue( wp_delete_file_from_directory( 'file://' . $file, 'file://' . $directory ) ); + $this->assertFileDoesNotExist( $file ); + } + + public function test_wp_delete_file_from_directory_non_file_steam() { + $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; + $directory = realpath( DIR_TESTDATA ); + touch( $file ); + + $this->assertFalse( wp_delete_file_from_directory( 'php://' . $file, 'php://' . $directory ) ); + $this->assertFileExists( $file ); + @unlink( $file ); + } + 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 ); + } +} From 23354e877825b1c2d06dfa66f5e536bb056d14c6 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 1 Nov 2023 15:20:22 -0400 Subject: [PATCH 2/5] Updated comments --- .../functions/wpDeleteFileFromDirectory.php | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php index 02fe8a2468030..cb595b271298d 100644 --- a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -9,6 +9,8 @@ class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase{ /** + * call with file and correct directory. + * * @ticket 59781 */ public function test_wp_delete_file_from_directory() { @@ -20,15 +22,24 @@ public function test_wp_delete_file_from_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 ); -// touch( $file ); $this->assertFalse( wp_delete_file_from_directory( $file, $directory ) ); $this->assertFileDoesNotExist( $file ); } + /** + * Call with steamed file. + * + * @ticket 59781 + */ public function test_wp_delete_file_from_directory_file_steam() { $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; $directory = realpath( DIR_TESTDATA ); @@ -38,6 +49,11 @@ public function test_wp_delete_file_from_directory_file_steam() { $this->assertFileDoesNotExist( $file ); } + /** + * Call with unsorted stream file. + * + * @ticket 59781 + */ public function test_wp_delete_file_from_directory_non_file_steam() { $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; $directory = realpath( DIR_TESTDATA ); @@ -47,6 +63,12 @@ public function test_wp_delete_file_from_directory_non_file_steam() { $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'; From 977ca9aec8bed8f2d7cf796ed7c4e849bde3b45b Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Wed, 1 Nov 2023 15:24:02 -0400 Subject: [PATCH 3/5] Update wpDeleteFileFromDirectory.php --- tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php index cb595b271298d..d666bea47fb09 100644 --- a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -6,7 +6,7 @@ * * @covers ::wp_delete_file_from_directory */# -class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase{ +class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase { /** * call with file and correct directory. From 920d95fe8e1e9e425800727af8c06cd782c119d5 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Mon, 27 Jul 2026 16:10:53 -0400 Subject: [PATCH 4/5] Apply suggestions from code review Co-authored-by: John Parris --- .../phpunit/tests/functions/wpDeleteFileFromDirectory.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php index d666bea47fb09..1c2b226926029 100644 --- a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -5,7 +5,7 @@ * @group functions.php * * @covers ::wp_delete_file_from_directory - */# + */ class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase { /** @@ -36,11 +36,11 @@ public function test_wp_delete_file_from_directory_no_file() { } /** - * Call with steamed file. + * Call with streamed file. * * @ticket 59781 */ - public function test_wp_delete_file_from_directory_file_steam() { + public function test_wp_delete_file_from_directory_file_stream() { $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; $directory = realpath( DIR_TESTDATA ); touch( $file ); @@ -54,7 +54,7 @@ public function test_wp_delete_file_from_directory_file_steam() { * * @ticket 59781 */ - public function test_wp_delete_file_from_directory_non_file_steam() { + public function test_wp_delete_file_from_directory_non_file_stream() { $file = realpath( DIR_TESTDATA ) . '/temp_file.txt'; $directory = realpath( DIR_TESTDATA ); touch( $file ); From 6d7e40b1fff3f9f930e62a613c18ea3e6336b921 Mon Sep 17 00:00:00 2001 From: Paul Bearne Date: Thu, 26 Nov 2026 15:04:36 -0500 Subject: [PATCH 5/5] Apply batched suggestions from code review Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> Co-authored-by: John Parris --- .../tests/functions/wpDeleteFileFromDirectory.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php index 1c2b226926029..9bb00e6b07258 100644 --- a/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php +++ b/tests/phpunit/tests/functions/wpDeleteFileFromDirectory.php @@ -2,14 +2,14 @@ /** * Tests for the wp_delete_file_from_directory function. * - * @group functions.php + * @group functions * * @covers ::wp_delete_file_from_directory */ -class Tests_functions_wpDeleteFileFromDirectory extends WP_UnitTestCase { +class Tests_Functions_wpDeleteFileFromDirectory extends WP_UnitTestCase { /** - * call with file and correct directory. + * Call with file and correct directory. * * @ticket 59781 */ @@ -45,12 +45,15 @@ public function test_wp_delete_file_from_directory_file_stream() { $directory = realpath( DIR_TESTDATA ); touch( $file ); - $this->assertTrue( wp_delete_file_from_directory( 'file://' . $file, 'file://' . $directory ) ); + $file_url = 'file://' . ( '/' === $file[0] ? $file : '/' . wp_normalize_path( $file ) ); + $directory_url = 'file://' . ( '/' === $directory[0] ? $directory : '/' . wp_normalize_path( $directory ) ); + + $this->assertTrue( wp_delete_file_from_directory( $file_url, $directory_url ) ); $this->assertFileDoesNotExist( $file ); } /** - * Call with unsorted stream file. + * Call with unsupported stream file. * * @ticket 59781 */ @@ -59,7 +62,7 @@ public function test_wp_delete_file_from_directory_non_file_stream() { $directory = realpath( DIR_TESTDATA ); touch( $file ); - $this->assertFalse( wp_delete_file_from_directory( 'php://' . $file, 'php://' . $directory ) ); + $this->assertFalse( wp_delete_file_from_directory( 'php://temp', 'php://temp' ) ); $this->assertFileExists( $file ); @unlink( $file ); }