Skip to content

Commit 4a28abb

Browse files
authored
Merge pull request #3980 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 527bb6f + 9515bc3 commit 4a28abb

File tree

8 files changed

+817
-116
lines changed

8 files changed

+817
-116
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Customer/CreateCustomerTest.php

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
namespace Magento\GraphQl\Customer;
99

1010
use Magento\Customer\Api\CustomerRepositoryInterface;
11-
use Magento\Customer\Model\CustomerRegistry;
11+
use Magento\Framework\Registry;
1212
use Magento\TestFramework\Helper\Bootstrap;
1313
use Magento\TestFramework\TestCase\GraphQlAbstract;
1414

15+
/**
16+
* Test for create customer functionallity
17+
*/
1518
class CreateCustomerTest extends GraphQlAbstract
1619
{
1720
/**
18-
* @var CustomerRegistry
21+
* @var Registry
1922
*/
20-
private $customerRegistry;
23+
private $registry;
2124

2225
/**
2326
* @var CustomerRepositoryInterface
@@ -28,7 +31,7 @@ protected function setUp()
2831
{
2932
parent::setUp();
3033

31-
$this->customerRegistry = Bootstrap::getObjectManager()->get(CustomerRegistry::class);
34+
$this->registry = Bootstrap::getObjectManager()->get(Registry::class);
3235
$this->customerRepository = Bootstrap::getObjectManager()->get(CustomerRepositoryInterface::class);
3336
}
3437

@@ -40,7 +43,7 @@ public function testCreateCustomerAccountWithPassword()
4043
$newFirstname = 'Richard';
4144
$newLastname = 'Rowe';
4245
$currentPassword = 'test123#';
43-
$newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
46+
$newEmail = 'new_customer@example.com';
4447

4548
$query = <<<QUERY
4649
mutation {
@@ -50,7 +53,7 @@ public function testCreateCustomerAccountWithPassword()
5053
lastname: "{$newLastname}"
5154
email: "{$newEmail}"
5255
password: "{$currentPassword}"
53-
is_subscribed: true
56+
is_subscribed: true
5457
}
5558
) {
5659
customer {
@@ -78,7 +81,7 @@ public function testCreateCustomerAccountWithoutPassword()
7881
{
7982
$newFirstname = 'Richard';
8083
$newLastname = 'Rowe';
81-
$newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
84+
$newEmail = 'new_customer@example.com';
8285

8386
$query = <<<QUERY
8487
mutation {
@@ -87,7 +90,7 @@ public function testCreateCustomerAccountWithoutPassword()
8790
firstname: "{$newFirstname}"
8891
lastname: "{$newLastname}"
8992
email: "{$newEmail}"
90-
is_subscribed: true
93+
is_subscribed: true
9194
}
9295
) {
9396
customer {
@@ -151,7 +154,7 @@ public function testCreateCustomerIfEmailMissed()
151154
firstname: "{$newFirstname}"
152155
lastname: "{$newLastname}"
153156
password: "{$currentPassword}"
154-
is_subscribed: true
157+
is_subscribed: true
155158
}
156159
) {
157160
customer {
@@ -186,7 +189,7 @@ public function testCreateCustomerIfEmailIsNotValid()
186189
lastname: "{$newLastname}"
187190
email: "{$newEmail}"
188191
password: "{$currentPassword}"
189-
is_subscribed: true
192+
is_subscribed: true
190193
}
191194
) {
192195
customer {
@@ -211,7 +214,7 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
211214
$newFirstname = 'Richard';
212215
$newLastname = 'Rowe';
213216
$currentPassword = 'test123#';
214-
$newEmail = 'customer_created' . rand(1, 2000000) . '@example.com';
217+
$newEmail = 'new_customer@example.com';
215218

216219
$query = <<<QUERY
217220
mutation {
@@ -222,7 +225,7 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
222225
test123: "123test123"
223226
email: "{$newEmail}"
224227
password: "{$currentPassword}"
225-
is_subscribed: true
228+
is_subscribed: true
226229
}
227230
) {
228231
customer {
@@ -237,4 +240,21 @@ public function testCreateCustomerIfPassedAttributeDosNotExistsInCustomerInput()
237240
QUERY;
238241
$this->graphQlQuery($query);
239242
}
243+
244+
public function tearDown()
245+
{
246+
$newEmail = 'new_customer@example.com';
247+
try {
248+
$customer = $this->customerRepository->get($newEmail);
249+
} catch (\Exception $exception) {
250+
return;
251+
}
252+
253+
$this->registry->unregister('isSecureArea');
254+
$this->registry->register('isSecureArea', true);
255+
$this->customerRepository->delete($customer);
256+
$this->registry->unregister('isSecureArea');
257+
$this->registry->register('isSecureArea', false);
258+
parent::tearDown();
259+
}
240260
}
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
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\Quote\Customer;
9+
10+
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
11+
use Magento\Integration\Api\CustomerTokenServiceInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\TestCase\GraphQlAbstract;
14+
15+
/**
16+
* Test for get specified billing address
17+
*/
18+
class GetSpecifiedBillingAddressTest extends GraphQlAbstract
19+
{
20+
/**
21+
* @var CustomerTokenServiceInterface
22+
*/
23+
private $customerTokenService;
24+
25+
/**
26+
* @var GetMaskedQuoteIdByReservedOrderId
27+
*/
28+
private $getMaskedQuoteIdByReservedOrderId;
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
protected function setUp()
34+
{
35+
$objectManager = Bootstrap::getObjectManager();
36+
$this->getMaskedQuoteIdByReservedOrderId = $objectManager->get(GetMaskedQuoteIdByReservedOrderId::class);
37+
$this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class);
38+
}
39+
40+
/**
41+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
42+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
43+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
44+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
45+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
46+
*/
47+
public function testGeSpecifiedBillingAddress()
48+
{
49+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
50+
$query = $this->getQuery($maskedQuoteId);
51+
52+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
53+
self::assertArrayHasKey('cart', $response);
54+
self::assertArrayHasKey('billing_address', $response['cart']);
55+
56+
$expectedBillingAddressData = [
57+
'firstname' => 'John',
58+
'lastname' => 'Smith',
59+
'company' => 'CompanyName',
60+
'street' => [
61+
'Green str, 67'
62+
],
63+
'city' => 'CityM',
64+
'region' => [
65+
'code' => 'AL',
66+
'label' => 'Alabama',
67+
],
68+
'postcode' => '75477',
69+
'country' => [
70+
'code' => 'US',
71+
'label' => 'US',
72+
],
73+
'telephone' => '3468676',
74+
'address_type' => 'BILLING',
75+
'customer_notes' => null,
76+
];
77+
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
78+
}
79+
80+
/**
81+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
82+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
83+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
84+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
85+
*/
86+
public function testGeSpecifiedBillingAddressIfBillingAddressIsNotSet()
87+
{
88+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
89+
$query = $this->getQuery($maskedQuoteId);
90+
91+
$response = $this->graphQlQuery($query, [], '', $this->getHeaderMap());
92+
self::assertArrayHasKey('cart', $response);
93+
self::assertArrayHasKey('billing_address', $response['cart']);
94+
95+
$expectedBillingAddressData = [
96+
'firstname' => null,
97+
'lastname' => null,
98+
'company' => null,
99+
'street' => [
100+
''
101+
],
102+
'city' => null,
103+
'region' => [
104+
'code' => null,
105+
'label' => null,
106+
],
107+
'postcode' => null,
108+
'country' => [
109+
'code' => null,
110+
'label' => null,
111+
],
112+
'telephone' => null,
113+
'address_type' => 'BILLING',
114+
'customer_notes' => null,
115+
];
116+
self::assertEquals($expectedBillingAddressData, $response['cart']['billing_address']);
117+
}
118+
119+
/**
120+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
121+
* @expectedException \Exception
122+
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
123+
*/
124+
public function testGeSpecifiedBillingAddressOfNonExistentCart()
125+
{
126+
$maskedQuoteId = 'non_existent_masked_id';
127+
$query = $this->getQuery($maskedQuoteId);
128+
129+
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
130+
}
131+
132+
/**
133+
* _security
134+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
135+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
136+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/guest/create_empty_cart.php
137+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
138+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
139+
*/
140+
public function testGeSpecifiedBillingAddressFromAnotherGuestCart()
141+
{
142+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
143+
144+
$this->expectExceptionMessage(
145+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
146+
);
147+
$this->graphQlQuery($this->getQuery($maskedQuoteId), [], '', $this->getHeaderMap());
148+
}
149+
150+
/**
151+
* _security
152+
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
153+
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
154+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php
155+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php
156+
* @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php
157+
*/
158+
public function testGeSpecifiedBillingAddressFromAnotherCustomerCart()
159+
{
160+
$maskedQuoteId = $this->getMaskedQuoteIdByReservedOrderId->execute('test_quote');
161+
162+
$this->expectExceptionMessage(
163+
"The current user cannot perform operations on cart \"$maskedQuoteId\""
164+
);
165+
$this->graphQlQuery(
166+
$this->getQuery($maskedQuoteId),
167+
[],
168+
'',
169+
$this->getHeaderMap('customer2@search.example.com')
170+
);
171+
}
172+
173+
/**
174+
* @param string $maskedQuoteId
175+
* @return string
176+
*/
177+
private function getQuery(string $maskedQuoteId): string
178+
{
179+
return <<<QUERY
180+
{
181+
cart(cart_id: "$maskedQuoteId") {
182+
billing_address {
183+
firstname
184+
lastname
185+
company
186+
street
187+
city
188+
region
189+
{
190+
code
191+
label
192+
}
193+
postcode
194+
country
195+
{
196+
code
197+
label
198+
}
199+
telephone
200+
address_type
201+
customer_notes
202+
}
203+
}
204+
}
205+
QUERY;
206+
}
207+
208+
/**
209+
* @param string $username
210+
* @param string $password
211+
* @return array
212+
*/
213+
private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array
214+
{
215+
$customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password);
216+
$headerMap = ['Authorization' => 'Bearer ' . $customerToken];
217+
return $headerMap;
218+
}
219+
}

0 commit comments

Comments
 (0)