6
6
7
7
namespace Magento \SalesRule \Model \Rule \Condition ;
8
8
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 ;
9
14
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 ;
10
18
use Magento \SalesRule \Model \Rule ;
19
+ use Magento \SalesRule \Model \Rule \Condition \Product \Found ;
20
+ use Magento \SalesRule \Model \Rule \Condition \Product \Subselect ;
11
21
use Magento \SalesRule \Test \Fixture \ProductCondition as ProductConditionFixture ;
12
22
use Magento \SalesRule \Test \Fixture \ProductFoundInCartConditions as ProductFoundInCartConditionsFixture ;
13
23
use Magento \SalesRule \Test \Fixture \ProductSubselectionInCartConditions as ProductSubselectionInCartConditionsFixture ;
@@ -34,13 +44,19 @@ class ProductTest extends \PHPUnit\Framework\TestCase
34
44
*/
35
45
private $ fixtures ;
36
46
47
+ /**
48
+ * @var QuoteRepository
49
+ */
50
+ private $ quote ;
51
+
37
52
/**
38
53
* @inheritDoc
39
54
*/
40
55
protected function setUp (): void
41
56
{
42
57
$ this ->objectManager = \Magento \TestFramework \Helper \Bootstrap::getObjectManager ();
43
58
$ this ->fixtures = DataFixtureStorageManager::getStorage ();
59
+ $ this ->quote = $ this ->objectManager ->get (QuoteRepository::class);
44
60
}
45
61
46
62
/**
@@ -197,7 +213,7 @@ public function testValidateParentCategoryWithConfigurable(array $conditions, bo
197
213
$ rule ->load ($ ruleId );
198
214
$ rule ->getConditions ()->setConditions ([])->loadArray (
199
215
[
200
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Combine::class,
216
+ 'type ' => Combine::class,
201
217
'attribute ' => null ,
202
218
'operator ' => null ,
203
219
'value ' => '1 ' ,
@@ -226,16 +242,15 @@ public function conditionsDataProvider(): array
226
242
'Category (Parent Only) is not "Default Category" ' => [
227
243
'conditions ' => [
228
244
[
229
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product \ Subselect::class,
245
+ 'type ' => Subselect::class,
230
246
'attribute ' => 'qty ' ,
231
247
'operator ' => '== ' ,
232
248
'value ' => '1 ' ,
233
249
'is_value_processed ' => null ,
234
250
'aggregator ' => 'all ' ,
235
- 'conditions ' =>
236
- [
251
+ 'conditions ' => [
237
252
[
238
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product::class,
253
+ 'type ' => Product::class,
239
254
'attribute ' => 'category_ids ' ,
240
255
'attribute_scope ' => 'parent ' ,
241
256
'operator ' => '!= ' ,
@@ -251,16 +266,15 @@ public function conditionsDataProvider(): array
251
266
'Category (Parent Only) is "Default Category" ' => [
252
267
'conditions ' => [
253
268
[
254
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product \ Subselect::class,
269
+ 'type ' => Subselect::class,
255
270
'attribute ' => 'qty ' ,
256
271
'operator ' => '== ' ,
257
272
'value ' => '1 ' ,
258
273
'is_value_processed ' => null ,
259
274
'aggregator ' => 'all ' ,
260
- 'conditions ' =>
261
- [
275
+ 'conditions ' => [
262
276
[
263
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product::class,
277
+ 'type ' => Product::class,
264
278
'attribute ' => 'category_ids ' ,
265
279
'attribute_scope ' => 'parent ' ,
266
280
'operator ' => '== ' ,
@@ -276,14 +290,13 @@ public function conditionsDataProvider(): array
276
290
'Category (Parent Only) is not "Default Category" ' => [
277
291
'conditions ' => [
278
292
[
279
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product \ Found::class,
293
+ 'type ' => Found::class,
280
294
'value ' => '1 ' ,
281
295
'is_value_processed ' => null ,
282
296
'aggregator ' => 'all ' ,
283
- 'conditions ' =>
284
- [
297
+ 'conditions ' => [
285
298
[
286
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product::class,
299
+ 'type ' => Product::class,
287
300
'attribute ' => 'category_ids ' ,
288
301
'attribute_scope ' => 'parent ' ,
289
302
'operator ' => '!= ' ,
@@ -299,14 +312,13 @@ public function conditionsDataProvider(): array
299
312
'Category (Parent Only) is "Default Category" ' => [
300
313
'conditions ' => [
301
314
[
302
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product \ Found::class,
315
+ 'type ' => Found::class,
303
316
'value ' => '1 ' ,
304
317
'is_value_processed ' => null ,
305
318
'aggregator ' => 'all ' ,
306
- 'conditions ' =>
307
- [
319
+ 'conditions ' => [
308
320
[
309
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product::class,
321
+ 'type ' => Product::class,
310
322
'attribute ' => 'category_ids ' ,
311
323
'attribute_scope ' => 'parent ' ,
312
324
'operator ' => '== ' ,
@@ -322,14 +334,13 @@ public function conditionsDataProvider(): array
322
334
'Category (Parent Only) is "Default Category" ' => [
323
335
'conditions ' => [
324
336
[
325
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product \ Found::class,
337
+ 'type ' => Found::class,
326
338
'value ' => '0 ' ,
327
339
'is_value_processed ' => null ,
328
340
'aggregator ' => 'all ' ,
329
- 'conditions ' =>
330
- [
341
+ 'conditions ' => [
331
342
[
332
- 'type ' => \ Magento \ SalesRule \ Model \ Rule \ Condition \ Product::class,
343
+ 'type ' => Product::class,
333
344
'attribute ' => 'category_ids ' ,
334
345
'attribute_scope ' => 'parent ' ,
335
346
'operator ' => '== ' ,
@@ -343,4 +354,80 @@ public function conditionsDataProvider(): array
343
354
],
344
355
];
345
356
}
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
+ }
346
433
}
0 commit comments