Skip to content

Commit d7f5afe

Browse files
committed
Merge remote-tracking branch 'magento-l3/ACP2E-2029' into JUN062023_PR_sarmistha
2 parents d845f76 + abf434f commit d7f5afe

File tree

2 files changed

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

2 files changed

+127
-25
lines changed

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\SalesRule\Model\Rule\Condition\Product;
77

8+
use Magento\Framework\Model\AbstractModel;
9+
810
class Found extends \Magento\SalesRule\Model\Rule\Condition\Product\Combine
911
{
1012
/**
@@ -53,21 +55,34 @@ public function asHtml()
5355
/**
5456
* Validate
5557
*
56-
* @param \Magento\Framework\Model\AbstractModel $model
58+
* @param AbstractModel $model
5759
* @return bool
5860
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
5961
*/
60-
public function validate(\Magento\Framework\Model\AbstractModel $model)
62+
public function validate(AbstractModel $model)
6163
{
6264
$isValid = false;
65+
$all = $this->getAggregator() === 'all';
66+
$true = (bool)$this->getValue();
6367

6468
foreach ($model->getAllItems() as $item) {
65-
if (parent::validate($item)) {
69+
$validated = parent::validate($item);
70+
if (!$true && !$validated) {
71+
$isValid = false;
72+
break;
73+
}
74+
if (!$all && $validated) {
6675
$isValid = true;
6776
break;
6877
}
78+
if ($all && $true && $validated) {
79+
$isValid = true;
80+
break;
81+
}
82+
if ($all && !$true && $validated) {
83+
$isValid = true;
84+
}
6985
}
70-
7186
return $isValid;
7287
}
7388
}

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

Lines changed: 108 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,18 @@
66

77
namespace Magento\SalesRule\Model\Rule\Condition;
88

9+
use Magento\Catalog\Test\Fixture\Category as CategoryFixture;
10+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
11+
use Magento\Checkout\Test\Fixture\SetBillingAddress as SetBillingAddressFixture;
12+
use Magento\Checkout\Test\Fixture\SetShippingAddress as SetShippingAddressFixture;
13+
use Magento\Framework\Exception\NoSuchEntityException;
914
use Magento\Framework\Registry;
15+
use Magento\Quote\Model\QuoteRepository;
16+
use Magento\Quote\Test\Fixture\AddProductToCart as AddProductToCartFixture;
17+
use Magento\Quote\Test\Fixture\GuestCart as GuestCartFixture;
1018
use Magento\SalesRule\Model\Rule;
19+
use Magento\SalesRule\Model\Rule\Condition\Product\Found;
20+
use Magento\SalesRule\Model\Rule\Condition\Product\Subselect;
1121
use Magento\SalesRule\Test\Fixture\ProductCondition as ProductConditionFixture;
1222
use Magento\SalesRule\Test\Fixture\ProductFoundInCartConditions as ProductFoundInCartConditionsFixture;
1323
use Magento\SalesRule\Test\Fixture\ProductSubselectionInCartConditions as ProductSubselectionInCartConditionsFixture;
@@ -34,13 +44,19 @@ class ProductTest extends \PHPUnit\Framework\TestCase
3444
*/
3545
private $fixtures;
3646

47+
/**
48+
* @var QuoteRepository
49+
*/
50+
private $quote;
51+
3752
/**
3853
* @inheritDoc
3954
*/
4055
protected function setUp(): void
4156
{
4257
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
4358
$this->fixtures = DataFixtureStorageManager::getStorage();
59+
$this->quote = $this->objectManager->get(QuoteRepository::class);
4460
}
4561

4662
/**
@@ -197,7 +213,7 @@ public function testValidateParentCategoryWithConfigurable(array $conditions, bo
197213
$rule->load($ruleId);
198214
$rule->getConditions()->setConditions([])->loadArray(
199215
[
200-
'type' => \Magento\SalesRule\Model\Rule\Condition\Combine::class,
216+
'type' => Combine::class,
201217
'attribute' => null,
202218
'operator' => null,
203219
'value' => '1',
@@ -226,16 +242,15 @@ public function conditionsDataProvider(): array
226242
'Category (Parent Only) is not "Default Category"' => [
227243
'conditions' => [
228244
[
229-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
245+
'type' => Subselect::class,
230246
'attribute' => 'qty',
231247
'operator' => '==',
232248
'value' => '1',
233249
'is_value_processed' => null,
234250
'aggregator' => 'all',
235-
'conditions' =>
236-
[
251+
'conditions' => [
237252
[
238-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
253+
'type' => Product::class,
239254
'attribute' => 'category_ids',
240255
'attribute_scope' => 'parent',
241256
'operator' => '!=',
@@ -251,16 +266,15 @@ public function conditionsDataProvider(): array
251266
'Category (Parent Only) is "Default Category"' => [
252267
'conditions' => [
253268
[
254-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Subselect::class,
269+
'type' => Subselect::class,
255270
'attribute' => 'qty',
256271
'operator' => '==',
257272
'value' => '1',
258273
'is_value_processed' => null,
259274
'aggregator' => 'all',
260-
'conditions' =>
261-
[
275+
'conditions' => [
262276
[
263-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
277+
'type' => Product::class,
264278
'attribute' => 'category_ids',
265279
'attribute_scope' => 'parent',
266280
'operator' => '==',
@@ -276,14 +290,13 @@ public function conditionsDataProvider(): array
276290
'Category (Parent Only) is not "Default Category"' => [
277291
'conditions' => [
278292
[
279-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
293+
'type' => Found::class,
280294
'value' => '1',
281295
'is_value_processed' => null,
282296
'aggregator' => 'all',
283-
'conditions' =>
284-
[
297+
'conditions' => [
285298
[
286-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
299+
'type' => Product::class,
287300
'attribute' => 'category_ids',
288301
'attribute_scope' => 'parent',
289302
'operator' => '!=',
@@ -299,14 +312,13 @@ public function conditionsDataProvider(): array
299312
'Category (Parent Only) is "Default Category"' => [
300313
'conditions' => [
301314
[
302-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
315+
'type' => Found::class,
303316
'value' => '1',
304317
'is_value_processed' => null,
305318
'aggregator' => 'all',
306-
'conditions' =>
307-
[
319+
'conditions' => [
308320
[
309-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
321+
'type' => Product::class,
310322
'attribute' => 'category_ids',
311323
'attribute_scope' => 'parent',
312324
'operator' => '==',
@@ -322,14 +334,13 @@ public function conditionsDataProvider(): array
322334
'Category (Parent Only) is "Default Category"' => [
323335
'conditions' => [
324336
[
325-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product\Found::class,
337+
'type' => Found::class,
326338
'value' => '0',
327339
'is_value_processed' => null,
328340
'aggregator' => 'all',
329-
'conditions' =>
330-
[
341+
'conditions' => [
331342
[
332-
'type' => \Magento\SalesRule\Model\Rule\Condition\Product::class,
343+
'type' => Product::class,
333344
'attribute' => 'category_ids',
334345
'attribute_scope' => 'parent',
335346
'operator' => '==',
@@ -343,4 +354,80 @@ public function conditionsDataProvider(): array
343354
],
344355
];
345356
}
357+
358+
/**
359+
* Ensure that the coupon code shouldn't get applied as the cart contains products from restricted category
360+
*
361+
* @throws NoSuchEntityException
362+
* @return void
363+
*/
364+
#[
365+
AppIsolation(true),
366+
DbIsolation(true),
367+
DataFixture(CategoryFixture::class, as: 'c1'),
368+
DataFixture(CategoryFixture::class, as: 'c2'),
369+
DataFixture(ProductFixture::class, [
370+
'price' => 40,
371+
'sku' => 'p1',
372+
'category_ids' => ['$c1.id$']
373+
], 'p1'),
374+
DataFixture(ProductFixture::class, [
375+
'price' => 30,
376+
'sku' => 'p2',
377+
'category_ids' => ['$c2.id$']
378+
], 'p2'),
379+
DataFixture(
380+
RuleFixture::class,
381+
[
382+
'stop_rules_processing'=> 0,
383+
'coupon_code' => 'test',
384+
'discount_amount' => 10,
385+
'conditions' => [
386+
[
387+
'type' => Combine::class,
388+
'attribute' => null,
389+
'operator' => null,
390+
'value' => '1',
391+
'is_value_processed' => null,
392+
'aggregator' => 'all',
393+
'conditions' => [
394+
[
395+
'type' => Found::class,
396+
'value' => '0',
397+
'is_value_processed' => null,
398+
'aggregator' => 'all',
399+
'conditions' => [
400+
[
401+
'type' => Product::class,
402+
'attribute' => 'category_ids',
403+
'operator' => '==',
404+
'value' => '$c1.id$',
405+
'is_value_processed' => false,
406+
],
407+
],
408+
],
409+
],
410+
],
411+
],
412+
'simple_action' => Rule::BY_FIXED_ACTION,
413+
'sort_order' => 0
414+
],
415+
'rule'
416+
),
417+
DataFixture(GuestCartFixture::class, as: 'cart'),
418+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p1.id$', 'qty' => 1]),
419+
DataFixture(AddProductToCartFixture::class, ['cart_id' => '$cart.id$', 'product_id' => '$p2.id$', 'qty' => 1]),
420+
DataFixture(SetBillingAddressFixture::class, ['cart_id' => '$cart.id$'], as: 'billingAddress'),
421+
DataFixture(SetShippingAddressFixture::class, ['cart_id' => '$cart.id$'], as: 'shippingAddress'),
422+
]
423+
public function testValidateSalesRuleForRestrictedCategories(): void
424+
{
425+
$cartId = (int)$this->fixtures->get('cart')->getId();
426+
$quote = $this->quote->get($cartId);
427+
428+
$ruleId = $this->fixtures->get('rule')->getId();
429+
$rule = $this->objectManager->create(Rule::class)->load($ruleId);
430+
431+
$this->assertFalse($rule->validate($quote->getShippingAddress()));
432+
}
346433
}

0 commit comments

Comments
 (0)