Skip to content

Commit e2d6bae

Browse files
committed
Merge remote-tracking branch 'tango/MC-29519' into DEC-PR-01
2 parents 3a6b76e + da17dd5 commit e2d6bae

File tree

3 files changed

+129
-35
lines changed

3 files changed

+129
-35
lines changed

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
namespace Magento\Catalog\Model\Category\Attribute\Backend;
77

88
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\App\ObjectManager;
910
use Magento\Framework\File\Uploader;
11+
use Magento\Store\Api\Data\StoreInterface;
12+
use Magento\Store\Model\StoreManagerInterface;
1013

1114
/**
1215
* Catalog category image attribute backend model
@@ -54,19 +57,28 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
5457
*/
5558
private $additionalData = '_additional_data_';
5659

60+
/**
61+
* @var StoreManagerInterface
62+
*/
63+
private $storeManager;
64+
5765
/**
5866
* @param \Psr\Log\LoggerInterface $logger
5967
* @param \Magento\Framework\Filesystem $filesystem
6068
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
69+
* @param StoreManagerInterface $storeManager
6170
*/
6271
public function __construct(
6372
\Psr\Log\LoggerInterface $logger,
6473
\Magento\Framework\Filesystem $filesystem,
65-
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
74+
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
75+
StoreManagerInterface $storeManager = null
6676
) {
6777
$this->_filesystem = $filesystem;
6878
$this->_fileUploaderFactory = $fileUploaderFactory;
6979
$this->_logger = $logger;
80+
$this->storeManager = $storeManager ?
81+
$storeManager : ObjectManager::getInstance()->get(StoreManagerInterface::class);
7082
}
7183

7284
/**
@@ -91,6 +103,7 @@ private function getUploadedImageName($value)
91103
*
92104
* @param string $imageName
93105
* @return string
106+
* @throws \Magento\Framework\Exception\FileSystemException
94107
*/
95108
private function checkUniqueImageName(string $imageName): string
96109
{
@@ -112,14 +125,26 @@ private function checkUniqueImageName(string $imageName): string
112125
*
113126
* @param \Magento\Framework\DataObject $object
114127
* @return $this
128+
* @throws \Magento\Framework\Exception\FileSystemException
115129
* @since 101.0.8
116130
*/
117131
public function beforeSave($object)
118132
{
119133
$attributeName = $this->getAttribute()->getName();
120134
$value = $object->getData($attributeName);
121135

122-
if ($this->fileResidesOutsideCategoryDir($value)) {
136+
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
137+
try {
138+
/** @var StoreInterface $store */
139+
$store = $this->storeManager->getStore();
140+
$baseMediaDir = $store->getBaseMediaDir();
141+
$newImgRelativePath = $this->getImageUploader()->moveFileFromTmp($imageName, true);
142+
$value[0]['url'] = '/' . $baseMediaDir . '/' . $newImgRelativePath;
143+
$value[0]['name'] = $value[0]['url'];
144+
} catch (\Exception $e) {
145+
$this->_logger->critical($e);
146+
}
147+
} elseif ($this->fileResidesOutsideCategoryDir($value)) {
123148
// use relative path for image attribute so we know it's outside of category dir when we fetch it
124149
// phpcs:ignore Magento2.Functions.DiscouragedFunction
125150
$value[0]['url'] = parse_url($value[0]['url'], PHP_URL_PATH);
@@ -194,19 +219,10 @@ private function fileResidesOutsideCategoryDir($value)
194219
*
195220
* @param \Magento\Framework\DataObject $object
196221
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
222+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
197223
*/
198224
public function afterSave($object)
199225
{
200-
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
201-
202-
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
203-
try {
204-
$this->getImageUploader()->moveFileFromTmp($imageName);
205-
} catch (\Exception $e) {
206-
$this->_logger->critical($e);
207-
}
208-
}
209-
210226
return $this;
211227
}
212228
}

app/code/Magento/Catalog/Model/ImageUploader.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,12 +191,12 @@ public function getFilePath($path, $imageName)
191191
* Checking file for moving and move it
192192
*
193193
* @param string $imageName
194-
*
194+
* @param bool $returnRelativePath
195195
* @return string
196196
*
197197
* @throws \Magento\Framework\Exception\LocalizedException
198198
*/
199-
public function moveFileFromTmp($imageName)
199+
public function moveFileFromTmp($imageName, $returnRelativePath = false)
200200
{
201201
$baseTmpPath = $this->getBaseTmpPath();
202202
$basePath = $this->getBasePath();
@@ -226,7 +226,7 @@ public function moveFileFromTmp($imageName)
226226
);
227227
}
228228

229-
return $imageName;
229+
return $returnRelativePath ? $baseImagePath : $imageName;
230230
}
231231

