Skip to content

Commit 7c19f68

Browse files
committed
Merge remote-tracking branch 'origin/MC-17764' into 2.2-develop-pr105
2 parents 8cc9290 + 3ac97e0 commit 7c19f68

File tree

8 files changed

+224
-42
lines changed

8 files changed

+224
-42
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Catalog\Model\Category\Attribute\Backend;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\File\Uploader;
10+
811
/**
912
* Catalog category image attribute backend model
1013
*
@@ -68,7 +71,8 @@ public function __construct(
6871

6972
/**
7073
* Gets image name from $value array.
71-
* Will return empty string in a case when $value is not an array
74+
*
75+
* Will return empty string in a case when $value is not an array.
7276
*
7377
* @param array $value Attribute value
7478
* @return string
@@ -83,8 +87,28 @@ private function getUploadedImageName($value)
8387
}
8488

8589
/**
86-
* Avoiding saving potential upload data to DB
87-
* Will set empty image attribute value if image was not uploaded
90+
* Check that image name exists in catalog/category directory and return new image name if it already exists.
91+
*
92+
* @param string $imageName
93+
* @return string
94+
*/
95+
private function checkUniqueImageName(string $imageName): string
96+
{
97+
$imageUploader = $this->getImageUploader();
98+
$mediaDirectory = $this->_filesystem->getDirectoryWrite(DirectoryList::MEDIA);
99+
$imageAbsolutePath = $mediaDirectory->getAbsolutePath(
100+
$imageUploader->getBasePath() . DIRECTORY_SEPARATOR . $imageName
101+
);
102+
103+
$imageName = Uploader::getNewFilename($imageAbsolutePath);
104+
105+
return $imageName;
106+
}
107+
108+
/**
109+
* Avoiding saving potential upload data to DB.
110+
*
111+
* Will set empty image attribute value if image was not uploaded.
88112
*
89113
* @param \Magento\Framework\DataObject $object
90114
* @return $this
@@ -96,6 +120,7 @@ public function beforeSave($object)
96120
$value = $object->getData($attributeName);
97121

98122
if ($imageName = $this->getUploadedImageName($value)) {
123+
$imageName = $this->checkUniqueImageName($imageName);
99124
$object->setData($this->additionalData . $attributeName, $value);
100125
$object->setData($attributeName, $imageName);
101126
} elseif (!is_string($value)) {
@@ -106,6 +131,8 @@ public function beforeSave($object)
106131
}
107132

108133
/**
134+
* Get image uploader.
135+
*
109136
* @return \Magento\Catalog\Model\ImageUploader
110137
*
111138
* @deprecated 101.0.0

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Magento\Catalog\Model;
99

10+
use Magento\Framework\File\Uploader;
11+
1012
/**
1113
* Catalog image uploader
1214
*/
@@ -201,7 +203,14 @@ public function moveFileFromTmp($imageName)
201203
$baseTmpPath = $this->getBaseTmpPath();
202204
$basePath = $this->getBasePath();
203205

204-
$baseImagePath = $this->getFilePath($basePath, $imageName);
206+
$baseImagePath = $this->getFilePath(
207+
$basePath,
208+
Uploader::getNewFileName(
209+
$this->mediaDirectory->getAbsolutePath(
210+
$this->getFilePath($basePath, $imageName)
211+
)
212+
)
213+
);
205214
$baseTmpImagePath = $this->getFilePath($baseTmpPath, $imageName);
206215

