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 \Request \HttpFactory as RequestFactory ;
12
13
use Magento \Framework \App \Response \Http as HttpResponse ;
13
14
use Magento \Framework \ObjectManagerInterface ;
15
+ use Magento \Framework \Registry ;
14
16
use Magento \GraphQl \App \State \Comparator ;
17
+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
15
18
use Magento \TestFramework \Helper \Bootstrap ;
16
19
17
20
/**
@@ -38,6 +41,12 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
38
41
/** @var RequestFactory */
39
42
private RequestFactory $ requestFactory ;
40
43
44
+ /** @var CustomerRepositoryInterface */
45
+ private CustomerRepositoryInterface $ customerRepository ;
46
+
47
+ /** @var Registry */
48
+ private $ registry ;
49
+
41
50
/**
42
51
* @return void
43
52
*/
@@ -49,6 +58,33 @@ protected function setUp(): void
49
58
parent ::setUp ();
50
59
}
51
60
61
+ /**
62
+ * @magentoDataFixture Magento/Customer/_files/customer.php
63
+ * @magentoDataFixture Magento/Customer/_files/customer_address.php
64
+ * @dataProvider customerDataProvider
65
+ * @return void
66
+ * @throws \Exception
67
+ */
68
+ public function testCustomerState (string $ query , array $ variables , array $ variables2 , array $ authInfo , string $ operationName , string $ expected )
69
+ {
70
+ if ($ operationName === 'createCustomer ' ) {
71
+ $ this ->customerRepository = $ this ->objectManager ->get (CustomerRepositoryInterface::class);
72
+ $ this ->registry = $ this ->objectManager ->get (Registry::class);
73
+ $ this ->registry ->register ('isSecureArea ' , true );
74
+ try {
75
+ $ customer = $ this ->customerRepository ->get ($ variables ['email ' ]);
76
+ $ this ->customerRepository ->delete ($ customer );
77
+ $ customer2 = $ this ->customerRepository ->get ($ variables2 ['email ' ]);
78
+ $ this ->customerRepository ->delete ($ customer2 );
79
+ } catch (\Exception $ e ) {
80
+ // Customer does not exist
81
+ } finally {
82
+ $ this ->registry ->unregister ('isSecureArea ' , false );
83
+ }
84
+ }
85
+ $ this ->testState ($ query , $ variables , $ variables2 , $ authInfo , $ operationName , $ expected );
86
+ }
87
+
52
88
/**
53
89
* Runs various GraphQL queries and checks if state of shared objects in Object Manager have changed
54
90
* @magentoConfigFixture base_website btob/website_configuration/company_active 1
@@ -58,19 +94,20 @@ protected function setUp(): void
58
94
* @param string $query
59
95
* @param array $variables
60
96
* @param array $variables2 This is the second set of variables to be used in the second request
97
+ * @param array $authInfo
61
98
* @param string $operationName
62
99
* @param string $expected
63
100
* @return void
64
101
* @throws \Exception
65
102
*/
66
- public function testState (string $ query , array $ variables , array $ variables2 , string $ operationName , string $ expected ): void
103
+ public function testState (string $ query , array $ variables , array $ variables2 , array $ authInfo , string $ operationName , string $ expected ): void
67
104
{
68
105
$ jsonEncodedRequest = json_encode ([
69
106
'query ' => $ query ,
70
107
'variables ' => $ variables ,
71
108
'operationName ' => $ operationName
72
109
]);
73
- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
110
+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
74
111
$ this ->assertStringContainsString ($ expected , $ output1 );
75
112
if ($ variables2 ) {
76
113
$ jsonEncodedRequest = json_encode ([
@@ -79,21 +116,22 @@ public function testState(string $query, array $variables, array $variables2, st
79
116
'operationName ' => $ operationName
80
117
]);
81
118
}
82
- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
119
+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
83
120
$ this ->assertStringContainsString ($ expected , $ output2 );
84
121
}
85
122
86
123
/**
87
124
* @param string $query
88
125
* @param string $operationName
126
+ * @param array $authInfo
89
127
* @param bool $firstRequest
90
128
* @return string
91
129
* @throws \Exception
92
130
*/
93
- private function request (string $ query , string $ operationName , bool $ firstRequest = false ): string
131
+ private function request (string $ query , string $ operationName , array $ authInfo , bool $ firstRequest = false ): string
94
132
{
95
133
$ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
96
- $ response = $ this ->doRequest ($ query );
134
+ $ response = $ this ->doRequest ($ query, $ authInfo );
97
135
$ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
98
136
$ result = $ this ->comparator ->compare ($ operationName );
99
137
$ this ->assertEmpty (
@@ -113,13 +151,20 @@ private function request(string $query, string $operationName, bool $firstReques
113
151
* @param string $query
114
152
* @return string
115
153
*/
116
- private function doRequest (string $ query )
154
+ private function doRequest (string $ query, array $ authInfo )
117
155
{
118
156
$ request = $ this ->requestFactory ->create ();
119
157
$ request ->setContent ($ query );
120
158
$ request ->setMethod ('POST ' );
121
159
$ request ->setPathInfo ('/graphql ' );
122
160
$ request ->getHeaders ()->addHeaders (['content_type ' => self ::CONTENT_TYPE ]);
161
+ if ($ authInfo ) {
162
+ $ email = $ authInfo ['email ' ];
163
+ $ password = $ authInfo ['password ' ];
164
+ $ customerToken = $ this ->objectManager ->get (CustomerTokenServiceInterface::class)
165
+ ->createCustomerAccessToken ($ email , $ password );
166
+ $ request ->getHeaders ()->addHeaders (['Authorization ' => 'Bearer ' . $ customerToken ]);
167
+ }
123
168
$ unusedResponse = $ this ->objectManager ->create (HttpResponse::class);
124
169
$ httpApp = $ this ->objectManager ->create (
125
170
HttpApp::class,
@@ -170,6 +215,7 @@ public function queryDataProvider(): array
170
215
QUERY,
171
216
['id ' => 4 ],
172
217
[],
218
+ [],
173
219
'navigationMenu ' ,
174
220
'"id":4,"name":"Category 1.1","product_count":2, '
175
221
],
@@ -220,6 +266,7 @@ public function queryDataProvider(): array
220
266
QUERY,
221
267
['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
222
268
[],
269
+ [],
223
270
'productDetailByName ' ,
224
271
'"sku":"configurable","name":"Configurable Product" '
225
272
],
@@ -269,6 +316,7 @@ public function queryDataProvider(): array
269
316
QUERY,
270
317
['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
271
318
[],
319
+ [],
272
320
'category ' ,
273
321
'"url_key":"category-1-1","name":"Category 1.1" '
274
322
],
@@ -333,6 +381,7 @@ public function queryDataProvider(): array
333
381
QUERY,
334
382
['name ' => 'Simple Product1 ' , 'onServer ' => false ],
335
383
[],
384
+ [],
336
385
'productDetail ' ,
337
386
'"sku":"simple1","name":"Simple Product1" '
338
387
],
@@ -347,8 +396,175 @@ public function queryDataProvider(): array
347
396
QUERY,
348
397
['urlKey ' => 'no-route ' ],
349
398
[],
399
+ [],
350
400
'resolveUrl ' ,
351
401
'"type":"CMS_PAGE","id":1 '
402
+ ],
403
+ ];
404
+ }
405
+ /**
406
+ * Queries, variables, operation names, and expected responses for test
407
+ *
408
+ * @return array[]
409
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
410
+ */
411
+ public function customerDataProvider (): array
412
+ {
413
+ return [
414
+ 'Create Customer ' => [
415
+ <<<'QUERY'
416
+ mutation($firstname: String!, $lastname: String!, $email: String!, $password: String!) {
417
+ createCustomerV2(
418
+ input: {
419
+ firstname: $firstname,
420
+ lastname: $lastname,
421
+ email: $email,
422
+ password: $password
423
+ }
424
+ ) {
425
+ customer {
426
+ created_at
427
+ prefix
428
+ firstname
429
+ middlename
430
+ lastname
431
+ suffix
432
+ email
433
+ default_billing
434
+ default_shipping
435
+ date_of_birth
436
+ taxvat
437
+ is_subscribed
438
+ gender
439
+ allow_remote_shopping_assistance
440
+ }
441
+ }
442
+ }
443
+ QUERY,
444
+ [
445
+ 'firstname ' => 'John ' ,
446
+ 'lastname ' => 'Doe ' ,
447
+ 'email ' => 'email1@example.com ' ,
448
+ 'password ' => 'Password-1 ' ,
449
+ ],
450
+ [
451
+ 'firstname ' => 'John ' ,
452
+ 'lastname ' => 'Doe ' ,
453
+ 'email ' => 'email2@adobe.com ' ,
454
+ 'password ' => 'Password-2 ' ,
455
+ ],
456
+ [],
457
+ 'createCustomer ' ,
458
+ '"email":" ' ,
459
+ ],
460
+ 'Update Customer ' => [
461
+ <<<'QUERY'
462
+ mutation($allow: Boolean!) {
463
+ updateCustomerV2(
464
+ input: {
465
+ allow_remote_shopping_assistance: $allow
466
+ }
467
+ ) {
468
+ customer {
469
+ allow_remote_shopping_assistance
470
+ }
471
+ }
472
+ }
473
+ QUERY,
474
+ ['allow ' => true ],
475
+ ['allow ' => false ],
476
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
477
+ 'updateCustomer ' ,
478
+ 'allow_remote_shopping_assistance '
479
+ ],
480
+ 'Update Customer Address ' => [
481
+ <<<'QUERY'
482
+ mutation($addressId: Int!, $city: String!) {
483
+ updateCustomerAddress(id: $addressId, input: {
484
+ region: {
485
+ region: "Alberta"
486
+ region_id: 66
487
+ region_code: "AB"
488
+ }
489
+ country_code: CA
490
+ street: ["Line 1 Street","Line 2"]
491
+ company: "Company Name"
492
+ telephone: "123456789"
493
+ fax: "123123123"
494
+ postcode: "7777"
495
+ city: $city
496
+ firstname: "Adam"
497
+ lastname: "Phillis"
498
+ middlename: "A"
499
+ prefix: "Mr."
500
+ suffix: "Jr."
501
+ vat_id: "1"
502
+ default_shipping: true
503
+ default_billing: true
504
+ }) {
505
+ id
506
+ customer_id
507
+ region {
508
+ region
509
+ region_id
510
+ region_code
511
+ }
512
+ country_code
513
+ street
514
+ company
515
+ telephone
516
+ fax
517
+ postcode
518
+ city
519
+ firstname
520
+ lastname
521
+ middlename
522
+ prefix
523
+ suffix
524
+ vat_id
525
+ default_shipping
526
+ default_billing
527
+ }
528
+ }
529
+ QUERY,
530
+ ['addressId ' => 1 , 'city ' => 'New York ' ],
531
+ ['addressId ' => 1 , 'city ' => 'Austin ' ],
532
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
533
+ 'updateCustomerAddress ' ,
534
+ 'city '
535
+ ],
536
+ 'Update Customer Email ' => [
537
+ <<<'QUERY'
538
+ mutation($email: String!, $password: String!) {
539
+ updateCustomerEmail(
540
+ email: $email
541
+ password: $password
542
+ ) {
543
+ customer {
544
+ email
545
+ }
546
+ }
547
+ }
548
+ QUERY,
549
+ ['email ' => 'customer2@example.com ' , 'password ' => 'password ' ],
550
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
551
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
552
+ 'updateCustomerEmail ' ,
553
+ 'email '
554
+ ],
555
+ 'Generate Customer Token ' => [
556
+ <<<'QUERY'
557
+ mutation($email: String!, $password: String!) {
558
+ generateCustomerToken(email: $email, password: $password) {
559
+ token
560
+ }
561
+ }
562
+ QUERY,
563
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
564
+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
565
+ [],
566
+ 'generateCustomerToken ' ,
567
+ 'token '
352
568
]
353
569
];
354
570
}
0 commit comments