77
88namespace Magento \GraphQl \App ;
99
10+ use Magento \Customer \Api \CustomerRepositoryInterface ;
1011use Magento \Framework \App \Http as HttpApp ;
1112use Magento \Framework \App \Request \HttpFactory as RequestFactory ;
1213use Magento \Framework \App \Response \Http as HttpResponse ;
1314use Magento \Framework \ObjectManagerInterface ;
15+ use Magento \Framework \Registry ;
1416use Magento \GraphQl \App \State \Comparator ;
17+ use Magento \Integration \Api \CustomerTokenServiceInterface ;
1518use Magento \TestFramework \Helper \Bootstrap ;
1619
1720/**
@@ -38,6 +41,12 @@ class GraphQlStateTest extends \PHPUnit\Framework\TestCase
3841 /** @var RequestFactory */
3942 private RequestFactory $ requestFactory ;
4043
44+ /** @var CustomerRepositoryInterface */
45+ private CustomerRepositoryInterface $ customerRepository ;
46+
47+ /** @var Registry */
48+ private $ registry ;
49+
4150 /**
4251 * @return void
4352 */
@@ -49,6 +58,33 @@ protected function setUp(): void
4958 parent ::setUp ();
5059 }
5160
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+
5288 /**
5389 * Runs various GraphQL queries and checks if state of shared objects in Object Manager have changed
5490 * @magentoConfigFixture base_website btob/website_configuration/company_active 1
@@ -58,19 +94,20 @@ protected function setUp(): void
5894 * @param string $query
5995 * @param array $variables
6096 * @param array $variables2 This is the second set of variables to be used in the second request
97+ * @param array $authInfo
6198 * @param string $operationName
6299 * @param string $expected
63100 * @return void
64101 * @throws \Exception
65102 */
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
67104 {
68105 $ jsonEncodedRequest = json_encode ([
69106 'query ' => $ query ,
70107 'variables ' => $ variables ,
71108 'operationName ' => $ operationName
72109 ]);
73- $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , true );
110+ $ output1 = $ this ->request ($ jsonEncodedRequest , $ operationName , $ authInfo , true );
74111 $ this ->assertStringContainsString ($ expected , $ output1 );
75112 if ($ variables2 ) {
76113 $ jsonEncodedRequest = json_encode ([
@@ -79,21 +116,22 @@ public function testState(string $query, array $variables, array $variables2, st
79116 'operationName ' => $ operationName
80117 ]);
81118 }
82- $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName );
119+ $ output2 = $ this ->request ($ jsonEncodedRequest , $ operationName, $ authInfo );
83120 $ this ->assertStringContainsString ($ expected , $ output2 );
84121 }
85122
86123 /**
87124 * @param string $query
88125 * @param string $operationName
126+ * @param array $authInfo
89127 * @param bool $firstRequest
90128 * @return string
91129 * @throws \Exception
92130 */
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
94132 {
95133 $ this ->comparator ->rememberObjectsStateBefore ($ firstRequest );
96- $ response = $ this ->doRequest ($ query );
134+ $ response = $ this ->doRequest ($ query, $ authInfo );
97135 $ this ->comparator ->rememberObjectsStateAfter ($ firstRequest );
98136 $ result = $ this ->comparator ->compare ($ operationName );
99137 $ this ->assertEmpty (
@@ -113,13 +151,20 @@ private function request(string $query, string $operationName, bool $firstReques
113151 * @param string $query
114152 * @return string
115153 */
116- private function doRequest (string $ query )
154+ private function doRequest (string $ query, array $ authInfo )
117155 {
118156 $ request = $ this ->requestFactory ->create ();
119157 $ request ->setContent ($ query );
120158 $ request ->setMethod ('POST ' );
121159 $ request ->setPathInfo ('/graphql ' );
122160 $ 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+ }
123168 $ unusedResponse = $ this ->objectManager ->create (HttpResponse::class);
124169 $ httpApp = $ this ->objectManager ->create (
125170 HttpApp::class,
@@ -170,6 +215,7 @@ public function queryDataProvider(): array
170215 QUERY,
171216 ['id ' => 4 ],
172217 [],
218+ [],
173219 'navigationMenu ' ,
174220 '"id":4,"name":"Category 1.1","product_count":2, '
175221 ],
@@ -220,6 +266,7 @@ public function queryDataProvider(): array
220266 QUERY,
221267 ['name ' => 'Configurable%20Product ' , 'onServer ' => false ],
222268 [],
269+ [],
223270 'productDetailByName ' ,
224271 '"sku":"configurable","name":"Configurable Product" '
225272 ],
@@ -269,6 +316,7 @@ public function queryDataProvider(): array
269316 QUERY,
270317 ['id ' => 4 , 'currentPage ' => 1 , 'pageSize ' => 12 ],
271318 [],
319+ [],
272320 'category ' ,
273321 '"url_key":"category-1-1","name":"Category 1.1" '
274322 ],
@@ -333,6 +381,7 @@ public function queryDataProvider(): array
333381 QUERY,
334382 ['name ' => 'Simple Product1 ' , 'onServer ' => false ],
335383 [],
384+ [],
336385 'productDetail ' ,
337386 '"sku":"simple1","name":"Simple Product1" '
338387 ],
@@ -347,8 +396,175 @@ public function queryDataProvider(): array
347396 QUERY,
348397 ['urlKey ' => 'no-route ' ],
349398 [],
399+ [],
350400 'resolveUrl ' ,
351401 '"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 '
352568 ]
353569 ];
354570 }
0 commit comments