7
7
8
8
namespace Magento \GraphQl \App ;
9
9
10
+ use Magento \Customer \Api \CustomerRepositoryInterface ;
10
11
use Magento \Framework \App \Http as HttpApp ;
11
12
use Magento \Framework \App \ObjectManager as AppObjectManager ;
12
13
use Magento \Framework \App \Request \HttpFactory as RequestFactory ;
13
14
use Magento \Framework \App \Response \Http as HttpResponse ;
14
15
use Magento \Framework \ObjectManagerInterface ;
16
+ use Magento \Framework \Registry ;
15
17
use Magento \GraphQl \App \State \Comparator ;
16
18
use Magento \GraphQl \App \State \ObjectManager ;
19
+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
17
20
use Magento \TestFramework \Helper \Bootstrap ;
18
21
19
22
/**
@@ -43,6 +46,12 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
43
46
/** @var RequestFactory */
44
47
private RequestFactory $ requestFactory ;
45
48
49
+ /** @var CustomerRepositoryInterface */
50
+ private CustomerRepositoryInterface $ customerRepository ;
51
+
52
+ /** @var Registry */
53
+ private $ registry ;
54
+
46
55
/**
47
56
* @inheritDoc
48
57
*/
@@ -73,6 +82,33 @@ protected function tearDown(): void
73
82
parent ::tearDown ();
74
83
}
75
84
85
+ /**
86
+ * @magentoDataFixture Magento/Customer/_files/customer.php
87
+ * @magentoDataFixture Magento/Customer/_files/customer_address.php
88
+ * @dataProvider customerDataProvider
89
+ * @return void
90
+ * @throws \Exception
91
+ */
92
+ public function testCustomerState (string $ query , array $ variables , array $ variables2 , array $ authInfo , string $ operationName , string $ expected )
93
+ {
94
+ if ($ operationName === 'createCustomer ' ) {
95
+ $ this ->customerRepository = $ this ->objectManagerForTest ->get (CustomerRepositoryInterface::class);
96
+ $ this ->registry = $ this ->objectManagerForTest ->get (Registry::class);
97
+ $ this ->registry ->register ('isSecureArea ' , true );
98
+ try {
99
+ $ customer = $ this ->customerRepository ->get ($ variables ['email ' ]);
100
+ $ this ->customerRepository ->delete ($ customer );
101
+ $ customer2 = $ this ->customerRepository ->get ($ variables2 ['email ' ]);
102
+ $ this ->customerRepository ->delete ($ customer2 );
103
+ } catch (\Exception $ e ) {
104
+ // Customer does not exist
105
+ } finally {
106
+ $ this ->registry ->unregister ('isSecureArea ' , false );
107
+ }
108
+ }
109
+ $ this ->testState ($ query , $ variables , $ variables2 , $ authInfo , $ operationName , $ expected );
110
+ }
111
+
76
112
/**
77
113
* Runs various GraphQL queries and checks if state of shared objects in Object Manager have changed
78
114
* @magentoConfigFixture base_website btob/website_configuration/company_active 1
@@ -82,6 +118,7 @@ protected function tearDown(): void
82
118
* @param string $query
83
119
* @param array $variables
84
120
* @param array $variables2 This is the second set of variables to be used in the second request
121
+ * @param array $authInfo
85
122
* @param string $operationName
86
123
* @param string $expected
87
124
* @return void
@@ -91,6 +128,7 @@ public function testState(
91
128
string $ query ,
92
129
array $ variables ,
93
130
array $ variables2 ,
131
+ array $ authInfo ,
94
132
string $ operationName ,
95
133
string $ expected ,
96
134
): void {
@@ -99,7 +137,7 @@ public function testState(
99
137
'variables ' => $ variables ,
100
138
'operationName ' => $ operationName
101
139
]);
102
- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
140
+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
103
141
$ this ->assertStringContainsString ($ expected , $ output1 );
104
142
if ($ variables2 ) {
105
143
$ jsonEncodedRequest = json_encode ([
@@ -108,22 +146,23 @@ public function testState(
108
146
'operationName ' => $ operationName
109
147
]);
110
148
}
111
- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
149
+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
112
150
$ this ->assertStringContainsString ($ expected , $ output2 );
113
151
}
114
152
115
153
/**
116
154
* @param string $query
117
155
* @param string $operationName
156
+ * @param array $authInfo
118
157
* @param bool $firstRequest
119
158
* @return string
120
159
* @throws \Exception
121
160
*/
122
- private function request (string $ query , string $ operationName , bool $ firstRequest = false ): string
161
+ private function request (string $ query , string $ operationName , array $ authInfo , bool $ firstRequest = false ): string
123
162
{
124
163
$ this ->objectManagerForTest ->resetStateSharedInstances ();
125
164
$ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
126
- $ response = $ this ->doRequest ($ query );
165
+ $ response = $ this ->doRequest ($ query, $ authInfo );
127
166
$ this ->objectManagerForTest ->resetStateSharedInstances ();
128
167
$ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
129
168
$ result = $ this ->comparator ->compareBetweenRequests ($ operationName );
@@ -153,13 +192,20 @@ private function request(string $query, string $operationName, bool $firstReques
153
192
* @param string $query
154
193
* @return string
155
194
*/
156
- private function doRequest (string $ query )
195
+ private function doRequest (string $ query, array $ authInfo )
157
196
{
158
197
$ request = $ this ->requestFactory ->create ();
159
198
$ request ->setContent ($ query );
160
199
$ request ->setMethod ('POST ' );
161
200
$ request ->setPathInfo ('/graphql ' );
162
201
$ request ->getHeaders ()->addHeaders (['content_type ' => self ::CONTENT_TYPE ]);
202
+ if ($ authInfo ) {
203
+ $ email = $ authInfo ['email ' ];
204
+ $ password = $ authInfo ['password ' ];
205
+ $ customerToken = $ this ->objectManagerForTest ->get (CustomerTokenServiceInterface::class)
206
+ ->createCustomerAccessToken ($ email , $ password );
207
+ $ request ->getHeaders ()->addHeaders (['Authorization ' => 'Bearer ' . $ customerToken ]);
208
+ }
163
209
$ unusedResponse = $ this ->objectManagerForTest ->create (HttpResponse::class);
164
210
$ httpApp = $ this ->objectManagerForTest ->create (
165
211
HttpApp::class,
@@ -210,6 +256,7 @@ public function queryDataProvider(): array
210
256
QUERY,
211
257
['id ' => 4 ],
212
258
[],
259
+ [],
213
260
'navigationMenu ' ,
214
261
'"id":4,"name":"Category 1.1","product_count":2, '
215
262
],
@@ -260,6 +307,7 @@ public function queryDataProvider(): array
260
307
QUERY,
261
308
['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
262
309
[],
310
+ [],
263
311
'productDetailByName ' ,
264
312
'"sku":"configurable","name":"Configurable Product" '
265
313
],
@@ -309,6 +357,7 @@ public function queryDataProvider(): array
309
357
QUERY,
310
358
['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
311
359
[],
360
+ [],
312
361
'category ' ,
313
362
'"url_key":"category-1-1","name":"Category 1.1" '
314
363
],
@@ -373,6 +422,7 @@ public function queryDataProvider(): array
373
422
QUERY,
374
423
['name ' => 'Simple Product1 ' , 'onServer ' => false ],
375
424
[],
425
+ [],
376
426
'productDetail ' ,
377
427
'"sku":"simple1","name":"Simple Product1" '
378
428
],
@@ -387,8 +437,175 @@ public function queryDataProvider(): array
387
437
QUERY,
388
438
['urlKey ' => 'no-route ' ],
389
439
[],
440
+ [],
390
441
'resolveUrl ' ,
391
442
'"type":"CMS_PAGE","id":1 '
443
+ ],
444
+ ];
445
+ }
446
+ /**
447
+ * Queries, variables, operation names, and expected responses for test
448
+ *
449
+ * @return array[]
450
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
451
+ */
452
+ public function customerDataProvider (): array
453
+ {
454
+ return [
455
+ 'Create Customer ' => [
456
+ <<<'QUERY'
457
+ mutation($firstname: String!, $lastname: String!, $email: String!, $password: String!) {
458
+ createCustomerV2(
459
+ input: {
460
+ firstname: $firstname,
461
+ lastname: $lastname,
462
+ email: $email,
463
+ password: $password
464
+ }
465
+ ) {
466
+ customer {
467
+ created_at
468
+ prefix
469
+ firstname
470
+ middlename
471
+ lastname
472
+ suffix
473
+ email
474
+ default_billing
475
+ default_shipping
476
+ date_of_birth
477
+ taxvat
478
+ is_subscribed
479
+ gender
480
+ allow_remote_shopping_assistance
481
+ }
482
+ }
483
+ }
484
+ QUERY,
485
+ [
486
+ 'firstname ' => 'John ' ,
487
+ 'lastname ' => 'Doe ' ,
488
+ 'email ' => 'email1@example.com ' ,
489
+ 'password ' => 'Password-1 ' ,
490
+ ],
491
+ [
492
+ 'firstname ' => 'John ' ,
493
+ 'lastname ' => 'Doe ' ,
494
+ 'email ' => 'email2@adobe.com ' ,
495
+ 'password ' => 'Password-2 ' ,
496
+ ],
497
+ [],
498
+ 'createCustomer ' ,
499
+ '"email":" ' ,
500
+ ],
501
+ 'Update Customer ' => [
502
+ <<<'QUERY'
503
+ mutation($allow: Boolean!) {
504
+ updateCustomerV2(
505
+ input: {
506
+ allow_remote_shopping_assistance: $allow
507
+ }
508
+ ) {
509
+ customer {
510
+ allow_remote_shopping_assistance
511
+ }
512
+ }
513
+ }
514
+ QUERY,
515
+ ['allow ' => true ],
516
+ ['allow ' => false ],
517
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
518
+ 'updateCustomer ' ,
519
+ 'allow_remote_shopping_assistance '
520
+ ],
521
+ 'Update Customer Address ' => [
522
+ <<<'QUERY'
523
+ mutation($addressId: Int!, $city: String!) {
524
+ updateCustomerAddress(id: $addressId, input: {
525
+ region: {
526
+ region: "Alberta"
527
+ region_id: 66
528
+ region_code: "AB"
529
+ }
530
+ country_code: CA
531
+ street: ["Line 1 Street","Line 2"]
532
+ company: "Company Name"
533
+ telephone: "123456789"
534
+ fax: "123123123"
535
+ postcode: "7777"
536
+ city: $city
537
+ firstname: "Adam"
538
+ lastname: "Phillis"
539
+ middlename: "A"
540
+ prefix: "Mr."
541
+ suffix: "Jr."
542
+ vat_id: "1"
543
+ default_shipping: true
544
+ default_billing: true
545
+ }) {
546
+ id
547
+ customer_id
548
+ region {
549
+ region
550
+ region_id
551
+ region_code
552
+ }
553
+ country_code
554
+ street
555
+ company
556
+ telephone
557
+ fax
558
+ postcode
559
+ city
560
+ firstname
561
+ lastname
562
+ middlename
563
+ prefix
564
+ suffix
565
+ vat_id
566
+ default_shipping
567
+ default_billing
568
+ }
569
+ }
570
+ QUERY,
571
+ ['addressId ' => 1 , 'city ' => 'New York ' ],
572
+ ['addressId ' => 1 , 'city ' => 'Austin ' ],
573
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
574
+ 'updateCustomerAddress ' ,
575
+ 'city '
576
+ ],
577
+ 'Update Customer Email ' => [
578
+ <<<'QUERY'
579
+ mutation($email: String!, $password: String!) {
580
+ updateCustomerEmail(
581
+ email: $email
582
+ password: $password
583
+ ) {
584
+ customer {
585
+ email
586
+ }
587
+ }
588
+ }
589
+ QUERY,
590
+ ['email ' => 'customer2@example.com ' , 'password ' => 'password ' ],
591
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
592
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
593
+ 'updateCustomerEmail ' ,
594
+ 'email '
595
+ ],
596
+ 'Generate Customer Token ' => [
597
+ <<<'QUERY'
598
+ mutation($email: String!, $password: String!) {
599
+ generateCustomerToken(email: $email, password: $password) {
600
+ token
601
+ }
602
+ }
603
+ QUERY,
604
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
605
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
606
+ [],
607
+ 'generateCustomerToken ' ,
608
+ 'token '
392
609
]
393
610
];
394
611
}
0 commit comments