Skip to content

Commit a75a71f

Browse files
Merge branch '5.0'
* 5.0: [Filesystem] chown and chgrp should also accept int as owner and group [DI] Fix EnvVar not loaded when Loader requires an env var Fixed #34713 Move new messages to intl domain when possible [FrameworkBundle] Fix small typo in output comment chown and chgrp should also accept int as owner and group Revert "Fixed translations file dumper behavior" Fix RememberMe with null password [Validator] Fix plurals for sr_Latn (Serbian language written in latin script) validation messages Set booted flag to false when test kernel is unset [FrameworkBundle] remove messenger cache if not enabled [PhpUnitBridge][SymfonyTestsListenerTrait] Remove some unneeded code [HttpClient] Fix strict parsing of response status codes fix PHP const mapping keys using the inline notation [SecurityBundle] Drop duplicated code [FrameworkBundle] Make sure one can use fragments.hinclude_default_template Fix that no-cache requires positive validation with the origin, even for fresh responses Improve upgrading instructions for deprecated router options [DI] Suggest typed argument when binding fails with untyped argument
2 parents c2e4794 + 3afadc0 commit a75a71f

File tree

3 files changed

+88
-11
lines changed

3 files changed

+88
-11
lines changed

Filesystem.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,13 @@ public function chmod($files, int $mode, int $umask = 0000, bool $recursive = fa
206206
/**
207207
* Change the owner of an array of files or directories.
208208
*
209-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change owner
209+
* @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
211+
* @param bool $recursive Whether change the owner recursively or not
210212
*
211213
* @throws IOException When the change fails
212214
*/
213-
public function chown($files, string $user, bool $recursive = false)
215+
public function chown($files, $user, bool $recursive = false)
214216
{
215217
foreach ($this->toIterable($files) as $file) {
216218
if ($recursive && is_dir($file) && !is_link($file)) {
@@ -231,11 +233,13 @@ public function chown($files, string $user, bool $recursive = false)
231233
/**
232234
* Change the group of an array of files or directories.
233235
*
234-
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
236+
* @param string|iterable $files A filename, an array of files, or a \Traversable instance to change group
237+
* @param string|int $group A group name or number
238+
* @param bool $recursive Whether change the group recursively or not
235239
*
236240
* @throws IOException When the change fails
237241
*/
238-
public function chgrp($files, string $group, bool $recursive = false)
242+
public function chgrp($files, $group, bool $recursive = false)
239243
{
240244
foreach ($this->toIterable($files) as $file) {
241245
if ($recursive && is_dir($file) && !is_link($file)) {

Tests/FilesystemTest.php

Lines changed: 62 additions & 4 deletions
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

Lines changed: 18 additions & 3 deletions
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)