Skip to content

Commit cfa9a39

Browse files
committed
MAGETWO-57989: Unable to create custom image attribute in category
1 parent 90e0bd1 commit cfa9a39

File tree

4 files changed

+14
-103
lines changed

4 files changed

+14
-103
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
1515
{
16-
const ADDITIONAL_DATA_PREFIX = '_additional_data_';
17-
1816
/**
1917
* @var \Magento\MediaStorage\Model\File\UploaderFactory
2018
*
@@ -48,6 +46,11 @@ class Image extends \Magento\Eav\Model\Entity\Attribute\Backend\AbstractBackend
4846
*/
4947
private $imageUploader;
5048

49+
/**
50+
* @var string
51+
*/
52+
private $additionalData = '_additional_data_';
53+
5154
/**
5255
* @param \Psr\Log\LoggerInterface $logger
5356
* @param \Magento\Framework\Filesystem $filesystem
@@ -92,7 +95,7 @@ public function beforeSave($object)
9295
$value = $object->getData($attributeName);
9396

9497
if ($imageName = $this->getUploadedImageName($value)) {
95-
$object->setData(self::ADDITIONAL_DATA_PREFIX . $attributeName, $value);
98+
$object->setData($this->additionalData . $attributeName, $value);
9699
$object->setData($attributeName, $imageName);
97100
} else if (!is_string($value)) {
98101
$object->setData($attributeName, '');
@@ -124,7 +127,7 @@ private function getImageUploader()
124127
*/
125128
public function afterSave($object)
126129
{
127-
$value = $object->getData(self::ADDITIONAL_DATA_PREFIX . $this->getAttribute()->getName());
130+
$value = $object->getData($this->additionalData . $this->getAttribute()->getName());
128131

129132
if ($imageName = $this->getUploadedImageName($value)) {
130133
try {

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\Catalog\Model\ImageUploader;
1111
use Magento\Framework\Controller\ResultFactory;
1212
use Magento\Framework\DataObject;
13-
use Magento\Backend\App\Action\Context;
1413

1514
/**
1615
* Class UploadTest
@@ -52,10 +51,8 @@ public function testExecute($name, $savedName)
5251
->will($this->returnValue(new DataObject()));
5352

5453
$model = $this->objectManager->getObject(Model::class, [
55-
'context' => $this->objectManager->getObject(Context::class, [
56-
'request' => $request,
57-
'resultFactory' => $resultFactory
58-
]),
54+
'request' => $request,
55+
'resultFactory' => $resultFactory,
5956
'imageUploader' => $uploader
6057
]);
6158

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

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,6 @@ class SaveTest extends \PHPUnit_Framework_TestCase
1818
*/
1919
private $resultRedirectFactoryMock;
2020

21-
/**
22-
* @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
23-
*/
24-
private $resultRawFactoryMock;
25-
2621
/**
2722
* @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject
2823
*/
@@ -33,16 +28,6 @@ class SaveTest extends \PHPUnit_Framework_TestCase
3328
*/
3429
private $layoutFactoryMock;
3530

36-
/**
37-
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
38-
*/
39-
private $contextMock;
40-
41-
/**
42-
* @var \Magento\Framework\View\Page\Title|\PHPUnit_Framework_MockObject_MockObject
43-
*/
44-
private $titleMock;
45-
4631
/**
4732
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
4833
*/
@@ -58,11 +43,6 @@ class SaveTest extends \PHPUnit_Framework_TestCase
5843
*/
5944
private $eventManagerMock;
6045

61-
/**
62-
* @var \Magento\Framework\App\ResponseInterface|\PHPUnit_Framework_MockObject_MockObject
63-
*/
64-
private $responseMock;
65-
6646
/**
6747
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
6848
*/
@@ -87,36 +67,13 @@ class SaveTest extends \PHPUnit_Framework_TestCase
8767
protected function setUp()
8868
{
8969
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
90-
91-
$this->contextMock = $this->getMock(
92-
\Magento\Backend\App\Action\Context::class,
93-
[
94-
'getTitle',
95-
'getRequest',
96-
'getObjectManager',
97-
'getEventManager',
98-
'getResponse',
99-
'getMessageManager',
100-
'getResultRedirectFactory'
101-
],
102-
[],
103-
'',
104-
false
105-
);
10670
$this->resultRedirectFactoryMock = $this->getMock(
10771
\Magento\Backend\Model\View\Result\RedirectFactory::class,
10872
['create'],
10973
[],
11074
'',
11175
false
11276
);
113-
$this->resultRawFactoryMock = $this->getMock(
114-
\Magento\Framework\Controller\Result\RawFactory::class,
115-
[],
116-
[],
117-
'',
118-
false
119-
);
12077
$this->resultJsonFactoryMock = $this->getMock(
12178
\Magento\Framework\Controller\Result\JsonFactory::class,
12279
['create'],
@@ -152,12 +109,6 @@ protected function setUp()
152109
true,
153110
['dispatch']
154111
);
155-
$this->responseMock = $this->getMockForAbstractClass(
156-
\Magento\Framework\App\ResponseInterface::class,
157-
[],
158-
'',
159-
false
160-
);
161112
$this->messageManagerMock = $this->getMockForAbstractClass(
162113
\Magento\Framework\Message\ManagerInterface::class,
163114
[],
@@ -168,23 +119,15 @@ protected function setUp()
168119
['addSuccess', 'getMessages']
169120
);
170121

171-
$this->contextMock->expects($this->any())->method('getTitle')->willReturn($this->titleMock);
172-
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
173-
$this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
174-
$this->contextMock->expects($this->any())->method('getEventManager')->willReturn($this->eventManagerMock);
175-
$this->contextMock->expects($this->any())->method('getResponse')->willReturn($this->responseMock);
176-
$this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
177-
$this->contextMock->expects($this->any())
178-
->method('getResultRedirectFactory')
179-
->willReturn($this->resultRedirectFactoryMock);
180-
181122
$this->save = $this->objectManager->getObject(
182123
\Magento\Catalog\Controller\Adminhtml\Category\Save::class,
183124
[
184-
'context' => $this->contextMock,
185-
'resultRawFactory' => $this->resultRawFactoryMock,
125+
'request' => $this->requestMock,
126+
'eventManager' => $this->eventManagerMock,
127+
'messageManager' => $this->messageManagerMock,
186128
'resultJsonFactory' => $this->resultJsonFactoryMock,
187-
'layoutFactory' => $this->layoutFactoryMock
129+
'layoutFactory' => $this->layoutFactoryMock,
130+
'resultRedirectFactory' => $this->resultRedirectFactoryMock
188131
]
189132
);
190133
}

app/code/Magento/Catalog/Test/Unit/Model/CategoryTest.php

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,6 @@ class CategoryTest extends \PHPUnit_Framework_TestCase
2222
*/
2323
private $category;
2424

25-
/**
26-
* @var \Magento\Framework\Model\Context|\PHPUnit_Framework_MockObject_MockObject
27-
*/
28-
private $context;
29-
30-
/**
31-
* @var \Magento\Framework\Event\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
32-
*/
33-
private $eventManager;
34-
35-
/**
36-
* @var \Magento\Framework\App\CacheInterface|\PHPUnit_Framework_MockObject_MockObject
37-
*/
38-
private $cacheManager;
39-
4025
/**
4126
* @var \Magento\Framework\Registry|\PHPUnit_Framework_MockObject_MockObject
4227
*/
@@ -140,22 +125,6 @@ class CategoryTest extends \PHPUnit_Framework_TestCase
140125
protected function setUp()
141126
{
142127
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
143-
144-
$this->context = $this->getMock(
145-
\Magento\Framework\Model\Context::class,
146-
['getEventDispatcher', 'getCacheManager'],
147-
[],
148-
'',
149-
false
150-
);
151-
152-
$this->eventManager = $this->getMock(\Magento\Framework\Event\ManagerInterface::class);
153-
$this->context->expects($this->any())->method('getEventDispatcher')
154-
->will($this->returnValue($this->eventManager));
155-
$this->cacheManager = $this->getMock(\Magento\Framework\App\CacheInterface::class);
156-
$this->context->expects($this->any())->method('getCacheManager')
157-
->will($this->returnValue($this->cacheManager));
158-
159128
$this->registry = $this->getMock(\Magento\Framework\Registry::class);
160129
$this->storeManager = $this->getMock(\Magento\Store\Model\StoreManagerInterface::class);
161130
$this->categoryTreeResource = $this->getMock(
@@ -366,7 +335,6 @@ protected function getCategoryModel()
366335
return $this->objectManager->getObject(
367336
\Magento\Catalog\Model\Category::class,
368337
[
369-
'context' => $this->context,
370338
'registry' => $this->registry,
371339
'storeManager' => $this->storeManager,
372340
'categoryTreeResource' => $this->categoryTreeResource,

0 commit comments

Comments
 (0)