Skip to content

Commit a142ef2

Browse files
committed
Fix #19872 - replace hardcoded pub path with getRelativePath call
1 parent 07a296a commit a142ef2

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

app/code/Magento/Catalog/Model/Category/FileInfo.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class FileInfo
4343
*/
4444
private $baseDirectory;
4545

46+
/**
47+
* @var ReadInterface
48+
*/
49+
private $pubDirectory;
50+
4651
/**
4752
* @param Filesystem $filesystem
4853
* @param Mime $mime
@@ -82,6 +87,20 @@ private function getBaseDirectory()
8287
return $this->baseDirectory;
8388
}
8489

90+
/**
91+
* Get Pub Directory read instance
92+
*
93+
* @return ReadInterface
94+
*/
95+
private function getPubDirectory()
96+
{
97+
if (!isset($this->pubDirectory)) {
98+
$this->pubDirectory = $this->filesystem->getDirectoryRead(DirectoryList::PUB);
99+
}
100+
101+
return $this->pubDirectory;
102+
}
103+
85104
/**
86105
* Retrieve MIME type of requested file
87106
*
@@ -174,11 +193,13 @@ public function isBeginsWithMediaDirectoryPath($fileName)
174193
*/
175194
private function getMediaDirectoryPathRelativeToBaseDirectoryPath(string $filePath = '')
176195
{
177-
$baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
196+
$baseDirectory = $this->getBaseDirectory();
197+
$baseDirectoryPath = $baseDirectory->getAbsolutePath();
178198
$mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();
199+
$pubDirectoryPath = $this->getPubDirectory()->getAbsolutePath();
179200

180201
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));
181-
$pubDirectory = 'pub' . DIRECTORY_SEPARATOR;
202+
$pubDirectory = $baseDirectory->getRelativePath($pubDirectoryPath);
182203

183204
if (strpos($mediaDirectoryRelativeSubpath, $pubDirectory) === 0 && strpos($filePath, $pubDirectory) !== 0) {
184205
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryRelativeSubpath, strlen($pubDirectory));

app/code/Magento/Catalog/Test/Unit/Model/Category/FileInfoTest.php

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,36 @@
1111
use Magento\Framework\Filesystem;
1212
use Magento\Framework\Filesystem\Directory\WriteInterface;
1313
use Magento\Framework\Filesystem\Directory\ReadInterface;
14+
use PHPUnit\Framework\MockObject\MockObject;
15+
use PHPUnit\Framework\TestCase;
1416

15-
class FileInfoTest extends \PHPUnit\Framework\TestCase
17+
class FileInfoTest extends TestCase
1618
{
1719
/**
18-
* @var Filesystem|\PHPUnit_Framework_MockObject_MockObject
20+
* @var Filesystem|MockObject
1921
*/
2022
private $filesystem;
2123

2224
/**
23-
* @var Mime|\PHPUnit_Framework_MockObject_MockObject
25+
* @var Mime|MockObject
2426
*/
2527
private $mime;
2628

2729
/**
28-
* @var WriteInterface|\PHPUnit_Framework_MockObject_MockObject
30+
* @var WriteInterface|MockObject
2931
*/
3032
private $mediaDirectory;
3133

3234
/**
33-
* @var ReadInterface|\PHPUnit_Framework_MockObject_MockObject
35+
* @var ReadInterface|MockObject
3436
*/
3537
private $baseDirectory;
3638

39+
/**
40+
* @var ReadInterface|MockObject
41+
*/
42+
private $pubDirectory;
43+
3744
/**
3845
* @var FileInfo
3946
*/
@@ -44,31 +51,42 @@ protected function setUp()
4451
$this->mediaDirectory = $this->getMockBuilder(WriteInterface::class)
4552
->getMockForAbstractClass();
4653

47-
$this->baseDirectory = $this->getMockBuilder(ReadInterface::class)
54+
$this->baseDirectory = $baseDirectory = $this->getMockBuilder(ReadInterface::class)
55+
->getMockForAbstractClass();
56+
57+
$this->pubDirectory = $pubDirectory = $this->getMockBuilder(ReadInterface::class)
4858
->getMockForAbstractClass();
4959

5060
$this->filesystem = $this->getMockBuilder(Filesystem::class)
5161
->disableOriginalConstructor()
5262
->getMock();
53-
$this->filesystem->expects($this->any())
54-
->method('getDirectoryWrite')
63+
64+
$this->filesystem->method('getDirectoryWrite')
5565
->with(DirectoryList::MEDIA)
5666
->willReturn($this->mediaDirectory);
5767

58-
$this->filesystem->expects($this->any())
59-
->method('getDirectoryRead')
60-
->with(DirectoryList::ROOT)
61-
->willReturn($this->baseDirectory);
68+
$this->filesystem->method('getDirectoryRead')
69+
->willReturnCallback(function ($arg) use ($baseDirectory, $pubDirectory) {
70+
if ($arg === DirectoryList::PUB) {
71+
return $pubDirectory;
72+
}
73+
return $baseDirectory;
74+
});
6275

6376
$this->mime = $this->getMockBuilder(Mime::class)
6477
->disableOriginalConstructor()
6578
->getMock();
6679

67-
$this->baseDirectory->expects($this->any())
68-
->method('getAbsolutePath')
69-
->with(null)
80+
$this->baseDirectory->method('getAbsolutePath')
7081
->willReturn('/a/b/c/');
7182

83+
$this->baseDirectory->method('getRelativePath')
84+
->with('/a/b/c/pub/')
85+
->willReturn('pub/');
86+
87+
$this->pubDirectory->method('getAbsolutePath')
88+
->willReturn('/a/b/c/pub/');
89+
7290
$this->model = new FileInfo(
7391
$this->filesystem,
7492
$this->mime
@@ -113,13 +131,11 @@ public function testGetStat()
113131

114132
$expected = ['size' => 1];
115133

116-
$this->mediaDirectory->expects($this->any())
117-
->method('getAbsolutePath')
134+
$this->mediaDirectory->method('getAbsolutePath')
118135
->with(null)
119136
->willReturn('/a/b/c/pub/media/');
120137

121-
$this->mediaDirectory->expects($this->once())
122-
->method('stat')
138+
$this->mediaDirectory->method('stat')
123139
->with($mediaPath . $fileName)
124140
->willReturn($expected);
125141

@@ -137,12 +153,10 @@ public function testGetStat()
137153
*/
138154
public function testIsExist($fileName, $fileMediaPath)
139155
{
140-
$this->mediaDirectory->expects($this->any())
141-
->method('getAbsolutePath')
156+
$this->mediaDirectory->method('getAbsolutePath')
142157
->willReturn('/a/b/c/pub/media/');
143158

144-
$this->mediaDirectory->expects($this->once())
145-
->method('isExist')
159+
$this->mediaDirectory->method('isExist')
146160
->with($fileMediaPath)
147161
->willReturn(true);
148162

@@ -165,8 +179,7 @@ public function isExistProvider()
165179
*/
166180
public function testIsBeginsWithMediaDirectoryPath($fileName, $expected)
167181
{
168-
$this->mediaDirectory->expects($this->any())
169-
->method('getAbsolutePath')
182+
$this->mediaDirectory->method('getAbsolutePath')
170183
->willReturn('/a/b/c/pub/media/');
171184

172185
$this->assertEquals($expected, $this->model->isBeginsWithMediaDirectoryPath($fileName));

0 commit comments

Comments
 (0)