232232
/**

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

Lines changed: 98 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Catalog\Test\Unit\Model\Category\Attribute\Backend;
78

89
use Magento\Framework\App\Filesystem\DirectoryList;
10+
use Magento\Framework\Exception\FileSystemException;
911
use Magento\Framework\Filesystem\Directory\WriteInterface;
12+
use Magento\Store\Model\Store;
13+
use Magento\Store\Model\StoreManagerInterface;
1014

1115
/**
1216
* Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
@@ -39,6 +43,16 @@ class ImageTest extends \PHPUnit\Framework\TestCase
3943
*/
4044
private $filesystem;
4145

46+
/**
47+
* @var StoreManagerInterface|\PHPUnit_Framework_MockObject_MockObject;
48+
*/
49+
private $storeManagerInterfaceMock;
50+
51+
/**
52+
* @var Store|\PHPUnit_Framework_MockObject_MockObject
53+
*/
54+
private $storeMock;
55+
4256
/**
4357
* @inheritdoc
4458
*/
@@ -56,10 +70,6 @@ protected function setUp()
5670
['getName']
5771
);
5872

59-
$this->attribute->expects($this->once())
60-
->method('getName')
61-
->will($this->returnValue('test_attribute'));
62-
6373
$this->logger = $this->getMockForAbstractClass(
6474
\Psr\Log\LoggerInterface::class,
6575
[],
@@ -75,6 +85,14 @@ protected function setUp()
7585
['moveFileFromTmp', 'getBasePath']
7686
);
7787

