Skip to content

Commit 98ce0ba

Browse files
author
costdev
committed
Add tests for ::delete().
1 parent 7017607 commit 98ce0ba

File tree

1 file changed

+191
-0
lines changed
  • tests/phpunit/tests/filesystem/wpFilesystemDirect

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
<?php
2+
/**
3+
* Tests for the WP_Filesystem_Direct::delete() method.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
require_once __DIR__ . '/base.php';
9+
10+
/**
11+
* @group admin
12+
* @group filesystem
13+
* @group filesystem-direct
14+
*
15+
* @covers WP_Filesystem_Direct::delete
16+
*/
17+
class Tests_Filesystem_WpFilesystemDirect_Delete extends WP_Filesystem_Direct_UnitTestCase {
18+
19+
/**
20+
* Tests that `WP_Filesystem_Direct::delete()` returns false
21+
* for an empty path.
22+
*
23+
* @ticket 57774
24+
*/
25+
public function test_should_return_false_for_empty_path() {
26+
$this->assertFalse( self::$filesystem->delete( '' ) );
27+
}
28+
29+
/**
30+
* Tests that `WP_Filesystem_Direct::delete()` deletes an empty directory.
31+
*
32+
* @ticket 57774
33+
*/
34+
public function test_should_delete_an_empty_directory() {
35+
$dir = self::$file_structure['test_dir']['path'] . 'directory-to-delete';
36+
37+
$this->assertTrue(
38+
mkdir( $dir ),
39+
'The directory was not created.'
40+
);
41+
42+
$this->assertTrue(
43+
self::$filesystem->delete( $dir ),
44+
'The directory was not deleted.'
45+
);
46+
}
47+
48+
/**
49+
* Tests that `WP_Filesystem_Direct::delete()` deletes a directory with contents.
50+
*
51+
* @ticket 57774
52+
*/
53+
public function test_should_delete_a_directory_with_contents() {
54+
$dir = self::$file_structure['test_dir']['path'] . 'directory-to-delete/';
55+
$file = $dir . 'file-to-delete.txt';
56+
$subdir = $dir . 'subdirectory-to-delete/';
57+
$subfile = $subdir . 'subfile-to-delete.txt';
58+
59+
mkdir( $dir, 0755 );
60+
mkdir( $subdir, 0755 );
61+
touch( $file, 0644 );
62+
touch( $subfile, 0644 );
63+
64+
$actual = self::$filesystem->delete( $dir, true );
65+
66+
if ( ! $actual ) {
67+
unlink( $file );
68+
unlink( $subfile );
69+
rmdir( $subdir );
70+
rmdir( $dir );
71+
}
72+
73+
$this->assertTrue( $actual, 'The directory was not deleted.' );
74+
}
75+
76+
/**
77+
* Tests that `WP_Filesystem_Direct::delete()` deletes a file.
78+
*
79+
* @ticket 57774
80+
*/
81+
public function test_should_delete_a_file() {
82+
$file = self::$file_structure['test_dir']['path'] . 'file-to-delete.txt';
83+
84+
touch( $file );
85+
86+
$this->assertTrue( self::$filesystem->delete( $file ), 'The directory was not deleted.' );
87+
}
88+
89+
/**
90+
* Tests that `WP_Filesystem_Direct::delete()`
91+
* returns true when deleting a path that does not exist.
92+
*
93+
* @ticket 57774
94+
*
95+
* @dataProvider data_paths_that_do_not_exist
96+
*
97+
* @param string $path The path.
98+
*/
99+
public function test_should_return_true_when_deleting_path_that_does_not_exist( $path ) {
100+
if (
101+
'' === $path
102+
|| str_starts_with( $path, '.' )
103+
|| str_starts_with( $path, '/' )
104+
) {
105+
$this->markTestSkipped( 'Dangerous delete path.' );
106+
}
107+
108+
$this->assertTrue( self::$filesystem->delete( self::$file_structure['test_dir']['path'] . $path ) );
109+
}
110+
111+
/**
112+
* Tests that `WP_Filesystem_Direct::delete()`
113+
* returns false when a directory's contents cannot be deleted.
114+
*
115+
* @ticket 57774
116+
*/
117+
public function test_should_return_false_when_contents_cannot_be_deleted() {
118+
global $wp_filesystem;
119+
120+
$wp_filesystem = new WP_Filesystem_Direct( array() );
121+
122+
$path = self::$file_structure['test_dir']['path'] . 'dir-to-delete/';
123+
124+
if ( ! is_dir( $path ) ) {
125+
mkdir( $path );
126+
}
127+
128+
// Set up mock filesystem.
129+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
130+
->setConstructorArgs( array( null ) )
131+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
132+
->setMethods( array( 'dirlist' ) )
133+
->getMock();
134+
135+
$filesystem_mock->expects( $this->once() )
136+
->method( 'dirlist' )
137+
->willReturn(
138+
array( 'a_file_that_does_not_exist.txt' => array( 'type' => 'f' ) )
139+
);
140+
141+
$wp_filesystem_backup = $wp_filesystem;
142+
$wp_filesystem = $filesystem_mock;
143+
144+
$actual = $filesystem_mock->delete( $path, true );
145+
146+
if ( $actual ) {
147+
rmdir( $path );
148+
}
149+
150+
$wp_filesystem = $wp_filesystem_backup;
151+
152+
$this->assertFalse( $actual );
153+
}
154+
155+
/**
156+
* Tests that `WP_Filesystem_Direct::delete()`
157+
* returns false when the path is not a file or directory, but exists.
158+
*
159+
* @ticket 57774
160+
*/
161+
public function test_should_return_false_when_path_exists_but_is_not_a_file_or_directory() {
162+
global $wp_filesystem;
163+
164+
$wp_filesystem = new WP_Filesystem_Direct( array() );
165+
166+
// Set up mock filesystem.
167+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
168+
->setConstructorArgs( array( null ) )
169+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
170+
->setMethods( array( 'is_file', 'dirlist' ) )
171+
->getMock();
172+
173+
$filesystem_mock->expects( $this->once() )
174+
->method( 'is_file' )
175+
->willReturn( false );
176+
177+
$filesystem_mock->expects( $this->once() )
178+
->method( 'dirlist' )
179+
->willReturn( false );
180+
181+
$wp_filesystem_backup = $wp_filesystem;
182+
$wp_filesystem = $filesystem_mock;
183+
184+
$actual = $filesystem_mock->delete( self::$file_structure['subdir']['path'], true );
185+
186+
$wp_filesystem = $wp_filesystem_backup;
187+
188+
$this->assertFalse( $actual );
189+
}
190+
191+
}

0 commit comments

Comments
 (0)