Skip to content

Commit f9c4f7a

Browse files
Slamdunknicolas-grekas
authored andcommittedJan 21, 2020
[Filesystem] chown and chgrp should also accept int as owner and group
1 parent 4a5206e commit f9c4f7a

File tree

3 files changed

+84
-9
lines changed

3 files changed

+84
-9
lines changed
 

‎Filesystem.php

+4-2
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,11 @@ public function chmod($files, int $mode, int $umask = 0000, bool $recursive = fa
207207
* Change the owner of an array of files or directories.
208208
*
209209
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
210+
* @param string|int $user A user name or number
210211
*
211212
* @throws IOException When the change fails
212213
*/
213-
public function chown($files, string $user, bool $recursive = false)
214+
public function chown($files, $user, bool $recursive = false)
214215
{
215216
foreach ($this->toIterable($files) as $file) {
216217
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -232,10 +233,11 @@ public function chown($files, string $user, bool $recursive = false)
232233
* Change the group of an array of files or directories.
233234
*
234235
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
236+
* @param string|int $group A group name or number
235237
*
236238
* @throws IOException When the change fails
237239
*/
238-
public function chgrp($files, string $group, bool $recursive = false)
240+
public function chgrp($files, $group, bool $recursive = false)
239241
{
240242
foreach ($this->toIterable($files) as $file) {
241243
if ($recursive && is_dir($file) && !is_link($file)) {

‎Tests/FilesystemTest.php

+62-4
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ public function testChmodChangesZeroModeOnSubdirectoriesOnRecursive()
521521
$this->assertFilePermissions(753, $subdirectory);
522522
}
523523

524-
public function testChown()
524+
public function testChownByName()
525525
{
526526
$this->markAsSkippedIfPosixIsMissing();
527527

@@ -534,7 +534,20 @@ public function testChown()
534534
$this->assertSame($owner, $this->getFileOwner($dir));
535535
}
536536

537-
public function testChownRecursive()
537+
public function testChownById()
538+
{
539+
$this->markAsSkippedIfPosixIsMissing();
540+
541+
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
542+
mkdir($dir);
543+
544+
$ownerId = $this->getFileOwnerId($dir);
545+
$this->filesystem->chown($dir, $ownerId);
546+
547+
$this->assertSame($ownerId, $this->getFileOwnerId($dir));
548+
}
549+
550+
public function testChownRecursiveByName()
538551
{
539552
$this->markAsSkippedIfPosixIsMissing();
540553

@@ -549,6 +562,21 @@ public function testChownRecursive()
549562
$this->assertSame($owner, $this->getFileOwner($file));
550563
}
551564

565+
public function testChownRecursiveById()
566+
{
567+
$this->markAsSkippedIfPosixIsMissing();
568+
569+
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
570+
mkdir($dir);
571+
$file = $dir.\DIRECTORY_SEPARATOR.'file';
572+
touch($file);
573+
574+
$ownerId = $this->getFileOwnerId($dir);
575+
$this->filesystem->chown($dir, $ownerId, true);
576+
577+
$this->assertSame($ownerId, $this->getFileOwnerId($file));
578+
}
579+
552580
public function testChownSymlink()
553581
{
554582
$this->markAsSkippedIfSymlinkIsMissing();
@@ -624,7 +652,7 @@ public function testChownFail()
624652
$this->filesystem->chown($dir, 'user'.time().mt_rand(1000, 9999));
625653
}
626654

627-
public function testChgrp()
655+
public function testChgrpByName()
628656
{
629657
$this->markAsSkippedIfPosixIsMissing();
630658

@@ -637,6 +665,19 @@ public function testChgrp()
637665
$this->assertSame($group, $this->getFileGroup($dir));
638666
}
639667

668+
public function testChgrpById()
669+
{
670+
$this->markAsSkippedIfPosixIsMissing();
671+
672+
$dir = $this->workspace.\DIRECTORY_SEPARATOR.'dir';
673+
mkdir($dir);
674+
675+
$groupId = $this->getFileGroupId($dir);
676+
$this->filesystem->chgrp($dir, $groupId);
677+
678+
$this->assertSame($groupId, $this->getFileGroupId($dir));
679+
}
680+
640681
public function testChgrpRecursive()
641682
{
642683
$this->markAsSkippedIfPosixIsMissing();
@@ -652,7 +693,7 @@ public function testChgrpRecursive()
652693
$this->assertSame($group, $this->getFileGroup($file));
653694
}
654695

655-
public function testChgrpSymlink()
696+
public function testChgrpSymlinkByName()
656697
{
657698
$this->markAsSkippedIfSymlinkIsMissing();
658699

@@ -669,6 +710,23 @@ public function testChgrpSymlink()
669710
$this->assertSame($group, $this->getFileGroup($link));
670711
}
671712

713+
public function testChgrpSymlinkById()
714+
{
715+
$this->markAsSkippedIfSymlinkIsMissing();
716+
717+
$file = $this->workspace.\DIRECTORY_SEPARATOR.'file';
718+
$link = $this->workspace.\DIRECTORY_SEPARATOR.'link';
719+
720+
touch($file);
721+
722+
$this->filesystem->symlink($file, $link);
723+
724+
$groupId = $this->getFileGroupId($link);
725+
$this->filesystem->chgrp($link, $groupId);
726+
727+
$this->assertSame($groupId, $this->getFileGroupId($link));
728+
}
729+
672730
public function testChgrpLink()
673731
{
674732
$this->markAsSkippedIfLinkIsMissing();

‎Tests/FilesystemTestCase.php

+18-3
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,36 @@ protected function assertFilePermissions($expectedFilePerms, $filePath)
105105
);
106106
}
107107

108+
protected function getFileOwnerId($filepath)
109+
{
110+
$this->markAsSkippedIfPosixIsMissing();
111+
112+
$infos = stat($filepath);
113+
114+
return $infos['uid'];
115+
}
116+
108117
protected function getFileOwner($filepath)
109118
{
110119
$this->markAsSkippedIfPosixIsMissing();
111120

121+
return ($datas = posix_getpwuid($this->getFileOwnerId($filepath))) ? $datas['name'] : null;
122+
}
123+
124+
protected function getFileGroupId($filepath)
125+
{
126+
$this->markAsSkippedIfPosixIsMissing();
127+
112128
$infos = stat($filepath);
113129

114-
return ($datas = posix_getpwuid($infos['uid'])) ? $datas['name'] : null;
130+
return $infos['gid'];
115131
}
116132

117133
protected function getFileGroup($filepath)
118134
{
119135
$this->markAsSkippedIfPosixIsMissing();
120136

121-
$infos = stat($filepath);
122-
if ($datas = posix_getgrgid($infos['gid'])) {
137+
if ($datas = posix_getgrgid($this->getFileGroupId($filepath))) {
123138
return $datas['name'];
124139
}
125140

0 commit comments

Comments
 (0)
Please sign in to comment.