Skip to content

Commit 36ff162

Browse files
committed
ACP2E-330: When partial indexer is executed catalog rule prices get removed
1 parent 1344d1e commit 36ff162

File tree

1 file changed

+123
-17
lines changed

1 file changed

+123
-17
lines changed

app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule/ConfigurableProductHandlerTest.php

Lines changed: 123 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ protected function setUp(): void
4444
{
4545
$this->configurableMock = $this->createPartialMock(
4646
Configurable::class,
47-
['getChildrenIds']
47+
['getChildrenIds', 'getParentIdsByChild']
4848
);
4949
$this->configurableProductsProviderMock = $this->createPartialMock(
5050
ConfigurableProductsProvider::class,
@@ -61,29 +61,38 @@ protected function setUp(): void
6161
/**
6262
* @return void
6363
*/
64-
public function testAfterGetMatchingProductIdsWithSimpleProduct()
64+
public function testAroundGetMatchingProductIdsWithSimpleProduct()
6565
{
6666
$this->configurableProductsProviderMock->expects($this->once())->method('getIds')->willReturn([]);
6767
$this->configurableMock->expects($this->never())->method('getChildrenIds');
68+
$this->ruleMock->expects($this->never())
69+
->method('setProductsFilter');
6870

6971
$productIds = ['product' => 'valid results'];
7072
$this->assertEquals(
7173
$productIds,
72-
$this->configurableProductHandler->afterGetMatchingProductIds($this->ruleMock, $productIds)
74+
$this->configurableProductHandler->aroundGetMatchingProductIds(
75+
$this->ruleMock,
76+
function () {
77+
return ['product' => 'valid results'];
78+
}
79+
)
7380
);
7481
}
7582

7683
/**
7784
* @return void
7885
*/
79-
public function testAfterGetMatchingProductIdsWithConfigurableProduct()
86+
public function testAroundGetMatchingProductIdsWithConfigurableProduct()
8087
{
8188
$this->configurableProductsProviderMock->expects($this->once())->method('getIds')
8289
->willReturn(['conf1', 'conf2']);
8390
$this->configurableMock->expects($this->any())->method('getChildrenIds')->willReturnMap([
8491
['conf1', true, [ 0 => ['simple1']]],
8592
['conf2', true, [ 0 => ['simple1', 'simple2']]],
8693
]);
94+
$this->ruleMock->expects($this->never())
95+
->method('setProductsFilter');
8796

8897
$this->assertEquals(
8998
[
@@ -96,21 +105,118 @@ public function testAfterGetMatchingProductIdsWithConfigurableProduct()
96105
3 => true,
97106
]
98107
],
99-
$this->configurableProductHandler->afterGetMatchingProductIds(
108+
$this->configurableProductHandler->aroundGetMatchingProductIds(
100109
$this->ruleMock,
101-
[
102-
'conf1' => [
103-
0 => true,
104-
1 => true,
105-
],
106-
'conf2' => [
107-
0 => false,
108-
1 => false,
109-
3 => true,
110-
4 => false,
111-
],
112-
]
110+
function () {
111+
return [
112+
'conf1' => [
113+
0 => true,
114+
1 => true,
115+
],
116+
'conf2' => [
117+
0 => false,
118+
1 => false,
119+
3 => true,
120+
4 => false,
121+
],
122+
];
123+
}
113124
)
114125
);
115126
}
127+
128+
/**
129+
* @param array $productsFilter
130+
* @param array $expectedProductsFilter
131+
* @param array $matchingProductIds
132+
* @param array $expectedMatchingProductIds
133+
* @return void
134+
* @dataProvider aroundGetMatchingProductIdsDataProvider
135+
*/
136+
public function testAroundGetMatchingProductIdsWithProductsFilter(
137+
array $productsFilter,
138+
array $expectedProductsFilter,
139+
array $matchingProductIds,
140+
array $expectedMatchingProductIds
141+
): void {
142+
$configurableProducts = [
143+
'conf1' => ['simple11', 'simple12'],
144+
'conf2' => ['simple21', 'simple22'],
145+
];
146+
$this->configurableProductsProviderMock->method('getIds')
147+
->willReturnCallback(
148+
function ($ids) use ($configurableProducts) {
149+
return array_intersect($ids, array_keys($configurableProducts));
150+
}
151+
);
152+
$this->configurableMock->method('getChildrenIds')
153+
->willReturnCallback(
154+
function ($id) use ($configurableProducts) {
155+
return [0 => $configurableProducts[$id] ?? []];
156+
}
157+
);
158+
159+
$this->configurableMock->method('getParentIdsByChild')
160+
->willReturnCallback(
161+
function ($ids) use ($configurableProducts) {
162+
$result = [];
163+
foreach ($configurableProducts as $configurableProduct => $childProducts) {
164+
if (array_intersect($ids, $childProducts)) {
165+
$result[] = $configurableProduct;
166+
}
167+
}
168+
return $result;
169+
}
170+
);
171+
172+
$this->ruleMock->method('getProductsFilter')
173+
->willReturn($productsFilter);
174+
175+
$this->ruleMock->expects($this->once())
176+
->method('setProductsFilter')
177+
->willReturn($expectedProductsFilter);
178+
179+
$this->assertEquals(
180+
$expectedMatchingProductIds,
181+
$this->configurableProductHandler->aroundGetMatchingProductIds(
182+
$this->ruleMock,
183+
function () use ($matchingProductIds) {
184+
return $matchingProductIds;
185+
}
186+
)
187+
);
188+
}
189+
190+
/**
191+
* @return array[]
192+
*/
193+
public function aroundGetMatchingProductIdsDataProvider(): array
194+
{
195+
return [
196+
[
197+
['simple1',],
198+
['simple1',],
199+
['simple1' => [1 => false]],
200+
['simple1' => [1 => false],],
201+
],
202+
[
203+
['simple11',],
204+
['simple11', 'conf1',],
205+
['simple11' => [1 => false], 'conf1' => [1 => true],],
206+
['simple11' => [1 => true],],
207+
],
208+
[
209+
['simple11', 'simple12',],
210+
['simple11', 'conf1',],
211+
['simple11' => [1 => false], 'conf1' => [1 => true],],
212+
['simple11' => [1 => true], 'simple12' => [1 => true],],
213+
],
214+
[
215+
['conf1', 'simple11', 'simple12'],
216+
['conf1', 'simple11', 'simple12'],
217+
['conf1' => [1 => true], 'simple11' => [1 => false], 'simple12' => [1 => false]],
218+
['simple11' => [1 => true], 'simple12' => [1 => true]],
219+
],
220+
];
221+
}
116222
}

0 commit comments

Comments
 (0)