Skip to content

Commit ac23f76

Browse files
committed
ACP2E-1579: Catalog rule discount disappears after a product is saved from Admin
1 parent 6312143 commit ac23f76

File tree

1 file changed

+56
-27
lines changed
  • app/code/Magento/CatalogRuleConfigurable/Test/Unit/Plugin/CatalogRule/Model/Rule

1 file changed

+56
-27
lines changed

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

Lines changed: 56 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
namespace Magento\CatalogRuleConfigurable\Test\Unit\Plugin\CatalogRule\Model\Rule;
99

10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Catalog\Model\Product;
1012
use Magento\CatalogRule\Model\Rule;
1113
use Magento\CatalogRuleConfigurable\Plugin\CatalogRule\Model\Rule\Validation;
1214
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
13-
use Magento\Framework\DataObject;
1415
use Magento\Rule\Model\Condition\Combine;
1516
use PHPUnit\Framework\MockObject\MockObject;
1617
use PHPUnit\Framework\TestCase;
@@ -30,13 +31,18 @@ class ValidationTest extends TestCase
3031
*/
3132
private $configurableMock;
3233

34+
/**
35+
* @var ProductRepositoryInterface|MockObject
36+
*/
37+
private $productRepositoryMock;
38+
3339
/** @var Rule|MockObject */
3440
private $ruleMock;
3541

3642
/** @var Combine|MockObject */
3743
private $ruleConditionsMock;
3844

39-
/** @var DataObject|MockObject */
45+
/** @var Product|MockObject */
4046
private $productMock;
4147

4248
/**
@@ -48,16 +54,15 @@ protected function setUp(): void
4854
Configurable::class,
4955
['getParentIdsByChild']
5056
);
57+
$this->productRepositoryMock = $this->createMock(ProductRepositoryInterface::class);
5158

5259
$this->ruleMock = $this->createMock(Rule::class);
5360
$this->ruleConditionsMock = $this->createMock(Combine::class);
54-
$this->productMock = $this->getMockBuilder(DataObject::class)
55-
->addMethods(['getId'])
56-
->disableOriginalConstructor()
57-
->getMock();
61+
$this->productMock = $this->createMock(Product::class);
5862

5963
$this->validation = new Validation(
60-
$this->configurableMock
64+
$this->configurableMock,
65+
$this->productRepositoryMock
6166
);
6267
}
6368

@@ -75,13 +80,49 @@ public function testAfterValidateWithValidConfigurableProduct(
7580
$runValidateAmount,
7681
$result
7782
) {
78-
$this->productMock->expects($this->once())->method('getId')->willReturn('product_id');
79-
$this->configurableMock->expects($this->once())->method('getParentIdsByChild')->with('product_id')
83+
$storeId = 1;
84+
$this->productMock->expects($this->once())
85+
->method('getId')
86+
->willReturn(10);
87+
$this->configurableMock->expects($this->once())
88+
->method('getParentIdsByChild')
89+
->with(10)
8090
->willReturn($parentsIds);
81-
$this->ruleMock->expects($this->exactly($runValidateAmount))->method('getConditions')
91+
$this->productMock->expects($this->exactly($runValidateAmount))
92+
->method('getStoreId')
93+
->willReturn($storeId);
94+
$parentsProducts = array_map(
95+
function ($parentsId) {
96+
$parent = $this->createMock(Product::class);
97+
$parent->method('getId')->willReturn($parentsId);
98+
return $parent;
99+
},
100+
$parentsIds
101+
);
102+
$this->productRepositoryMock->expects($this->exactly($runValidateAmount))
103+
->method('getById')
104+
->withConsecutive(
105+
...array_map(
106+
function ($parentsId) use ($storeId) {
107+
return [$parentsId, false, $storeId];
108+
},
109+
$parentsIds
110+
)
111+
)->willReturnOnConsecutiveCalls(...$parentsProducts);
112+
$this->ruleMock->expects($this->exactly($runValidateAmount))
113+
->method('getConditions')
82114
->willReturn($this->ruleConditionsMock);
83-
$this->ruleConditionsMock->expects($this->exactly($runValidateAmount))->method('validateByEntityId')
84-
->willReturnMap($validationResult);
115+
$this->ruleConditionsMock->expects($this->exactly($runValidateAmount))
116+
->method('validate')
117+
->withConsecutive(
118+
...array_map(
119+
function ($parentsProduct) {
120+
return [$parentsProduct];
121+
},
122+
$parentsProducts
123+
)
124+
)
125+
->willReturnOnConsecutiveCalls(...$validationResult);
85126

86127
$this->assertEquals(
87128
$result,
@@ -97,31 +138,19 @@ public function dataProviderForValidateWithValidConfigurableProduct()
97138
return [
98139
[
99140
[1, 2, 3],
100-
[
101-
[1, false],
102-
[2, true],
103-
[3, true],
104-
],
141+
[false, true, true],
105142
2,
106143
true,
107144
],
108145
[
109146
[1, 2, 3],
110-
[
111-
[1, true],
112-
[2, false],
113-
[3, true],
114-
],
147+
[true, false, true],
115148
1,
116149
true,
117150
],
118151
[
119152
[1, 2, 3],
120-
[
121-
[1, false],
122-
[2, false],
123-
[3, false],
124-
],
153+
[false, false, false],
125154
3,
126155
false,
127156
],

0 commit comments

Comments
 (0)