|
16 | 16 | use Magento\TestFramework\TestCase\GraphQlAbstract;
|
17 | 17 | use Magento\Tax\Model\ClassModel as TaxClassModel;
|
18 | 18 | use Magento\Tax\Model\ResourceModel\TaxClass\CollectionFactory as TaxClassCollectionFactory;
|
| 19 | +use Magento\SalesRule\Api\RuleRepositoryInterface; |
19 | 20 |
|
20 | 21 | /**
|
21 | 22 | * Test cases for applying cart promotions to items in cart
|
@@ -372,6 +373,73 @@ public function testCartPromotionsWithNoRuleLabels()
|
372 | 373 | }
|
373 | 374 | }
|
374 | 375 |
|
| 376 | + /** |
| 377 | + * Test fixed discount cannot be higher than products price |
| 378 | + * |
| 379 | + * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php |
| 380 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 381 | + */ |
| 382 | + public function testCartPromotionsFixedDiscountNotHigherThanProductsPrice() |
| 383 | + { |
| 384 | + /** @var ProductRepositoryInterface $productRepository */ |
| 385 | + $productRepository = Bootstrap::getObjectManager()->get(ProductRepositoryInterface::class); |
| 386 | + /** @var Product $prod2 */ |
| 387 | + $prod1 = $productRepository->get('simple1'); |
| 388 | + $prod2 = $productRepository->get('simple2'); |
| 389 | + $productsInCart = [$prod1, $prod2]; |
| 390 | + $skus =['simple1', 'simple2']; |
| 391 | + $qty = 2; |
| 392 | + $sumOfPricesForBothProducts = 43.96; |
| 393 | + $couponCode = '2?ds5!2d'; |
| 394 | + /** @var RuleRepositoryInterface $ruleRepository */ |
| 395 | + $ruleRepository = Bootstrap::getObjectManager()->get(RuleRepositoryInterface::class); |
| 396 | + /** @var Collection $ruleCollection */ |
| 397 | + $ruleCollection = Bootstrap::getObjectManager()->get(Collection::class); |
| 398 | + $ruleLabels = []; |
| 399 | + /** @var Rule $rule */ |
| 400 | + foreach ($ruleCollection as $rule) { |
| 401 | + $ruleLabels = $rule->getStoreLabels(); |
| 402 | + $salesRule = $ruleRepository->getById($rule->getRuleId()); |
| 403 | + $salesRule->setDiscountAmount(50); |
| 404 | + $ruleRepository->save($salesRule); |
| 405 | + } |
| 406 | + $cartId = $this->createEmptyCart(); |
| 407 | + $this->addMultipleSimpleProductsToCart($cartId, $qty, $skus[0], $skus[1]); |
| 408 | + $this->applyCouponsToCart($cartId, $couponCode); |
| 409 | + $query = $this->getCartItemPricesQuery($cartId); |
| 410 | + $response = $this->graphQlMutation($query); |
| 411 | + |
| 412 | + $this->assertCount(2, $response['cart']['items']); |
| 413 | + $productsInResponse = array_map(null, $response['cart']['items'], $productsInCart); |
| 414 | + $count = count($productsInCart); |
| 415 | + for ($itemIndex = 0; $itemIndex < $count; $itemIndex++) { |
| 416 | + $this->assertNotEmpty($productsInResponse[$itemIndex]); |
| 417 | + $rowTotal = ($productsInCart[$itemIndex]->getSpecialPrice()*$qty); |
| 418 | + $this->assertResponseFields( |
| 419 | + $productsInResponse[$itemIndex][0], |
| 420 | + [ |
| 421 | + 'quantity' => $qty, |
| 422 | + 'prices' => [ |
| 423 | + 'row_total' => ['value' => $rowTotal], |
| 424 | + 'row_total_including_tax' => ['value' => $rowTotal], |
| 425 | + 'total_item_discount' => ['value' => $rowTotal], |
| 426 | + 'discounts' => [ |
| 427 | + 0 =>[ |
| 428 | + 'amount' => |
| 429 | + ['value' => $rowTotal], |
| 430 | + 'label' => $ruleLabels[0] |
| 431 | + ] |
| 432 | + ] |
| 433 | + ], |
| 434 | + ] |
| 435 | + ); |
| 436 | + } |
| 437 | + $this->assertEquals( |
| 438 | + $response['cart']['prices']['discounts'][0]['amount']['value'], |
| 439 | + $sumOfPricesForBothProducts |
| 440 | + ); |
| 441 | + } |
| 442 | + |
375 | 443 | /**
|
376 | 444 | * Apply coupon to the cart
|
377 | 445 | *
|
@@ -426,7 +494,6 @@ private function getCartItemPricesQuery(string $cartId): string
|
426 | 494 | discounts{
|
427 | 495 | amount{value}
|
428 | 496 | }
|
429 |
| - |
430 | 497 | }
|
431 | 498 | }
|
432 | 499 | }
|
@@ -460,27 +527,27 @@ private function addMultipleSimpleProductsToCart(string $cartId, int $qty, strin
|
460 | 527 | $query = <<<QUERY
|
461 | 528 | mutation {
|
462 | 529 | addSimpleProductsToCart(input: {
|
463 |
| - cart_id: "{$cartId}", |
| 530 | + cart_id: "{$cartId}", |
464 | 531 | cart_items: [
|
465 | 532 | {
|
466 | 533 | data: {
|
467 | 534 | quantity: $qty
|
468 | 535 | sku: "$sku1"
|
469 | 536 | }
|
470 |
| - } |
| 537 | + } |
471 | 538 | {
|
472 | 539 | data: {
|
473 | 540 | quantity: $qty
|
474 | 541 | sku: "$sku2"
|
475 | 542 | }
|
476 |
| - } |
| 543 | + } |
477 | 544 | ]
|
478 | 545 | }
|
479 | 546 | ) {
|
480 | 547 | cart {
|
481 | 548 | items {
|
482 | 549 | product{sku}
|
483 |
| - quantity |
| 550 | + quantity |
484 | 551 | }
|
485 | 552 | }
|
486 | 553 | }
|
|
0 commit comments