@@ -48,6 +48,10 @@ public function setUp(): void
4848 /**
4949 * Test for checking if graphQL query sets session cookies
5050 *
51+ * Note: The reason why the first response doesn't have cookies, but the subsequent responses do is
52+ * because Magento/Framework/App/PageCache/Kernel.php removes Set-Cookie headers when the response has a
53+ * public Cache-Control. This test asserts that behaviour.
54+ *
5155 * @magentoApiDataFixture Magento/Catalog/_files/categories.php
5256 * @magentoConfigFixture graphql/session/disable 0
5357 */
@@ -71,8 +75,7 @@ public function testCheckSessionCookieWithGetCategoryList(): void
7175 $ result = $ this ->graphQlClient ->getWithResponseHeaders ($ query , [], '' , [], true );
7276 $ this ->assertEmpty ($ result ['cookies ' ]);
7377 // perform secondary request after cookies have been flushed
74- $ result = $ this ->graphQlClient ->getWithResponseHeaders ($ query , [], '' , []);
75-
78+ $ result = $ this ->graphQlClient ->getWithResponseHeaders ($ query , [], '' , [], true );
7679 // may have other cookies than session
7780 $ this ->assertNotEmpty ($ result ['cookies ' ]);
7881 $ this ->assertAnyCookieMatchesRegex ('/PHPSESSID=[a-z0-9]+;/ ' , $ result ['cookies ' ]);
@@ -280,4 +283,46 @@ private function assertNoCookiesMatchRegex(string $pattern, array $cookies): voi
280283 }
281284 $ this ->assertTrue ($ result , 'Failed assertion. At least one cookie in the array matches pattern: ' . $ pattern );
282285 }
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+ }
283328}
0 commit comments