Skip to content

Commit 6cdc167

Browse files
author
Prabhu Ram
committed
PWA-1379: [GraphQl] AddWishlistItemsToCartOutput.status doesn't return the correct status if items are not added to cart
- Added fix, minor schema changes and tests
1 parent 50efec2 commit 6cdc167

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

app/code/Magento/WishlistGraphQl/Model/Resolver/Wishlist/AddToCart.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public function resolve(
147147

148148
$collection = $this->getWishlistItems($wishlist, $itemIds);
149149

150+
if (!empty($itemIds)) {
151+
$unknownItemIds = array_diff($itemIds, array_keys($collection->getItems()));
152+
if (!empty($unknownItemIds)) {
153+
throw new GraphQlInputException(__('The wishlist item ids "'.implode(',', $unknownItemIds).'" were not found.'));
154+
}
155+
}
150156
$maskedCartId = $this->createEmptyCartForCustomer->execute($customerId);
151157

152158
$cartErrors = [];
@@ -180,6 +186,7 @@ function (Error $error) {
180186
$wishlist->save();
181187
}
182188
return [
189+
'wishlist' => $this->wishlistDataMapper->map($wishlist),
183190
'status' => empty($cartErrors) ? true : false,
184191
'add_wishlist_items_to_cart_user_errors' => $cartErrors,
185192
];

app/code/Magento/WishlistGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type Mutation {
6767
}
6868

6969
type AddWishlistItemsToCartOutput {
70+
wishlist: Wishlist! @doc(description: "Contains the wish list with all items that were successfully added")
7071
status: Boolean! @doc(description: "Indicates whether the attempt to add items to the customer's cart was successful")
7172
add_wishlist_items_to_cart_user_errors: [CartUserInputError!]! @doc(description: "An array of errors encountered while adding products to the customer's cart")
7273
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Wishlist/AddWishlistItemsToCartTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,31 @@ public function testAddItemsToCart(): void
4949
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
5050

5151
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
52+
$wishlistAfterAddingToCart = $response['addWishlistItemsToCart']['wishlist'];
53+
$wishlistItems = $wishlistAfterAddingToCart['items_v2']['items'];
54+
$this->assertEmpty($wishlistItems);
55+
$this->assertArrayHasKey('status', $response['addWishlistItemsToCart']);
56+
$this->assertEquals($response['addWishlistItemsToCart']['status'], true);
57+
}
58+
59+
/**
60+
* @magentoConfigFixture default_store wishlist/general/active 1
61+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
62+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_multiple_products.php
63+
*/
64+
public function testAddAllItemsToCart(): void
65+
{
66+
$wishlist = $this->getWishlist();
67+
$customerWishlist = $wishlist['customer']['wishlists'][0];
68+
$wishlistId = $customerWishlist['id'];
69+
70+
$query = $this->getAddAllItemsToCartQuery($wishlistId);
71+
$response = $this->graphQlMutation($query, [], '', $this->getHeaderMap());
72+
73+
$this->assertArrayHasKey('addWishlistItemsToCart', $response);
74+
$wishlistAfterAddingToCart = $response['addWishlistItemsToCart']['wishlist'];
75+
$wishlistItems = $wishlistAfterAddingToCart['items_v2']['items'];
76+
$this->assertEmpty($wishlistItems);
5277
$this->assertArrayHasKey('status', $response['addWishlistItemsToCart']);
5378
$this->assertEquals($response['addWishlistItemsToCart']['status'], true);
5479
}
@@ -134,6 +159,25 @@ public function testAddItemsToCartWithInvalidId(): void
134159
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
135160
}
136161

162+
/**
163+
* @magentoConfigFixture default_store wishlist/general/active 1
164+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
165+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_with_simple_product.php
166+
*/
167+
public function testAddItemsToCartWithInvalidItemId(): void
168+
{
169+
$itemId = '9999';
170+
171+
$this->expectException(Exception::class);
172+
$this->expectExceptionMessage('The wishlist item ids "9999" were not found.');
173+
174+
$wishlist = $this->getWishlist();
175+
$customerWishlist = $wishlist['customer']['wishlists'][0];
176+
177+
$query = $this->getQuery($customerWishlist['id'], $itemId);
178+
$this->graphQlMutation($query, [], '', $this->getHeaderMap());
179+
}
180+
137181
/**
138182
* Authentication header map
139183
*
@@ -170,6 +214,46 @@ private function getQuery(
170214
wishlistItemIds: ["{$itemId}"]
171215
) {
172216
status
217+
wishlist {
218+
items_v2 {
219+
items {
220+
id
221+
}
222+
}
223+
}
224+
add_wishlist_items_to_cart_user_errors{
225+
message
226+
code
227+
}
228+
}
229+
}
230+
MUTATION;
231+
}
232+
233+
/**
234+
* Returns GraphQl mutation string
235+
*
236+
* @param string $wishlistId
237+
* @param string $itemId
238+
* @return string
239+
*/
240+
private function getAddAllItemsToCartQuery(
241+
string $wishlistId
242+
): string {
243+
return <<<MUTATION
244+
mutation {
245+
addWishlistItemsToCart
246+
(
247+
wishlistId: "{$wishlistId}"
248+
) {
249+
status
250+
wishlist {
251+
items_v2 {
252+
items {
253+
id
254+
}
255+
}
256+
}
173257
add_wishlist_items_to_cart_user_errors{
174258
message
175259
code
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
use Magento\Catalog\Api\ProductRepositoryInterface;
9+
use Magento\Customer\Model\CustomerRegistry;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\Wishlist\Model\WishlistFactory;
12+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
13+
14+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
15+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/multiple_products.php');
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var CustomerRegistry $customerRegistry */
19+
$customerRegistry = Bootstrap::getObjectManager()->create(CustomerRegistry::class);
20+
$customer = $customerRegistry->retrieve(1);
21+
/** @var ProductRepositoryInterface $productRepository */
22+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
23+
$product = $productRepository->get('simple1');
24+
$product2 = $productRepository->get('simple2');
25+
26+
$wishlistFactory = $objectManager->get(WishlistFactory::class);
27+
$wishlist = $wishlistFactory->create();
28+
$wishlist->loadByCustomerId($customer->getId(), true);
29+
/** @var \Magento\Catalog\Helper\Product $productHelper */
30+
$productHelper = $objectManager->get(\Magento\Catalog\Helper\Product::class);
31+
$isSkipSaleableCheck = $productHelper->getSkipSaleableCheck();
32+
$productHelper->setSkipSaleableCheck(true);
33+
$wishlist->addNewItem($product);
34+
$wishlist->addNewItem($product2);
35+
$productHelper->setSkipSaleableCheck($isSkipSaleableCheck);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_rollback.php');
11+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/multiple_products_rollback.php');

0 commit comments

Comments
 (0)