Skip to content

Commit f2a82b1

Browse files
MAGETWO-92693: Some improvements on product create|edit page in admin area
1 parent b08a1e8 commit f2a82b1

File tree

2 files changed

+140
-72
lines changed

2 files changed

+140
-72
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Builder
5555
* @param Registry $registry
5656
* @param WysiwygModel\Config $wysiwygConfig
5757
* @param StoreFactory|null $storeFactory
58+
* @param ProductRepositoryInterface|null $productRepository
5859
*/
5960
public function __construct(
6061
ProductFactory $productFactory,
@@ -84,7 +85,7 @@ public function __construct(
8485
public function build(RequestInterface $request)
8586
{
8687
$productId = (int) $request->getParam('id');
87-
$storeId = (int) $request->getParam('store', 0);
88+
$storeId = $request->getParam('store', 0);
8889
$attributeSetId = (int) $request->getParam('set');
8990
$typeId = $request->getParam('type');
9091

app/code/Magento/Catalog/Test/Unit/Controller/Adminhtml/Product/BuilderTest.php

Lines changed: 138 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
use Magento\Framework\Registry;
1515
use Magento\Cms\Model\Wysiwyg\Config as WysiwygConfig;
1616
use Magento\Framework\App\Request\Http;
17+
use Magento\Catalog\Api\ProductRepositoryInterface;
18+
use Magento\Framework\Exception\NoSuchEntityException;
19+
use Magento\Catalog\Model\Product\Type as ProductTypes;
1720

1821
/**
1922
* Class BuilderTest
@@ -67,6 +70,11 @@ class BuilderTest extends \PHPUnit\Framework\TestCase
6770
*/
6871
protected $storeFactoryMock;
6972

73+
/**
74+
* @var ProductRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
75+
*/
76+
protected $productRepositoryMock;
77+
7078
/**
7179
* @var StoreInterface|\PHPUnit_Framework_MockObject_MockObject
7280
*/
@@ -90,9 +98,10 @@ protected function setUp()
9098
->setMethods(['load'])
9199
->getMockForAbstractClass();
92100

93-
$this->storeFactoryMock->expects($this->any())
94-
->method('create')
95-
->willReturn($this->storeMock);
101+
$this->productRepositoryMock = $this->getMockBuilder(ProductRepositoryInterface::class)
102+
->setMethods(['getById'])
103+
->disableOriginalConstructor()
104+
->getMockForAbstractClass();
96105

97106
$this->builder = $this->objectManager->getObject(
98107
Builder::class,
@@ -102,140 +111,198 @@ protected function setUp()
102111
'registry' => $this->registryMock,
103112
'wysiwygConfig' => $this->wysiwygConfigMock,
104113
'storeFactory' => $this->storeFactoryMock,
114+
'productRepository' => $this->productRepositoryMock
105115
]
106116
);
107117
}
108118

109119
public function testBuildWhenProductExistAndPossibleToLoadProduct()
110120
{
121+
$productId = 2;
122+
$productType = 'type_id';
123+
$productStore = 'store';
124+
$productSet = 3;
125+
111126
$valueMap = [
112-
['id', null, 2],
113-
['store', 0, 'some_store'],
114-
['type', null, 'type_id'],
115-
['set', null, 3],
116-
['store', null, 'store'],
127+
['id', null, $productId],
128+
['type', null, $productType],
129+
['set', null, $productSet],
130+
['store', 0, $productStore],
117131
];
132+
118133
$this->requestMock->expects($this->any())
119134
->method('getParam')
120135
->willReturnMap($valueMap);
121-
$this->productFactoryMock->expects($this->once())
136+
137+
$this->productRepositoryMock->expects($this->any())
138+
->method('getById')
139+
->with($productId, true, $productStore)
140+
->willReturn($this->productMock);
141+
142+
$this->storeFactoryMock->expects($this->any())
122143
->method('create')
123-
->will($this->returnValue($this->productMock));
124-
$this->productMock->expects($this->once())
125-
->method('setStoreId')
126-
->with('some_store')
127-
->willReturnSelf();
128-
$this->productMock->expects($this->never())
129-
->method('setTypeId');
130-
$this->productMock->expects($this->once())
144+
->willReturn($this->storeMock);
145+
146+
$this->storeMock->expects($this->any())
131147
->method('load')
132-
->with(2)
133-
->will($this->returnSelf());
134-
$this->productMock->expects($this->once())
135-
->method('setAttributeSetId')
136-
->with(3)
137-
->will($this->returnSelf());
148+
->with($productStore)
149+
->willReturnSelf();
150+
138151
$registryValueMap = [
139152
['product', $this->productMock, $this->registryMock],
140153
['current_product', $this->productMock, $this->registryMock],
154+
['current_store', $this->registryMock, $this->storeMock],
141155
];
156+
142157
$this->registryMock->expects($this->any())
143158
->method('register')
144159
->willReturn($registryValueMap);
160+
145161
$this->wysiwygConfigMock->expects($this->once())
146162
->method('setStoreId')
147-
->with('store');
163+
->with($productStore);
164+
148165
$this->assertEquals($this->productMock, $this->builder->build($this->requestMock));
149166
}
150167

151168
public function testBuildWhenImpossibleLoadProduct()
152169
{
170+
$productId = 2;
171+
$productType = 'type_id';
172+
$productStore = 'store';
173+
$productSet = 3;
174+
153175
$valueMap = [
154-
['id', null, 15],
155-
['store', 0, 'some_store'],
156-
['type', null, 'type_id'],
157-
['set', null, 3],
158-
['store', null, 'store'],
176+
['id', null, $productId],
177+
['type', null, $productType],
178+
['set', null, $productSet],
179+
['store', 0, $productStore],
159180
];
181+
160182
$this->requestMock->expects($this->any())
161183
->method('getParam')
162-
->will($this->returnValueMap($valueMap));
184+
->willReturnMap($valueMap);
185+
186+
$this->productRepositoryMock->expects($this->any())
187+
->method('getById')
188+
->with($productId, true, $productStore)
189+
->willThrowException(new NoSuchEntityException());
190+
163191
$this->productFactoryMock->expects($this->once())
164192
->method('create')
165-
->willReturn($this->productMock);
166-
$this->productMock->expects($this->once())
167-
->method('setStoreId')
168-
->with('some_store')
169-
->willReturnSelf();
170-
$this->productMock->expects($this->once())
193+
->will($this->returnValue($this->productMock));
194+
195+
$this->productMock->expects($this->any())
196+
->method('setData')
197+
->with('_edit_mode', true);
198+
199+
$this->productMock->expects($this->any())
171200
->method('setTypeId')
172-
->with(\Magento\Catalog\Model\Product\Type::DEFAULT_TYPE)
173-
->willReturnSelf();
174-
$this->productMock->expects($this->once())
175-
->method('load')
176-
->with(15)
177-
->willThrowException(new \Exception());
201+
->with(ProductTypes::DEFAULT_TYPE);
202+
203+
$this->productMock->expects($this->any())
204+
->method('setStoreId')
205+
->with($productStore);
206+
207+
$this->productMock->expects($this->any())
208+
->method('setAttributeSetId')
209+
->with($productSet);
210+
178211
$this->loggerMock->expects($this->once())
179212
->method('critical');
180-
$this->productMock->expects($this->once())
181-
->method('setAttributeSetId')
182-
->with(3)
183-
->will($this->returnSelf());
213+
214+
$this->storeFactoryMock->expects($this->any())
215+
->method('create')
216+
->willReturn($this->storeMock);
217+
218+
$this->storeMock->expects($this->any())
219+
->method('load')
220+
->with($productStore)
221+
->willReturnSelf();
222+
184223
$registryValueMap = [
185224
['product', $this->productMock, $this->registryMock],
186225
['current_product', $this->productMock, $this->registryMock],
226+
['current_store', $this->registryMock, $this->storeMock],
187227
];
228+
188229
$this->registryMock->expects($this->any())
189230
->method('register')
190-
->will($this->returnValueMap($registryValueMap));
231+
->willReturn($registryValueMap);
232+
191233
$this->wysiwygConfigMock->expects($this->once())
192234
->method('setStoreId')
193-
->with('store');
235+
->with($productStore);
236+
194237
$this->assertEquals($this->productMock, $this->builder->build($this->requestMock));
195238
}
196239

197240
public function testBuildWhenProductNotExist()
198241
{
242+
$productId = 0;
243+
$productType = 'type_id';
244+
$productStore = 'store';
245+
$productSet = 3;
246+
199247
$valueMap = [
200-
['id', null, null],
201-
['store', 0, 'some_store'],
202-
['type', null, 'type_id'],
203-
['set', null, 3],
204-
['store', null, 'store'],
248+
['id', null, $productId],
249+
['type', null, $productType],
250+
['set', null, $productSet],
251+
['store', 0, $productStore],
205252
];
253+
206254
$this->requestMock->expects($this->any())
207255
->method('getParam')
208-
->will($this->returnValueMap($valueMap));
256+
->willReturnMap($valueMap);
257+
258+
$this->productRepositoryMock->expects($this->any())
259+
->method('getById')
260+
->with($productId, true, $productStore)
261+
->willThrowException(new NoSuchEntityException());
262+
209263
$this->productFactoryMock->expects($this->once())
210264
->method('create')
211-
->willReturn($this->productMock);
212-
$this->productMock->expects($this->once())
213-
->method('setStoreId')
214-
->with('some_store')
215-
->willReturnSelf();
216-
$productValueMap = [
217-
['type_id', $this->productMock],
218-
[\Magento\Catalog\Model\Product\Type::DEFAULT_TYPE, $this->productMock],
219-
];
265+
->will($this->returnValue($this->productMock));
266+
267+
$this->productMock->expects($this->any())
268+
->method('setData')
269+
->with('_edit_mode', true);
270+
220271
$this->productMock->expects($this->any())
221272
->method('setTypeId')
222-
->willReturnMap($productValueMap);
223-
$this->productMock->expects($this->never())
224-
->method('load');
225-
$this->productMock->expects($this->once())
273+
->with($productType);
274+
275+
$this->productMock->expects($this->any())
276+
->method('setStoreId')
277+
->with($productStore);
278+
279+
$this->productMock->expects($this->any())
226280
->method('setAttributeSetId')
227-
->with(3)
228-
->will($this->returnSelf());
281+
->with($productSet);
282+
283+
$this->storeFactoryMock->expects($this->any())
284+
->method('create')
285+
->willReturn($this->storeMock);
286+
287+
$this->storeMock->expects($this->any())
288+
->method('load')
289+
->with($productStore)
290+
->willReturnSelf();
291+
229292
$registryValueMap = [
230293
['product', $this->productMock, $this->registryMock],
231294
['current_product', $this->productMock, $this->registryMock],
295+
['current_store', $this->registryMock, $this->storeMock],
232296
];
297+
233298
$this->registryMock->expects($this->any())
234299
->method('register')
235-
->will($this->returnValueMap($registryValueMap));
300+
->willReturn($registryValueMap);
301+
236302
$this->wysiwygConfigMock->expects($this->once())
237303
->method('setStoreId')
238-
->with('store');
304+
->with($productStore);
305+
239306
$this->assertEquals($this->productMock, $this->builder->build($this->requestMock));
240307
}
241308
}

0 commit comments

Comments
 (0)