1212use Magento \Framework \App \Response \Http as HttpResponse ;
1313use Magento \Framework \ObjectManagerInterface ;
1414use Magento \GraphQl \App \State \Comparator ;
15+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
1516use Magento \TestFramework \Helper \Bootstrap ;
1617
1718/**
@@ -38,6 +39,11 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
3839 /** @var RequestFactory */
3940 private RequestFactory $ requestFactory ;
4041
42+ /**
43+ * @var CustomerTokenServiceInterface
44+ */
45+ private CustomerTokenServiceInterface $ customerTokenService ;
46+
4147 /**
4248 * @return void
4349 */
@@ -46,6 +52,7 @@ protected function setUp(): void
4652 $ this ->objectManager = Bootstrap::getObjectManager ();
4753 $ this ->comparator = $ this ->objectManager ->create (Comparator::class);
4854 $ this ->requestFactory = $ this ->objectManager ->get (RequestFactory::class);
55+ $ this ->customerTokenService = $ this ->objectManager ->get (CustomerTokenServiceInterface::class);
4956 parent ::setUp ();
5057 }
5158
@@ -54,23 +61,26 @@ protected function setUp(): void
5461 * @magentoConfigFixture base_website btob/website_configuration/company_active 1
5562 * @magentoConfigFixture default_store btob/website_configuration/company_active 1
5663 * @magentoConfigFixture default_store company/general/allow_company_registration 1
64+ * @magentoDataFixture Magento/Customer/_files/customer.php
65+ * @magentoDataFixture Magento/Customer/_files/customer_address.php
5766 * @dataProvider queryDataProvider
5867 * @param string $query
5968 * @param array $variables
6069 * @param array $variables2 This is the second set of variables to be used in the second request
70+ * @param array $authInfo
6171 * @param string $operationName
6272 * @param string $expected
6373 * @return void
6474 * @throws \Exception
6575 */
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
6777 {
6878 $ jsonEncodedRequest = json_encode ([
6979 'query ' => $ query ,
7080 'variables ' => $ variables ,
7181 'operationName ' => $ operationName
7282 ]);
73- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
83+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
7484 $ this ->assertStringContainsString ($ expected , $ output1 );
7585 if ($ variables2 ) {
7686 $ jsonEncodedRequest = json_encode ([
@@ -79,21 +89,22 @@ public function testState(string $query, array $variables, array $variables2, st
7989 'operationName ' => $ operationName
8090 ]);
8191 }
82- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
92+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
8393 $ this ->assertStringContainsString ($ expected , $ output2 );
8494 }
8595
8696 /**
8797 * @param string $query
8898 * @param string $operationName
99+ * @param array $authInfo
89100 * @param bool $firstRequest
90101 * @return string
91102 * @throws \Exception
92103 */
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
94105 {
95106 $ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
96- $ response = $ this ->doRequest ($ query );
107+ $ response = $ this ->doRequest ($ query, $ authInfo );
97108 $ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
98109 $ result = $ this ->comparator ->compare ($ operationName );
99110 $ this ->assertEmpty (
@@ -113,13 +124,19 @@ private function request(string $query, string $operationName, bool $firstReques
113124 * @param string $query
114125 * @return string
115126 */
116- private function doRequest (string $ query )
127+ private function doRequest (string $ query, array $ authInfo )
117128 {
118129 $ request = $ this ->requestFactory ->create ();
119130 $ request ->setContent ($ query );
120131 $ request ->setMethod ('POST ' );
121132 $ request ->setPathInfo ('/graphql ' );
122133 $ 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+ }
123140 $ unusedResponse = $ this ->objectManager ->create (HttpResponse::class);
124141 $ httpApp = $ this ->objectManager ->create (
125142 HttpApp::class,
@@ -170,6 +187,7 @@ public function queryDataProvider(): array
170187 QUERY,
171188 ['id ' => 4 ],
172189 [],
190+ [],
173191 'navigationMenu ' ,
174192 '"id":4,"name":"Category 1.1","product_count":2, '
175193 ],
@@ -220,6 +238,7 @@ public function queryDataProvider(): array
220238 QUERY,
221239 ['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
222240 [],
241+ [],
223242 'productDetailByName ' ,
224243 '"sku":"configurable","name":"Configurable Product" '
225244 ],
@@ -269,6 +288,7 @@ public function queryDataProvider(): array
269288 QUERY,
270289 ['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
271290 [],
291+ [],
272292 'category ' ,
273293 '"url_key":"category-1-1","name":"Category 1.1" '
274294 ],
@@ -333,6 +353,7 @@ public function queryDataProvider(): array
333353 QUERY,
334354 ['name ' => 'Simple Product1 ' , 'onServer ' => false ],
335355 [],
356+ [],
336357 'productDetail ' ,
337358 '"sku":"simple1","name":"Simple Product1" '
338359 ],
@@ -347,9 +368,167 @@ public function queryDataProvider(): array
347368 QUERY,
348369 ['urlKey ' => 'no-route ' ],
349370 [],
371+ [],
350372 'resolveUrl ' ,
351373 '"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+ 'email ' => 'email@adobe.com ' ,
410+ 'password ' => 'Password-1 ' ,
411+ ],
412+ [
413+ 'firstname ' => 'John ' ,
414+ 'lastname ' => 'Doe ' ,
415+ 'email ' => 'email2@adobe.com ' ,
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 ' => 'customer@example.com ' , '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 ' => 'customer@example.com ' , '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 ' => 'customer2@example.com ' , 'password ' => 'password ' ],
512+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
513+ ['email ' => 'customer@example.com ' , '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 ' => 'customer@example.com ' , 'password ' => 'password ' ],
526+ ['email ' => 'customer@example.com ' , 'password ' => 'password ' ],
527+ [],
528+ 'generateCustomerToken ' ,
529+ 'token '
352530 ]
531+
353532 ];
354533 }
355534}
0 commit comments