Skip to content

Commit 822b22d

Browse files
committed
Merge branch 'graphql-issue-230' of github.com:magento-honey-badgers/magento2ce into graphql-issue-230
2 parents 965e3bf + 124ad9d commit 822b22d

File tree

3 files changed

+98
-24
lines changed

3 files changed

+98
-24
lines changed

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

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
/**
1818
* Class CacheTagTest
1919
*/
20+
/**
21+
* Test the caching works properly for products and categories
22+
*/
2023
class CacheTagTest extends GraphQlAbstract
2124
{
2225
/**
2326
* 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
2528
*/
26-
public function testCacheTagsAndCacheDebugHeaderFromResponse()
29+
public function testCacheTagsAndCacheDebugHeaderForProducts()
2730
{
2831
$this->markTestSkipped(
2932
'This test will stay skipped until DEVOPS-4924 is resolved'
@@ -79,18 +82,19 @@ public function testCacheTagsAndCacheDebugHeaderFromResponse()
7982
}
8083

8184
/**
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
8386
*
84-
* @magentoApiDataFixture Magento/Catalog/_files/category_product.php
87+
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
8588
*/
8689
public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
8790
{
88-
$this->markTestSkipped(
91+
/*$this->markTestSkipped(
8992
'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
9498
= <<<'QUERY'
9599
query GetCategoryQuery($id: Int!, $pageSize: Int!, $currentPage: Int!) {
96100
category(id: $id) {
@@ -110,33 +114,82 @@ public function testCacheTagFromResponseHeaderForCategoriesWithProduct()
110114
}
111115
QUERY;
112116
$variables =[
113-
'id' => 333,
117+
'id' => 10,
114118
'pageSize'=> 10,
115119
'currentPage' => 1
116120
];
117121

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, '', []);
123150

124151
/** cache-debug header value should be a MISS when category is loaded first time */
125152
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
126153
$this->assertEquals('MISS', rtrim($matchesMiss[1], "\r"));
127154

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+
128162
/** checks to see if the X-Magento-Tags for category is displayed correctly */
129163
preg_match('/X-Magento-Tags: (.*?)\n/', $responseMissHeaders, $headerCacheTags);
130164
$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, '', []);
139182
preg_match('/X-Magento-Cache-Debug: (.*?)\n/', $responseMissHeaders, $matchesMiss);
140183
$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"));
141194
}
142195
}

dev/tests/integration/testsuite/Magento/Catalog/_files/categories.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,24 @@
267267
$product->getSku(),
268268
[10, 11, 12, 13]
269269
);
270+
271+
/** @var $product \Magento\Catalog\Model\Product */
272+
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
273+
$product->isObjectNew(true);
274+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
275+
->setAttributeSetId($defaultAttributeSet)
276+
->setStoreId(1)
277+
->setWebsiteIds([1])
278+
->setName('Simple Product Five')
279+
->setSku('simple-5')
280+
->setPrice(10)
281+
->setWeight(18)
282+
->setStockData(['use_config_manage_stock' => 0])
283+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
284+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
285+
->save();
286+
287+
$categoryLinkManagement->assignProductToCategories(
288+
$product->getSku(),
289+
[10, 11, 12, 13]
290+
);

dev/tests/integration/testsuite/Magento/Catalog/_files/categories_rollback.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// Remove products
1515
/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
1616
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
17-
$productsToDelete = ['simple', '12345', 'simple-3', 'simple-4'];
17+
$productsToDelete = ['simple', '12345', 'simple-3', 'simple-4','simple-5'];
1818

1919
foreach ($productsToDelete as $sku) {
2020
try {

0 commit comments

Comments
 (0)