Skip to content

Commit 2211d36

Browse files
committed
Merge branch 'productRepository-plugin' of github.com:rmsundar1/magento2 into 2.4-develop-prs
2 parents b51bf75 + 50a4c10 commit 2211d36

File tree

2 files changed

+19
-78
lines changed

2 files changed

+19
-78
lines changed

app/code/Magento/ConfigurableProduct/Model/Plugin/ProductRepositorySave.php

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function __construct(
4949
* @param ProductRepositoryInterface $subject
5050
* @param ProductInterface $product
5151
* @param bool $saveOptions
52-
* @return array
52+
* @return void
5353
* @throws InputException
5454
* @throws NoSuchEntityException
5555
*
@@ -59,34 +59,23 @@ public function beforeSave(
5959
ProductRepositoryInterface $subject,
6060
ProductInterface $product,
6161
$saveOptions = false
62-
): array {
63-
$result[] = $product;
64-
if ($product->getTypeId() !== Configurable::TYPE_CODE) {
65-
return $result;
66-
}
67-
62+
): void {
6863
$extensionAttributes = $product->getExtensionAttributes();
69-
if ($extensionAttributes === null) {
70-
return $result;
71-
}
72-
73-
$configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
74-
$configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
64+
if ($extensionAttributes !== null && $product->getTypeId() === Configurable::TYPE_CODE) {
65+
$configurableLinks = (array) $extensionAttributes->getConfigurableProductLinks();
66+
$configurableOptions = (array) $extensionAttributes->getConfigurableProductOptions();
7567

76-
if (empty($configurableLinks) && empty($configurableOptions)) {
77-
return $result;
78-
}
79-
80-
$attributeCodes = [];
81-
/** @var OptionInterface $configurableOption */
82-
foreach ($configurableOptions as $configurableOption) {
83-
$eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId());
84-
$attributeCode = $eavAttribute->getAttributeCode();
85-
$attributeCodes[] = $attributeCode;
68+
if (!empty($configurableLinks) || !empty($configurableOptions)) {
69+
$attributeCodes = [];
70+
/** @var OptionInterface $configurableOption */
71+
foreach ($configurableOptions as $configurableOption) {
72+
$eavAttribute = $this->productAttributeRepository->get($configurableOption->getAttributeId());
73+
$attributeCode = $eavAttribute->getAttributeCode();
74+
$attributeCodes[] = $attributeCode;
75+
}
76+
$this->validateProductLinks($attributeCodes, $configurableLinks);
77+
}
8678
}
87-
$this->validateProductLinks($attributeCodes, $configurableLinks);
88-
89-
return $result;
9079
}
9180

9281
/**

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Plugin/ProductRepositorySaveTest.php

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,13 @@ protected function setUp(): void
113113
*/
114114
public function testBeforeSaveWhenProductIsSimple(): void
115115
{
116-
$this->product->expects(static::once())
116+
$this->product->expects(static::atMost(1))
117117
->method('getTypeId')
118118
->willReturn('simple');
119-
$this->product->expects(static::never())
119+
$this->product->expects(static::once())
120120
->method('getExtensionAttributes');
121121

122-
$this->assertEquals(
123-
$this->product,
124-
$this->plugin->beforeSave($this->productRepository, $this->product)[0]
125-
);
122+
$this->assertNull($this->plugin->beforeSave($this->productRepository, $this->product));
126123
}
127124

128125
/**
@@ -150,52 +147,7 @@ public function testBeforeSaveWithoutOptions(): void
150147
$this->productAttributeRepository->expects(static::never())
151148
->method('get');
152149

153-
$this->assertEquals(
154-
$this->product,
155-
$this->plugin->beforeSave($this->productRepository, $this->product)[0]
156-
);
157-
}
158-
159-
/**
160-
* Test saving a configurable product with same set of attribute values
161-
*
162-
* @return void
163-
*/
164-
public function testBeforeSaveWithLinks(): void
165-
{
166-
$this->expectException(InputException::class);
167-
$this->expectExceptionMessage('Products "5" and "4" have the same set of attribute values.');
168-
$links = [4, 5];
169-
$this->product->expects(static::once())
170-
->method('getTypeId')
171-
->willReturn(Configurable::TYPE_CODE);
172-
173-
$this->product->expects(static::once())
174-
->method('getExtensionAttributes')
175-
->willReturn($this->extensionAttributes);
176-
$this->extensionAttributes->expects(static::once())
177-
->method('getConfigurableProductOptions')
178-
->willReturn(null);
179-
$this->extensionAttributes->expects(static::once())
180-
->method('getConfigurableProductLinks')
181-
->willReturn($links);
182-
183-
$this->productAttributeRepository->expects(static::never())
184-
->method('get');
185-
186-
$product = $this->getMockBuilder(Product::class)
187-
->disableOriginalConstructor()
188-
->setMethods(['getData'])
189-
->getMock();
190-
191-
$this->productRepository->expects(static::exactly(2))
192-
->method('getById')
193-
->willReturn($product);
194-
195-
$product->expects(static::never())
196-
->method('getData');
197-
198-
$this->plugin->beforeSave($this->productRepository, $this->product);
150+
$this->assertNull($this->plugin->beforeSave($this->productRepository, $this->product));
199151
}
200152

201153
/**

0 commit comments

Comments
 (0)