Skip to content

Commit a5cfd35

Browse files
ACQE-8879: Create a simple product with custom options using area and field.
- Added New webapi test coverage for adding product with custom option to wishlist.
1 parent f6aa682 commit a5cfd35

File tree

1 file changed

+209
-0
lines changed

1 file changed

+209
-0
lines changed
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
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 Magento\Catalog\Api\Data\ProductCustomOptionInterface;
11+
use Magento\Catalog\Api\ProductCustomOptionRepositoryInterface;
12+
use Magento\Catalog\Test\Fixture\Product as ProductFixture;
13+
use Magento\Customer\Test\Fixture\Customer as CustomerFixture;
14+
use Magento\Framework\ObjectManagerInterface;
15+
use Magento\Integration\Api\CustomerTokenServiceInterface;
16+
use Magento\TestFramework\Fixture\Config;
17+
use Magento\TestFramework\Fixture\DataFixture;
18+
use Magento\TestFramework\Fixture\DataFixtureStorage;
19+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
20+
use Magento\TestFramework\Helper\Bootstrap;
21+
use Magento\TestFramework\TestCase\GraphQlAbstract;
22+
23+
/**
24+
* Test to verify add simple product with required field and area options to wishlist via GraphQL
25+
*/
26+
class SimpleProductWithCustomOptionToWishlistTest extends GraphQlAbstract
27+
{
28+
/**
29+
* @var CustomerTokenServiceInterface
30+
*/
31+
private CustomerTokenServiceInterface $customerTokenService;
32+
33+
/**
34+
* @var ProductCustomOptionRepositoryInterface
35+
*/
36+
private ProductCustomOptionRepositoryInterface $productCustomOptionRepository;
37+
38+
/**
39+
* @var ObjectManagerInterface
40+
*/
41+
private ObjectManagerInterface $objectManager;
42+
43+
/**
44+
* @var DataFixtureStorage
45+
*/
46+
private DataFixtureStorage $fixtures;
47+
48+
/**
49+
* @return void
50+
*/
51+
protected function setUp(): void
52+
{
53+
parent::setUp();
54+
$this->objectManager = Bootstrap::getObjectManager();
55+
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
56+
$this->customerTokenService = $this->objectManager->get(CustomerTokenServiceInterface::class);
57+
$this->productCustomOptionRepository = $this->objectManager->get(ProductCustomOptionRepositoryInterface::class);
58+
}
59+
60+
#[
61+
Config('wishlist/general/active', '1', 'store', 'default'),
62+
DataFixture(
63+
ProductFixture::class,
64+
[
65+
'sku' => 'simple_co',
66+
'name' => 'Simple CO',
67+
'price' => 10,
68+
'options' => [
69+
[
70+
'type' => ProductCustomOptionInterface::OPTION_TYPE_FIELD,
71+
'title' => 'field_opt',
72+
'is_require' => true,
73+
],
74+
[
75+
'type' => ProductCustomOptionInterface::OPTION_TYPE_AREA,
76+
'title' => 'area_opt',
77+
'is_require' => true,
78+
],
79+
],
80+
],
81+
as: 'product1'
82+
),
83+
DataFixture(CustomerFixture::class, as: 'customer'),
84+
]
85+
/**
86+
* Test to verify adding simple product with custom option to wishlist
87+
*
88+
* @return void
89+
*/
90+
public function testAddSimpleProductWithFieldAndAreaOptions(): void
91+
{
92+
$sku = 'simple_co';
93+
$uids = $this->getEnteredOptionUids($sku, [
94+
ProductCustomOptionInterface::OPTION_TYPE_FIELD,
95+
ProductCustomOptionInterface::OPTION_TYPE_AREA,
96+
]);
97+
$query = $this->getMutation($sku, $uids['field'], $uids['area']);
98+
$response = $this->graphQlMutation($query, [], '', $this->getHeadersMap());
99+
$this->assertArrayHasKey('addProductsToWishlist', $response);
100+
$this->assertArrayHasKey('wishlist', $response['addProductsToWishlist']);
101+
$this->assertEmpty($response['addProductsToWishlist']['user_errors'] ?? []);
102+
$wishlist = $response['addProductsToWishlist']['wishlist'];
103+
$this->assertEquals(1, $wishlist['items_count']);
104+
$this->assertCount(1, $wishlist['items_v2']['items']);
105+
$item = $wishlist['items_v2']['items'][0];
106+
$this->assertEquals($sku, $item['product']['sku']);
107+
$this->assertCount(2, $item['customizable_options']);
108+
foreach ($item['customizable_options'] as $opt) {
109+
if ($opt['type'] === 'field') {
110+
$this->assertEquals('test field value', $opt['values'][0]['value']);
111+
}
112+
if ($opt['type'] === 'area') {
113+
$this->assertEquals('test area value', $opt['values'][0]['value']);
114+
}
115+
}
116+
}
117+
118+
/**
119+
* Get addProductsToWishlist mutation
120+
*
121+
* @param string $sku
122+
* @param string $fieldUid
123+
* @param string $areaUid
124+
* @param int $wishlistId
125+
* @return string
126+
*/
127+
private function getMutation(string $sku, string $fieldUid, string $areaUid, int $wishlistId = 0): string
128+
{
129+
return <<<MUTATION
130+
mutation {
131+
addProductsToWishlist(
132+
wishlistId: {$wishlistId},
133+
wishlistItems: [
134+
{
135+
sku: "{$sku}"
136+
quantity: 1
137+
entered_options: [
138+
{ uid: "{$fieldUid}", value: "test field value" }
139+
{ uid: "{$areaUid}", value: "test area value" }
140+
]
141+
}
142+
]
143+
) {
144+
user_errors { code message }
145+
wishlist {
146+
id
147+
items_count
148+
items_v2(currentPage:1,pageSize:10) {
149+
items {
150+
id
151+
quantity
152+
customizable_options {
153+
customizable_option_uid
154+
label
155+
type
156+
values { label value }
157+
}
158+
product { sku }
159+
}
160+
}
161+
}
162+
}
163+
}
164+
MUTATION;
165+
}
166+
167+
/**
168+
* Get product custom option uids
169+
*
170+
* @param string $sku
171+
* @param array $types
172+
* @return array
173+
*/
174+
private function getEnteredOptionUids(string $sku, array $types): array
175+
{
176+
$customOptions = $this->productCustomOptionRepository->getList($sku);
177+
$map = [];
178+
foreach ($customOptions as $customOption) {
179+
$type = $customOption->getType();
180+
if (!in_array($type, $types, true)) {
181+
continue;
182+
}
183+
if (!isset($map[$type])) {
184+
$map[$type] = base64_encode('custom-option/' . (int) $customOption->getOptionId());
185+
}
186+
if (count($map) === count($types)) {
187+
break;
188+
}
189+
}
190+
return [
191+
'field' => $map[ProductCustomOptionInterface::OPTION_TYPE_FIELD],
192+
'area' => $map[ProductCustomOptionInterface::OPTION_TYPE_AREA],
193+
];
194+
}
195+
196+
/**
197+
* Get customer token for authentication
198+
*
199+
* @return string[]
200+
* @throws \Magento\Framework\Exception\AuthenticationException
201+
* @throws \Magento\Framework\Exception\EmailNotConfirmedException
202+
*/
203+
private function getHeadersMap(): array
204+
{
205+
$username = $this->fixtures->get('customer')->getEmail();
206+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, 'password');
207+
return ['Authorization' => 'Bearer ' . $customerToken];
208+
}
209+
}

0 commit comments

Comments
 (0)