Skip to content

Commit e6f2e65

Browse files
author
Gurzhyi, Andrii
committed
MAGETWO-48227: Fix API/Integration tests
- Fixed tests
1 parent 9f0d54e commit e6f2e65

File tree

3 files changed

+132
-11
lines changed

3 files changed

+132
-11
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\ConfigurableProduct\Model\Plugin;
7+
8+
use Magento\Catalog\Model\ProductFactory;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Framework\Exception\InputException;
11+
use Magento\Catalog\Api\ProductRepositoryInterface;
12+
use Magento\Framework\Exception\CouldNotSaveException;
13+
use Magento\ConfigurableProduct\Api\Data\OptionInterface;
14+
use Magento\Catalog\Api\ProductAttributeRepositoryInterface;
15+
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
16+
17+
/**
18+
* Class AroundProductRepositorySave
19+
*/
20+
class AroundProductRepositorySave
21+
{
22+
/**
23+
* @var ProductAttributeRepositoryInterface
24+
*/
25+
private $productAttributeRepository;
26+
27+
/**
28+
* @var ProductFactory
29+
*/
30+
private $productFactory;
31+
32+
/**
33+
* @param ProductAttributeRepositoryInterface $productAttributeRepository
34+
* @param ProductFactory $productFactory
35+
*/
36+
public function __construct(
37+
ProductAttributeRepositoryInterface $productAttributeRepository,
38+
ProductFactory $productFactory
39+
) {
40+
$this->productAttributeRepository = $productAttributeRepository;
41+
$this->productFactory = $productFactory;
42+
}
43+
44+
/**
45+
* @param ProductRepositoryInterface $subject
46+
* @param callable $proceed
47+
* @param ProductInterface $product
48+
* @param bool $saveOptions
49+
* @return ProductInterface
50+
* @throws CouldNotSaveException
51+
* @throws InputException
52+
*
53+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
54+
*/
55+
public function aroundSave(
56+
ProductRepositoryInterface $subject,
57+
\Closure $proceed,
58+
ProductInterface $product,
59+
$saveOptions = false
60+
) {
61+
/** @var ProductInterface $result */
62+
$result = $proceed($product, $saveOptions);
63+
if ($product->getTypeId() !== Configurable::TYPE_CODE) {
64+
return $result;
65+
}
66+
67+
$extensionAttributes = $product->getExtensionAttributes();
68+
if ($extensionAttributes === null) {
69+
return $result;
70+
}
71+
72+
$configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
73+
$configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
74+
75+
if (empty($configurableLinks) && empty($extensionAttributes)) {
76+
return $result;
77+
}
78+
79+
$attributeCodes = [];
80+
/** @var OptionInterface $configurableOption */
81+
foreach ($configurableOptions as $configurableOption) {
82+
$eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId());
83+
$attributeCode = $eavAttribute->getAttributeCode();
84+
$attributeCodes[] = $attributeCode;
85+
}
86+
$this->validateProductLinks($attributeCodes, $configurableLinks);
87+
88+
return $subject->get($result->getSku(), false, $result->getStoreId(), true);
89+
}
90+
91+
/**
92+
* @param array $attributeCodes
93+
* @param array $linkIds
94+
* @return $this
95+
* @throws InputException
96+
*/
97+
private function validateProductLinks(array $attributeCodes, array $linkIds)
98+
{
99+
$valueMap = [];
100+
if (empty($attributeCodes) && !empty($linkIds)) {
101+
throw new InputException(
102+
__('The configurable product does not have any variation attribute.')
103+
);
104+
}
105+
106+
foreach ($linkIds as $productId) {
107+
$variation = $this->productFactory->create()->load($productId);
108+
if (!$variation->getId()) {
109+
throw new InputException(__('Product with id "%1" does not exist.', $productId));
110+
}
111+
$valueKey = '';
112+
foreach ($attributeCodes as $attributeCode) {
113+
if (!$variation->getData($attributeCode)) {
114+
throw new InputException(
115+
__('Product with id "%1" does not contain required attribute "%2".', $productId, $attributeCode)
116+
);
117+
}
118+
$valueKey = $valueKey . $attributeCode . ':' . $variation->getData($attributeCode) . ';';
119+
}
120+
if (isset($valueMap[$valueKey])) {
121+
throw new InputException(
122+
__('Products "%1" and %2 have the same set of attribute values.', $productId, $valueMap[$valueKey])
123+
);
124+
}
125+
$valueMap[$valueKey] = $productId;
126+
}
127+
}
128+
}

app/code/Magento/ConfigurableProduct/Model/Product/SaveHandler.php

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Magento\ConfigurableProduct\Api\OptionRepositoryInterface;
1111
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1212
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\Configurable as ResourceModelConfigurable;
13-
use Magento\ConfigurableProduct\Model\ResourceModel\Product\Type\ConfigurableFactory;
1413

1514
/**
1615
* Class SaveHandler
@@ -22,11 +21,6 @@ class SaveHandler
2221
*/
2322
private $optionRepository;
2423

25-
/**
26-
* @var ConfigurableFactory
27-
*/
28-
private $configurableFactory;
29-
3024
/**
3125
* @var ProductAttributeRepositoryInterface
3226
*/
@@ -42,17 +36,14 @@ class SaveHandler
4236
*
4337
* @param ResourceModelConfigurable $resourceModel
4438
* @param OptionRepositoryInterface $optionRepository
45-
* @param ConfigurableFactory $configurableFactory
4639
* @param ProductAttributeRepositoryInterface $productAttributeRepository
4740
*/
4841
public function __construct(
4942
ResourceModelConfigurable $resourceModel,
5043
OptionRepositoryInterface $optionRepository,
51-
ConfigurableFactory $configurableFactory,
5244
ProductAttributeRepositoryInterface $productAttributeRepository
5345
) {
5446
$this->optionRepository = $optionRepository;
55-
$this->configurableFactory = $configurableFactory;
5647
$this->productAttributeRepository = $productAttributeRepository;
5748
$this->resourceModel = $resourceModel;
5849
}
@@ -76,15 +67,14 @@ public function execute($entityType, ProductInterface $entity)
7667
}
7768

7869
$ids = [];
79-
$configurableOptions = $extensionAttributes->getConfigurableProductOptions();
70+
$configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
8071
if (!empty($configurableOptions)) {
8172
$ids = $this->saveConfigurableProductAttributes($entity, $configurableOptions);
8273
}
8374

8475
$this->deleteConfigurableProductAttributes($entity, $ids);
8576

8677
$configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
87-
8878
$this->resourceModel->saveProducts($entity, $configurableLinks);
8979

9080
return $entity;

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
</argument>
5757
</arguments>
5858
</type>
59+
<type name="Magento\Catalog\Api\ProductRepositoryInterface">
60+
<plugin name="configurableProductSaveOptions" type="\Magento\ConfigurableProduct\Model\Plugin\AroundProductRepositorySave"/>
61+
</type>
5962
<type name="Magento\Catalog\Model\Product\Type">
6063
<plugin name="configurable_output" type="Magento\ConfigurableProduct\Model\Product\Type\Plugin" />
6164
</type>

0 commit comments

Comments
 (0)