Skip to content

Commit 3e4a196

Browse files
ENGCOM-4591: Fix Issue #19872 - checking if image is in media directory #21131
- Merge Pull Request #21131 from Bartlomiejsz/magento2:feature/fix_19872_category_image_path - Merged commits: 1. ed43407 2. 619475d 3. 17ed8d4 4. ae61b70 5. 112476a 6. 8522a14 7. 07a296a 8. a142ef2
2 parents 514e3d0 + a142ef2 commit 3e4a196

File tree

2 files changed

+112
-39
lines changed

2 files changed

+112
-39
lines changed

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

Lines changed: 31 additions & 4 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
*
@@ -135,7 +154,7 @@ private function getFilePath($fileName)
135154
{
136155
$filePath = ltrim($fileName, '/');
137156

138-
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
157+
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
139158
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);
140159

141160
// if the file is not using a relative path, it resides in the catalog/category media directory
@@ -160,7 +179,7 @@ public function isBeginsWithMediaDirectoryPath($fileName)
160179
{
161180
$filePath = ltrim($fileName, '/');
162181

163-
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath();
182+
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
164183
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, $mediaDirectoryRelativeSubpath) === 0;
165184

166185
return $isFileNameBeginsWithMediaDirectoryPath;
@@ -169,14 +188,22 @@ public function isBeginsWithMediaDirectoryPath($fileName)
169188
/**
170189
* Get media directory subpath relative to base directory path
171190
*
191+
* @param string $filePath
172192
* @return string
173193
*/
174-
private function getMediaDirectoryPathRelativeToBaseDirectoryPath()
194+
private function getMediaDirectoryPathRelativeToBaseDirectoryPath(string $filePath = '')
175195
{
176-
$baseDirectoryPath = $this->getBaseDirectory()->getAbsolutePath();
196+
$baseDirectory = $this->getBaseDirectory();
197+
$baseDirectoryPath = $baseDirectory->getAbsolutePath();
177198
$mediaDirectoryPath = $this->getMediaDirectory()->getAbsolutePath();
199+
$pubDirectoryPath = $this->getPubDirectory()->getAbsolutePath();
178200

179201
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryPath, strlen($baseDirectoryPath));
202+
$pubDirectory = $baseDirectory->getRelativePath($pubDirectoryPath);
203+
204+
if (strpos($mediaDirectoryRelativeSubpath, $pubDirectory) === 0 && strpos($filePath, $pubDirectory) !== 0) {
205+
$mediaDirectoryRelativeSubpath = substr($mediaDirectoryRelativeSubpath, strlen($pubDirectory));
206+
}
180207

181208
return $mediaDirectoryRelativeSubpath;
182209
}

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

Lines changed: 81 additions & 35 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,30 +51,41 @@ 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)
70-
->willReturn('/a/b/c');
80+
$this->baseDirectory->method('getAbsolutePath')
81+
->willReturn('/a/b/c/');
82+
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/');
7189

7290
$this->model = new FileInfo(
7391
$this->filesystem,
@@ -85,12 +103,12 @@ public function testGetMimeType()
85103
$this->mediaDirectory->expects($this->at(0))
86104
->method('getAbsolutePath')
87105
->with(null)
88-
->willReturn('/a/b/c/pub/media');
106+
->willReturn('/a/b/c/pub/media/');
89107

90108
$this->mediaDirectory->expects($this->at(1))
91109
->method('getAbsolutePath')
92110
->with(null)
93-
->willReturn('/a/b/c/pub/media');
111+
->willReturn('/a/b/c/pub/media/');
94112

95113
$this->mediaDirectory->expects($this->at(2))
96114
->method('getAbsolutePath')
@@ -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)
119-
->willReturn('/a/b/c/pub/media');
136+
->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

@@ -130,22 +146,52 @@ public function testGetStat()
130146
$this->assertEquals(1, $result['size']);
131147
}
132148

133-
public function testIsExist()
149+
/**
150+
* @param $fileName
151+
* @param $fileMediaPath
152+
* @dataProvider isExistProvider
153+
*/
154+
public function testIsExist($fileName, $fileMediaPath)
134155
{
135-
$mediaPath = '/catalog/category';
136-
137-
$fileName = '/filename.ext1';
156+
$this->mediaDirectory->method('getAbsolutePath')
157+
->willReturn('/a/b/c/pub/media/');
138158

139-
$this->mediaDirectory->expects($this->any())
140-
->method('getAbsolutePath')
141-
->with(null)
142-
->willReturn('/a/b/c/pub/media');
143-
144-
$this->mediaDirectory->expects($this->once())
145-
->method('isExist')
146-
->with($mediaPath . $fileName)
159+
$this->mediaDirectory->method('isExist')
160+
->with($fileMediaPath)
147161
->willReturn(true);
148162

149163
$this->assertTrue($this->model->isExist($fileName));
150164
}
165+
166+
public function isExistProvider()
167+
{
168+
return [
169+
['/filename.ext1', '/catalog/category/filename.ext1'],
170+
['/pub/media/filename.ext1', 'filename.ext1'],
171+
['/media/filename.ext1', 'filename.ext1']
172+
];
173+
}
174+
175+
/**
176+
* @param $fileName
177+
* @param $expected
178+
* @dataProvider isBeginsWithMediaDirectoryPathProvider
179+
*/
180+
public function testIsBeginsWithMediaDirectoryPath($fileName, $expected)
181+
{
182+
$this->mediaDirectory->method('getAbsolutePath')
183+
->willReturn('/a/b/c/pub/media/');
184+
185+
$this->assertEquals($expected, $this->model->isBeginsWithMediaDirectoryPath($fileName));
186+
}
187+
188+
public function isBeginsWithMediaDirectoryPathProvider()
189+
{
190+
return [
191+
['/pub/media/test/filename.ext1', true],
192+
['/media/test/filename.ext1', true],
193+
['/test/filename.ext1', false],
194+
['test2/filename.ext1', false]
195+
];
196+
}
151197
}

0 commit comments

Comments
 (0)