Skip to content

Commit 9febf63

Browse files
author
Prabhu Ram
committed
Merge remote-tracking branch 'origin/PWA-1619-PR' into PWA-1655
2 parents 2059e95 + 2e285be commit 9febf63

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

app/code/Magento/WishlistGraphQl/Model/Resolver/CustomerWishlists.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\WishlistGraphQl\Model\Resolver;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
1112
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
1213
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -16,6 +17,7 @@
1617
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory as WishlistCollectionFactory;
1718
use Magento\Wishlist\Model\Wishlist;
1819
use Magento\Wishlist\Model\Wishlist\Config as WishlistConfig;
20+
use Magento\Wishlist\Model\WishlistFactory;
1921
use Magento\WishlistGraphQl\Mapper\WishlistDataMapper;
2022

2123
/**
@@ -38,19 +40,28 @@ class CustomerWishlists implements ResolverInterface
3840
*/
3941
private $wishlistCollectionFactory;
4042

43+
/**
44+
* @var WishlistFactory
45+
*/
46+
private $wishlistFactory;
47+
4148
/**
4249
* @param WishlistDataMapper $wishlistDataMapper
4350
* @param WishlistConfig $wishlistConfig
4451
* @param WishlistCollectionFactory $wishlistCollectionFactory
52+
* @param WishlistFactory $wishlistFactory
4553
*/
4654
public function __construct(
4755
WishlistDataMapper $wishlistDataMapper,
4856
WishlistConfig $wishlistConfig,
49-
WishlistCollectionFactory $wishlistCollectionFactory
57+
WishlistCollectionFactory $wishlistCollectionFactory,
58+
WishlistFactory $wishlistFactory = null
5059
) {
5160
$this->wishlistDataMapper = $wishlistDataMapper;
5261
$this->wishlistConfig = $wishlistConfig;
5362
$this->wishlistCollectionFactory = $wishlistCollectionFactory;
63+
$this->wishlistFactory = $wishlistFactory ?:
64+
ObjectManager::getInstance()->get(WishlistFactory::class);
5465
}
5566

5667
/**
@@ -96,7 +107,11 @@ public function resolve(
96107
foreach ($collection->getItems() as $wishList) {
97108
array_push($wishlists, $this->wishlistDataMapper->map($wishList));
98109
}
99-
110+
if (empty($wishlists)) {
111+
$newWishlist = $this->wishlistFactory->create();
112+
$newWishlist->loadByCustomerId($context->getUserId(), true);
113+
array_push($wishlists, $this->wishlistDataMapper->map($newWishlist));
114+
}
100115
return $wishlists;
101116
}
102117
}

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Magento\Wishlist\Model\Item;
1616
use Magento\Wishlist\Model\ResourceModel\Wishlist\CollectionFactory;
1717
use Magento\Wishlist\Model\Wishlist;
18+
use Magento\Customer\Api\CustomerRepositoryInterface;
19+
use Magento\Framework\Registry;
1820

1921
/**
2022
* Test coverage for customer wishlists
@@ -68,6 +70,80 @@ public function testCustomerWishlist(): void
6870
$this->assertEquals('simple', $wishlistItemResponse['product']['sku']);
6971
}
7072

73+
/**
74+
* @magentoConfigFixture default_store wishlist/general/active 1
75+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple_duplicated.php
76+
* @throws Exception
77+
*/
78+
public function testWishlistCreationScenario(): void
79+
{
80+
try {
81+
$customerEmail = 'customer@wishlist.com';
82+
$this->graphQlMutation(
83+
$this->getCreateCustomerQuery($customerEmail),
84+
[],
85+
''
86+
);
87+
$response = $this->graphQlQuery(
88+
$this->getQuery(),
89+
[],
90+
'',
91+
$this->getCustomerAuthHeaders($customerEmail, '123123^q')
92+
);
93+
$this->assertArrayHasKey('wishlists', $response['customer']);
94+
$wishlists = $response['customer']['wishlists'];
95+
$this->assertNotEmpty($wishlists);
96+
$wishlist = $wishlists[0];
97+
$this->assertEquals(0, $wishlist['items_count']);
98+
$sku = 'simple-1';
99+
$qty = 1;
100+
$addProductToWishlistQuery =
101+
<<<QUERY
102+
mutation{
103+
addProductsToWishlist(
104+
wishlistId:{$wishlist['id']}
105+
wishlistItems:[
106+
{
107+
sku:"{$sku}"
108+
quantity:{$qty}
109+
}
110+
])
111+
{
112+
wishlist{
113+
id
114+
items_count
115+
items{product{name sku} description qty}
116+
}
117+
user_errors{code message}
118+
}
119+
}
120+
121+
QUERY;
122+
$addToWishlistResponse = $this->graphQlMutation(
123+
$addProductToWishlistQuery,
124+
[],
125+
'',
126+
$this->getCustomerAuthHeaders($customerEmail, '123123^q')
127+
);
128+
$this->assertArrayHasKey('user_errors', $addToWishlistResponse['addProductsToWishlist']);
129+
$this->assertCount(0, $addToWishlistResponse['addProductsToWishlist']['user_errors']);
130+
} finally {
131+
/** @var Registry $registry */
132+
$registry = Bootstrap::getObjectManager()
133+
->get(Registry::class);
134+
$registry->unregister('isSecureArea');
135+
$registry->register('isSecureArea', true);
136+
137+
$objectManager = Bootstrap::getObjectManager();
138+
$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
139+
$customer = $customerRepository->get($customerEmail);
140+
$customerRepository->delete($customer);
141+
142+
$registry->unregister('isSecureArea');
143+
$registry->register('isSecureArea', false);
144+
}
145+
}
146+
71147
/**
72148
* Testing fetching the wishlist when wishlist is disabled
73149
*
@@ -109,11 +185,13 @@ private function getQuery(): string
109185
query {
110186
customer {
111187
wishlists {
188+
id
112189
items_count
113190
sharing_code
114191
updated_at
115192
items_v2 {
116-
items {product {name sku}
193+
items {
194+
product {name sku}
117195
}
118196
}
119197
}
@@ -122,6 +200,25 @@ private function getQuery(): string
122200
QUERY;
123201
}
124202

203+
private function getCreateCustomerQuery($customerEmail): string
204+
{
205+
return <<<QUERY
206+
mutation {
207+
createCustomer(input: {
208+
firstname: "test"
209+
lastname: "test"
210+
email: "$customerEmail"
211+
password: "123123^q"
212+
})
213+
{
214+
customer {
215+
email
216+
}
217+
}
218+
}
219+
QUERY;
220+
}
221+
125222
/**
126223
* Getting customer auth headers
127224
*

0 commit comments

Comments
 (0)