Skip to content

Commit 74553ec

Browse files
committed
MAGETWO-59325: Unable to add custom attribute to product
1 parent 7003dff commit 74553ec

File tree

3 files changed

+268
-2
lines changed

3 files changed

+268
-2
lines changed

app/code/Magento/Catalog/Controller/Adminhtml/Product/AddAttributeToTemplate.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ public function execute()
115115
$attributeGroupSearchCriteria = $this->getSearchCriteriaBuilder()
116116
->addFilter('attribute_set_id', $attributeSet->getAttributeSetId())
117117
->addFilter('attribute_group_code', $groupCode)
118-
->addSortOrder($this->getSortOrderBuilder()->setAscendingDirection()->create())
119118
->setPageSize(1)
120119
->create();
121120

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Catalog\Test\Unit\Controller\Adminhtml\Product;
7+
8+
use Magento\Catalog\Controller\Adminhtml\Product\AddAttributeToTemplate;
9+
use Magento\Framework\Exception\LocalizedException;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Backend\App\Action\Context;
12+
use Magento\Catalog\Controller\Adminhtml\Product\Builder as ProductBuilder;
13+
use Magento\Framework\Controller\Result\JsonFactory;
14+
use Magento\Framework\App\RequestInterface;
15+
use Magento\Catalog\Api\AttributeSetRepositoryInterface;
16+
use Magento\Eav\Api\Data\AttributeSetInterface;
17+
use Magento\Framework\Api\SearchCriteriaBuilder;
18+
use Magento\Framework\Api\SearchCriteria;
19+
use Magento\Eav\Api\AttributeGroupRepositoryInterface;
20+
use Magento\Eav\Api\Data\AttributeGroupSearchResultsInterface;
21+
use Magento\Eav\Api\Data\AttributeGroupInterfaceFactory;
22+
use Magento\Eav\Api\Data\AttributeGroupInterface;
23+
use Magento\Framework\Controller\Result\Json;
24+
25+
class AddAttributeToTemplateTest extends \PHPUnit_Framework_TestCase
26+
{
27+
/**
28+
* @var ObjectManager
29+
*/
30+
private $objectManager;
31+
32+
/**
33+
* @var AddAttributeToTemplate
34+
*/
35+
private $controller;
36+
37+
/**
38+
* @var Context|\PHPUnit_Framework_MockObject_MockObject
39+
*/
40+
private $contextMock;
41+
42+
/**
43+
* @var ProductBuilder|\PHPUnit_Framework_MockObject_MockObject
44+
*/
45+
private $productBuilderMock;
46+
47+
/**
48+
* @var JsonFactory|\PHPUnit_Framework_MockObject_MockObject
49+
*/
50+
private $resultJsonFactoryMock;
51+
52+
/**
53+
* @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject
54+
*/
55+
private $requestMock;
56+
57+
/**
58+
* @var AttributeSetRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
59+
*/
60+
private $attributeSetRepositoryMock;
61+
62+
/**
63+
* @var AttributeSetInterface|\PHPUnit_Framework_MockObject_MockObject
64+
*/
65+
private $attributeSetInterfaceMock;
66+
67+
/**
68+
* @var SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
69+
*/
70+
private $searchCriteriaBuilderMock;
71+
72+
/**
73+
* @var SearchCriteria|\PHPUnit_Framework_MockObject_MockObject
74+
*/
75+
private $searchCriteriaMock;
76+
77+
/**
78+
* @var AttributeGroupRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
79+
*/
80+
private $attributeGroupRepositoryMock;
81+
82+
/**
83+
* @var AttributeGroupSearchResultsInterface|\PHPUnit_Framework_MockObject_MockObject
84+
*/
85+
private $attributeGroupSearchResultsMock;
86+
87+
/**
88+
* @var AttributeGroupInterfaceFactory|\PHPUnit_Framework_MockObject_MockObject
89+
*/
90+
private $attributeGroupInterfaceFactoryMock;
91+
92+
/**
93+
* @var AttributeGroupInterface|\PHPUnit_Framework_MockObject_MockObject
94+
*/
95+
private $attributeGroupInterfaceMock;
96+
97+
/**
98+
* @var Json|\PHPUnit_Framework_MockObject_MockObject
99+
*/
100+
private $jsonMock;
101+
102+
protected function setUp()
103+
{
104+
$this->objectManager = new ObjectManager($this);
105+
$this->contextMock = $this->getMockBuilder(Context::class)
106+
->disableOriginalConstructor()
107+
->getMock();
108+
$this->productBuilderMock = $this->getMockBuilder(ProductBuilder::class)
109+
->disableOriginalConstructor()
110+
->getMock();
111+
$this->resultJsonFactoryMock = $this->getMockBuilder(JsonFactory::class)
112+
->disableOriginalConstructor()
113+
->setMethods(['create'])
114+
->getMock();
115+
$this->requestMock = $this->getMockBuilder(\Magento\Framework\App\RequestInterface::class)
116+
->setMethods(['getParam', 'setParam'])
117+
->getMockForAbstractClass();
118+
$this->contextMock->expects($this->once())
119+
->method('getRequest')
120+
->willReturn($this->requestMock);
121+
$this->attributeSetRepositoryMock = $this->getMockBuilder(AttributeSetRepositoryInterface::class)
122+
->setMethods(['get'])
123+
->getMockForAbstractClass();
124+
$this->attributeSetInterfaceMock = $this->getMockBuilder(AttributeSetInterface::class)
125+
->getMockForAbstractClass();
126+
$this->searchCriteriaBuilderMock = $this->getMockBuilder(SearchCriteriaBuilder::class)
127+
->disableOriginalConstructor()
128+
->setMethods(['addFilter', 'create', 'setPageSize'])
129+
->getMockForAbstractClass();
130+
$this->searchCriteriaMock = $this->getMockBuilder(SearchCriteria::class)
131+
->disableOriginalConstructor()
132+
->getMock();
133+
$this->attributeGroupRepositoryMock = $this->getMockBuilder(AttributeGroupRepositoryInterface::class)
134+
->setMethods(['getList'])
135+
->getMockForAbstractClass();
136+
$this->attributeGroupSearchResultsMock = $this->getMockBuilder(AttributeGroupSearchResultsInterface::class)
137+
->setMethods(['getItems'])
138+
->getMockForAbstractClass();
139+
$this->attributeGroupInterfaceFactoryMock = $this->getMockBuilder(AttributeGroupInterfaceFactory::class)
140+
->setMethods(['create'])
141+
->disableOriginalConstructor()
142+
->getMock();
143+
$this->attributeGroupInterfaceMock = $this->getMockBuilder(AttributeGroupInterface::class)
144+
->setMethods(['getExtensionAttributes'])
145+
->getMockForAbstractClass();
146+
$this->jsonMock = $this->getMockBuilder(Json::class)
147+
->disableOriginalConstructor()
148+
->getMock();
149+
150+
$this->controller = $this->objectManager->getObject(
151+
AddAttributeToTemplate::class,
152+
[
153+
'context' => $this->contextMock,
154+
'productBuilder' => $this->productBuilderMock,
155+
'resultJsonFactory' => $this->resultJsonFactoryMock,
156+
]
157+
);
158+
159+
$this->objectManager->setBackwardCompatibleProperty(
160+
$this->controller,
161+
'attributeSetRepository',
162+
$this->attributeSetRepositoryMock
163+
);
164+
$this->objectManager->setBackwardCompatibleProperty(
165+
$this->controller,
166+
'searchCriteriaBuilder',
167+
$this->searchCriteriaBuilderMock
168+
);
169+
$this->objectManager->setBackwardCompatibleProperty(
170+
$this->controller,
171+
'attributeGroupRepository',
172+
$this->attributeGroupRepositoryMock
173+
);
174+
$this->objectManager->setBackwardCompatibleProperty(
175+
$this->controller,
176+
'attributeGroupFactory',
177+
$this->attributeGroupInterfaceFactoryMock
178+
);
179+
}
180+
181+
public function testExecuteWithoutAttributeGroupItems()
182+
{
183+
$groupCode = 'attributes';
184+
$groupName = 'Attributes';
185+
$groupSortOrder = '15';
186+
$templateId = '4';
187+
$attributeIds = [
188+
'selected' => ["178"],
189+
'total' => '1'
190+
];
191+
192+
$this->requestMock
193+
->expects($this->any())
194+
->method('getParam')
195+
->willReturnMap(
196+
[
197+
['groupCode', null, $groupCode],
198+
['groupName', null, $groupName],
199+
['groupSortOrder', null, $groupSortOrder],
200+
['templateId', null, $templateId],
201+
['attributeIds', [], $attributeIds]
202+
]
203+
);
204+
205+
$this->attributeSetRepositoryMock->expects($this->once())
206+
->method('get')
207+
->willReturn($this->attributeSetInterfaceMock);
208+
209+
$this->searchCriteriaBuilderMock->expects($this->any())
210+
->method('addFilter')
211+
->willReturnSelf();
212+
$this->searchCriteriaBuilderMock->expects($this->any())
213+
->method('create')
214+
->willReturn($this->searchCriteriaMock);
215+
$this->searchCriteriaBuilderMock->expects($this->once())
216+
->method('setPageSize')
217+
->willReturnSelf();
218+
$this->searchCriteriaBuilderMock->expects($this->never())
219+
->method('addSortOrder')
220+
->willReturnSelf();
221+
222+
$this->attributeGroupRepositoryMock->expects($this->once())
223+
->method('getList')
224+
->willReturn($this->attributeGroupSearchResultsMock);
225+
$this->attributeGroupSearchResultsMock->expects($this->once())
226+
->method('getItems')
227+
->willReturn(null);
228+
229+
$this->attributeGroupInterfaceFactoryMock->expects($this->once())
230+
->method('create')
231+
->willReturn($this->attributeGroupInterfaceMock);
232+
$this->attributeGroupInterfaceMock->expects($this->once())
233+
->method('getExtensionAttributes')
234+
->willThrowException(new LocalizedException(__('Could not get extension attributes')));
235+
236+
$this->resultJsonFactoryMock->expects($this->once())
237+
->method('create')
238+
->willReturn($this->jsonMock);
239+
$this->jsonMock->expects($this->once())->method('setJsonData')
240+
->willReturnSelf();
241+
242+
$this->controller->execute();
243+
}
244+
}

