@@ -283,4 +283,46 @@ private function assertNoCookiesMatchRegex(string $pattern, array $cookies): voi
283
283
}
284
284
$ this ->assertTrue ($ result , 'Failed assertion. At least one cookie in the array matches pattern: ' . $ pattern );
285
285
}
286
+
287
+ /**
288
+ * Tests that Magento\Customer\Model\Session works properly when graphql/session/disable=0
289
+ *
290
+ * @magentoApiDataFixture Magento/Customer/_files/customer.php
291
+ * @magentoConfigFixture graphql/session/disable 0
292
+ */
293
+ public function testCustomerCanQueryOwnEmailUsingSession () : void
294
+ {
295
+ $ query = '{customer{email}} ' ;
296
+ $ result = $ this ->graphQlClient ->postWithResponseHeaders ($ query , [], '' , $ this ->getAuthHeaders (), true );
297
+ // cookies are never empty and session is restarted for the authorized customer regardless current session
298
+ $ this ->assertNotEmpty ($ result ['cookies ' ]);
299
+ $ this ->assertAnyCookieMatchesRegex ('/PHPSESSID=[a-z0-9]+;/ ' , $ result ['cookies ' ]);
300
+ $ this ->assertEquals ('customer@example.com ' , $ result ['body ' ]['customer ' ]['email ' ] ?? '' );
301
+ $ result = $ this ->graphQlClient ->postWithResponseHeaders ($ query , [], '' , $ this ->getAuthHeaders ());
302
+ // cookies are never empty and session is restarted for the authorized customer
303
+ // regardless current session and missing flush
304
+ $ this ->assertNotEmpty ($ result ['cookies ' ]);
305
+ $ this ->assertAnyCookieMatchesRegex ('/PHPSESSID=[a-z0-9]+;/ ' , $ result ['cookies ' ]);
306
+ $ this ->assertEquals ('customer@example.com ' , $ result ['body ' ]['customer ' ]['email ' ] ?? '' );
307
+ /* Note: This third request is the actual one that tests that the session cookie is properly used.
308
+ * This time we don't send the Authorization header and rely on Cookie header instead.
309
+ * Because of bug in postWithResponseHeaders's $flushCookies parameter not being properly used,
310
+ * We have to manually set cookie header ourselves. :-(
311
+ */
312
+ $ cookiesToSend = '' ;
313
+ foreach ($ result ['cookies ' ] as $ cookie ) {
314
+ preg_match ('/^([^;]*);/ ' , $ cookie , $ matches );
315
+ if (!strlen ($ matches [1 ] ?? '' )) {
316
+ continue ;
317
+ }
318
+ if (!empty ($ cookiesToSend )) {
319
+ $ cookiesToSend .= '; ' ;
320
+ }
321
+ $ cookiesToSend .= $ matches [1 ];
322
+ }
323
+ $ result = $ this ->graphQlClient ->postWithResponseHeaders ($ query , [], '' , ['Cookie: ' . $ cookiesToSend ]);
324
+ $ this ->assertNotEmpty ($ result ['cookies ' ]);
325
+ $ this ->assertAnyCookieMatchesRegex ('/PHPSESSID=[a-z0-9]+;/ ' , $ result ['cookies ' ]);
326
+ $ this ->assertEquals ('customer@example.com ' , $ result ['body ' ]['customer ' ]['email ' ] ?? '' );
327
+ }
286
328
}
0 commit comments