Skip to content

Commit e9327c6

Browse files
author
costdev
committed
Add tests for ::rmdir().
1 parent ad99aa8 commit e9327c6

File tree

1 file changed

+202
-0
lines changed
  • tests/phpunit/tests/filesystem/direct

1 file changed

+202
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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() is not available in the tests.
112+
|| str_starts_with( $path, '.' )
113+
|| str_starts_with( $path, '/' )
114+
) {
115+
$this->markTestSkipped( 'Dangerous delete path.' );
116+
}
117+
118+
$this->assertTrue( self::$filesystem->rmdir( self::$file_structure['test_dir']['path'] . $path ) );
119+
}
120+
121+
/**
122+
* Tests that `WP_Filesystem_Direct::rmdir()`
123+
* returns false when a directory's contents cannot be deleted.
124+
*
125+
* @ticket 57774
126+
*/
127+
public function test_should_return_false_when_contents_cannot_be_deleted() {
128+
129+
global $wp_filesystem;
130+
131+
$wp_filesystem = new WP_Filesystem_Direct( array() );
132+
133+
$path = self::$file_structure['test_dir']['path'] . 'dir-to-delete/';
134+
135+
if ( ! is_dir( $path ) ) {
136+
mkdir( $path );
137+
}
138+
139+
// Set up mock filesystem.
140+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
141+
->setConstructorArgs( array( null ) )
142+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
143+
->setMethods( array( 'dirlist' ) )
144+
->getMock();
145+
146+
$filesystem_mock->expects( $this->once() )
147+
->method( 'dirlist' )
148+
->willReturn(
149+
array( 'a_file_that_does_not_exist.txt' => array( 'type' => 'f' ) )
150+
);
151+
152+
$wp_filesystem_backup = $wp_filesystem;
153+
$wp_filesystem = $filesystem_mock;
154+
155+
$actual = $filesystem_mock->rmdir( $path, true );
156+
157+
if ( $actual ) {
158+
rmdir( $path );
159+
}
160+
161+
$wp_filesystem = $wp_filesystem_backup;
162+
163+
$this->assertFalse( $actual );
164+
}
165+
166+
/**
167+
* Tests that `WP_Filesystem_Direct::rmdir()`
168+
* returns false when the path is not a file or directory, but exists.
169+
*
170+
* @ticket 57774
171+
*/
172+
public function test_should_return_false_when_path_exists_but_is_not_a_file_or_directory() {
173+
global $wp_filesystem;
174+
175+
$wp_filesystem = new WP_Filesystem_Direct( array() );
176+
177+
// Set up mock filesystem.
178+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
179+
->setConstructorArgs( array( null ) )
180+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
181+
->setMethods( array( 'is_file', 'dirlist' ) )
182+
->getMock();
183+
184+
$filesystem_mock->expects( $this->once() )
185+
->method( 'is_file' )
186+
->willReturn( false );
187+
188+
$filesystem_mock->expects( $this->once() )
189+
->method( 'dirlist' )
190+
->willReturn( false );
191+
192+
$wp_filesystem_backup = $wp_filesystem;
193+
$wp_filesystem = $filesystem_mock;
194+
195+
$actual = $filesystem_mock->rmdir( self::$file_structure['subdir']['path'], true );
196+
197+
$wp_filesystem = $wp_filesystem_backup;
198+
199+
$this->assertFalse( $actual );
200+
}
201+
202+
}

0 commit comments

Comments
 (0)