Skip to content

Commit 5abc047

Browse files
committed
Tests: Cover filtering the path passed to wp_delete_file().
Verify that an empty filtered path prevents deletion and that a replacement path is deleted while the original file remains. Extend the existing basic deletion tests with both filter outcomes. Developed in: #14890 Props bluefairydevelopment, pbearne. Fixes #59788. git-svn-id: https://develop.svn.wordpress.org/trunk@65087 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5dfb6f9 commit 5abc047

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎tests/phpunit/tests/functions/wpDeleteFile.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,39 @@ public function test_wp_delete_file_with_file_that_does_not_exist() {
3535
$this->assertFileDoesNotExist( $file, "$file already existed as a file before testing." );
3636
$this->assertFalse( wp_delete_file( $file ), 'Attempting to delete a non-existent file should return false.' );
3737
}
38+
39+
/**
40+
* @ticket 59788
41+
*/
42+
public function test_wp_delete_file_does_not_delete_when_filter_returns_empty() {
43+
$file = wp_tempnam( 'a_file_that_exists.txt' );
44+
45+
add_filter( 'wp_delete_file', '__return_empty_string' );
46+
47+
$this->assertFalse( wp_delete_file( $file ), 'wp_delete_file() should return false when the filter returns an empty path.' );
48+
$this->assertFileExists( $file, 'The file should not be deleted when the filter returns an empty path.' );
49+
50+
unlink( $file );
51+
}
52+
53+
/**
54+
* @ticket 59788
55+
*/
56+
public function test_wp_delete_file_deletes_the_filtered_path() {
57+
$file = wp_tempnam( 'a_file_that_exists.txt' );
58+
$filtered = wp_tempnam( 'a_filtered_file.txt' );
59+
60+
add_filter(
61+
'wp_delete_file',
62+
static function () use ( $filtered ) {
63+
return $filtered;
64+
}
65+
);
66+
67+
$this->assertTrue( wp_delete_file( $file ), 'wp_delete_file() should return true when the filtered path is deleted.' );
68+
$this->assertFileDoesNotExist( $filtered, 'The filtered path should be deleted.' );
69+
$this->assertFileExists( $file, 'The original path should not be deleted when the filter changes it.' );
70+
71+
unlink( $file );
72+
}
3873
}

0 commit comments

Comments
 (0)