From 0ac82ebb63032909f5c61c40ee1b1bbc3b5d9c32 Mon Sep 17 00:00:00 2001 From: Oleksii Korshenko Date: Wed, 5 Jul 2017 17:24:48 -0500 Subject: [PATCH] MAGETWO-70395: Fixed pointless exception in logs every time a category with image is saved #9904 - fixed unit tests and code style --- .../Category/Attribute/Backend/Image.php | 25 ++++++++++++------- .../Category/Attribute/Backend/ImageTest.php | 4 +-- 2 files changed, 18 insertions(+), 11 deletions(-) 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']] ] );