Skip to content

Commit ef96b77

Browse files
author
Prabhu Ram
committed
PWA-1619: New accounts created through GQL are not provisioned a wishlist
- Added implementation and tests
1 parent d4f3adf commit ef96b77

File tree

4 files changed

+130
-3
lines changed

4 files changed

+130
-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: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,64 @@ public function testCustomerWishlist(): void
6868
$this->assertEquals('simple', $wishlistItemResponse['product']['sku']);
6969
}
7070

71+
/**
72+
* @magentoConfigFixture default_store wishlist/general/active 1
73+
* @magentoApiDataFixture Magento/Wishlist/_files/wishlist_product.php
74+
* @throws Exception
75+
*/
76+
public function testWishlistCreationScenario(): void
77+
{
78+
$customerEmail = '[email protected]';
79+
$this->graphQlMutation(
80+
$this->getCreateCustomerQuery($customerEmail),
81+
[],
82+
''
83+
);
84+
$response = $this->graphQlQuery(
85+
$this->getQuery(),
86+
[],
87+
'',
88+
$this->getCustomerAuthHeaders($customerEmail, '123123^q')
89+
);
90+
$this->assertArrayHasKey('wishlists', $response['customer']);
91+
$wishlists = $response['customer']['wishlists'];
92+
$this->assertNotEmpty($wishlists);
93+
$wishlist = $wishlists[0];
94+
$this->assertEquals(0, $wishlist['items_count']);
95+
$sku = 'simple-1';
96+
$qty = 1;
97+
$addProductToWishlistQuery =
98+
<<<QUERY
99+
mutation{
100+
addProductsToWishlist(
101+
wishlistId:{$wishlist['id']}
102+
wishlistItems:[
103+
{
104+
sku:"{$sku}"
105+
quantity:{$qty}
106+
}
107+
])
108+
{
109+
wishlist{
110+
id
111+
items_count
112+
items{product{name sku} description qty}
113+
}
114+
user_errors{code message}
115+
}
116+
}
117+
118+
QUERY;
119+
$addToWishlistResponse = $this->graphQlMutation(
120+
$addProductToWishlistQuery,
121+
[],
122+
'',
123+
$this->getCustomerAuthHeaders($customerEmail, '123123^q')
124+
);
125+
$this->assertArrayHasKey('user_errors', $addToWishlistResponse['addProductsToWishlist']);
126+
$this->assertCount(0, $addToWishlistResponse['addProductsToWishlist']['user_errors']);
127+
}
128+
71129
/**
72130
* Testing fetching the wishlist when wishlist is disabled
73131
*
@@ -109,11 +167,13 @@ private function getQuery(): string
109167
query {
110168
customer {
111169
wishlists {
170+
id
112171
items_count
113172
sharing_code
114173
updated_at
115174
items_v2 {
116-
items {product {name sku}
175+
items {
176+
product {name sku}
117177
}
118178
}
119179
}
@@ -122,6 +182,25 @@ private function getQuery(): string
122182
QUERY;
123183
}
124184

185+
private function getCreateCustomerQuery($customerEmail): string
186+
{
187+
return <<<QUERY
188+
mutation {
189+
createCustomer(input: {
190+
firstname: "test"
191+
lastname: "test"
192+
email: "$customerEmail"
193+
password: "123123^q"
194+
})
195+
{
196+
customer {
197+
email
198+
}
199+
}
200+
}
201+
QUERY;
202+
}
203+
125204
/**
126205
* Getting customer auth headers
127206
*
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
8+
9+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_duplicated.php');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Customer\Api\CustomerRepositoryInterface;
8+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
9+
10+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_duplicated_rollback.php');
11+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer_rollback.php');
12+
13+
/** @var \Magento\Framework\Registry $registry */
14+
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);
15+
$registry->unregister('isSecureArea');
16+
$registry->register('isSecureArea', true);
17+
18+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
19+
$customerRepository = $objectManager->create(CustomerRepositoryInterface::class);
20+
$customer = $customerRepository->get('[email protected]');
21+
$customerRepository->delete($customer);
22+
23+
$registry->unregister('isSecureArea');
24+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)