Skip to content

Commit 9ec8923

Browse files
author
costdev
committed
Add tests for ::rmdir().
1 parent 5a97083 commit 9ec8923

File tree

1 file changed

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

1 file changed

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

0 commit comments

Comments
 (0)