17
17
/**
18
18
* Class CacheTagTest
19
19
*/
20
+ /**
21
+ * Test the caching works properly for products and categories
22
+ */
20
23
class CacheTagTest extends GraphQlAbstract
21
24
{
22
25
/**
23
26
* Tests if Magento cache tags and debug headers for products are generated properly
24
- * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_url_key .php
27
+ * @magentoApiDataFixture Magento/Catalog/_files/multiple_products .php
25
28
*/
26
- public function testCacheTagsAndCacheDebugHeaderFromResponse ()
29
+ public function testCacheTagsAndCacheDebugHeaderForProducts ()
27
30
{
28
31
$ this ->markTestSkipped (
29
32
'This test will stay skipped until DEVOPS-4924 is resolved '
@@ -79,18 +82,19 @@ public function testCacheTagsAndCacheDebugHeaderFromResponse()
79
82
}
80
83
81
84
/**
82
- * Tests if Magento cache tags for categories are generated properly
85
+ * Tests if Magento cache tags for categories are generated properly. Also tests the use case for cache invalidation
83
86
*
84
- * @magentoApiDataFixture Magento/Catalog/_files/category_product .php
87
+ * @magentoApiDataFixture Magento/Catalog/_files/categories .php
85
88
*/
86
89
public function testCacheTagFromResponseHeaderForCategoriesWithProduct ()
87
90
{
88
- $ this ->markTestSkipped (
91
+ /* $this->markTestSkipped(
89
92
'This test will stay skipped until DEVOPS-4924 is resolved'
90
- );
91
- $ productSku = 'simple333 ' ;
92
- $ categoryId ='333 ' ;
93
- $ query
93
+ );*/
94
+ $ firstProductSku = 'simple-4 ' ;
95
+ $ secondProductSku = 'simple-5 ' ;
96
+ $ categoryId ='10 ' ;
97
+ $ categoryQuery
94
98
= <<<'QUERY'
95
99
query GetCategoryQuery($id: Int!, $pageSize: Int!, $currentPage: Int!) {
96
100
category(id: $id) {
@@ -110,33 +114,82 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
110
114
}
111
115
QUERY;
112
116
$ variables =[
113
- 'id ' => 333 ,
117
+ 'id ' => 10 ,
114
118
'pageSize ' => 10 ,
115
119
'currentPage ' => 1
116
120
];
117
121
118
- $ responseMissHeaders = $ this ->graphQlQueryForHttpHeaders ($ query , $ variables , '' , []);
119
- /** @var ProductRepositoryInterface $productRepository */
120
- $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
121
- /** @var Product $product */
122
- $ product =$ productRepository ->get ($ productSku , false , null , true );
122
+ $ product1Query
123
+ = <<<QUERY
124
+ {
125
+ products(filter: {sku: {eq: " {$ firstProductSku }"}})
126
+ {
127
+ items {
128
+ id
129
+ name
130
+ sku
131
+ }
132
+ }
133
+ }
134
+ QUERY ;
135
+ $ product2Query
136
+ = <<<QUERY
137
+ {
138
+ products(filter: {sku: {eq: " {$ secondProductSku }"}})
139
+ {
140
+ items {
141
+ id
142
+ name
143
+ sku
144
+ }
145
+ }
146
+ }
147
+ QUERY ;
148
+
149
+ $ responseMissHeaders = $ this ->graphQlQueryForHttpHeaders ($ categoryQuery , $ variables , '' , []);
123
150
124
151
/** cache-debug header value should be a MISS when category is loaded first time */
125
152
preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseMissHeaders , $ matchesMiss );
126
153
$ this ->assertEquals ('MISS ' , rtrim ($ matchesMiss [1 ], "\r" ));
127
154
155
+ /** @var ProductRepositoryInterface $productRepository */
156
+ $ productRepository = ObjectManager::getInstance ()->get (ProductRepositoryInterface::class);
157
+ /** @var Product $firstProduct */
158
+ $ firstProduct = $ productRepository ->get ($ firstProductSku , false , null , true );
159
+ /** @var Product $secondProduct */
160
+ $ secondProduct = $ productRepository ->get ($ secondProductSku , false , null , true );
161
+
128
162
/** checks to see if the X-Magento-Tags for category is displayed correctly */
129
163
preg_match ('/X-Magento-Tags: (.*?)\n/ ' , $ responseMissHeaders , $ headerCacheTags );
130
164
$ actualCacheTags = explode (', ' , rtrim ($ headerCacheTags [1 ], "\r" ));
131
- $ expectedCacheTags =['cat_c ' ,'cat_c_ ' . $ categoryId ,'cat_p ' ,'cat_p_ ' . $ product ->getId (),'FPC ' ];
132
- foreach (array_keys ($ actualCacheTags ) as $ key ) {
133
- $ this ->assertEquals ($ expectedCacheTags [$ key ], $ actualCacheTags [$ key ]);
134
- }
135
- /** cache-debug header value should be MISS after updating child-product and reloading the category */
136
- $ product ->setPrice (15 );
137
- $ product ->save ();
138
- $ responseMissHeaders = $ this ->graphQlQueryForHttpHeaders ($ query , $ variables , '' , []);
165
+ $ expectedCacheTags =
166
+ ['cat_c ' ,'cat_c_ ' . $ categoryId ,'cat_p ' ,'cat_p_ ' . $ firstProduct ->getId (),'cat_p_ ' .$ secondProduct ->getId (),'FPC ' ];
167
+ $ this ->assertEquals ($ expectedCacheTags , $ actualCacheTags );
168
+ // Cach-debug header should be a MISS for product 1 during first load
169
+ $ responseHeadersFirstProduct = $ this ->graphQlQueryForHttpHeaders ($ product1Query , [], '' , []);
170
+ preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseHeadersFirstProduct , $ match );
171
+ $ this ->assertEquals ('MISS ' , rtrim ($ match [1 ], "\r" ));
172
+
173
+ // Cach-debug header should be a MISS for product 2 during first load
174
+ $ responseHeadersSecondProduct = $ this ->graphQlQueryForHttpHeaders ($ product2Query , [], '' , []);
175
+ preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseHeadersSecondProduct , $ match );
176
+ $ this ->assertEquals ('MISS ' , rtrim ($ match [1 ], "\r" ));
177
+
178
+ /** cache-debug header value should be MISS after updating product1 and reloading the category */
179
+ $ firstProduct ->setPrice (20 );
180
+ $ firstProduct ->save ();
181
+ $ responseMissHeaders = $ this ->graphQlQueryForHttpHeaders ($ categoryQuery , $ variables , '' , []);
139
182
preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseMissHeaders , $ matchesMiss );
140
183
$ this ->assertEquals ('MISS ' , rtrim ($ matchesMiss [1 ], "\r" ));
184
+
185
+ /** cache-debug should be a MISS for product 1 after it is updated - cache invalidation */
186
+ $ responseHeadersForProd1 = $ this ->graphQlQueryForHttpHeaders ($ product1Query , [], '' , []);
187
+ preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseHeadersForProd1 , $ match );
188
+ $ this ->assertEquals ('MISS ' , rtrim ($ match [1 ], "\r" ));
189
+
190
+ // Cach-debug header should be a HIT for prod 2 during second load since prod 2 should be fetched from cache
191
+ $ responseHeadersSecondProduct = $ this ->graphQlQueryForHttpHeaders ($ product2Query , [], '' , []);
192
+ preg_match ('/X-Magento-Cache-Debug: (.*?)\n/ ' , $ responseHeadersSecondProduct , $ match );
193
+ $ this ->assertEquals ('HIT ' , rtrim ($ match [1 ], "\r" ));
141
194
}
142
195
}
0 commit comments