app/code/Magento/Eav/etc/di.xml

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,32 @@
9393
</virtualType>
9494
<type name="Magento\Eav\Model\AttributeRepository">
9595
<arguments>
96-
<argument name="collectionProcessor" xsi:type="object">Magento\Framework\Api\SearchCriteria\CollectionProcessor</argument>
96+
<argument name="collectionProcessor" xsi:type="object">Magento\Eav\Model\Api\SearchCriteria\AttributeCollectionProcessor</argument>
9797
</arguments>
9898
</type>
99+
<virtualType name="Magento\Eav\Model\Api\SearchCriteria\AttributeCollectionProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor">
100+
<arguments>
101+
<argument name="processors" xsi:type="array">
102+
<item name="filters" xsi:type="object">Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\AttributeFilterProcessor</item>
103+
<item name="sorting" xsi:type="object">Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\AttributeSortingProcessor</item>
104+
<item name="pagination" xsi:type="object">Magento\Framework\Api\SearchCriteria\CollectionProcessor\PaginationProcessor</item>
105+
</argument>
106+
</arguments>
107+
</virtualType>
108+
<virtualType name="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\AttributeFilterProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
109+
<arguments>
110+
<argument name="fieldMapping" xsi:type="array">
111+
<item name="attribute_id" xsi:type="string">main_table.attribute_id</item>
112+
</argument>
113+
</arguments>
114+
</virtualType>
115+
<virtualType name="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\AttributeSortingProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\SortingProcessor">
116+
<arguments>
117+
<argument name="fieldMapping" xsi:type="array">
118+
<item name="attribute_id" xsi:type="string">main_table.attribute_id</item>
119+
</argument>
120+
</arguments>
121+
</virtualType>
99122
<virtualType name="Magento\Eav\Model\Api\SearchCriteria\CollectionProcessor\AttributeSetFilterProcessor" type="Magento\Framework\Api\SearchCriteria\CollectionProcessor\FilterProcessor">
100123
<arguments>
101124
<argument name="customFilters" xsi:type="array">

0 commit comments

Comments
 (0)