207216
try {

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

Lines changed: 91 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Model\Category\Attribute\Backend;
77

8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
use Magento\Framework\Filesystem\Directory\WriteInterface;
10+
11+
/**
12+
* Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
815
class ImageTest extends \PHPUnit\Framework\TestCase
916
{
1017
/**
@@ -27,6 +34,14 @@ class ImageTest extends \PHPUnit\Framework\TestCase
2734
*/
2835
private $logger;
2936

37+
/**
38+
* @var \Magento\Framework\Filesystem|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $filesystem;
41+
42+
/**
43+
* @inheritdoc
44+
*/
3045
protected function setUp()
3146
{
3247
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
@@ -57,8 +72,12 @@ protected function setUp()
5772

5873
$this->imageUploader = $this->createPartialMock(
5974
\Magento\Catalog\Model\ImageUploader::class,
60-
['moveFileFromTmp']
75+
['moveFileFromTmp', 'getBasePath']
6176
);
77+
78+
$this->filesystem = $this->getMockBuilder(\Magento\Framework\Filesystem::class)
79+
->disableOriginalConstructor()
80+
->getMock();
6281
}
6382

6483
/**
@@ -82,9 +101,7 @@ public function testBeforeSaveValueDeletion($value)
82101
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
83102
$model->setAttribute($this->attribute);
84103

85-
$object = new \Magento\Framework\DataObject([
86-
'test_attribute' => $value
87-
]);
104+
$object = new \Magento\Framework\DataObject(['test_attribute' => $value]);
88105

89106
$model->beforeSave($object);
90107

@@ -119,57 +136,84 @@ public function testBeforeSaveValueInvalid($value)
119136
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
120137
$model->setAttribute($this->attribute);
121138

122-
$object = new \Magento\Framework\DataObject([
123-
'test_attribute' => $value
124-
]);
139+
$object = new \Magento\Framework\DataObject(['test_attribute' => $value]);
125140

126141
$model->beforeSave($object);
127142

128143
$this->assertEquals('', $object->getTestAttribute());
129144
}
130145

146+
/**
147+
* Test beforeSaveAttributeFileName.
148+
*/
131149
public function testBeforeSaveAttributeFileName()
132150
{
133-
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
134-
$model->setAttribute($this->attribute);
151+
$model = $this->setUpModelForAfterSave();
152+
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
153+
$this->filesystem->expects($this->once())
154+
->method('getDirectoryWrite')
155+
->with(DirectoryList::MEDIA)
156+
->willReturn($mediaDirectoryMock);
157+
$this->imageUploader->expects($this->once())->method('getBasePath')->willReturn('base/path');
158+
$mediaDirectoryMock->expects($this->once())
159+
->method('getAbsolutePath')
160+
->with('base/path/test123.jpg')
161+
->willReturn('absolute/path/base/path/test123.jpg');
135162

136-
$object = new \Magento\Framework\DataObject([
137-
'test_attribute' => [
138-
['name' => 'test123.jpg']
163+
$object = new \Magento\Framework\DataObject(
164+
[
165+
'test_attribute' => [
166+
['name' => 'test123.jpg'],
167+
],
139168
]
140-
]);
169+
);
141170

142171
$model->beforeSave($object);
143172

144173
$this->assertEquals('test123.jpg', $object->getTestAttribute());
145174
}
146175

176+
/**
177+
* Test beforeSaveTemporaryAttribute.
178+
*/
147179
public function testBeforeSaveTemporaryAttribute()
148180
{
149-
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
181+
$model = $this->setUpModelForAfterSave();
150182
$model->setAttribute($this->attribute);
151183

152-
$object = new \Magento\Framework\DataObject([
153-
'test_attribute' => [
154-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
184+
$mediaDirectoryMock = $this->createMock(WriteInterface::class);
185+
$this->filesystem->expects($this->once())
186+
->method('getDirectoryWrite')
187+
->with(DirectoryList::MEDIA)
188+
->willReturn($mediaDirectoryMock);
189+
190+
$object = new \Magento\Framework\DataObject(
191+
[
192+
'test_attribute' => [
193+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
194+
],
155195
]
156-
]);
196+
);
157197

158198
$model->beforeSave($object);
159199

160-
$this->assertEquals([
161-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
162-
], $object->getData('_additional_data_test_attribute'));
200+
$this->assertEquals(
201+
[
202+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
203+
],
204+
$object->getData('_additional_data_test_attribute')
205+
);
163206
}
164207

208+
/**
209+
* Test beforeSaveAttributeStringValue.
210+
*/
165211
public function testBeforeSaveAttributeStringValue()
166212
{
167213
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
168214
$model->setAttribute($this->attribute);
169215

170-
$object = new \Magento\Framework\DataObject([
171-
'test_attribute' => 'test123.jpg'
172-
]);
216+
$object = new \Magento\Framework\DataObject(['test_attribute' => 'test123.jpg']);
173217

174218
$model->beforeSave($object);
175219

@@ -188,18 +232,26 @@ private function setUpModelForAfterSave()
188232

189233
$objectManagerMock->expects($this->any())
190234
->method('get')
191-
->will($this->returnCallback(function ($class, $params = []) use ($imageUploaderMock) {
192-
if ($class == \Magento\Catalog\CategoryImageUpload::class) {
193-
return $imageUploaderMock;
194-
}
195-
196-
return $this->objectManager->get($class, $params);
197-
}));
198-
199-
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class, [
200-
'objectManager' => $objectManagerMock,
201-
'logger' => $this->logger
202-
]);
235+
->will(
236+
$this->returnCallback(
237+
function ($class, $params = []) use ($imageUploaderMock) {
238+
if ($class == \Magento\Catalog\CategoryImageUpload::class) {
239+
return $imageUploaderMock;
240+
}
241+
242+
return $this->objectManager->get($class, $params);
243+
}
244+
)
245+
);
246+
247+
$model = $this->objectManager->getObject(
248+
\Magento\Catalog\Model\Category\Attribute\Backend\Image::class,
249+
[
250+
'objectManager' => $objectManagerMock,
251+
'logger' => $this->logger,
252+
'filesystem' => $this->filesystem,
253+
]
254+
);
203255
$this->objectManager->setBackwardCompatibleProperty($model, 'imageUploader', $this->imageUploader);
204256

205257
return $model->setAttribute($this->attribute);
@@ -262,6 +314,9 @@ public function testAfterSaveWithoutAdditionalData($value)
262314
$model->afterSave($object);
263315
}
264316

317+
/**
318+
* Test afterSaveWithExceptions.
319+
*/
265320
public function testAfterSaveWithExceptions()
266321
{
267322
$model = $this->setUpModelForAfterSave();

dev/tests/integration/testsuite/Magento/Catalog/Model/ImageUploaderTest.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ protected function setUp()
4444
$this->imageUploader = $this->objectManager->create(
4545
\Magento\Catalog\Model\ImageUploader::class,
4646
[
47-
'baseTmpPath' => $this->mediaDirectory->getRelativePath('tmp'),
48-
'basePath' => __DIR__,
47+
'baseTmpPath' => 'catalog/tmp/category',
48+
'basePath' => 'catalog/category',
4949
'allowedExtensions' => ['jpg', 'jpeg', 'gif', 'png'],
5050
'allowedMimeTypes' => ['image/jpg', 'image/jpeg', 'image/gif', 'image/png']
5151
]
@@ -73,6 +73,24 @@ public function testSaveFileToTmpDir()
7373
$this->assertTrue(is_file($this->mediaDirectory->getAbsolutePath($filePath)));
7474
}
7575

76+
/**
77+
* Test that method rename files when move it with the same name into base directory.
78+
*
79+
* @return void
80+
* @magentoDataFixture Magento/Catalog/_files/catalog_category_image.php
81+
* @magentoDataFixture Magento/Catalog/_files/catalog_tmp_category_image.php
82+
*/
83+
public function testMoveFileFromTmp()
84+
{
85+
$expectedFilePath = $this->imageUploader->getBasePath() . DIRECTORY_SEPARATOR . 'magento_small_image_1.jpg';
86+
87+
$this->assertFileNotExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
88+
89+
$this->imageUploader->moveFileFromTmp('magento_small_image.jpg');
90+
91+
$this->assertFileExists($this->mediaDirectory->getAbsolutePath($expectedFilePath));
92+
}
93+
7694
/**
7795
* @expectedException \Magento\Framework\Exception\LocalizedException
7896
* @expectedExceptionMessage File validation failed.
@@ -135,5 +153,6 @@ public static function tearDownAfterClass()
135153
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
136154
$mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
137155
$mediaDirectory->delete('tmp');
156+
$mediaDirectory->delete('catalog');
138157
}
139158
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
11+
12+
/** @var $mediaDirectory \Magento\Framework\Filesystem\Directory\WriteInterface */
13+
$mediaDirectory = $objectManager->get(\Magento\Framework\Filesystem::class)
14+
->getDirectoryWrite(DirectoryList::MEDIA);
15+
$fileName = 'magento_small_image.jpg';
16+
$filePath = 'catalog/category/' . $fileName;
17+
$mediaDirectory->create('catalog/category');
18+
19+
copy(__DIR__ . DIRECTORY_SEPARATOR . $fileName, $mediaDirectory->getAbsolutePath($filePath));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
use Magento\Framework\App\Filesystem\DirectoryList;
9+
10+
/** @var \Magento\Framework\Filesystem\Directory\WriteInterface $mediaDirectory */
11+
$mediaDirectory = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
12+
\Magento\Framework\Filesystem::class
13+
)->getDirectoryWrite(
14+
DirectoryList::MEDIA
15+
);
16+
17+
$mediaDirectory->delete('catalog/category');

0 commit comments

Comments
 (0)