Skip to content

Commit 1fc6856

Browse files
dthampyamolina-adobe
authored andcommitted
PWA-1861: Caching Customer Requests for GraphQL in Varnish
- completed varnish tests
1 parent 026e5b2 commit 1fc6856

File tree

1 file changed

+181
-44
lines changed
  • dev/tests/api-functional/testsuite/Magento/GraphQl/PageCache

1 file changed

+181
-44
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/PageCache/VarnishTest.php

Lines changed: 181 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
*/
1616
class VarnishTest extends GraphQlAbstract
1717
{
18+
protected function setUp(): void
19+
{
20+
$this->markTestSkipped("Tests are skipped until vcl files are merged into mainline");
21+
}
1822
/**
1923
* Test that we obtain cache MISS/HIT when expected for a guest.
2024
*
@@ -42,8 +46,8 @@ public function testCacheResultForGuest()
4246
* Test that changing the STORE header returns different cache results.
4347
*
4448
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
45-
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
4649
* @magentoApiDataFixture Magento/Store/_files/second_store.php
50+
* @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
4751
*/
4852
public function testCacheResultForGuestWithStoreHeader()
4953
{
@@ -58,85 +62,186 @@ public function testCacheResultForGuestWithStoreHeader()
5862
$this->assertCacheHit($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]);
5963

6064
// Obtain a new X-Magento-Cache-Id using after updating the STORE header
61-
$secondStoreResponse = $this->graphQlQueryWithResponseHeaders($query, [
65+
$secondStoreResponse = $this->graphQlQueryWithResponseHeaders(
66+
$query,
67+
[],
68+
'',
69+
[
6270
CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId,
63-
'STORE' => 'fixture_second_store'
64-
]);
71+
'Store' => 'fixture_second_store'
72+
]
73+
);
6574
$secondStoreCacheId = $secondStoreResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
6675

6776
// Verify we obtain a cache MISS the first time we search by this X-Magento-Cache-Id
6877
$this->assertCacheMiss($query, [
6978
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
70-
'STORE' => 'fixture_second_store'
79+
'Store' => 'fixture_second_store'
7180
]);
7281

7382
// Verify we obtain a cache HIT the second time around with the STORE header
7483
$this->assertCacheHit($query, [
7584
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
76-
'STORE' => 'fixture_second_store'
85+
'Store' => 'fixture_second_store'
7786
]);
7887

7988
// Verify we still obtain a cache HIT for the default store
8089
$this->assertCacheHit($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]);
8190
}
8291

8392
/**
84-
* Test that changing the CONTENT-CURRENCY header returns different cache results.
93+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
94+
* @magentoApiDataFixture Magento/Store/_files/multiple_currencies.php
95+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
8596
*/
8697
public function testCacheResultForGuestWithCurrencyHeader()
8798
{
88-
// obtain cache id
89-
// cache miss
90-
// cache hit
91-
// set CONTENT-CURRENCY header
92-
// obtain new cache id and set it on the request
93-
// cache miss
94-
// cache hit
95-
// remove CONTENT-CURRENCY header and use original cache id
96-
// cache hit
99+
$productSku = 'simple_product';
100+
$query = $this->getProductQuery($productSku);
101+
102+
// Verify caching works as expected without a currency header
103+
$response = $this->graphQlQueryWithResponseHeaders($query);
104+
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
105+
$defaultCurrencyCacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
106+
$this->assertCacheMiss($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]);
107+
$this->assertCacheHit($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]);
108+
109+
// Obtain a new X-Magento-Cache-Id using after updating the CONTENT-CURRENCY header
110+
$secondCurrencyResponse = $this->graphQlQueryWithResponseHeaders(
111+
$query,
112+
[],
113+
'',
114+
[
115+
CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId,
116+
'content-currency' => 'EUR'
117+
]
118+
);
119+
$secondCurrencyCacheId = $secondCurrencyResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
120+
121+
// Verify we obtain a cache MISS the first time we search by this X-Magento-Cache-Id
122+
$this->assertCacheMiss($query, [
123+
CacheIdCalculator::CACHE_ID_HEADER => $secondCurrencyCacheId,
124+
'content-currency' => 'EUR'
125+
]);
126+
127+
// Verify we obtain a cache HIT the second time around with the changed currency header
128+
$this->assertCacheHit($query, [
129+
CacheIdCalculator::CACHE_ID_HEADER => $secondCurrencyCacheId,
130+
'content-currency' => 'EUR'
131+
]);
132+
133+
// Verify we still obtain a cache HIT for the default currency ( no content-currency header)
134+
$this->assertCacheHit($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCurrencyCacheId]);
97135
}
98136

