77
88namespace Magento \GraphQl \App ;
99
10+ use Magento \Customer \Api \CustomerRepositoryInterface ;
1011use Magento \Framework \App \Http as HttpApp ;
1112use Magento \Framework \App \ObjectManager as AppObjectManager ;
1213use Magento \Framework \App \Request \HttpFactory as RequestFactory ;
1314use Magento \Framework \App \Response \Http as HttpResponse ;
1415use Magento \Framework \ObjectManagerInterface ;
16+ use Magento \Framework \Registry ;
1517use Magento \GraphQl \App \State \Comparator ;
1618use Magento \GraphQl \App \State \ObjectManager ;
19+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
1720use Magento \TestFramework \Helper \Bootstrap ;
1821
1922/**
@@ -43,6 +46,12 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
4346 /** @var RequestFactory */
4447 private RequestFactory $ requestFactory ;
4548
49+ /** @var CustomerRepositoryInterface */
50+ private CustomerRepositoryInterface $ customerRepository ;
51+
52+ /** @var Registry */
53+ private $ registry ;
54+
4655 /**
4756 * @inheritDoc
4857 */
@@ -73,6 +82,33 @@ protected function tearDown(): void
7382 parent ::tearDown ();
7483 }
7584
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+
76112 /**
77113 * Runs various GraphQL queries and checks if state of shared objects in Object Manager have changed
78114 * @magentoConfigFixture base_website btob/website_configuration/company_active 1
@@ -82,6 +118,7 @@ protected function tearDown(): void
82118 * @param string $query
83119 * @param array $variables
84120 * @param array $variables2 This is the second set of variables to be used in the second request
121+ * @param array $authInfo
85122 * @param string $operationName
86123 * @param string $expected
87124 * @return void
@@ -91,6 +128,7 @@ public function testState(
91128 string $ query ,
92129 array $ variables ,
93130 array $ variables2 ,
131+ array $ authInfo ,
94132 string $ operationName ,
95133 string $ expected ,
96134 ): void {
@@ -99,7 +137,7 @@ public function testState(
99137 'variables ' => $ variables ,
100138 'operationName ' => $ operationName
101139 ]);
102- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
140+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
103141 $ this ->assertStringContainsString ($ expected , $ output1 );
104142 if ($ variables2 ) {
105143 $ jsonEncodedRequest = json_encode ([
@@ -108,22 +146,23 @@ public function testState(
108146 'operationName ' => $ operationName
109147 ]);
110148 }
111- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
149+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
112150 $ this ->assertStringContainsString ($ expected , $ output2 );
113151 }
114152
115153 /**
116154 * @param string $query
117155 * @param string $operationName
156+ * @param array $authInfo
118157 * @param bool $firstRequest
119158 * @return string
120159 * @throws \Exception
121160 */
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
123162 {
124163 $ this ->objectManagerForTest ->resetStateSharedInstances ();
125164 $ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
126- $ response = $ this ->doRequest ($ query );
165+ $ response = $ this ->doRequest ($ query, $ authInfo );
127166 $ this ->objectManagerForTest ->resetStateSharedInstances ();
128167 $ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
129168 $ result = $ this ->comparator ->compareBetweenRequests ($ operationName );
@@ -153,13 +192,20 @@ private function request(string $query, string $operationName, bool $firstReques
153192 * @param string $query
154193 * @return string
155194 */
156- private function doRequest (string $ query )
195+ private function doRequest (string $ query, array $ authInfo )
157196 {
158197 $ request = $ this ->requestFactory ->create ();
159198 $ request ->setContent ($ query );
160199 $ request ->setMethod ('POST ' );
161200 $ request ->setPathInfo ('/graphql ' );
162201 $ 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+ }
163209 $ unusedResponse = $ this ->objectManagerForTest ->create (HttpResponse::class);
164210 $ httpApp = $ this ->objectManagerForTest ->create (
165211 HttpApp::class,
@@ -210,6 +256,7 @@ public function queryDataProvider(): array
210256 QUERY,
211257 ['id ' => 4 ],
212258 [],
259+ [],
213260 'navigationMenu ' ,
214261 '"id":4,"name":"Category 1.1","product_count":2, '
215262 ],
@@ -260,6 +307,7 @@ public function queryDataProvider(): array
260307 QUERY,
261308 ['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
262309 [],
310+ [],
263311 'productDetailByName ' ,
264312 '"sku":"configurable","name":"Configurable Product" '
265313 ],
@@ -309,6 +357,7 @@ public function queryDataProvider(): array
309357 QUERY,
310358 ['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
311359 [],
360+ [],
312361 'category ' ,
313362 '"url_key":"category-1-1","name":"Category 1.1" '
314363 ],
@@ -373,6 +422,7 @@ public function queryDataProvider(): array
373422 QUERY,
374423 ['name ' => 'Simple Product1 ' , 'onServer ' => false ],
375424 [],
425+ [],
376426 'productDetail ' ,
377427 '"sku":"simple1","name":"Simple Product1" '
378428 ],
@@ -387,8 +437,175 @@ public function queryDataProvider(): array
387437 QUERY,
388438 ['urlKey ' => 'no-route ' ],
389439 [],
440+ [],
390441 'resolveUrl ' ,
391442 '"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 '
392609 ]
393610 ];
394611 }
0 commit comments