Skip to content

Commit b3b80e6

Browse files
author
Hwashiang Yu
committed
MC-31652: File system directory write update
- Updated write directory class - Added integration test coverage for change
1 parent cc6286a commit b3b80e6

File tree

2 files changed

+53
-16
lines changed
  • dev/tests/integration/testsuite/Magento/Framework/Filesystem/Directory
  • lib/internal/Magento/Framework/Filesystem/Directory

2 files changed

+53
-16
lines changed

dev/tests/integration/testsuite/Magento/Framework/Filesystem/Directory/WriteTest.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77
*/
88
namespace Magento\Framework\Filesystem\Directory;
99

10+
use Magento\Framework\Exception\FileSystemException;
1011
use Magento\Framework\Exception\ValidatorException;
1112
use Magento\Framework\Filesystem\DriverPool;
1213
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\TestCase;
1315

1416
/**
1517
* Class ReadTest
1618
* Test for Magento\Framework\Filesystem\Directory\Read class
1719
*/
18-
class WriteTest extends \PHPUnit\Framework\TestCase
20+
class WriteTest extends TestCase
1921
{
2022
/**
2123
* Test data to be cleaned
@@ -231,6 +233,8 @@ public function renameTargetDirProvider()
231233
* @param int $permissions
232234
* @param string $name
233235
* @param string $newName
236+
* @throws ValidatorException
237+
* @throws FileSystemException
234238
*/
235239
public function testCopy($basePath, $permissions, $name, $newName)
236240
{
@@ -298,6 +302,8 @@ public function testCopyOutside()
298302
* @param int $permission
299303
* @param string $name
300304
* @param string $newName
305+
* @throws FileSystemException
306+
* @throws ValidatorException
301307
*/
302308
public function testCopyTargetDir($firstDir, $secondDir, $permission, $name, $newName)
303309
{
@@ -400,6 +406,8 @@ public function testChangePermissionsRecursivelyOutside()
400406
* @param int $permissions
401407
* @param string $path
402408
* @param int $time
409+
* @throws FileSystemException
410+
* @throws ValidatorException
403411
*/
404412
public function testTouch($basePath, $permissions, $path, $time)
405413
{
@@ -485,6 +493,8 @@ public function testIsWritableOutside()
485493
* @param int $permissions
486494
* @param string $path
487495
* @param string $mode
496+
* @throws FileSystemException
497+
* @throws ValidatorException
488498
*/
489499
public function testOpenFile($basePath, $permissions, $path, $mode)
490500
{
@@ -536,6 +546,8 @@ public function testOpenFileOutside()
536546
* @param string $path
537547
* @param string $content
538548
* @param string $extraContent
549+
* @throws FileSystemException
550+
* @throws ValidatorException
539551
*/
540552
public function testWriteFile($path, $content, $extraContent)
541553
{
@@ -553,6 +565,8 @@ public function testWriteFile($path, $content, $extraContent)
553565
* @param string $path
554566
* @param string $content
555567
* @param string $extraContent
568+
* @throws FileSystemException
569+
* @throws ValidatorException
556570
*/
557571
public function testWriteFileAppend($path, $content, $extraContent)
558572
{
@@ -595,6 +609,18 @@ public function testWriteFileOutside()
595609
$this->assertEquals(3, $exceptions);
596610
}
597611

612+
/**
613+
* @throws ValidatorException
614+
*/
615+
public function testInvalidDeletePath()
616+
{
617+
$this->expectException(FileSystemException::class);
618+
$directory = $this->getDirectoryInstance('newDir', 0777);
619+
$invalidPath = 'invalidPath/../';
620+
$directory->create($invalidPath);
621+
$directory->delete($invalidPath);
622+
}
623+
598624
/**
599625
* Tear down
600626
*/
@@ -620,8 +646,8 @@ private function getDirectoryInstance($path, $permissions)
620646
{
621647
$fullPath = __DIR__ . '/../_files/' . $path;
622648
$objectManager = Bootstrap::getObjectManager();
623-
/** @var \Magento\Framework\Filesystem\Directory\WriteFactory $directoryFactory */
624-
$directoryFactory = $objectManager->create(\Magento\Framework\Filesystem\Directory\WriteFactory::class);
649+
/** @var WriteFactory $directoryFactory */
650+
$directoryFactory = $objectManager->create(WriteFactory::class);
625651
$directory = $directoryFactory->create($fullPath, DriverPool::FILE, $permissions);
626652
$this->testDirectories[] = $directory;
627653
return $directory;

lib/internal/Magento/Framework/Filesystem/Directory/Write.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
use Magento\Framework\Exception\FileSystemException;
1010
use Magento\Framework\Exception\ValidatorException;
11+
use Magento\Framework\Filesystem\DriverInterface;
12+
use Magento\Framework\Phrase;
1113

1214
/**
1315
* Write Interface implementation
@@ -25,14 +27,14 @@ class Write extends Read implements WriteInterface
2527
* Constructor
2628
*
2729
* @param \Magento\Framework\Filesystem\File\WriteFactory $fileFactory
28-
* @param \Magento\Framework\Filesystem\DriverInterface $driver
30+
* @param DriverInterface $driver
2931
* @param string $path
3032
* @param int $createPermissions
3133
* @param PathValidatorInterface|null $pathValidator
3234
*/
3335
public function __construct(
3436
\Magento\Framework\Filesystem\File\WriteFactory $fileFactory,
35-
\Magento\Framework\Filesystem\DriverInterface $driver,
37+
DriverInterface $driver,
3638
$path,
3739
$createPermissions = null,
3840
?PathValidatorInterface $pathValidator = null
@@ -48,13 +50,13 @@ public function __construct(
4850
*
4951
* @param string $path
5052
* @return void
51-
* @throws \Magento\Framework\Exception\FileSystemException
53+
* @throws FileSystemException|ValidatorException
5254
*/
5355
protected function assertWritable($path)
5456
{
5557
if ($this->isWritable($path) === false) {
5658
$path = $this->getAbsolutePath($path);
57-
throw new FileSystemException(new \Magento\Framework\Phrase('The path "%1" is not writable.', [$path]));
59+
throw new FileSystemException(new Phrase('The path "%1" is not writable.', [$path]));
5860
}
5961
}
6062

@@ -63,15 +65,15 @@ protected function assertWritable($path)
6365
*
6466
* @param string $path
6567
* @return void
66-
* @throws \Magento\Framework\Exception\FileSystemException
68+
* @throws FileSystemException
6769
*/
6870
protected function assertIsFile($path)
6971
{
7072
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
7173
clearstatcache(true, $absolutePath);
7274
if (!$this->driver->isFile($absolutePath)) {
7375
throw new FileSystemException(
74-
new \Magento\Framework\Phrase('The "%1" file doesn\'t exist.', [$absolutePath])
76+
new Phrase('The "%1" file doesn\'t exist.', [$absolutePath])
7577
);
7678
}
7779
}
@@ -149,7 +151,7 @@ public function copyFile($path, $destination, WriteInterface $targetDirectory =
149151
* @param string $destination
150152
* @param WriteInterface $targetDirectory [optional]
151153
* @return bool
152-
* @throws \Magento\Framework\Exception\FileSystemException
154+
* @throws FileSystemException
153155
* @throws ValidatorException
154156
*/
155157
public function createSymlink($path, $destination, WriteInterface $targetDirectory = null)
@@ -178,10 +180,18 @@ public function delete($path = null)
178180
{
179181
$exceptionMessages = [];
180182
$this->validatePath($path);
183+
181184
if (!$this->isExist($path)) {
182185
return true;
183186
}
187+
184188
$absolutePath = $this->driver->getAbsolutePath($this->path, $path);
189+
$basePath = $this->driver->getRealPathSafety($this->driver->getAbsolutePath($this->path, ''));
190+
191+
if ($path !== null && $path !== '' && $this->driver->getRealPathSafety($absolutePath) === $basePath) {
192+
throw new FileSystemException(new Phrase('The path "%1" is not writable.', [$path]));
193+
}
194+
185195
if ($this->driver->isFile($absolutePath)) {
186196
$this->driver->deleteFile($absolutePath);
187197
} else {
@@ -198,12 +208,13 @@ public function delete($path = null)
198208

199209
if (!empty($exceptionMessages)) {
200210
throw new FileSystemException(
201-
new \Magento\Framework\Phrase(
211+
new Phrase(
202212
\implode(' ', $exceptionMessages)
203213
)
204214
);
205215
}
206216
}
217+
207218
return true;
208219
}
209220

@@ -231,7 +242,7 @@ private function deleteFilesRecursively(string $path)
231242
}
232243
if (!empty($exceptionMessages)) {
233244
throw new FileSystemException(
234-
new \Magento\Framework\Phrase(
245+
new Phrase(
235246
\implode(' ', $exceptionMessages)
236247
)
237248
);
@@ -297,7 +308,7 @@ public function touch($path, $modificationTime = null)
297308
*
298309
* @param string|null $path
299310
* @return bool
300-
* @throws \Magento\Framework\Exception\FileSystemException
311+
* @throws FileSystemException
301312
* @throws ValidatorException
302313
*/
303314
public function isWritable($path = null)
@@ -313,7 +324,7 @@ public function isWritable($path = null)
313324
* @param string $path
314325
* @param string $mode
315326
* @return \Magento\Framework\Filesystem\File\WriteInterface
316-
* @throws \Magento\Framework\Exception\FileSystemException
327+
* @throws FileSystemException
317328
* @throws ValidatorException
318329
*/
319330
public function openFile($path, $mode = 'w')
@@ -334,7 +345,7 @@ public function openFile($path, $mode = 'w')
334345
* @param string $content
335346
* @param string|null $mode
336347
* @return int The number of bytes that were written.
337-
* @throws FileSystemException
348+
* @throws FileSystemException|ValidatorException
338349
*/
339350
public function writeFile($path, $content, $mode = 'w+')
340351
{
@@ -344,7 +355,7 @@ public function writeFile($path, $content, $mode = 'w+')
344355
/**
345356
* Get driver
346357
*
347-
* @return \Magento\Framework\Filesystem\DriverInterface
358+
* @return DriverInterface
348359
*/
349360
public function getDriver()
350361
{

0 commit comments

Comments
 (0)