Skip to content

Commit 7ba4e19

Browse files
committed
Merge remote-tracking branch 'remotes/origin/MAGETWO-51256' into PR-3
2 parents 5197bd5 + e09e4c9 commit 7ba4e19

File tree

4 files changed

+78
-7
lines changed

4 files changed

+78
-7
lines changed

app/code/Magento/Catalog/Model/Product/Option.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ protected function _getValidationRulesBeforeSave()
562562
public function getProductSku()
563563
{
564564
$productSku = $this->_getData(self::KEY_PRODUCT_SKU);
565-
if (!$productSku) {
565+
if (!$productSku && $this->getProduct()) {
566566
$productSku = $this->getProduct()->getSku();
567567
}
568568
return $productSku;

app/code/Magento/Catalog/Model/Product/Option/Repository.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,11 @@ public function duplicate(
125125
*/
126126
public function save(\Magento\Catalog\Api\Data\ProductCustomOptionInterface $option)
127127
{
128-
$product = $this->productRepository->get($option->getProductSku());
128+
$productSku = $option->getProductSku();
129+
if (!$productSku) {
130+
throw new CouldNotSaveException(__('ProductSku should be specified'));
131+
}
132+
$product = $this->productRepository->get($productSku);
129133
$metadata = $this->getMetadataPool()->getMetadata(ProductInterface::class);
130134
$option->setData('product_id', $product->getData($metadata->getLinkField()));
131135
$option->setOptionId(null);

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductCustomOptionRepositoryTest.php

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
namespace Magento\Catalog\Api;
1111

12+
use Magento\Catalog\Model\ProductRepository;
1213
use Magento\TestFramework\Helper\Bootstrap;
1314
use Magento\TestFramework\TestCase\WebapiAbstract;
1415

@@ -39,7 +40,7 @@ protected function setUp()
3940
public function testRemove()
4041
{
4142
$sku = 'simple';
42-
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
43+
/** @var ProductRepository $productRepository */
4344
$productRepository = $this->objectManager->create(
4445
'Magento\Catalog\Model\ProductRepository'
4546
);
@@ -142,7 +143,7 @@ public function testGetList()
142143
* @magentoApiDataFixture Magento/Catalog/_files/product_without_options.php
143144
* @magentoAppIsolation enabled
144145
* @dataProvider optionDataProvider
145-
* @SuppressWarnings(PHPMD.UnusedLocalVariable)
146+
* @param array $optionData
146147
*/
147148
public function testSave($optionData)
148149
{
@@ -166,7 +167,7 @@ public function testSave($optionData)
166167
unset($result['product_sku']);
167168
unset($result['option_id']);
168169
if (!empty($result['values'])) {
169-
foreach ($result['values'] as $key => $value) {
170+
foreach (array_keys($result['values']) as $key) {
170171
unset($result['values'][$key]['option_type_id']);
171172
}
172173
}
@@ -240,7 +241,7 @@ public function optionNegativeDataProvider()
240241
public function testUpdate()
241242
{
242243
$productSku = 'simple';
243-
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
244+
/** @var ProductRepository $productRepository */
244245
$productRepository = $this->objectManager->create(
245246
'Magento\Catalog\Model\ProductRepository'
246247
);
@@ -306,7 +307,7 @@ public function testUpdateOptionAddingNewValue($optionType)
306307
'sort_order' => 100,
307308
];
308309

309-
/** @var \Magento\Catalog\Model\ProductRepository $productRepository */
310+
/** @var ProductRepository $productRepository */
310311
$productRepository = $this->objectManager->create(
311312
'Magento\Catalog\Model\ProductRepository'
312313
);
@@ -380,4 +381,48 @@ public function validOptionDataProvider()
380381
'multiple' => ['multiple']
381382
];
382383
}
384+
385+
/**
386+
* @magentoApiDataFixture Magento/Catalog/_files/product_with_options.php
387+
* @magentoAppIsolation enabled
388+
* @dataProvider optionNegativeUpdateDataProvider
389+
* @param array $optionData
390+
* @param string $message
391+
*/
392+
public function testUpdateNegative($optionData, $message)
393+
{
394+
$productSku = 'simple';
395+
/** @var ProductRepository $productRepository */
396+
$productRepository = $this->objectManager->create(ProductRepository::class);
397+
$options = $productRepository->get($productSku, true)->getOptions();
398+
$option = array_shift($options);
399+
$optionId = $option->getOptionId();
400+
401+
$serviceInfo = [
402+
'rest' => [
403+
'resourcePath' => '/V1/products/options/' . $optionId,
404+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
405+
],
406+
'soap' => [
407+
'service' => self::SERVICE_NAME,
408+
'serviceVersion' => 'V1',
409+
'operation' => self::SERVICE_NAME . 'Save',
410+
],
411+
];
412+
413+
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
414+
$this->setExpectedException('SoapFault');
415+
} else {
416+
$this->setExpectedException('Exception', $message, 400);
417+
}
418+
$this->_webApiCall($serviceInfo, ['option' => $optionData]);
419+
}
420+
421+
/**
422+
* @return array
423+
*/
424+
public function optionNegativeUpdateDataProvider()
425+
{
426+
return include '_files/product_options_update_negative.php';
427+
}
383428
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
return [
8+
'missed_product_sku' =>
9+
[
10+
[
11+
'title' => 'title',
12+
'type' => 'field',
13+
'sort_order' => 1,
14+
'is_require' => 1,
15+
'price' => 10.0,
16+
'price_type' => 'fixed',
17+
'sku' => 'sku1',
18+
'max_characters' => 10,
19+
],
20+
'ProductSku should be specified',
21+
]
22+
];

0 commit comments

Comments
 (0)