Skip to content

Commit e8832ac

Browse files
author
costdev
committed
Add tests for ::delete().
1 parent 9bd80ff commit e8832ac

File tree

1 file changed

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

1 file changed

+205
-0
lines changed
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
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+
$this->assertTrue(
55+
self::$filesystem->delete( self::$file_structure['test_dir']['path'], true ),
56+
'Directory deletion failed.'
57+
);
58+
59+
$this->assertDirectoryDoesNotExist(
60+
self::$file_structure['test_dir']['path'],
61+
'The directory was not deleted.'
62+
);
63+
}
64+
65+
/**
66+
* Tests that `WP_Filesystem_Direct::delete()` deletes a file.
67+
*
68+
* @ticket 57774
69+
*
70+
* @dataProvider data_should_delete_a_file
71+
*
72+
* @param string $key The key for the file in `self::$filesystem_structure`.
73+
*/
74+
public function test_should_delete_a_file( $file ) {
75+
$file = self::$file_structure[ $file ]['path'] . $file;
76+
77+
$this->assertTrue( self::$filesystem->delete( $file ), 'File deletion failed.' );
78+
$this->assertFileDoesNotExist( $file, 'The file was not deleted.' );
79+
}
80+
81+
/**
82+
* Data provider.
83+
*
84+
* @return array[]
85+
*/
86+
public function data_should_delete_a_file() {
87+
return array(
88+
'A visible file' => array(
89+
'key' => 'visible_file',
90+
),
91+
'A hidden file' => array(
92+
'key' => 'hidden_file',
93+
),
94+
);
95+
}
96+
97+
/**
98+
* Tests that `WP_Filesystem_Direct::delete()`
99+
* returns true when deleting a path that does not exist.
100+
*
101+
* @ticket 57774
102+
*
103+
* @dataProvider data_paths_that_do_not_exist
104+
*
105+
* @param string $path The path.
106+
*/
107+
public function test_should_return_true_when_deleting_path_that_does_not_exist( $path ) {
108+
$path = self::$file_structure['test_dir']['path'] . $path;
109+
110+
/*
111+
* Verify that the path doesn't exist before testing.
112+
*
113+
* assertFileDoesNotExist() uses file_exists(), which returns the same result for both
114+
* files and directories.
115+
* assertDirectoryDoesNotExist() uses is_dir(), which tests strictly for a directory.
116+
*
117+
* For more useful debugging in the event of a failure, test for a directory first.
118+
*/
119+
$this->assertDirectoryDoesNotExist( $path, "$path already existed as a directory before testing." );
120+
$this->assertFileDoesNotExist( $path, "$path already existed as a file before testing." );
121+
122+
$this->assertTrue( self::$filesystem->delete( $path ), 'Attempting to delete a non-existent path should return true.' );
123+
}
124+
125+
/**
126+
* Tests that `WP_Filesystem_Direct::delete()`
127+
* returns false when a directory's contents cannot be deleted.
128+
*
129+
* @ticket 57774
130+
*/
131+
public function test_should_return_false_when_contents_cannot_be_deleted() {
132+
global $wp_filesystem;
133+
134+
$wp_filesystem = new WP_Filesystem_Direct( array() );
135+
136+
$path = self::$file_structure['test_dir']['path'] . 'dir-to-delete/';
137+
138+
if ( ! is_dir( $path ) ) {
139+
mkdir( $path );
140+
}
141+
142+
// Set up mock filesystem.
143+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
144+
->setConstructorArgs( array( null ) )
145+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
146+
->setMethods( array( 'dirlist' ) )
147+
->getMock();
148+
149+
$filesystem_mock->expects( $this->once() )
150+
->method( 'dirlist' )
151+
->willReturn(
152+
array( 'a_file_that_does_not_exist.txt' => array( 'type' => 'f' ) )
153+
);
154+
155+
$wp_filesystem_backup = $wp_filesystem;
156+
$wp_filesystem = $filesystem_mock;
157+
158+
$actual = $filesystem_mock->delete( $path, true );
159+
160+
if ( $actual ) {
161+
rmdir( $path );
162+
}
163+
164+
$wp_filesystem = $wp_filesystem_backup;
165+
166+
$this->assertFalse( $actual );
167+
}
168+
169+
/**
170+
* Tests that `WP_Filesystem_Direct::delete()`
171+
* returns false when the path is not a file or directory, but exists.
172+
*
173+
* @ticket 57774
174+
*/
175+
public function test_should_return_false_when_path_exists_but_is_not_a_file_or_directory() {
176+
global $wp_filesystem;
177+
178+
$wp_filesystem = new WP_Filesystem_Direct( array() );
179+
180+
// Set up mock filesystem.
181+
$filesystem_mock = $this->getMockBuilder( 'WP_Filesystem_Direct' )
182+
->setConstructorArgs( array( null ) )
183+
// Note: setMethods() is deprecated in PHPUnit 9, but still supported.
184+
->setMethods( array( 'is_file', 'dirlist' ) )
185+
->getMock();
186+
187+
$filesystem_mock->expects( $this->once() )
188+
->method( 'is_file' )
189+
->willReturn( false );
190+
191+
$filesystem_mock->expects( $this->once() )
192+
->method( 'dirlist' )
193+
->willReturn( false );
194+
195+
$wp_filesystem_backup = $wp_filesystem;
196+
$wp_filesystem = $filesystem_mock;
197+
198+
$actual = $filesystem_mock->delete( self::$file_structure['subdir']['path'], true );
199+
200+
$wp_filesystem = $wp_filesystem_backup;
201+
202+
$this->assertFalse( $actual );
203+
}
204+
205+
}

0 commit comments

Comments
 (0)