Skip to content

Commit 0ac82eb

Browse files
author
Oleksii Korshenko
committed
MAGETWO-70395: Fixed pointless exception in logs every time a category with image is saved #9904
- fixed unit tests and code style
1 parent b96019f commit 0ac82eb

File tree

2 files changed

+18
-11
lines changed
  • app/code/Magento/Catalog

2 files changed

+18
-11
lines changed

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ private function getImageUploader()
118118
return $this->imageUploader;
119119
}
120120

121+
/**
122+
* Check if temporary file is available for new image upload.
123+
*
124+
* @param array $value
125+
* @return bool
126+
*/
127+
private function isTmpFileAvailable($value)
128+
{
129+
return is_array($value) && isset($value[0]['tmp_name']);
130+
}
131+
121132
/**
122133
* Save uploaded file and set its name to category
123134
*
@@ -128,17 +139,13 @@ public function afterSave($object)
128139
{
129140
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
130141

131-
if(isset($value[0]['tmp_name'])) {
132-
// A file was just uploaded, so process it
133-
if ($imageName = $this->getUploadedImageName($value)) {
134-
try {
135-
$this->getImageUploader()->moveFileFromTmp($imageName);
136-
} catch (\Exception $e) {
137-
$this->_logger->critical($e);
138-
}
142+
if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) {
143+
try {
144+
$this->getImageUploader()->moveFileFromTmp($imageName);
145+
} catch (\Exception $e) {
146+
$this->_logger->critical($e);
139147
}
140148
}
141-
142149
return $this;
143150
}
144151
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function testAfterSaveWithAdditionalData($value)
240240
$object = new \Magento\Framework\DataObject(
241241
[
242242
'test_attribute' => $value,
243-
'_additional_data_test_attribute' => [['name' => 'test1234.jpg']]
243+
'_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
244244
]
245245
);
246246

@@ -284,7 +284,7 @@ public function testAfterSaveWithExceptions()
284284

285285
$object = new \Magento\Framework\DataObject(
286286
[
287-
'_additional_data_test_attribute' => [['name' => 'test1234.jpg']]
287+
'_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']]
288288
]
289289
);
290290

0 commit comments

Comments
 (0)