Skip to content

Commit b93da7a

Browse files
author
Roman Glushko
committed
#481 Created a webapi test for removing coupon from the guest quote
1 parent 3666e03 commit b93da7a

File tree

1 file changed

+143
-0
lines changed

1 file changed

+143
-0
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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\Quote\Model\Quote;
11+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
12+
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test for getting cart information
18+
*/
19+
class RemoveCouponFromCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var QuoteResource
23+
*/
24+
private $quoteResource;
25+
26+
/**
27+
* @var Quote
28+
*/
29+
private $quote;
30+
31+
/**
32+
* @var QuoteIdToMaskedQuoteIdInterface
33+
*/
34+
private $quoteIdToMaskedId;
35+
36+
protected function setUp()
37+
{
38+
$objectManager = Bootstrap::getObjectManager();
39+
$this->quoteResource = $objectManager->create(QuoteResource::class);
40+
$this->quote = $objectManager->create(Quote::class);
41+
$this->quoteIdToMaskedId = $objectManager->create(QuoteIdToMaskedQuoteIdInterface::class);
42+
}
43+
44+
/**
45+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
46+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
47+
*/
48+
public function testRemoveCouponFromCart()
49+
{
50+
$couponCode = '2?ds5!2d';
51+
52+
/* Apply coupon to the quote */
53+
$this->quoteResource->load(
54+
$this->quote,
55+
'test_order_with_simple_product_without_address',
56+
'reserved_order_id'
57+
);
58+
59+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
60+
61+
$query = $this->prepareAddCouponRequestQuery($maskedQuoteId, $couponCode);
62+
$this->graphQlQuery($query);
63+
64+
/* Remove coupon from quote */
65+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
66+
$response = $this->graphQlQuery($query);
67+
68+
self::assertArrayHasKey('removeCouponFromCart', $response);
69+
self::assertNull($response['removeCouponFromCart']['cart']['applied_coupon']['code']);
70+
}
71+
72+
/**
73+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
74+
* @magentoApiDataFixture Magento/SalesRule/_files/coupon_code_with_wildcard.php
75+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
76+
*/
77+
public function testRemoveCouponFromCustomerCart()
78+
{
79+
$this->quoteResource->load(
80+
$this->quote,
81+
'test_order_with_simple_product_without_address',
82+
'reserved_order_id'
83+
);
84+
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$this->quote->getId());
85+
86+
$this->quote->setCustomerId(1);
87+
$this->quoteResource->save($this->quote);
88+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
89+
90+
self::expectExceptionMessage('The current user cannot perform operations on cart "' . $maskedQuoteId . '"');
91+
$this->graphQlQuery($query);
92+
}
93+
94+
public function testRemoveCouponFromNonExistentCart()
95+
{
96+
$maskedQuoteId = '1234000000099912';
97+
98+
/* Remove coupon from quote */
99+
$query = $this->prepareRemoveCouponRequestQuery($maskedQuoteId);
100+
101+
self::expectExceptionMessage('Could not find a cart with ID "' . $maskedQuoteId. '"');
102+
$this->graphQlQuery($query);
103+
}
104+
105+
/**
106+
* @param string $maskedQuoteId
107+
* @param string $couponCode
108+
* @return string
109+
*/
110+
private function prepareAddCouponRequestQuery(string $maskedQuoteId, string $couponCode): string
111+
{
112+
return <<<QUERY
113+
mutation {
114+
applyCouponToCart(input: {cart_id: "$maskedQuoteId", coupon_code: "$couponCode"}) {
115+
cart {
116+
applied_coupon {
117+
code
118+
}
119+
}
120+
}
121+
}
122+
QUERY;
123+
}
124+
125+
/**
126+
* @param string $maskedQuoteId
127+
* @return string
128+
*/
129+
private function prepareRemoveCouponRequestQuery(string $maskedQuoteId): string
130+
{
131+
return <<<QUERY
132+
mutation {
133+
removeCouponFromCart(input: {cart_id: "$maskedQuoteId"}) {
134+
cart {
135+
applied_coupon {
136+
code
137+
}
138+
}
139+
}
140+
}
141+
QUERY;
142+
}
143+
}

0 commit comments

Comments
 (0)