diff --git a/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php b/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php index 52260079ddbfd..a0f1fbfa25733 100644 --- a/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php +++ b/app/code/Magento/Catalog/Model/Category/Attribute/Backend/Image.php @@ -118,6 +118,17 @@ private function getImageUploader() return $this->imageUploader; } + /** + * Check if temporary file is available for new image upload. + * + * @param array $value + * @return bool + */ + private function isTmpFileAvailable($value) + { + return is_array($value) && isset($value[0]['tmp_name']); + } + /** * Save uploaded file and set its name to category * @@ -128,17 +139,13 @@ public function afterSave($object) { $value = $object->getData($this->additionalData . $this->getAttribute()->getName()); - if(isset($value[0]['tmp_name'])) { - // A file was just uploaded, so process it - if ($imageName = $this->getUploadedImageName($value)) { - try { - $this->getImageUploader()->moveFileFromTmp($imageName); - } catch (\Exception $e) { - $this->_logger->critical($e); - } + if ($this->isTmpFileAvailable($value) && $imageName = $this->getUploadedImageName($value)) { + try { + $this->getImageUploader()->moveFileFromTmp($imageName); + } catch (\Exception $e) { + $this->_logger->critical($e); } } - return $this; } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php b/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php index b12acb5fee4d9..180229b3b7fcc 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/Category/Attribute/Backend/ImageTest.php @@ -240,7 +240,7 @@ public function testAfterSaveWithAdditionalData($value) $object = new \Magento\Framework\DataObject( [ 'test_attribute' => $value, - '_additional_data_test_attribute' => [['name' => 'test1234.jpg']] + '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']] ] ); @@ -284,7 +284,7 @@ public function testAfterSaveWithExceptions() $object = new \Magento\Framework\DataObject( [ - '_additional_data_test_attribute' => [['name' => 'test1234.jpg']] + '_additional_data_test_attribute' => [['name' => 'test1234.jpg', 'tmp_name' => 'test-test-1234']] ] );