99137
/**
100138
* Test that a request with a cache id which differs from the one returned by the response is not cacheable.
139+
*
140+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
141+
* @magentoApiDataFixture Magento/Store/_files/second_store.php
142+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
101143
*/
102144
public function testCacheResultForGuestWithOutdatedCacheId()
103145
{
104-
// obtain cache id
105-
// cache miss
106-
// cache hit
107-
// set STORE header
108-
// obtain new cache id, but continue using old cache id
109-
// cache miss
110-
// cache miss (since supplied cache id does not match cache id from response)
111-
// update header with new cache id
112-
// cache miss
113-
// cache hit
146+
$productSku = 'simple_product';
147+
$query = $this->getProductQuery($productSku);
148+
149+
// Verify caching with no headers in the request
150+
$response = $this->graphQlQueryWithResponseHeaders($query);
151+
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $response['headers']);
152+
$defaultCacheId = $response['headers'][CacheIdCalculator::CACHE_ID_HEADER];
153+
$this->assertCacheMiss($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId]);
154+
$this->assertCacheHit($query, [CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId]);
155+
156+
// Obtain a new X-Magento-Cache-Id using after updating the request with STORE header
157+
$responseWithStore = $this->graphQlQueryWithResponseHeaders(
158+
$query,
159+
[],
160+
'',
161+
[
162+
CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId,
163+
'STORE' => 'fixture_second_store'
164+
]
165+
);
166+
$storeCacheId = $responseWithStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
167+
168+
// Verify we obtain a cache MISS since we use the old cache id
169+
$this->assertCacheMiss($query, [
170+
CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId,
171+
'STORE' => 'fixture_second_store'
172+
]);
173+
174+
// Verify we obtain cache MISS again since the cache id in the request doesn't match the cache id from response
175+
$this->assertCacheMiss($query, [
176+
CacheIdCalculator::CACHE_ID_HEADER => $defaultCacheId,
177+
'STORE' => 'fixture_second_store'
178+
]);
179+
180+
// Verify we get a cache MISS first time with the updated cache id
181+
$this->assertCacheMiss($query, [
182+
CacheIdCalculator::CACHE_ID_HEADER => $storeCacheId,
183+
'STORE' => 'fixture_second_store'
184+
]);
185+
186+
// Verify we obtain a cache HIT second time around with the updated cache id
187+
$this->assertCacheHit($query, [
188+
CacheIdCalculator::CACHE_ID_HEADER => $storeCacheId,
189+
'STORE' => 'fixture_second_store'
190+
]);
114191
}
115192

116193
/**
117194
* Test that we obtain cache MISS/HIT when expected for a customer.
195+
*
196+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
197+
* @magentoApiDataFixture Magento/Customer/_files/customer.php
198+
* @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php
118199
*/
119200
public function testCacheResultForCustomer()
120201
{
121-
// generateCustomerToken
122-
// obtain auth token
123-
// obtain cache id
124-
// cache miss
125-
// cache hit
126-
}
202+
$productSku = 'simple_product';
203+
$query = $this->getProductQuery($productSku);
127204

128-
/**
129-
* Test that omitting the Auth token does not send cached content for a logged-in customer.
130-
*/
131-
public function testCacheResultForCustomerWithMissingAuthToken()
132-
{
133-
// generateCustomerToken
134-
// obtain auth token
135-
// obtain cache id
136-
// cache miss
137-
// cache hit
138-
// unset auth token
139-
// cache miss
205+
$email = 'customer@example.com';
206+
$password = 'password';
207+
$generateToken = $this->generateCustomerToken($email, $password);
208+
$tokenResponse = $this->graphQlMutationWithResponseHeaders($generateToken);
209+
210+
// Obtain the X-Magento-Cache-id from the response and authorization token - customer logs in
211+
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $tokenResponse['headers']);
212+
$cacheIdCustomer = $tokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
213+
$customerToken = $tokenResponse['body']['generateCustomerToken']['token'];
214+
215+
// Verify we obtain cache MISS the first time we search by this X-Magento-Cache-Id
216+
$this->assertCacheMiss($query, [
217+
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
218+
'Authorization' => 'Bearer ' . $customerToken
219+
]);
220+
221+
// Verify we obtain cache HIT second time using the same X-Magento-Cache-Id
222+
$this->assertCacheHit($query, [
223+
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
224+
'Authorization' => 'Bearer ' . $customerToken
225+
]);
226+
$revokeTokenQuery = $this->revokeCustomerToken();
227+
228+
// Verify that once customer logs out, X-Magento-Cache-Id will be that of an unregistered user
229+
$revokeTokenResponse = $this->graphQlMutationWithResponseHeaders(
230+
$revokeTokenQuery,
231+
[],
232+
'',
233+
[
234+
CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer,
235+
'Authorization' => 'Bearer ' . $customerToken
236+
]
237+
);
238+
239+
$cacheIdGuest = $revokeTokenResponse['headers'][CacheIdCalculator::CACHE_ID_HEADER];
240+
$this->assertNotEquals($cacheIdCustomer, $cacheIdGuest);
241+
242+
//Verify that omitting the Auth token doesn't send cached content for a logged-in customer
243+
$this->assertCacheMiss($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
244+
$this->assertCacheMiss($query, [CacheIdCalculator::CACHE_ID_HEADER => $cacheIdCustomer]);
140245
}
141246

142247
/**
@@ -188,4 +293,36 @@ private function getProductQuery(string $productSku): string
188293

189294
return $productQuery;
190295
}
296+
297+
/**
298+
* @param string $email
299+
* @param string $password
300+
* @return string
301+
*/
302+
private function generateCustomerToken(string $email, string $password) : string
303+
{
304+
return <<<MUTATION
305+
mutation {
306+
generateCustomerToken(
307+
email: "{$email}"
308+
password: "{$password}"
309+
) {
310+
token
311+
}
312+
}
313+
MUTATION;
314+
}
315+
316+
/**
317+
* @return string
318+
*/
319+
private function revokeCustomerToken() : string
320+
{
321+
return <<<MUTATION
322+
mutation {
323+
revokeCustomerToken
324+
{ result }
325+
}
326+
MUTATION;
327+
}
191328
}

0 commit comments

Comments
 (0)