Skip to content

Commit 59fa1f3

Browse files
author
Oleksandr Iegorov
committed
Merge branch 'MC-19090' of https://github.com/magento-tango/magento2ce into PR-2019-08-22
2 parents cba14f4 + dfdc9dc commit 59fa1f3

File tree

4 files changed

+76
-19
lines changed

4 files changed

+76
-19
lines changed

app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,15 @@ public function beforeSave($object)
121121

122122
if ($this->fileResidesOutsideCategoryDir($value)) {
123123
// use relative path for image attribute so we know it's outside of category dir when we fetch it
124+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125+
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
124126
$value[0]['name'] = $value[0]['url'];
125127
}
126128

127129
if ($imageName = $this->getUploadedImageName($value)) {
128-
$imageName = $this->checkUniqueImageName($imageName);
130+
if (!$this->fileResidesOutsideCategoryDir($value)) {
131+
$imageName = $this->checkUniqueImageName($imageName);
132+
}
129133
$object->setData($this->additionalData . $attributeName, $value);
130134
$object->setData($attributeName, $imageName);
131135
} elseif (!is_string($value)) {
@@ -182,7 +186,7 @@ private function fileResidesOutsideCategoryDir($value)
182186
return false;
183187
}
184188

185-
return strpos($fileUrl, $baseMediaDir) === 0;
189+
return strpos($fileUrl, $baseMediaDir) !== false;
186190
}
187191

188192
/**

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use Magento\Framework\Filesystem;
1111
use Magento\Framework\Filesystem\Directory\WriteInterface;
1212
use Magento\Framework\Filesystem\Directory\ReadInterface;
13+
use Magento\Framework\Exception\NoSuchEntityException;
14+
use Magento\Store\Model\StoreManagerInterface;
1315

1416
/**
1517
* Class FileInfo
@@ -48,16 +50,26 @@ class FileInfo
4850
*/
4951
private $pubDirectory;
5052

53+
/**
54+
* Store manager
55+
*
56+
* @var \Magento\Store\Model\StoreManagerInterface
57+
*/
58+
private $storeManager;
59+
5160
/**
5261
* @param Filesystem $filesystem
5362
* @param Mime $mime
63+
* @param StoreManagerInterface $storeManager
5464
*/
5565
public function __construct(
5666
Filesystem $filesystem,
57-
Mime $mime
67+
Mime $mime,
68+
StoreManagerInterface $storeManager
5869
) {
5970
$this->filesystem = $filesystem;
6071
$this->mime = $mime;
72+
$this->storeManager = $storeManager;
6173
}
6274

