|
| 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\Customer; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\AuthenticationException; |
| 11 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 12 | +use Magento\Quote\Model\Quote; |
| 13 | +use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface; |
| 14 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 15 | +use Magento\TestFramework\Helper\Bootstrap; |
| 16 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 17 | + |
| 18 | +/** |
| 19 | + * Check removing of the coupon from customer quotes |
| 20 | + */ |
| 21 | +class RemoveCouponFromCartTest extends GraphQlAbstract |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var QuoteResource |
| 25 | + */ |
| 26 | + private $quoteResource; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var Quote |
| 30 | + */ |
| 31 | + private $quote; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var QuoteIdToMaskedQuoteIdInterface |
| 35 | + */ |
| 36 | + private $quoteIdToMaskedId; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var CustomerTokenServiceInterface |
| 40 | + */ |
| 41 | + private $customerTokenService; |
| 42 | + |
| 43 | + protected function setUp() |
| 44 | + { |
| 45 | + $objectManager = Bootstrap::getObjectManager(); |
| 46 | + $this->quoteResource = $objectManager->create(QuoteResource::class); |
| 47 | + $this->quote = $objectManager->create(Quote::class); |
| 48 | + $this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class); |
| 49 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 54 | + * @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php |
| 55 | + * @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php |
| 56 | + */ |
| 57 | + public function testRemoveCouponFromCart() |
| 58 | + { |
| 59 | + $couponCode = '2?ds5!2d'; |
| 60 | + |
| 61 | + /* Apply coupon to the quote */ |
| 62 | + $this->quoteResource->load( |
| 63 | + $this->quote, |
| 64 | + 'test_order_with_simple_product_without_address', |
| 65 | + 'reserved_order_id' |
| 66 | + ); |
| 67 | + $maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId()); |
| 68 | + |
| 69 | + $this->quote->setCustomerId(1); |
| 70 | + $this->quoteResource->save($this->quote); |
| 71 | + |
| 72 | + $queryHeaders = $this-> prepareAuthorizationHeaders( '[email protected]', 'password'); |
| 73 | + $query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode); |
| 74 | + $this->graphQlQuery($query, [], '', $queryHeaders); |
| 75 | + |
| 76 | + /* Remove coupon from quote */ |
| 77 | + $query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId); |
| 78 | + $response = $this->graphQlQuery($query, [], '', $queryHeaders); |
| 79 | + |
| 80 | + self::assertArrayHasKey('removeCouponFromCart', $response); |
| 81 | + self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Retrieve customer authorization headers |
| 86 | + * |
| 87 | + * @param string $email |
| 88 | + * @param string $password |
| 89 | + * @return array |
| 90 | + * @throws AuthenticationException |
| 91 | + */ |
| 92 | + private function prepareAuthorizationHeaders(string $email, string $password): array |
| 93 | + { |
| 94 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($email, $password); |
| 95 | + |
| 96 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Retrieve add coupon GraphQL query |
| 101 | + * |
| 102 | + * @param string $maskedQuoteId |
| 103 | + * @param string $couponCode |
| 104 | + * @return string |
| 105 | + */ |
| 106 | + private function prepareAddCouponRequestQuery(string $maskedQuoteId, string $couponCode): string |
| 107 | + { |
| 108 | + return <<<QUERY |
| 109 | +mutation { |
| 110 | + applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) { |
| 111 | + cart { |
| 112 | + applied_coupon { |
| 113 | + code |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +} |
| 118 | +QUERY; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Retrieve remove coupon GraphQL query |
| 123 | + * |
| 124 | + * @param string $maskedQuoteId |
| 125 | + * @return string |
| 126 | + */ |
| 127 | + private function prepareRemoveCouponRequestQuery(string $maskedQuoteId): string |
| 128 | + { |
| 129 | + return <<<QUERY |
| 130 | +mutation { |
| 131 | + removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) { |
| 132 | + cart { |
| 133 | + applied_coupon { |
| 134 | + code |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +
|
| 140 | +QUERY; |
| 141 | + } |
| 142 | +} |
0 commit comments