Skip to content

Commit c1828e9

Browse files
committed
Merge branch 'ACP2E-213' of https://github.com/magento-l3/magento2ce into L3-PR-20211001
2 parents be9b1fe + ac7a17c commit c1828e9

File tree

2 files changed

+152
-23
lines changed
  • app/code/Magento/SalesRule/Model/Rule/Condition/Product
  • dev/tests/integration/testsuite/Magento/SalesRule/Model/Rule/Condition

2 files changed

+152
-23
lines changed

app/code/Magento/SalesRule/Model/Rule/Condition/Product/Found.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,29 +59,15 @@ public function asHtml()
5959
*/
6060
public function validate(\Magento\Framework\Model\AbstractModel $model)
6161
{
62-
$all = $this->getAggregator() === 'all';
63-
$true = (bool)$this->getValue();
64-
$found = false;
62+
$isValid = false;
63+
6564
foreach ($model->getAllItems() as $item) {
66-
$found = $all;
67-
foreach ($this->getConditions() as $cond) {
68-
$validated = $cond->validate($item);
69-
if ($all && !$validated || !$all && $validated) {
70-
$found = $validated;
71-
break;
72-
}
73-
}
74-
if ($found && $true || !$true && $found) {
65+
if (parent::validate($item)) {
66+
$isValid = true;
7567
break;
7668
}
7769
}
78-
// found an item and we're looking for existing one
79-
if ($found && $true) {
80-
return true;
81-
} elseif (!$found && !$true) {
82-
// not found and we're making sure it doesn't exist
83-
return true;
84-
}
85-
return false;
70+
71+
return $isValid;
8672
}
8773
}

dev/tests/integration/testsuite/Magento/SalesRule/Model/Rule/Condition/ProductTest.php

Lines changed: 146 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,19 +136,162 @@ public function testValidateQtySalesRuleWithConfigurable()
136136
*
137137
* @magentoDataFixture Magento/ConfigurableProduct/_files/quote_with_configurable_product.php
138138
* @magentoDataFixture Magento/SalesRule/_files/rules_parent_category.php
139+
* @dataProvider conditionsDataProvider
139140
*/
140-
public function testValidateParentCategoryWithConfigurable()
141+
public function testValidateParentCategoryWithConfigurable(array $conditions, bool $expected): void
141142
{
142143
$quote = $this->getQuote('test_cart_with_configurable');
143144
$registry = $this->objectManager->get(Registry::class);
144145
/** @var Rule $rule */
145146
$rule = $this->objectManager->create(Rule::class);
146147
$ruleId = $registry->registry('50% Off on Configurable parent category');
147148
$rule->load($ruleId);
149+
$rule->getConditions()->setConditions([])->loadArray(
150+
[
151+
'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
152+
'attribute' => null,
153+
'operator' => null,
154+
'value' => '1',
155+
'is_value_processed' => null,
156+
'aggregator' => 'all',
157+
'conditions' => $conditions
158+
]
159+
);
160+
$rule->save();
148161

149-
$this->assertFalse(
150-
$rule->validate($quote->getBillingAddress()),
162+
$this->assertEquals(
163+
$expected,
164+
$rule->validate($quote->getShippingAddress()),
151165
'Cart price rule validation failed.'
152166
);
153167
}
168+
169+
/**
170+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
171+
* @return array
172+
*/
173+
public function conditionsDataProvider(): array
174+
{
175+
return [
176+
'If total quantity is 1 for a subselection of items in cart matching ALL of these conditions: ' .
177+
'Category (Parent Only) is not "Default Category"' => [
178+
'conditions' => [
179+
[
180+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
181+
'attribute' => 'qty',
182+
'operator' => '==',
183+
'value' => '1',
184+
'is_value_processed' => null,
185+
'aggregator' => 'all',
186+
'conditions' =>
187+
[
188+
[
189+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
190+
'attribute' => 'category_ids',
191+
'attribute_scope' => 'parent',
192+
'operator' => '!=',
193+
'value' => '2',
194+
'is_value_processed' => false,
195+
],
196+
],
197+
],
198+
],
199+
'expected' => false
200+
],
201+
'If total quantity is 1 for a subselection of items in cart matching ALL of these conditions: ' .
202+
'Category (Parent Only) is "Default Category"' => [
203+
'conditions' => [
204+
[
205+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
206+
'attribute' => 'qty',
207+
'operator' => '==',
208+
'value' => '1',
209+
'is_value_processed' => null,
210+
'aggregator' => 'all',
211+
'conditions' =>
212+
[
213+
[
214+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
215+
'attribute' => 'category_ids',
216+
'attribute_scope' => 'parent',
217+
'operator' => '==',
218+
'value' => '2',
219+
'is_value_processed' => false,
220+
],
221+
],
222+
],
223+
],
224+
'expected' => true
225+
],
226+
'If an item is found in the cart with all these conditions true: ' .
227+
'Category (Parent Only) is not "Default Category"' => [
228+
'conditions' => [
229+
[
230+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
231+
'value' => '1',
232+
'is_value_processed' => null,
233+
'aggregator' => 'all',
234+
'conditions' =>
235+
[
236+
[
237+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
238+
'attribute' => 'category_ids',
239+
'attribute_scope' => 'parent',
240+
'operator' => '!=',
241+
'value' => '2',
242+
'is_value_processed' => false,
243+
],
244+
],
245+
],
246+
],
247+
'expected' => false
248+
],
249+
'If an item is found in the cart with all these conditions true: ' .
250+
'Category (Parent Only) is "Default Category"' => [
251+
'conditions' => [
252+
[
253+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
254+
'value' => '1',
255+
'is_value_processed' => null,
256+
'aggregator' => 'all',
257+
'conditions' =>
258+
[
259+
[
260+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
261+
'attribute' => 'category_ids',
262+
'attribute_scope' => 'parent',
263+
'operator' => '==',
264+
'value' => '2',
265+
'is_value_processed' => false,
266+
],
267+
],
268+
],
269+
],
270+
'expected' => true
271+
],
272+
'If an item is not found in the cart with all these conditions true: ' .
273+
'Category (Parent Only) is "Default Category"' => [
274+
'conditions' => [
275+
[
276+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
277+
'value' => '0',
278+
'is_value_processed' => null,
279+
'aggregator' => 'all',
280+
'conditions' =>
281+
[
282+
[
283+
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
284+
'attribute' => 'category_ids',
285+
'attribute_scope' => 'parent',
286+
'operator' => '==',
287+
'value' => '2',
288+
'is_value_processed' => false,
289+
],
290+
],
291+
],
292+
],
293+
'expected' => false
294+
],
295+
];
296+
}
154297
}

0 commit comments

Comments
 (0)