15
15
*/
16
16
class VarnishTest extends GraphQlAbstract
17
17
{
18
+ protected function setUp (): void
19
+ {
20
+ $ this ->markTestSkipped ("Tests are skipped until vcl files are merged into mainline " );
21
+ }
18
22
/**
19
23
* Test that we obtain cache MISS/HIT when expected for a guest.
20
24
*
@@ -42,8 +46,8 @@ public function testCacheResultForGuest()
42
46
* Test that changing the STORE header returns different cache results.
43
47
*
44
48
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
45
- * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
46
49
* @magentoApiDataFixture Magento/Store/_files/second_store.php
50
+ * @magentoApiDataFixture Magento/Catalog/_files/multiple_products.php
47
51
*/
48
52
public function testCacheResultForGuestWithStoreHeader ()
49
53
{
@@ -58,85 +62,186 @@ public function testCacheResultForGuestWithStoreHeader()
58
62
$ this ->assertCacheHit ($ query , [CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ]);
59
63
60
64
// 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
+ [
62
70
CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ,
63
- 'STORE ' => 'fixture_second_store '
64
- ]);
71
+ 'Store ' => 'fixture_second_store '
72
+ ]
73
+ );
65
74
$ secondStoreCacheId = $ secondStoreResponse ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
66
75
67
76
// Verify we obtain a cache MISS the first time we search by this X-Magento-Cache-Id
68
77
$ this ->assertCacheMiss ($ query , [
69
78
CacheIdCalculator::CACHE_ID_HEADER => $ secondStoreCacheId ,
70
- 'STORE ' => 'fixture_second_store '
79
+ 'Store ' => 'fixture_second_store '
71
80
]);
72
81
73
82
// Verify we obtain a cache HIT the second time around with the STORE header
74
83
$ this ->assertCacheHit ($ query , [
75
84
CacheIdCalculator::CACHE_ID_HEADER => $ secondStoreCacheId ,
76
- 'STORE ' => 'fixture_second_store '
85
+ 'Store ' => 'fixture_second_store '
77
86
]);
78
87
79
88
// Verify we still obtain a cache HIT for the default store
80
89
$ this ->assertCacheHit ($ query , [CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ]);
81
90
}
82
91
83
92
/**
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
85
96
*/
86
97
public function testCacheResultForGuestWithCurrencyHeader ()
87
98
{
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 ]);
97
135
}
98
136
99
137
/**
100
138
* 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
101
143
*/
102
144
public function testCacheResultForGuestWithOutdatedCacheId ()
103
145
{
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
+ ]);
114
191
}
115
192
116
193
/**
117
194
* 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
118
199
*/
119
200
public function testCacheResultForCustomer ()
120
201
{
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 );
127
204
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 ]);
140
245
}
141
246
142
247
/**
@@ -188,4 +293,36 @@ private function getProductQuery(string $productSku): string
188
293
189
294
return $ productQuery ;
190
295
}
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
+ }
191
328
}
0 commit comments