6375
/**
@@ -152,7 +164,8 @@ public function isExist($fileName)
152164
*/
153165
private function getFilePath($fileName)
154166
{
155-
$filePath = ltrim($fileName, '/');
167+
$filePath = $this->removeStorePath($fileName);
168+
$filePath = ltrim($filePath, '/');
156169

157170
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
158171
$isFileNameBeginsWithMediaDirectoryPath = $this->isBeginsWithMediaDirectoryPath($fileName);
@@ -177,14 +190,39 @@ private function getFilePath($fileName)
177190
*/
178191
public function isBeginsWithMediaDirectoryPath($fileName)
179192
{
180-
$filePath = ltrim($fileName, '/');
193+
$filePath = $this->removeStorePath($fileName);
194+
$filePath = ltrim($filePath, '/');
181195

182196
$mediaDirectoryRelativeSubpath = $this->getMediaDirectoryPathRelativeToBaseDirectoryPath($filePath);
183197
$isFileNameBeginsWithMediaDirectoryPath = strpos($filePath, (string) $mediaDirectoryRelativeSubpath) === 0;
184198

185199
return $isFileNameBeginsWithMediaDirectoryPath;
186200
}
187201

202+
/**
203+
* Clean store path in case if it's exists
204+
*
205+
* @param string $path
206+
* @return string
207+
*/
208+
private function removeStorePath(string $path): string
209+
{
210+
$result = $path;
211+
try {
212+
$storeUrl = $this->storeManager->getStore()->getBaseUrl();
213+
} catch (NoSuchEntityException $e) {
214+
return $result;
215+
}
216+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
217+
$path = parse_url($path, PHP_URL_PATH);
218+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
219+
$storePath = parse_url($storeUrl, PHP_URL_PATH);
220+
$storePath = rtrim($storePath, '/');
221+
222+
$result = preg_replace('/^' . preg_quote($storePath, '/') . '/', '', $path);
223+
return $result;
224+
}
225+
188226
/**
189227
* Get media directory subpath relative to base directory path
190228
*

app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -179,37 +179,29 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
179179
{
180180
$model = $this->setUpModelForAfterSave();
181181
$model->setAttribute($this->attribute);
182-
183-
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
184-
$this->filesystem->expects($this->once())
185-
->method('getDirectoryWrite')
186-
->with(DirectoryList::MEDIA)
187-
->willReturn($mediaDirectoryMock);
182+
$imagePath = '/pub/media/wysiwyg/test123.jpg';
188183
$this->filesystem
189-
->expects($this->once())
184+
->expects($this->exactly(2))
190185
->method('getUri')
191186
->with(DirectoryList::MEDIA)
192187
->willReturn('pub/media');
193-
$mediaDirectoryMock->expects($this->once())
194-
->method('getAbsolutePath')
195-
->willReturn('/pub/media/wysiwyg/test123.jpg');
196188

197189
$object = new \Magento\Framework\DataObject(
198190
[
199191
'test_attribute' => [
200192
[
201193
'name' => 'test123.jpg',
202-
'url' => '/pub/media/wysiwyg/test123.jpg',
194+
'url' => $imagePath,
203195
],
204196
],
205197
]
206198
);
207199

208200
$model->beforeSave($object);
209201

210-
$this->assertEquals('test123.jpg', $object->getTestAttribute());
202+
$this->assertEquals($imagePath, $object->getTestAttribute());
211203
$this->assertEquals(
212-
[['name' => '/pub/media/wysiwyg/test123.jpg', 'url' => '/pub/media/wysiwyg/test123.jpg']],
204+
[['name' => $imagePath, 'url' => $imagePath]],
213205
$object->getData('_additional_data_test_attribute')
214206
);
215207
}

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
use Magento\Framework\Filesystem\Directory\WriteInterface;
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
16+
use Magento\Store\Model\StoreManagerInterface;
17+
use Magento\Store\Model\Store;
1618

1719
/**
1820
* Test for Magento\Catalog\Model\Category\FileInfo class.
@@ -44,6 +46,16 @@ class FileInfoTest extends TestCase
4446
*/
4547
private $pubDirectory;
4648

49+
/**
50+
* @var StoreManagerInterface|MockObject
51+
*/
52+
private $storeManager;
53+
54+
/**
55+
* @var Store|MockObject
56+
*/
57+
private $store;
58+
4759
/**
4860
* @var FileInfo
4961
*/
@@ -60,6 +72,16 @@ protected function setUp()
6072
$this->pubDirectory = $pubDirectory = $this->getMockBuilder(ReadInterface::class)
6173
->getMockForAbstractClass();
6274

75+
$this->store = $this->getMockBuilder(Store::class)
76+
->disableOriginalConstructor()
77+
->getMock();
78+
$this->storeManager = $this->getMockBuilder(StoreManagerInterface::class)
79+
->setMethods(['getStore'])
80+
->getMockForAbstractClass();
81+
$this->storeManager->expects($this->any())
82+
->method('getStore')
83+
->willReturn($this->store);
84+
6385
$this->filesystem = $this->getMockBuilder(Filesystem::class)
6486
->disableOriginalConstructor()
6587
->getMock();
@@ -94,7 +116,8 @@ function ($arg) use ($baseDirectory, $pubDirectory) {
94116

95117
$this->model = new FileInfo(
96118
$this->filesystem,
97-
$this->mime
119+
$this->mime,
120+
$this->storeManager
98121
);
99122
}
100123

0 commit comments

Comments
 (0)