12
12
use Magento \Framework \App \Response \Http as HttpResponse ;
13
13
use Magento \Framework \ObjectManagerInterface ;
14
14
use Magento \GraphQl \App \State \Comparator ;
15
+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
15
16
use Magento \TestFramework \Helper \Bootstrap ;
16
17
17
18
/**
@@ -38,6 +39,11 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
38
39
/** @var RequestFactory */
39
40
private RequestFactory $ requestFactory ;
40
41
42
+ /**
43
+ * @var CustomerTokenServiceInterface
44
+ */
45
+ private CustomerTokenServiceInterface $ customerTokenService ;
46
+
41
47
/**
42
48
* @return void
43
49
*/
@@ -46,6 +52,7 @@ protected function setUp(): void
46
52
$ this ->objectManager = Bootstrap::getObjectManager ();
47
53
$ this ->comparator = $ this ->objectManager ->create (Comparator::class);
48
54
$ this ->requestFactory = $ this ->objectManager ->get (RequestFactory::class);
55
+ $ this ->customerTokenService = $ this ->objectManager ->get (CustomerTokenServiceInterface::class);
49
56
parent ::setUp ();
50
57
}
51
58
@@ -54,23 +61,26 @@ protected function setUp(): void
54
61
* @magentoConfigFixture base_website btob/website_configuration/company_active 1
55
62
* @magentoConfigFixture default_store btob/website_configuration/company_active 1
56
63
* @magentoConfigFixture default_store company/general/allow_company_registration 1
64
+ * @magentoDataFixture Magento/Customer/_files/customer.php
65
+ * @magentoDataFixture Magento/Customer/_files/customer_address.php
57
66
* @dataProvider queryDataProvider
58
67
* @param string $query
59
68
* @param array $variables
60
69
* @param array $variables2 This is the second set of variables to be used in the second request
70
+ * @param array $authInfo
61
71
* @param string $operationName
62
72
* @param string $expected
63
73
* @return void
64
74
* @throws \Exception
65
75
*/
66
- public function testState (string $ query , array $ variables , array $ variables2 , string $ operationName , string $ expected ): void
76
+ public function testState (string $ query , array $ variables , array $ variables2 , array $ authInfo , string $ operationName , string $ expected ): void
67
77
{
68
78
$ jsonEncodedRequest = json_encode ([
69
79
'query ' => $ query ,
70
80
'variables ' => $ variables ,
71
81
'operationName ' => $ operationName
72
82
]);
73
- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
83
+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
74
84
$ this ->assertStringContainsString ($ expected , $ output1 );
75
85
if ($ variables2 ) {
76
86
$ jsonEncodedRequest = json_encode ([
@@ -79,21 +89,22 @@ public function testState(string $query, array $variables, array $variables2, st
79
89
'operationName ' => $ operationName
80
90
]);
81
91
}
82
- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
92
+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
83
93
$ this ->assertStringContainsString ($ expected , $ output2 );
84
94
}
85
95
86
96
/**
87
97
* @param string $query
88
98
* @param string $operationName
99
+ * @param array $authInfo
89
100
* @param bool $firstRequest
90
101
* @return string
91
102
* @throws \Exception
92
103
*/
93
- private function request (string $ query , string $ operationName , bool $ firstRequest = false ): string
104
+ private function request (string $ query , string $ operationName , array $ authInfo , bool $ firstRequest = false ): string
94
105
{
95
106
$ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
96
- $ response = $ this ->doRequest ($ query );
107
+ $ response = $ this ->doRequest ($ query, $ authInfo );
97
108
$ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
98
109
$ result = $ this ->comparator ->compare ($ operationName );
99
110
$ this ->assertEmpty (
@@ -113,13 +124,19 @@ private function request(string $query, string $operationName, bool $firstReques
113
124
* @param string $query
114
125
* @return string
115
126
*/
116
- private function doRequest (string $ query )
127
+ private function doRequest (string $ query, array $ authInfo )
117
128
{
118
129
$ request = $ this ->requestFactory ->create ();
119
130
$ request ->setContent ($ query );
120
131
$ request ->setMethod ('POST ' );
121
132
$ request ->setPathInfo ('/graphql ' );
122
133
$ request ->getHeaders ()->addHeaders (['content_type ' => self ::CONTENT_TYPE ]);
134
+ if ($ authInfo ) {
135
+ $ email = $ authInfo ['email ' ];
136
+ $ password = $ authInfo ['password ' ];
137
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ email , $ password );
138
+ $ request ->getHeaders ()->addHeaders (['Authorization ' => 'Bearer ' . $ customerToken ]);
139
+ }
123
140
$ unusedResponse = $ this ->objectManager ->create (HttpResponse::class);
124
141
$ httpApp = $ this ->objectManager ->create (
125
142
HttpApp::class,
@@ -170,6 +187,7 @@ public function queryDataProvider(): array
170
187
QUERY,
171
188
['id ' => 4 ],
172
189
[],
190
+ [],
173
191
'navigationMenu ' ,
174
192
'"id":4,"name":"Category 1.1","product_count":2, '
175
193
],
@@ -220,6 +238,7 @@ public function queryDataProvider(): array
220
238
QUERY,
221
239
['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
222
240
[],
241
+ [],
223
242
'productDetailByName ' ,
224
243
'"sku":"configurable","name":"Configurable Product" '
225
244
],
@@ -269,6 +288,7 @@ public function queryDataProvider(): array
269
288
QUERY,
270
289
['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
271
290
[],
291
+ [],
272
292
'category ' ,
273
293
'"url_key":"category-1-1","name":"Category 1.1" '
274
294
],
@@ -333,6 +353,7 @@ public function queryDataProvider(): array
333
353
QUERY,
334
354
['name ' => 'Simple Product1 ' , 'onServer ' => false ],
335
355
[],
356
+ [],
336
357
'productDetail ' ,
337
358
'"sku":"simple1","name":"Simple Product1" '
338
359
],
@@ -347,9 +368,167 @@ public function queryDataProvider(): array
347
368
QUERY,
348
369
['urlKey ' => 'no-route ' ],
349
370
[],
371
+ [],
350
372
'resolveUrl ' ,
351
373
'"type":"CMS_PAGE","id":1 '
374
+ ],
375
+ # Customer Scenarios
376
+ 'Create Customer ' => [
377
+ <<<'QUERY'
378
+ mutation($firstname: String!, $lastname: String!, $email: String!, $password: String!) {
379
+ createCustomerV2(
380
+ input: {
381
+ firstname: $firstname,
382
+ lastname: $lastname,
383
+ email: $email,
384
+ password: $password
385
+ }
386
+ ) {
387
+ customer {
388
+ created_at
389
+ prefix
390
+ firstname
391
+ middlename
392
+ lastname
393
+ suffix
394
+ email
395
+ default_billing
396
+ default_shipping
397
+ date_of_birth
398
+ taxvat
399
+ is_subscribed
400
+ gender
401
+ allow_remote_shopping_assistance
402
+ }
403
+ }
404
+ }
405
+ QUERY,
406
+ [
407
+ 'firstname ' => 'John ' ,
408
+ 'lastname ' => 'Doe ' ,
409
+
410
+ 'password ' => 'Password-1 ' ,
411
+ ],
412
+ [
413
+ 'firstname ' => 'John ' ,
414
+ 'lastname ' => 'Doe ' ,
415
+
416
+ 'password ' => 'Password-2 ' ,
417
+ ],
418
+ [],
419
+ 'createCustomer ' ,
420
+ '"email":" ' ,
421
+ ],
422
+ 'Update Customer ' => [
423
+ <<<'QUERY'
424
+ mutation($allow: Boolean!) {
425
+ updateCustomerV2(
426
+ input: {
427
+ allow_remote_shopping_assistance: $allow
428
+ }
429
+ ) {
430
+ customer {
431
+ allow_remote_shopping_assistance
432
+ }
433
+ }
434
+ }
435
+ QUERY,
436
+ ['allow ' => true ],
437
+ ['allow ' => false ],
438
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
439
+ 'updateCustomer ' ,
440
+ 'allow_remote_shopping_assistance '
441
+ ],
442
+ 'Update Customer Address ' => [
443
+ <<<'QUERY'
444
+ mutation($addressId: Int!, $city: String!) {
445
+ updateCustomerAddress(id: $addressId, input: {
446
+ region: {
447
+ region: "Alberta"
448
+ region_id: 66
449
+ region_code: "AB"
450
+ }
451
+ country_code: CA
452
+ street: ["Line 1 Street","Line 2"]
453
+ company: "Company Name"
454
+ telephone: "123456789"
455
+ fax: "123123123"
456
+ postcode: "7777"
457
+ city: $city
458
+ firstname: "Adam"
459
+ lastname: "Phillis"
460
+ middlename: "A"
461
+ prefix: "Mr."
462
+ suffix: "Jr."
463
+ vat_id: "1"
464
+ default_shipping: true
465
+ default_billing: true
466
+ }) {
467
+ id
468
+ customer_id
469
+ region {
470
+ region
471
+ region_id
472
+ region_code
473
+ }
474
+ country_code
475
+ street
476
+ company
477
+ telephone
478
+ fax
479
+ postcode
480
+ city
481
+ firstname
482
+ lastname
483
+ middlename
484
+ prefix
485
+ suffix
486
+ vat_id
487
+ default_shipping
488
+ default_billing
489
+ }
490
+ }
491
+ QUERY,
492
+ ['addressId ' => 1 , 'city ' => 'New York ' ],
493
+ ['addressId ' => 1 , 'city ' => 'Austin ' ],
494
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
495
+ 'updateCustomerAddress ' ,
496
+ 'city '
497
+ ],
498
+ 'Update Customer Email ' => [
499
+ <<<'QUERY'
500
+ mutation($email: String!, $password: String!) {
501
+ updateCustomerEmail(
502
+ email: $email
503
+ password: $password
504
+ ) {
505
+ customer {
506
+ email
507
+ }
508
+ }
509
+ }
510
+ QUERY,
511
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
512
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
513
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
514
+ 'updateCustomerEmail ' ,
515
+ 'email '
516
+ ],
517
+ 'Generate Customer Token ' => [
518
+ <<<'QUERY'
519
+ mutation($email: String!, $password: String!) {
520
+ generateCustomerToken(email: $email, password: $password) {
521
+ token
522
+ }
523
+ }
524
+ QUERY,
525
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
526
+ [
'email ' =>
'[email protected] ' ,
'password ' =>
'password ' ],
527
+ [],
528
+ 'generateCustomerToken ' ,
529
+ 'token '
352
530
]
531
+
353
532
];
354
533
}
355
534
}
0 commit comments