88+
$this->storeManagerInterfaceMock = $this->getMockBuilder(
89+
StoreManagerInterface::class
90+
)->disableOriginalConstructor()->getMock();
91+
92+
$this->storeMock = $this->getMockBuilder(
93+
Store::class
94+
)->disableOriginalConstructor()->getMock();
95+
7896
$this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)->disableOriginalConstructor()
7997
->getMock();
8098
}
@@ -97,6 +115,10 @@ public function deletedValueDataProvider()
97115
*/
98116
public function testBeforeSaveValueDeletion($value)
99117
{
118+
$this->attribute->expects($this->once())
119+
->method('getName')
120+
->will($this->returnValue('test_attribute'));
121+
100122
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
101123
$model->setAttribute($this->attribute);
102124

@@ -132,6 +154,10 @@ public function invalidValueDataProvider()
132154
*/
133155
public function testBeforeSaveValueInvalid($value)
134156
{
157+
$this->attribute->expects($this->once())
158+
->method('getName')
159+
->will($this->returnValue('test_attribute'));
160+
135161
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
136162
$model->setAttribute($this->attribute);
137163

@@ -147,7 +173,11 @@ public function testBeforeSaveValueInvalid($value)
147173
*/
148174
public function testBeforeSaveAttributeFileName()
149175
{
150-
$model = $this->setUpModelForAfterSave();
176+
$this->attribute->expects($this->once())
177+
->method('getName')
178+
->will($this->returnValue('test_attribute'));
179+
180+
$model = $this->setUpModelForTests();
151181
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
152182
$this->filesystem->expects($this->once())
153183
->method('getDirectoryWrite')
@@ -177,7 +207,11 @@ public function testBeforeSaveAttributeFileName()
177207
*/
178208
public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
179209
{
180-
$model = $this->setUpModelForAfterSave();
210+
$this->attribute->expects($this->once())
211+
->method('getName')
212+
->will($this->returnValue('test_attribute'));
213+
214+
$model = $this->setUpModelForTests();
181215
$model->setAttribute($this->attribute);
182216
$imagePath = '/pub/media/wysiwyg/test123.jpg';
183217
$this->filesystem
@@ -211,7 +245,19 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
211245
*/
212246
public function testBeforeSaveTemporaryAttribute()
213247
{
214-
$model = $this->setUpModelForAfterSave();
248+
$this->attribute->expects($this->once())
249+
->method('getName')
250+
->will($this->returnValue('test_attribute'));
251+
252+
$this->storeManagerInterfaceMock->expects($this->once())
253+
->method('getStore')
254+
->willReturn($this->storeMock);
255+
256+
$this->storeMock->expects($this->once())
257+
->method('getBaseMediaDir')
258+
->willReturn('pub/media');
259+
260+
$model = $this->setUpModelForTests();
215261
$model->setAttribute($this->attribute);
216262

217263
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
@@ -220,10 +266,16 @@ public function testBeforeSaveTemporaryAttribute()
220266
->with(DirectoryList::MEDIA)
221267
->willReturn($mediaDirectoryMock);
222268

269+
$this->imageUploader->expects($this->any())->method('moveFileFromTmp')->willReturn('test123.jpg');
270+
223271
$object = new \Magento\Framework\DataObject(
224272
[
225273
'test_attribute' => [
226-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
274+
[
275+
'name' => 'test123.jpg',
276+
'tmp_name' => 'abc123',
277+
'url' => 'http://www.example.com/pub/media/temp/test123.jpg'
278+
],
227279
],
228280
]
229281
);
@@ -232,7 +284,7 @@ public function testBeforeSaveTemporaryAttribute()
232284

233285
$this->assertEquals(
234286
[
235-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
287+
['name' => '/pub/media/test123.jpg', 'tmp_name' => 'abc123', 'url' => '/pub/media/test123.jpg'],
236288
],
237289
$object->getData('_additional_data_test_attribute')
238290
);
@@ -257,7 +309,7 @@ public function testBeforeSaveAttributeStringValue()
257309
/**
258310
* @return \Magento\Catalog\Model\Category\Attribute\Backend\Image
259311
*/
260-
private function setUpModelForAfterSave()
312+
private function setUpModelForTests()
261313
{
262314
$objectManagerMock = $this->createPartialMock(\Magento\Framework\App\ObjectManager::class, ['get']);
263315

@@ -283,6 +335,7 @@ function ($class, $params = []) use ($imageUploaderMock) {
283335
'objectManager' => $objectManagerMock,
284336
'logger' => $this->logger,
285337
'filesystem' => $this->filesystem,
338+
'storeManager' => $this->storeManagerInterfaceMock
286339
]
287340
);
288341
$this->objectManager->setBackwardCompatibleProperty($model, 'imageUploader', $this->imageUploader);
@@ -307,12 +360,13 @@ public function attributeValueDataProvider()
307360
* @dataProvider attributeValueDataProvider
308361
*
309362
* @param array $value
363+
* @throws FileSystemException
310364
*/
311-
public function testAfterSaveWithAdditionalData($value)
365+
public function testBeforeSaveWithAdditionalData($value)
312366
{
313-
$model = $this->setUpModelForAfterSave();
367+
$model = $this->setUpModelForTests();
314368

315-
$this->imageUploader->expects($this->once())
369+
$this->imageUploader->expects($this->never())
316370
->method('moveFileFromTmp')
317371
->with($this->equalTo('test1234.jpg'));
318372

@@ -323,17 +377,18 @@ public function testAfterSaveWithAdditionalData($value)
323377
]
324378
);
325379

326-
$model->afterSave($object);
380+
$model->beforeSave($object);
327381
}
328382

329383
/**
330384
* @dataProvider attributeValueDataProvider
331385
*
332386
* @param array $value
387+
* @throws FileSystemException
333388
*/
334-
public function testAfterSaveWithoutAdditionalData($value)
389+
public function testBeforeSaveWithoutAdditionalData($value)
335390
{
336-
$model = $this->setUpModelForAfterSave();
391+
$model = $this->setUpModelForTests();
337392

338393
$this->imageUploader->expects($this->never())
339394
->method('moveFileFromTmp');
@@ -344,15 +399,38 @@ public function testAfterSaveWithoutAdditionalData($value)
344399
]
345400
);
346401

347-
$model->afterSave($object);
402+
$model->beforeSave($object);
348403
}
349404

350405
/**
351406
* Test afterSaveWithExceptions.
352407
*/
353-
public function testAfterSaveWithExceptions()
408+
public function testBeforeSaveWithExceptions()
354409
{
355-
$model = $this->setUpModelForAfterSave();
410+
$model = $this->setUpModelForTests();
411+
412+
$this->storeManagerInterfaceMock->expects($this->once())
413+
->method('getStore')
414+
->willReturn($this->storeMock);
415+
416+
$this->storeMock->expects($this->once())
417+
->method('getBaseMediaDir')
418+
->willReturn('pub/media');
419+
420+
$this->attribute->expects($this->once())
421+
->method('getName')
422+
->will($this->returnValue('_additional_data_test_attribute'));
423+
424+
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
425+
$this->filesystem->expects($this->any())
426+
->method('getDirectoryWrite')
427+
->with(DirectoryList::MEDIA)
428+
->willReturn($mediaDirectoryMock);
429+
$this->imageUploader->expects($this->any())->method('getBasePath')->willReturn('base/path');
430+
$mediaDirectoryMock->expects($this->any())
431+
->method('getAbsolutePath')
432+
->with('base/path/test1234.jpg')
433+
->willReturn('absolute/path/base/path/test1234.jpg');
356434

357435
$exception = new \Exception();
358436

@@ -370,6 +448,6 @@ public function testAfterSaveWithExceptions()
370448
]
371449
);
372450

373-
$model->afterSave($object);
451+
$model->beforeSave($object);
374452
}
375453
}

0 commit comments

Comments
 (0)