Skip to content

Commit c2cab9a

Browse files
author
Andrii Kasian
committed
Merge remote-tracking branch 'origin/MAGETWO-36990' into S67
2 parents 8e37f84 + d7abf52 commit c2cab9a

File tree

2 files changed

+173
-65
lines changed

2 files changed

+173
-65
lines changed

app/code/Magento/Bundle/Test/Unit/Model/Product/TypeTest.php

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
*/
1717
class TypeTest extends \PHPUnit_Framework_TestCase
1818
{
19+
/**
20+
* @var \Magento\Bundle\Model\Resource\BundleFactory|\PHPUnit_Framework_MockObject_MockObject
21+
*/
22+
private $bundleFactory;
23+
/**
24+
* @var \Magento\Bundle\Model\SelectionFactory|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
private $bundleModelSelection;
1927
/**
2028
* @var \Magento\Bundle\Model\Product\Type
2129
*/
@@ -96,19 +104,18 @@ protected function setUp()
96104
->setMethods(['convert'])
97105
->disableOriginalConstructor()
98106
->getMockForAbstractClass();
99-
$bundleModelSelection = $this->getMockBuilder('\Magento\Bundle\Model\SelectionFactory')
107+
$this->bundleModelSelection = $this->getMockBuilder('Magento\Bundle\Model\SelectionFactory')
100108
->disableOriginalConstructor()
101109
->getMock();
102-
$bundleFactory = $this->getMockBuilder('\Magento\Bundle\Model\Resource\BundleFactory')
110+
$this->bundleFactory = $this->getMockBuilder('\Magento\Bundle\Model\Resource\BundleFactory')
103111
->disableOriginalConstructor()
104112
->getMock();
105-
106113
$objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
107114
$this->model = $objectHelper->getObject(
108115
'Magento\Bundle\Model\Product\Type',
109116
[
110-
'bundleModelSelection' => $bundleModelSelection,
111-
'bundleFactory' => $bundleFactory,
117+
'bundleModelSelection' => $this->bundleModelSelection,
118+
'bundleFactory' => $this->bundleFactory,
112119
'bundleCollection' => $this->bundleCollection,
113120
'bundleOption' => $this->bundleOptionFactory,
114121
'catalogData' => $this->catalogData,
@@ -2427,4 +2434,72 @@ protected function parentClass($group, $option, $buyRequest, $product)
24272434
->method('getSkipSaleableCheck')
24282435
->willReturn(false);
24292436
}
2437+
2438+
public function testSave()
2439+
{
2440+
$options = [
2441+
'some_option' => ['option_id' => '', 'delete' => false],
2442+
];
2443+
$selections = [
2444+
'some_option' => [
2445+
123 => ['selection_id' => '', 'delete' => false],
2446+
]
2447+
];
2448+
2449+
$resource = $this->getMockBuilder('Magento\Bundle\Model\Resource\Bundle')
2450+
->disableOriginalConstructor()
2451+
->getMock();
2452+
$this->bundleFactory->expects($this->once())
2453+
->method('create')
2454+
->willReturn($resource);
2455+
2456+
$product = $this->getMockBuilder('Magento\Catalog\Model\Product')
2457+
->setMethods(
2458+
[
2459+
'getStoreId',
2460+
'getOrigData',
2461+
'getData',
2462+
'getBundleOptionsData',
2463+
'getBundleSelectionsData'
2464+
]
2465+
)
2466+
->disableOriginalConstructor()
2467+
->getMock();
2468+
$product->expects($this->once())
2469+
->method('getBundleOptionsData')
2470+
->willReturn($options);
2471+
$product->expects($this->once())
2472+
->method('getBundleSelectionsData')
2473+
->willReturn($selections);
2474+
$option = $this->getMockBuilder('Magento\Bundle\Model\Resource\Option\Collection')
2475+
->setMethods(['setData', 'setParentId', 'setStoreId', 'isDeleted', 'save', 'getOptionId'])
2476+
->disableOriginalConstructor()
2477+
->getMock();
2478+
$option->expects($this->once())->method('setData')->willReturnSelf();
2479+
$option->expects($this->once())->method('setParentId')->willReturnSelf();
2480+
$option->expects($this->once())->method('setStoreId')->willReturnSelf();
2481+
$this->bundleOptionFactory->expects($this->once())->method('create')->will($this->returnValue($option));
2482+
2483+
$selection = $this->getMockBuilder('Magento\Bundle\Model\Selection')
2484+
->setMethods(['setData', 'setOptionId', 'setParentProductId', 'setWebsiteId', 'save'])
2485+
->disableOriginalConstructor()
2486+
->getMock();
2487+
$selection->expects($this->once())->method('setData')->willReturnSelf();
2488+
$selection->expects($this->once())->method('setOptionId')->willReturnSelf();
2489+
$selection->expects($this->once())->method('setParentProductId')->willReturnSelf();
2490+
$selection->expects($this->once())->method('setWebsiteId')->willReturnSelf();
2491+
$selection->expects($this->once())->method('setParentProductId')->willReturnSelf();
2492+
$this->bundleModelSelection->expects($this->once())->method('create')->willReturn($selection);
2493+
$store = $this->getMockBuilder('Magento\Store\Model\Store')
2494+
->setMethods(['getWebsiteId', '__wakeup'])
2495+
->disableOriginalConstructor()
2496+
->getMock();
2497+
$this->storeManager->expects($this->once())
2498+
->method('getStore')
2499+
->will($this->returnValue($store));
2500+
$store->expects($this->once())
2501+
->method('getWebsiteId')
2502+
->will($this->returnValue(10));
2503+
$this->model->save($product);
2504+
}
24302505
}

app/code/Magento/ConfigurableProduct/Model/OptionRepository.php

Lines changed: 93 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -151,69 +151,12 @@ public function deleteById($sku, $id)
151151
*/
152152
public function save($sku, \Magento\ConfigurableProduct\Api\Data\OptionInterface $option)
153153
{
154-
/** @var $configurableAttribute \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute */
154+
/** @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute */
155155
$configurableAttribute = $this->configurableAttributeFactory->create();
156156
if ($option->getId()) {
157-
/** @var \Magento\Catalog\Model\Product $product */
158-
$product = $this->getProduct($sku);
159-
$configurableAttribute->load($option->getId());
160-
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
161-
throw new NoSuchEntityException(
162-
__(
163-
'Option with id "%1" not found',
164-
$option->getId()
165-
)
166-
);
167-
}
168-
$configurableAttribute->addData($option->getData());
169-
$configurableAttribute->setValues(
170-
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
171-
);
172-
173-
try {
174-
$configurableAttribute->save();
175-
} catch (\Exception $e) {
176-
throw new CouldNotSaveException(
177-
__(
178-
'Could not update option with id "%1"',
179-
$option->getId()
180-
)
181-
);
182-
}
157+
$this->saveExistingOption($sku, $option, $configurableAttribute);
183158
} else {
184-
$this->validateNewOptionData($option);
185-
/** @var \Magento\Catalog\Model\Product $product */
186-
$product = $this->productRepository->get($sku);
187-
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
188-
if (!in_array($product->getTypeId(), $allowedTypes)) {
189-
throw new \InvalidArgumentException('Incompatible product type');
190-
}
191-
192-
$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
193-
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
194-
if ($configurableAttribute->getId()) {
195-
throw new CouldNotSaveException(__('Product already has this option'));
196-
}
197-
198-
$configurableAttributesData = [
199-
'attribute_id' => $option->getAttributeId(),
200-
'position' => $option->getPosition(),
201-
'use_default' => $option->getIsUseDefault(),
202-
'label' => $option->getLabel(),
203-
'values' => $option->getValues()
204-
];
205-
206-
try {
207-
$product->setTypeId(ConfigurableType::TYPE_CODE);
208-
$product->setConfigurableAttributesData([$configurableAttributesData]);
209-
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
210-
$product->save();
211-
} catch (\Exception $e) {
212-
throw new CouldNotSaveException(__('An error occurred while saving option'));
213-
}
214-
215-
$configurableAttribute = $this->configurableAttributeFactory->create();
216-
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
159+
$this->saveNewOption($sku, $option, $configurableAttribute);
217160
}
218161
if (!$configurableAttribute->getId()) {
219162
throw new CouldNotSaveException(__('An error occurred while saving option'));
@@ -275,4 +218,94 @@ public function validateNewOptionData(\Magento\ConfigurableProduct\Api\Data\Opti
275218
throw $inputException;
276219
}
277220
}
221+
222+
/**
223+
* @param string $sku
224+
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
225+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
226+
* @throws CouldNotSaveException
227+
* @throws InputException
228+
* @throws NoSuchEntityException
229+
* @return void
230+
*/
231+
private function saveExistingOption(
232+
$sku,
233+
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
234+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
235+
) {
236+
/** @var \Magento\Catalog\Model\Product $product */
237+
$product = $this->getProduct($sku);
238+
$configurableAttribute->load($option->getId());
239+
if (!$configurableAttribute->getId() || $configurableAttribute->getProductId() != $product->getId()) {
240+
throw new NoSuchEntityException(
241+
__(
242+
'Option with id "%1" not found',
243+
$option->getId()
244+
)
245+
);
246+
}
247+
$configurableAttribute->addData($option->getData());
248+
$configurableAttribute->setValues(
249+
$option->getValues() !== null ? $option->getValues() : $configurableAttribute->getPrices()
250+
);
251+
252+
try {
253+
$configurableAttribute->save();
254+
} catch (\Exception $e) {
255+
throw new CouldNotSaveException(
256+
__(
257+
'Could not update option with id "%1"',
258+
$option->getId()
259+
)
260+
);
261+
}
262+
}
263+
264+
/**
265+
* @param string $sku
266+
* @param \Magento\ConfigurableProduct\Api\Data\OptionInterface $option
267+
* @param \Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
268+
* @return void
269+
* @throws CouldNotSaveException
270+
* @throws InputException
271+
*/
272+
private function saveNewOption(
273+
$sku,
274+
\Magento\ConfigurableProduct\Api\Data\OptionInterface $option,
275+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable\Attribute $configurableAttribute
276+
) {
277+
$this->validateNewOptionData($option);
278+
/** @var \Magento\Catalog\Model\Product $product */
279+
$product = $this->productRepository->get($sku);
280+
$allowedTypes = [ProductType::TYPE_SIMPLE, ProductType::TYPE_VIRTUAL, ConfigurableType::TYPE_CODE];
281+
if (!in_array($product->getTypeId(), $allowedTypes)) {
282+
throw new \InvalidArgumentException('Incompatible product type');
283+
}
284+
285+
$eavAttribute = $this->productAttributeRepository->get($option->getAttributeId());
286+
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
287+
if ($configurableAttribute->getId()) {
288+
throw new CouldNotSaveException(__('Product already has this option'));
289+
}
290+
291+
$configurableAttributesData = [
292+
'attribute_id' => $option->getAttributeId(),
293+
'position' => $option->getPosition(),
294+
'use_default' => $option->getIsUseDefault(),
295+
'label' => $option->getLabel(),
296+
'values' => $option->getValues()
297+
];
298+
299+
try {
300+
$product->setTypeId(ConfigurableType::TYPE_CODE);
301+
$product->setConfigurableAttributesData([$configurableAttributesData]);
302+
$product->setStoreId($this->storeManager->getStore(Store::ADMIN_CODE)->getId());
303+
$product->save();
304+
} catch (\Exception $e) {
305+
throw new CouldNotSaveException(__('An error occurred while saving option'));
306+
}
307+
308+
$configurableAttribute = $this->configurableAttributeFactory->create();
309+
$configurableAttribute->loadByProductAndAttribute($product, $eavAttribute);
310+
}
278311
}

0 commit comments

Comments
 (0)