Skip to content

Commit a74d29d

Browse files
authored
Merge branch 'WordPress:trunk' into trunk
2 parents 7bd537c + f01bda1 commit a74d29d

5 files changed

Lines changed: 96 additions & 5 deletions

File tree

src/wp-admin/includes/class-wp-filesystem-direct.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ public function chgrp( $file, $group, $recursive = false ) {
139139
$file = trailingslashit( $file );
140140
$filelist = $this->dirlist( $file );
141141

142-
foreach ( $filelist as $filename ) {
143-
$this->chgrp( $file . $filename, $group, $recursive );
142+
foreach ( $filelist as $file_listing ) {
143+
$this->chgrp( $file . $file_listing['name'], $group, $recursive );
144144
}
145145

146146
return true;
@@ -227,8 +227,8 @@ public function chown( $file, $owner, $recursive = false ) {
227227
// Is a directory, and we want recursive.
228228
$filelist = $this->dirlist( $file );
229229

230-
foreach ( $filelist as $filename ) {
231-
$this->chown( $file . '/' . $filename, $owner, $recursive );
230+
foreach ( $filelist as $file_listing ) {
231+
$this->chown( $file . '/' . $file_listing['name'], $owner, $recursive );
232232
}
233233

234234
return true;

tests/phpunit/tests/filesystem/wpFilesystemDirect/base.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class WP_Filesystem_Direct_UnitTestCase extends WP_UnitTestCase {
1717
/**
1818
* The file structure for tests.
1919
*
20-
* @var array
20+
* @var array<string, array{type: string, path: string, contents?: string}>
2121
*/
2222
protected static $file_structure = array();
2323

tests/phpunit/tests/filesystem/wpFilesystemDirect/chgrp.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,36 @@ class Tests_Filesystem_WpFilesystemDirect_Chgrp extends WP_Filesystem_Direct_Uni
2929
public function test_should_fail_to_change_file_group( $path ) {
3030
$this->assertFalse( self::$filesystem->chgrp( self::$file_structure['test_dir']['path'] . $path, 0 ) );
3131
}
32+
33+
/**
34+
* Tests that recursive {@see WP_Filesystem_Direct::chgrp()} descends into subdirectories.
35+
*
36+
* The resulting group cannot be asserted without elevated privileges, so recursion is
37+
* verified by recording the paths passed to {@see WP_Filesystem_Direct::chgrp()}. Changing each item to its current
38+
* group is a permitted no-op that avoids requiring root and its "Operation not permitted" warning.
39+
*
40+
* @ticket 65584
41+
*/
42+
public function test_should_recurse_into_subdirectories(): void {
43+
$directory = untrailingslashit( self::$file_structure['test_dir']['path'] );
44+
$nested_file = self::$file_structure['subfile']['path'];
45+
46+
$spy = new class( null ) extends WP_Filesystem_Direct {
47+
/** @var string[] */
48+
public array $visited = array();
49+
50+
public function chgrp( $file, $group, $recursive = false ) {
51+
$this->visited[] = $file;
52+
return parent::chgrp( $file, $group, $recursive );
53+
}
54+
};
55+
56+
$spy->chgrp( $directory, (int) filegroup( $directory ), true );
57+
58+
$this->assertContains(
59+
$nested_file,
60+
$spy->visited,
61+
'chgrp() did not recurse into the nested subdirectory.'
62+
);
63+
}
3264
}

tests/phpunit/tests/filesystem/wpFilesystemDirect/chmod.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,31 @@ public function data_should_set_mode_when_not_passed() {
7474
),
7575
);
7676
}
77+
78+
/**
79+
* Tests that recursive {@see WP_Filesystem_Direct::chmod()} applies the mode to files in subdirectories.
80+
*
81+
* @ticket 65584
82+
*/
83+
public function test_should_change_mode_recursively(): void {
84+
if ( self::is_windows() ) {
85+
$this->markTestSkipped( 'chmod() does not support octal modes on Windows.' );
86+
}
87+
88+
$directory = untrailingslashit( self::$file_structure['test_dir']['path'] );
89+
$nested_file = self::$file_structure['subfile']['path'];
90+
91+
$this->assertTrue(
92+
self::$filesystem->chmod( $directory, 0640, true ),
93+
'chmod() did not report success.'
94+
);
95+
96+
clearstatcache();
97+
98+
$this->assertSame(
99+
'640',
100+
self::$filesystem->getchmod( $nested_file ),
101+
'The mode was not applied to a file in a nested subdirectory.'
102+
);
103+
}
77104
}

tests/phpunit/tests/filesystem/wpFilesystemDirect/chown.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,36 @@ class Tests_Filesystem_WpFilesystemDirect_Chown extends WP_Filesystem_Direct_Uni
2929
public function test_should_return_false( $path ) {
3030
$this->assertFalse( self::$filesystem->chown( $path, fileowner( __FILE__ ) ) );
3131
}
32+
33+
/**
34+
* Tests that recursive {@see WP_Filesystem_Direct::chown()} descends into subdirectories.
35+
*
36+
* The resulting owner cannot be asserted without elevated privileges, so recursion is
37+
* verified by recording the paths passed to {@see WP_Filesystem_Direct::chown()}. Changing each item to its current
38+
* owner is a permitted no-op that avoids requiring root and its "Operation not permitted" warning.
39+
*
40+
* @ticket 65584
41+
*/
42+
public function test_should_recurse_into_subdirectories(): void {
43+
$directory = untrailingslashit( self::$file_structure['test_dir']['path'] );
44+
$nested_file = self::$file_structure['subfile']['path'];
45+
46+
$spy = new class( null ) extends WP_Filesystem_Direct {
47+
/** @var string[] */
48+
public array $visited = array();
49+
50+
public function chown( $file, $owner, $recursive = false ) {
51+
$this->visited[] = $file;
52+
return parent::chown( $file, $owner, $recursive );
53+
}
54+
};
55+
56+
$spy->chown( $directory, (int) fileowner( $directory ), true );
57+
58+
$this->assertContains(
59+
$nested_file,
60+
$spy->visited,
61+
'chown() did not recurse into the nested subdirectory.'
62+
);
63+
}
3264
}

0 commit comments

Comments
 (0)