CouponTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Quote;
  8. use Magento\Quote\Model\Quote;
  9. use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
  10. use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
  11. use Magento\TestFramework\Helper\Bootstrap;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. /**
  14. * Test for adding/removing shopping cart coupon codes
  15. */
  16. class CouponTest extends GraphQlAbstract
  17. {
  18. /**
  19. * @var QuoteResource
  20. */
  21. private $quoteResource;
  22. /**
  23. * @var Quote
  24. */
  25. private $quote;
  26. /**
  27. * @var QuoteIdToMaskedQuoteIdInterface
  28. */
  29. private $quoteIdToMaskedId;
  30. protected function setUp()
  31. {
  32. $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/434');
  33. $objectManager = Bootstrap::getObjectManager();
  34. $this->quoteResource = $objectManager->create(QuoteResource::class);
  35. $this->quote = $objectManager->create(Quote::class);
  36. $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
  37. }
  38. /**
  39. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  40. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  41. */
  42. public function testApplyCouponToGuestCartWithItems()
  43. {
  44. $couponCode = '2?ds5!2d';
  45. $this->quoteResource->load(
  46. $this->quote,
  47. 'test_order_with_simple_product_without_address',
  48. 'reserved_order_id'
  49. );
  50. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  51. $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
  52. $response = $this->graphQlQuery($query);
  53. self::assertArrayHasKey("applyCouponToCart", $response);
  54. self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupon']['code']);
  55. }
  56. /**
  57. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  58. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  59. */
  60. public function testApplyCouponTwice()
  61. {
  62. $couponCode = '2?ds5!2d';
  63. $this->quoteResource->load(
  64. $this->quote,
  65. 'test_order_with_simple_product_without_address',
  66. 'reserved_order_id'
  67. );
  68. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  69. $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
  70. $response = $this->graphQlQuery($query);
  71. self::assertArrayHasKey("applyCouponToCart", $response);
  72. self::assertEquals($couponCode, $response['applyCouponToCart']['cart']['applied_coupon']['code']);
  73. self::expectExceptionMessage('A coupon is already applied to the cart. Please remove it to apply another');
  74. $this->graphQlQuery($query);
  75. }
  76. /**
  77. * @magentoApiDataFixture Magento/Checkout/_files/active_quote.php
  78. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  79. */
  80. public function testApplyCouponToCartWithNoItems()
  81. {
  82. $this->markTestIncomplete('https://github.com/magento/graphql-ce/issues/191');
  83. $couponCode = '2?ds5!2d';
  84. $this->quoteResource->load($this->quote, 'test_order_1', 'reserved_order_id');
  85. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  86. $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
  87. self::expectExceptionMessageRegExp('/Cart doesn\'t contain products/');
  88. $this->graphQlQuery($query);
  89. }
  90. /**
  91. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  92. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  93. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  94. */
  95. public function testGuestCustomerAttemptToChangeCustomerCart()
  96. {
  97. $couponCode = '2?ds5!2d';
  98. $this->quoteResource->load(
  99. $this->quote,
  100. 'test_order_with_simple_product_without_address',
  101. 'reserved_order_id'
  102. );
  103. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  104. $this->quote->setCustomerId(1);
  105. $this->quoteResource->save($this->quote);
  106. $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
  107. self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
  108. $this->graphQlQuery($query);
  109. }
  110. /**
  111. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  112. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  113. */
  114. public function testRemoveCoupon()
  115. {
  116. $couponCode = '2?ds5!2d';
  117. /* Apply coupon to the quote */
  118. $this->quoteResource->load(
  119. $this->quote,
  120. 'test_order_with_simple_product_without_address',
  121. 'reserved_order_id'
  122. );
  123. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  124. $this->quoteResource->load(
  125. $this->quote,
  126. 'test_order_with_simple_product_without_address',
  127. 'reserved_order_id'
  128. );
  129. $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
  130. $this->graphQlQuery($query);
  131. /* Remove coupon from quote */
  132. $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
  133. $response = $this->graphQlQuery($query);
  134. self::assertArrayHasKey('removeCouponFromCart', $response);
  135. self::assertSame('', $response['removeCouponFromCart']['cart']['applied_coupon']['code']);
  136. }
  137. /**
  138. * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
  139. * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
  140. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  141. */
  142. public function testRemoveCouponFromCustomerCartByGuest()
  143. {
  144. $this->quoteResource->load(
  145. $this->quote,
  146. 'test_order_with_simple_product_without_address',
  147. 'reserved_order_id'
  148. );
  149. $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
  150. $this->quoteResource->load(
  151. $this->quote,
  152. 'test_order_with_simple_product_without_address',
  153. 'reserved_order_id'
  154. );
  155. $this->quote->setCustomerId(1);
  156. $this->quoteResource->save($this->quote);
  157. $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
  158. self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
  159. $this->graphQlQuery($query);
  160. }
  161. /**
  162. * @param string $maskedQuoteId
  163. * @param string $couponCode
  164. * @return string
  165. */
  166. private function prepareAddCouponRequestQuery(string $maskedQuoteId, string $couponCode): string
  167. {
  168. return <<<QUERY
  169. mutation {
  170. applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
  171. cart {
  172. applied_coupon {
  173. code
  174. }
  175. }
  176. }
  177. }
  178. QUERY;
  179. }
  180. /**
  181. * @param string $maskedQuoteId
  182. * @return string
  183. */
  184. private function prepareRemoveCouponRequestQuery(string $maskedQuoteId): string
  185. {
  186. return <<<QUERY
  187. mutation {
  188. removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
  189. cart {
  190. applied_coupon {
  191. code
  192. }
  193. }
  194. }
  195. }
  196. QUERY;
  197. }
  198. }