Skip to content

Commit 5a97083

Browse files
author
costdev
committed
Add tests for ::mkdir().
1 parent bdb8c54 commit 5a97083

File tree

1 file changed

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

1 file changed

+212
-0
lines changed
Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
<?php
2+
/**
3+
* Tests for the WP_Filesystem_Direct::mkdir() 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::mkdir
16+
*/
17+
class Tests_Filesystem_WpFilesystemDirect_Mkdir extends WP_Filesystem_Direct_UnitTestCase {
18+
19+
/**
20+
* Tests that `WP_Filesystem_Direct::mkdir()` creates a directory.
21+
*
22+
* This test runs in a separate process so that it can define
23+
* constants without impacting other tests.
24+
*
25+
* This test does not preserve global state to prevent the exception
26+
* "Serialization of 'Closure' is not allowed." when running in a
27+
* separate process.
28+
*
29+
* @ticket 57774
30+
*
31+
* @dataProvider data_should_create_directory
32+
*
33+
* @runInSeparateProcess
34+
* @preserveGlobalState disabled
35+
*
36+
* @param mixed $path The path to create.
37+
*/
38+
public function test_should_create_directory( $path ) {
39+
define( 'FS_CHMOD_DIR', 0755 );
40+
41+
$path = str_replace( 'TEST_DIR', self::$file_structure['test_dir']['path'], $path );
42+
$actual = self::$filesystem->mkdir( $path );
43+
44+
if ( $path !== self::$file_structure['test_dir']['path'] && is_dir( $path ) ) {
45+
rmdir( $path );
46+
}
47+
48+
$this->assertTrue( $actual );
49+
}
50+
51+
/**
52+
* Data provider.
53+
*
54+
* @return array[]
55+
*/
56+
public function data_should_create_directory() {
57+
return array(
58+
'no trailing slash' => array(
59+
'path' => 'TEST_DIR/directory-to-create',
60+
),
61+
'a trailing slash' => array(
62+
'path' => 'TEST_DIR/directory-to-create/',
63+
),
64+
);
65+
}
66+
67+
/**
68+
* Tests that `WP_Filesystem_Direct::mkdir()` does not create a directory.
69+
*
70+
* This test runs in a separate process so that it can define
71+
* constants without impacting other tests.
72+
*
73+
* This test does not preserve global state to prevent the exception
74+
* "Serialization of 'Closure' is not allowed." when running in a
75+
* separate process.
76+
*
77+
* @ticket 57774
78+
*
79+
* @dataProvider data_should_not_create_directory
80+
*
81+
* @runInSeparateProcess
82+
* @preserveGlobalState disabled
83+
*
84+
* @param mixed $path The path to create.
85+
*/
86+
public function test_should_not_create_directory( $path ) {
87+
define( 'FS_CHMOD_DIR', 0755 );
88+
89+
$path = str_replace( 'TEST_DIR', self::$file_structure['test_dir']['path'], $path );
90+
$actual = self::$filesystem->mkdir( $path );
91+
92+
if ( $path !== self::$file_structure['test_dir']['path'] && is_dir( $path ) ) {
93+
rmdir( $path );
94+
}
95+
96+
$this->assertFalse( $actual );
97+
}
98+
99+
/**
100+
* Data provider.
101+
*
102+
* @return array[]
103+
*/
104+
public function data_should_not_create_directory() {
105+
return array(
106+
'empty path' => array(
107+
'path' => '',
108+
),
109+
'a path that exists' => array(
110+
'path' => 'TEST_DIR',
111+
),
112+
);
113+
}
114+
115+
/**
116+
* Tests that `WP_Filesystem_Direct::mkdir()` sets chmod.
117+
*
118+
* @ticket 57774
119+
*/
120+
public function test_should_set_chmod() {
121+
$path = self::$file_structure['test_dir']['path'] . 'directory-to-create';
122+
123+
$created = self::$filesystem->mkdir( $path, 0644 );
124+
$chmod = substr( sprintf( '%o', fileperms( $path ) ), -4 );
125+
126+
if ( $path !== self::$file_structure['test_dir']['path'] && is_dir( $path ) ) {
127+
rmdir( $path );
128+
}
129+
130+
$expected_permissions = $this->is_windows() ? '0777' : '0644';
131+
132+
$this->assertTrue( $created, 'The directory was not created.' );
133+
$this->assertSame( $expected_permissions, $chmod, 'The permissions are incorrect.' );
134+
}
135+
136+
/**
137+
* Tests that `WP_Filesystem_Direct::mkdir()` sets the owner.
138+
*
139+
* This test runs in a separate process so that it can define
140+
* constants without impacting other tests.
141+
*
142+
* This test does not preserve global state to prevent the exception
143+
* "Serialization of 'Closure' is not allowed." when running in a
144+
* separate process.
145+
*
146+
* @ticket 57774
147+
*
148+
* @runInSeparateProcess
149+
* @preserveGlobalState disabled
150+
*/
151+
public function test_should_set_owner() {
152+
define( 'FS_CHMOD_DIR', 0755 );
153+
154+
$path = self::$file_structure['test_dir']['path'] . 'directory-to-create';
155+
156+
// Get the default owner.
157+
self::$filesystem->mkdir( $path );
158+
$original_owner = fileowner( $path );
159+
160+
rmdir( $path );
161+
162+
$expected_group = $this->is_windows() ? $original_owner : $original_owner + 1;
163+
$created = self::$filesystem->mkdir( $path, 0755, $expected_group );
164+
$owner = fileowner( $path );
165+
166+
if ( $path !== self::$file_structure['test_dir']['path'] && is_dir( $path ) ) {
167+
rmdir( $path );
168+
}
169+
170+
$this->assertTrue( $created, 'The directory was not created.' );
171+
$this->assertSame( $expected_group, $owner, 'The owner is incorrect.' );
172+
}
173+
174+
/**
175+
* Tests that `WP_Filesystem_Direct::mkdir()` sets the group.
176+
*
177+
* This test runs in a separate process so that it can define
178+
* constants without impacting other tests.
179+
*
180+
* This test does not preserve global state to prevent the exception
181+
* "Serialization of 'Closure' is not allowed." when running in a
182+
* separate process.
183+
*
184+
* @ticket 57774
185+
*
186+
* @runInSeparateProcess
187+
* @preserveGlobalState disabled
188+
*/
189+
public function test_should_set_group() {
190+
define( 'FS_CHMOD_DIR', 0755 );
191+
192+
$path = self::$file_structure['test_dir']['path'] . 'directory-to-create';
193+
194+
// Get the default group.
195+
self::$filesystem->mkdir( $path );
196+
$original_group = filegroup( $path );
197+
198+
rmdir( $path );
199+
200+
$expected_group = $this->is_windows() ? $original_group : $original_group + 1;
201+
$created = self::$filesystem->mkdir( $path, 0755, false, $expected_group );
202+
$group = filegroup( $path );
203+
204+
if ( $path !== self::$file_structure['test_dir']['path'] && is_dir( $path ) ) {
205+
rmdir( $path );
206+
}
207+
208+
$this->assertTrue( $created, 'The directory was not created.' );
209+
$this->assertSame( $expected_group, $group, 'The group is incorrect.' );
210+
}
211+
212+
}

0 commit comments

Comments
 (0)