Skip to content

Commit 06034a1

Browse files
committed
MAGETWO-57989: Unable to create custom image attribute in category
1 parent b5f29b4 commit 06034a1

File tree

8 files changed

+223
-174
lines changed

8 files changed

+223
-174
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Category/Save.php

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

88
use Magento\Store\Model\StoreManagerInterface;
9-
use \Magento\Catalog\Api\Data\CategoryAttributeInterface;
9+
use Magento\Catalog\Api\Data\CategoryAttributeInterface;
1010

1111
/**
1212
* Class Save
@@ -70,14 +70,15 @@ public function __construct(
7070
\Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
7171
\Magento\Framework\View\LayoutFactory $layoutFactory,
7272
StoreManagerInterface $storeManager,
73-
\Magento\Eav\Model\Config $eavConfig
73+
\Magento\Eav\Model\Config $eavConfig = null
7474
) {
7575
parent::__construct($context);
7676
$this->resultRawFactory = $resultRawFactory;
7777
$this->resultJsonFactory = $resultJsonFactory;
7878
$this->layoutFactory = $layoutFactory;
7979
$this->storeManager = $storeManager;
80-
$this->eavConfig = $eavConfig;
80+
$this->eavConfig = $eavConfig
81+
?: \Magento\Framework\App\ObjectManager::getInstance()->get(\Magento\Eav\Model\Config::class);
8182
}
8283

8384
/**
@@ -233,9 +234,10 @@ public function execute()
233234
}
234235

235236
/**
236-
* @param array $data Category data
237-
* @return mixed
238-
* @throws \Magento\Framework\Exception\LocalizedException
237+
* Sets image attribute data to false if image was removed
238+
*
239+
* @param array $data
240+
* @return array
239241
*/
240242
public function imagePreprocessing(array $data)
241243
{

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,29 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4848
*/
4949
private $imageUploader;
5050

51-
/**
52-
* @var \Magento\Framework\ObjectManagerInterface
53-
*/
54-
private $objectManager;
55-
5651
/**
5752
* @param \Psr\Log\LoggerInterface $logger
5853
* @param \Magento\Framework\Filesystem $filesystem
5954
* @param \Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
60-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
6155
*/
6256
public function __construct(
6357
\Psr\Log\LoggerInterface $logger,
6458
\Magento\Framework\Filesystem $filesystem,
65-
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory,
66-
\Magento\Framework\ObjectManagerInterface $objectManager
59+
\Magento\MediaStorage\Model\File\UploaderFactory $fileUploaderFactory
6760
) {
6861
$this->_filesystem = $filesystem;
6962
$this->_fileUploaderFactory = $fileUploaderFactory;
7063
$this->_logger = $logger;
71-
$this->objectManager = $objectManager;
7264
}
7365

7466
/**
67+
* Gets image name from $value array.
68+
* Will return empty string in a case when $value is not an array
69+
*
7570
* @param array $value Attribute value
7671
* @return string
7772
*/
78-
protected function getUploadedImageName($value)
73+
private function getUploadedImageName($value)
7974
{
8075
if (is_array($value) && isset($value[0]['name'])) {
8176
return $value[0]['name'];
@@ -86,6 +81,7 @@ protected function getUploadedImageName($value)
8681

8782
/**
8883
* Avoiding saving potential upload data to DB
84+
* Will set empty image attribute value if image was not uploaded
8985
*
9086
* @param \Magento\Framework\DataObject $object
9187
* @return $this
@@ -113,7 +109,8 @@ public function beforeSave($object)
113109
private function getImageUploader()
114110
{
115111
if ($this->imageUploader === null) {
116-
$this->imageUploader = $this->objectManager->get(\Magento\Catalog\CategoryImageUpload::class);
112+
$this->imageUploader = \Magento\Framework\App\ObjectManager::getInstance()
113+
->get(\Magento\Catalog\CategoryImageUpload::class);
117114
}
118115

119116
return $this->imageUploader;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,11 +370,13 @@ protected function filterFields($categoryData)
370370
}
371371

372372
/**
373+
* Converts category image data to acceptable for rendering format
374+
*
373375
* @param \Magento\Catalog\Model\Category $category
374376
* @param array $categoryData
375377
* @return array
376378
*/
377-
protected function convertValues($category, $categoryData)
379+
private function convertValues($category, $categoryData)
378380
{
379381
foreach ($category->getAttributes() as $attributeCode => $attribute) {
380382
if (!isset($categoryData[$attributeCode])) {

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,26 @@
55
*/
66
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Category\Image;
77

8-
use \Magento\Catalog\Controller\Adminhtml\Category\Image\Upload as Model;
9-
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;
8+
use Magento\Catalog\Controller\Adminhtml\Category\Image\Upload as Model;
9+
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;
1414

1515
/**
1616
* Class UploadTest
17-
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1817
*/
1918
class UploadTest extends \PHPUnit_Framework_TestCase
2019
{
21-
protected $objectManager;
20+
private $objectManager;
2221

2322
protected function setUp()
2423
{
2524
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
2625
}
2726

28-
public function uploadedImageNameProvider()
27+
public function executeDataProvider()
2928
{
3029
return [
3130
['image1', 'image1'],
@@ -35,12 +34,12 @@ public function uploadedImageNameProvider()
3534
}
3635

3736
/**
38-
* @param $name
39-
* @param $savedName
37+
* @param string $name
38+
* @param string $savedName
4039
*
41-
* @dataProvider uploadedImageNameProvider
40+
* @dataProvider executeDataProvider
4241
*/
43-
public function testExecuteShouldSaveUploadedImageWithSpecifiedNameToTmpFolder($name, $savedName)
42+
public function testExecute($name, $savedName)
4443
{
4544
$request = $this->objectManager->getObject(Request::class);
4645

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,67 +16,67 @@ class SaveTest extends \PHPUnit_Framework_TestCase
1616
/**
1717
* @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
1818
*/
19-
protected $resultRedirectFactoryMock;
19+
private $resultRedirectFactoryMock;
2020

2121
/**
2222
* @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
2323
*/
24-
protected $resultRawFactoryMock;
24+
private $resultRawFactoryMock;
2525

2626
/**
2727
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
2828
*/
29-
protected $resultJsonFactoryMock;
29+
private $resultJsonFactoryMock;
3030

3131
/**
3232
* @var \Magento\Framework\View\LayoutFactory|\PHPUnit_Framework_MockObject_MockObject
3333
*/
34-
protected $layoutFactoryMock;
34+
private $layoutFactoryMock;
3535

3636
/**
3737
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
3838
*/
39-
protected $contextMock;
39+
private $contextMock;
4040

4141
/**
4242
* @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
4343
*/
44-
protected $titleMock;
44+
private $titleMock;
4545

4646
/**
4747
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
4848
*/
49-
protected $requestMock;
49+
private $requestMock;
5050

5151
/**
5252
* @var \Magento\Framework\ObjectManagerInterface|\PHPUnit_Framework_MockObject_MockObject
5353
*/
54-
protected $objectManagerMock;
54+
private $objectManagerMock;
5555

5656
/**
5757
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
5858
*/
59-
protected $eventManagerMock;
59+
private $eventManagerMock;
6060

6161
/**
6262
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
6363
*/
64-
protected $responseMock;
64+
private $responseMock;
6565

6666
/**
6767
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
6868
*/
69-
protected $messageManagerMock;
69+
private $messageManagerMock;
7070

7171
/**
7272
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
7373
*/
74-
protected $objectManager;
74+
private $objectManager;
7575

7676
/**
7777
* @var \Magento\Catalog\Controller\Adminhtml\Category\Save
7878
*/
79-
protected $save;
79+
private $save;
8080

8181
/**
8282
* Set up
@@ -581,7 +581,10 @@ public function dataProviderExecute()
581581
];
582582
}
583583

584-
public function attributeValueDataProvider()
584+
/**
585+
* @return array
586+
*/
587+
public function imagePreprocessingDataProvider()
585588
{
586589
return [
587590
[['attribute1' => null, 'attribute2' => 123]],
@@ -590,11 +593,11 @@ public function attributeValueDataProvider()
590593
}
591594

592595
/**
593-
* @dataProvider attributeValueDataProvider
596+
* @dataProvider imagePreprocessingDataProvider
594597
*
595-
* @param $data
598+
* @param array $data
596599
*/
597-
public function testImagePreprocessingShouldSetAttributesWithImageBackendToFalse($data)
600+
public function testImagePreprocessingWithoutValue($data)
598601
{
599602
$eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityType'], [], '', false);
600603

@@ -630,7 +633,7 @@ public function testImagePreprocessingShouldSetAttributesWithImageBackendToFalse
630633
], $result);
631634
}
632635

633-
public function testImagePreprocessingShouldNotSetValueToFalseWhenValueSet()
636+
public function testImagePreprocessingWithValue()
634637
{
635638
$eavConfig = $this->getMock(\Magento\Eav\Model\Config::class, ['getEntityType'], [], '', false);
636639

0 commit comments

Comments
 (0)