Skip to content

Commit eefb665

Browse files
committed
MC-17765: Category A image replaced by uploading to Category B
1 parent 5a5a246 commit eefb665

File tree

2 files changed

+58
-44
lines changed
  • app/code/Magento/Catalog

2 files changed

+58
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ public function __construct(
7070
}
7171

7272
/**
73-
* Gets image name from $value array.
74-
* Will return empty string in a case when $value is not an array
73+
* Gets image name from $value array. Will return empty string in a case when $value is not an array.
7574
*
7675
* @param array $value Attribute value
7776
* @return string
@@ -107,8 +106,7 @@ private function checkUniqueImageName(string $imageName): string
107106
}
108107

109108
/**
110-
* Avoiding saving potential upload data to DB
111-
* Will set empty image attribute value if image was not uploaded
109+
* Avoiding saving potential upload data to DB. Will set empty image attribute value if image was not uploaded.
112110
*
113111
* @param \Magento\Framework\DataObject $object
114112
* @return $this
@@ -136,6 +134,8 @@ public function beforeSave($object)
136134
}
137135

138136
/**
137+
* Get image uploader.
138+
*
139139
* @return \Magento\Catalog\Model\ImageUploader
140140
*
141141
* @deprecated 101.0.0

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

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Magento\Framework\App\Filesystem\DirectoryList;
99
use Magento\Framework\Filesystem\Directory\WriteInterface;
1010

11+
/**
12+
* Test for Magento\Catalog\Model\Category\Attribute\Backend\Image class.
13+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
14+
*/
1115
class ImageTest extends \PHPUnit\Framework\TestCase
1216
{
1317
/**
@@ -96,9 +100,7 @@ public function testBeforeSaveValueDeletion($value)
96100
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
97101
$model->setAttribute($this->attribute);
98102

99-
$object = new \Magento\Framework\DataObject([
100-
'test_attribute' => $value
101-
]);
103+
$object = new \Magento\Framework\DataObject(['test_attribute' => $value]);
102104

103105
$model->beforeSave($object);
104106

@@ -133,9 +135,7 @@ public function testBeforeSaveValueInvalid($value)
133135
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
134136
$model->setAttribute($this->attribute);
135137

136-
$object = new \Magento\Framework\DataObject([
137-
'test_attribute' => $value
138-
]);
138+
$object = new \Magento\Framework\DataObject(['test_attribute' => $value]);
139139

140140
$model->beforeSave($object);
141141

@@ -163,11 +163,13 @@ public function testBeforeSaveAttributeFileName()
163163
->with('absolute/path/base/path/test123.jpg')
164164
->willReturn(false);
165165

166-
$object = new \Magento\Framework\DataObject([
167-
'test_attribute' => [
168-
['name' => 'test123.jpg']
166+
$object = new \Magento\Framework\DataObject(
167+
[
168+
'test_attribute' => [
169+
['name' => 'test123.jpg'],
170+
],
169171
]
170-
]);
172+
);
171173

172174
$model->beforeSave($object);
173175

@@ -193,14 +195,16 @@ public function testBeforeSaveAttributeFileNameOutsideOfCategoryDir()
193195
->with(DirectoryList::MEDIA)
194196
->willReturn('pub/media');
195197

196-
$object = new \Magento\Framework\DataObject([
197-
'test_attribute' => [
198-
[
199-
'name' => '/test123.jpg',
200-
'url' => '/pub/media/wysiwyg/test123.jpg',
201-
]
198+
$object = new \Magento\Framework\DataObject(
199+
[
200+
'test_attribute' => [
201+
[
202+
'name' => '/test123.jpg',
203+
'url' => '/pub/media/wysiwyg/test123.jpg',
204+
],
205+
],
202206
]
203-
]);
207+
);
204208

205209
$model->beforeSave($object);
206210

@@ -225,17 +229,22 @@ public function testBeforeSaveTemporaryAttribute()
225229
->with(DirectoryList::MEDIA)
226230
->willReturn($mediaDirectoryMock);
227231

228-
$object = new \Magento\Framework\DataObject([
229-
'test_attribute' => [
230-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
232+
$object = new \Magento\Framework\DataObject(
233+
[
234+
'test_attribute' => [
235+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
236+
],
231237
]
232-
]);
238+
);
233239

234240
$model->beforeSave($object);
235241

236-
$this->assertEquals([
237-
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg']
238-
], $object->getData('_additional_data_test_attribute'));
242+
$this->assertEquals(
243+
[
244+
['name' => 'test123.jpg', 'tmp_name' => 'abc123', 'url' => 'http://www.example.com/test123.jpg'],
245+
],
246+
$object->getData('_additional_data_test_attribute')
247+
);
239248
}
240249

241250
/**
@@ -246,9 +255,7 @@ public function testBeforeSaveAttributeStringValue()
246255
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
247256
$model->setAttribute($this->attribute);
248257

249-
$object = new \Magento\Framework\DataObject([
250-
'test_attribute' => 'test123.jpg'
251-
]);
258+
$object = new \Magento\Framework\DataObject(['test_attribute' => 'test123.jpg']);
252259

253260
$model->beforeSave($object);
254261

@@ -267,19 +274,26 @@ private function setUpModelForAfterSave()
267274

268275
$objectManagerMock->expects($this->any())
269276
->method('get')
270-
->will($this->returnCallback(function ($class, $params = []) use ($imageUploaderMock) {
271-
if ($class == \Magento\Catalog\CategoryImageUpload::class) {
272-
return $imageUploaderMock;
273-
}
274-
275-
return $this->objectManager->get($class, $params);
276-
}));
277-
278-
$model = $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class, [
279-
'objectManager' => $objectManagerMock,
280-
'logger' => $this->logger,
281-
'filesystem' => $this->filesystem,
282-
]);
277+
->will(
278+
$this->returnCallback(
279+
function ($class, $params = []) use ($imageUploaderMock) {
280+
if ($class == \Magento\Catalog\CategoryImageUpload::class) {
281+
return $imageUploaderMock;
282+
}
283+
284+
return $this->objectManager->get($class, $params);
285+
}
286+
)
287+
);
288+
289+
$model = $this->objectManager->getObject(
290+
\Magento\Catalog\Model\Category\Attribute\Backend\Image::class,
291+
[
292+
'objectManager' => $objectManagerMock,
293+
'logger' => $this->logger,
294+
'filesystem' => $this->filesystem,
295+
]
296+
);
283297
$this->objectManager->setBackwardCompatibleProperty($model, 'imageUploader', $this->imageUploader);
284298

285299
return $model->setAttribute($this->attribute);

0 commit comments

Comments
 (0)