Skip to content

Commit 111ecf3

Browse files
committed
#31331:[GraphQl] Add wishlist item to cart Implementation
Added Test for wishlist items add to cart
1 parent 87827ee commit 111ecf3

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
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\Wishlist;
9+
10+
use Exception;
11+
use Magento\Framework\Exception\AuthenticationException;
12+
use Magento\Integration\Api\CustomerTokenServiceInterface;
13+
use Magento\TestFramework\Helper\Bootstrap;
14+
use Magento\TestFramework\TestCase\GraphQlAbstract;
15+
16+
/**
17+
* Test coverage for add requisition list items to cart
18+
*/
19+
class AddWishlistItemsToCartTest extends GraphQlAbstract
20+
{
21+
/**
22+
* @var CustomerTokenServiceInterface
23+
*/
24+
private $customerTokenService;
25+
26+
/**
27+
* Set Up
28+
*/
29+
protected function setUp(): void
30+
{
31+
$objectManager = Bootstrap::getObjectManager();
32+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
33+
}
34+
35+
/**
36+
* @magentoConfigFixture default_store wishlist/general/active 1
37+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
38+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
39+
*/
40+
public function testAddItemsToCart(): void
41+
{
42+
$wishlist = $this->getWishlist();
43+
$customerWishlist = $wishlist['customer']['wishlists'][0];
44+
$wishlistUid = $customerWishlist['uid'];
45+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
46+
$itemUid = $wishlistItem['uid'];
47+
48+
$query = $this->getQuery($wishlistUid, $itemUid);
49+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
50+
51+
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
52+
$this->assertArrayHasKey('status', $response['addWishlistItemsToCart']);
53+
$this->assertEquals($response['addWishlistItemsToCart']['status'], 1);
54+
}
55+
56+
/**
57+
* @magentoConfigFixture default_store wishlist/general/active 1
58+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
59+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
60+
*/
61+
public function testAddItemsToCartForInvalidUser(): void
62+
{
63+
$this->expectException(Exception::class);
64+
$this->expectExceptionMessage("The account sign-in was incorrect or your account is disabled temporarily. Please wait and try again later.");
65+
66+
$wishlist = $this->getWishlist();
67+
$customerWishlist = $wishlist['customer']['wishlists'][0];
68+
$wishlistUid = $customerWishlist['uid'];
69+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
70+
$itemUid = $wishlistItem['uid'];
71+
72+
$query = $this->getQuery($wishlistUid, $itemUid);
73+
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@example.com', 'password'));
74+
}
75+
76+
/**
77+
* @magentoConfigFixture default_store wishlist/general/active 1
78+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
79+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
80+
*/
81+
public function testAddItemsToCartForGuestUser(): void
82+
{
83+
$this->expectException(Exception::class);
84+
$this->expectExceptionMessage('The current user cannot perform operations on wishlist');
85+
86+
$wishlist = $this->getWishlist();
87+
$customerWishlist = $wishlist['customer']['wishlists'][0];
88+
$wishlistUid = $customerWishlist['uid'];
89+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
90+
$itemUid = $wishlistItem['uid'];
91+
92+
$query = $this->getQuery($wishlistUid, $itemUid);
93+
94+
$this->graphQlMutation($query, [], '', ['Authorization' => 'Bearer test_token']);
95+
}
96+
97+
/**
98+
* @magentoConfigFixture default_store wishlist/general/active 1
99+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
100+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
101+
*/
102+
public function testAddItemsToCartWithoutId(): void
103+
{
104+
$this->expectException(Exception::class);
105+
$this->expectExceptionMessage('"wishlistUid" value should be specified');
106+
107+
$wishlistUid = '';
108+
$wishlist = $this->getWishlist();
109+
$customerWishlist = $wishlist['customer']['wishlists'][0];
110+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
111+
$itemUid = $wishlistItem['uid'];
112+
$query = $this->getQuery($wishlistUid, $itemUid);
113+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
114+
}
115+
116+
/**
117+
* @magentoConfigFixture default_store wishlist/general/active 1
118+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
119+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
120+
*/
121+
public function testAddItemsToCartWithInvalidId(): void
122+
{
123+
$wishlistUid = '9999';
124+
125+
$this->expectException(Exception::class);
126+
$this->expectExceptionMessage('The wishlist was not found.');
127+
128+
$wishlistUid = base64_encode((string) $wishlistUid);
129+
$wishlist = $this->getWishlist();
130+
$customerWishlist = $wishlist['customer']['wishlists'][0];
131+
$wishlistItem = $customerWishlist['items_v2']['items'][0];
132+
$itemUid = $wishlistItem['uid'];
133+
134+
$query = $this->getQuery($wishlistUid, $itemUid);
135+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
136+
}
137+
138+
/**
139+
* Authentication header map
140+
*
141+
* @param string $username
142+
* @param string $password
143+
*
144+
* @return array
145+
*
146+
* @throws AuthenticationException
147+
*/
148+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
149+
{
150+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
151+
152+
return ['Authorization' => 'Bearer ' . $customerToken];
153+
}
154+
155+
/**
156+
* Returns GraphQl mutation string
157+
*
158+
* @param string $wishlistUid
159+
* @param string $itemId
160+
* @return string
161+
*/
162+
private function getQuery(
163+
string $wishlistUid,
164+
string $itemId
165+
): string {
166+
return <<<MUTATION
167+
mutation {
168+
addWishlistItemsToCart
169+
(
170+
wishlistUid: "{$wishlistUid}"
171+
wishlistItemUids: ["{$itemId}"]
172+
) {
173+
status
174+
add_wishlist_items_to_cart_user_errors{
175+
message
176+
code
177+
}
178+
}
179+
}
180+
MUTATION;
181+
}
182+
183+
/**
184+
* Get wishlist result
185+
*
186+
* @param string $username
187+
* @return array
188+
*
189+
* @throws Exception
190+
*/
191+
public function getWishlist(string $username = 'customer@example.com'): array
192+
{
193+
return $this->graphQlQuery($this->getCustomerWishlistQuery(), [], '', $this->getHeaderMap($username));
194+
}
195+
196+
/**
197+
* Get customer wishlist query
198+
*
199+
* @return string
200+
*/
201+
private function getCustomerWishlistQuery(): string
202+
{
203+
return <<<QUERY
204+
query {
205+
customer {
206+
wishlists {
207+
id
208+
uid
209+
items_count
210+
sharing_code
211+
updated_at
212+
items_v2 {
213+
items {
214+
id
215+
uid
216+
quantity
217+
description
218+
product {
219+
sku
220+
}
221+
}
222+
}
223+
}
224+
}
225+
}
226+
QUERY;
227+
}
228+
}

0 commit comments

Comments
 (0)