Skip to content

Commit ed35ab9

Browse files
author
Allan Paiste
committed
Code reformatted after style-checking failures
1 parent 0995b37 commit ed35ab9

File tree

4 files changed

+52
-47
lines changed

4 files changed

+52
-47
lines changed

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

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,26 +64,16 @@ public function __construct(
6464
}
6565

6666
/**
67-
* @param array|bool|null $value Attribute value
68-
* @return bool
67+
* @param array $value Attribute value
68+
* @return string
6969
*/
7070
protected function getUploadedImageName($value)
7171
{
72-
if (!is_array($value)) {
73-
return false;
72+
if (is_array($value) && isset($value[0]['name'])) {
73+
return $value[0]['name'];
7474
}
7575

76-
if (!count($value)) {
77-
return false;
78-
}
79-
80-
$imageData = reset($value);
81-
82-
if (!isset($imageData['name'])) {
83-
return false;
84-
}
85-
86-
return $imageData['name'];
76+
return '';
8777
}
8878

8979
/**
@@ -133,14 +123,12 @@ public function afterSave($object)
133123
{
134124
$value = $object->getData($this->getAttribute()->getName() . self::ADDITIONAL_DATA_SUFFIX);
135125

136-
if (!$imageName = $this->getUploadedImageName($value)) {
137-
return $this;
138-
}
139-
140-
try {
141-
$this->getImageUploader()->moveFileFromTmp($imageName);
142-
} catch (\Exception $e) {
143-
$this->_logger->critical($e);
126+
if ($imageName = $this->getUploadedImageName($value)) {
127+
try {
128+
$this->getImageUploader()->moveFileFromTmp($imageName);
129+
} catch (\Exception $e) {
130+
$this->_logger->critical($e);
131+
}
144132
}
145133

146134
return $this;

app/code/Magento/Catalog/Model/Category/DataProvider.php

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Ui\DataProvider\EavValidationRules;
1818
use Magento\Catalog\Model\CategoryFactory;
1919
use Magento\Framework\Exception\NoSuchEntityException;
20+
use Magento\Catalog\Model\Category\Attribute\Backend\Image as ImageBackendModel;
2021

2122
/**
2223
* Class DataProvider
@@ -206,21 +207,7 @@ public function getData()
206207
$categoryData = $this->addUseDefaultSettings($category, $categoryData);
207208
$categoryData = $this->addUseConfigSettings($categoryData);
208209
$categoryData = $this->filterFields($categoryData);
209-
210-
foreach ($category->getAttributes() as $attributeCode => $attribute) {
211-
$backendModel = $attribute->getBackend();
212-
213-
if (!isset($categoryData[$attributeCode])) {
214-
continue;
215-
}
216-
217-
if ($backendModel instanceof \Magento\Catalog\Model\Category\Attribute\Backend\Image) {
218-
unset($categoryData[$attributeCode]);
219-
220-
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
221-
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
222-
}
223-
}
210+
$categoryData = $this->convertValues($category, $categoryData);
224211

225212
$this->loadedData[$category->getId()] = $categoryData;
226213
}
@@ -382,6 +369,29 @@ protected function filterFields($categoryData)
382369
return array_diff_key($categoryData, array_flip($this->ignoreFields));
383370
}
384371

372+
/**
373+
* @param \Magento\Catalog\Model\Category $category
374+
* @param array $categoryData
375+
* @return array
376+
*/
377+
protected function convertValues($category, $categoryData)
378+
{
379+
foreach ($category->getAttributes() as $attributeCode => $attribute) {
380+
if (!isset($categoryData[$attributeCode])) {
381+
continue;
382+
}
383+
384+
if ($attribute->getBackend() instanceof ImageBackendModel) {
385+
unset($categoryData[$attributeCode]);
386+
387+
$categoryData[$attributeCode][0]['name'] = $category->getData($attributeCode);
388+
$categoryData[$attributeCode][0]['url'] = $category->getImageUrl($attributeCode);
389+
}
390+
}
391+
392+
return $categoryData;
393+
}
394+
385395
/**
386396
* Category's fields default values
387397
*

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/Image/UploadTest.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
use \Magento\Catalog\Controller\Adminhtml\Category\Image\Upload as Model;
99
use \Magento\Framework\App\Request\Http as Request;
10+
use \Magento\Catalog\Model\ImageUploader;
11+
use \Magento\Framework\Controller\ResultFactory;
12+
use \Magento\Framework\DataObject;
13+
use \Magento\Backend\App\Action\Context;
1014

1115
/**
1216
* Class UploadTest
@@ -40,18 +44,16 @@ public function testExecuteShouldSaveUploadedImageWithSpecifiedNameToTmpFolder($
4044
{
4145
$request = $this->objectManager->getObject(Request::class);
4246

43-
$uploader = $this->getMock(
44-
\Magento\Catalog\Model\ImageUploader::class, ['saveFileToTmpDir'], [], '', false);
47+
$uploader = $this->getMock(ImageUploader::class, ['saveFileToTmpDir'], [], '', false);
4548

46-
$resultFactory = $this->getMock(
47-
\Magento\Framework\Controller\ResultFactory::class, ['create'], [], '', false);
49+
$resultFactory = $this->getMock(ResultFactory::class, ['create'], [], '', false);
4850

4951
$resultFactory->expects($this->once())
5052
->method('create')
51-
->will($this->returnValue(new \Magento\Framework\DataObject()));
53+
->will($this->returnValue(new DataObject()));
5254

5355
$model = $this->objectManager->getObject(Model::class, [
54-
'context' => $this->objectManager->getObject(\Magento\Backend\App\Action\Context::class, [
56+
'context' => $this->objectManager->getObject(Context::class, [
5557
'request' => $request,
5658
'resultFactory' => $resultFactory
5759
]),
@@ -67,4 +69,4 @@ public function testExecuteShouldSaveUploadedImageWithSpecifiedNameToTmpFolder($
6769

6870
$model->execute();
6971
}
70-
}
72+
}

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Category/SaveTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category;
77

8+
use \Magento\Catalog\Controller\Adminhtml\Category\Save as Model;
9+
810
/**
911
* Class SaveTest
1012
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -628,10 +630,13 @@ public function testImagePreprocessingShouldNotSetValueToFalseWhenValueSet()
628630
{
629631
$eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityType'], [], '', false);
630632

633+
$imageBackendModel = $this->objectManager->getObject(
634+
\Magento\Catalog\Model\Category\Attribute\Backend\Image::class);
635+
631636
$collection = new \Magento\Framework\DataObject(['attribute_collection' => [
632637
new \Magento\Framework\DataObject([
633638
'attribute_code' => 'attribute1',
634-
'backend' => $this->objectManager->getObject(\Magento\Catalog\Model\Category\Attribute\Backend\Image::class)
639+
'backend' => $imageBackendModel
635640
]),
636641
new \Magento\Framework\DataObject([
637642
'attribute_code' => 'attribute2',
@@ -644,7 +649,7 @@ public function testImagePreprocessingShouldNotSetValueToFalseWhenValueSet()
644649
->with(\Magento\Catalog\Api\Data\CategoryAttributeInterface::ENTITY_TYPE_CODE)
645650
->will($this->returnValue($collection));
646651

647-
$model = $this->objectManager->getObject(\Magento\Catalog\Controller\Adminhtml\Category\Save::class, [
652+
$model = $this->objectManager->getObject(Model::class, [
648653
'eavConfig' => $eavConfig
649654
]);
650655

0 commit comments

Comments
 (0)