|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Quote\Guest; |
| 9 | + |
| 10 | +use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test Apply Coupons to Cart functionality for guest |
| 16 | + */ |
| 17 | +class ApplyCouponsToCartTest extends GraphQlAbstract |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var GetMaskedQuoteIdByReservedOrderId |
| 21 | + */ |
| 22 | + private $getMaskedQuoteIdByReservedOrderId; |
| 23 | + |
| 24 | + protected function setUp() |
| 25 | + { |
| 26 | + $objectManager = Bootstrap::getObjectManager(); |
| 27 | + $this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 32 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 33 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 34 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 35 | + */ |
| 36 | + public function testApplyCouponsToCart() |
| 37 | + { |
| 38 | + $couponCode = '2?ds5!2d'; |
| 39 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 40 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 41 | + $response = $this->graphQlMutation($query); |
| 42 | + |
| 43 | + self::assertArrayHasKey('applyCouponToCart', $response); |
| 44 | + self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupons'][0]['code']); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 49 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 50 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 51 | + * @expectedException \Exception |
| 52 | + * @expectedExceptionMessage Cart does not contain products. |
| 53 | + */ |
| 54 | + public function testApplyCouponsToCartWithoutItems() |
| 55 | + { |
| 56 | + $couponCode = '2?ds5!2d'; |
| 57 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 58 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 59 | + |
| 60 | + $this->graphQlMutation($query); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * _security |
| 65 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 66 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 67 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 68 | + * @expectedException \Exception |
| 69 | + */ |
| 70 | + public function testApplyCouponsToCustomerCart() |
| 71 | + { |
| 72 | + $couponCode = '2?ds5!2d'; |
| 73 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 74 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 75 | + |
| 76 | + self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"'); |
| 77 | + $this->graphQlMutation($query); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 82 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 83 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 84 | + * @expectedException \Exception |
| 85 | + * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again. |
| 86 | + */ |
| 87 | + public function testApplyNonExistentCouponToCart() |
| 88 | + { |
| 89 | + $couponCode = 'non_existent_coupon_code'; |
| 90 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 91 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 92 | + |
| 93 | + $this->graphQlMutation($query); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 98 | + * @expectedException \Exception |
| 99 | + */ |
| 100 | + public function testApplyCouponsToNonExistentCart() |
| 101 | + { |
| 102 | + $couponCode = '2?ds5!2d'; |
| 103 | + $maskedQuoteId = 'non_existent_masked_id'; |
| 104 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 105 | + |
| 106 | + self::expectExceptionMessage('Could not find a cart with ID "' . $maskedQuoteId . '"'); |
| 107 | + $this->graphQlMutation($query); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Products in cart don't fit to the coupon |
| 112 | + * |
| 113 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 114 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 115 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 116 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 117 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/restrict_coupon_usage_for_simple_product.php |
| 118 | + * @expectedException \Exception |
| 119 | + * @expectedExceptionMessage The coupon code isn't valid. Verify the code and try again. |
| 120 | + */ |
| 121 | + public function testApplyCouponsWhichIsNotApplicable() |
| 122 | + { |
| 123 | + $couponCode = '2?ds5!2d'; |
| 124 | + $maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote'); |
| 125 | + $query = $this->getQuery($maskedQuoteId, $couponCode); |
| 126 | + |
| 127 | + $this->graphQlMutation($query); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param string $input |
| 132 | + * @param string $message |
| 133 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 134 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php |
| 135 | + * @dataProvider dataProviderUpdateWithMissedRequiredParameters |
| 136 | + * @expectedException \Exception |
| 137 | + */ |
| 138 | + public function testApplyCouponsWithMissedRequiredParameters(string $input, string $message) |
| 139 | + { |
| 140 | + $query = <<<QUERY |
| 141 | +mutation { |
| 142 | + applyCouponToCart(input: {{$input}}) { |
| 143 | + cart { |
| 144 | + applied_coupons { |
| 145 | + code |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | +QUERY; |
| 151 | + |
| 152 | + $this->expectExceptionMessage($message); |
| 153 | + $this->graphQlMutation($query); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @return array |
| 158 | + */ |
| 159 | + public function dataProviderUpdateWithMissedRequiredParameters(): array |
| 160 | + { |
| 161 | + return [ |
| 162 | + 'missed_cart_id' => [ |
| 163 | + 'coupon_code: "test"', |
| 164 | + 'Required parameter "cart_id" is missing' |
| 165 | + ], |
| 166 | + 'missed_coupon_code' => [ |
| 167 | + 'cart_id: "test_quote"', |
| 168 | + 'Required parameter "coupon_code" is missing' |
| 169 | + ], |
| 170 | + ]; |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * @param string $maskedQuoteId |
| 175 | + * @param string $couponCode |
| 176 | + * @return string |
| 177 | + */ |
| 178 | + private function getQuery(string $maskedQuoteId, string $couponCode): string |
| 179 | + { |
| 180 | + return <<<QUERY |
| 181 | +mutation { |
| 182 | + applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) { |
| 183 | + cart { |
| 184 | + applied_coupons { |
| 185 | + code |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | +} |
| 190 | +QUERY; |
| 191 | + } |
| 192 | +} |
0 commit comments