Skip to content

Commit 138f451

Browse files
committed
B2B-2257: availableStores GraphQl query has no cache identity
1 parent 7fad247 commit 138f451

File tree

2 files changed

+197
-25
lines changed

2 files changed

+197
-25
lines changed

app/code/Magento/StoreGraphQl/Model/Resolver/Stores/ConfigIdentity.php

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,36 +33,41 @@ public function __construct(StoreManagerInterface $storeManager)
3333
public function getIdentities(array $resolvedData): array
3434
{
3535
$ids = [];
36-
$storeGroupIds = [];
37-
$store = null;
38-
$storeGroupCount = 0;
3936
foreach ($resolvedData as $storeConfig) {
4037
$ids[] = sprintf('%s_%s', StoreConfigIdentity::CACHE_TAG, $storeConfig['id']);
41-
if ($storeGroupCount < 2) {
42-
try {
43-
// Record store groups
44-
$store = $this->storeManager->getStore($storeConfig['id']);
45-
$storeGroupId = $store->getStoreGroupId();
46-
if ($storeGroupId !== null && !in_array($storeGroupId, $storeGroupIds)) {
47-
$storeGroupIds[] = $storeGroupId;
48-
$storeGroupCount ++;
49-
}
50-
} catch (NoSuchEntityException $e) {
51-
// Do nothing
52-
;
53-
}
54-
}
5538
}
56-
if ($storeGroupCount > 1) { // the resolved stores for any store groups in a website
57-
$ids[] = sprintf('%s_%s', StoreConfigIdentity::CACHE_TAG, 'website_' . $store->getWebsiteId());
58-
} elseif ($storeGroupCount == 1) { // the resolved stores for a particular store group in a website
59-
$ids[] = sprintf(
60-
'%s_%s',
61-
StoreConfigIdentity::CACHE_TAG,
62-
'website_' . $store->getWebsiteId() . 'group_' . $storeGroupIds[0]
63-
);
39+
if (!empty($resolvedData)) {
40+
$websiteId = $resolvedData[0]['website_id'];
41+
$currentStoreGroupId = $this->getCurrectStoreGroupId($resolvedData);
42+
$groupTag = $currentStoreGroupId ? 'group_' . $currentStoreGroupId : '';
43+
$ids[] = sprintf('%s_%s', StoreConfigIdentity::CACHE_TAG, 'website_' . $websiteId . $groupTag);
6444
}
6545

6646
return empty($ids) ? [] : array_merge([StoreConfigIdentity::CACHE_TAG], $ids);
6747
}
48+
49+
/**
50+
* Return current store group id if it is certain that useCurrentGroup is true in the querry
51+
*
52+
* @param array $resolvedData
53+
* @return string|int|null
54+
*/
55+
private function getCurrectStoreGroupId(array $resolvedData)
56+
{
57+
$storeGroupCodes = array_unique(array_column($resolvedData, 'store_group_code'));
58+
if (count($storeGroupCodes) == 1) {
59+
try {
60+
$store = $this->storeManager->getStore($resolvedData[0]['id']);
61+
if ($store->getWebsite()->getGroupCollection()->count() != 1) {
62+
// There are multiple store groups in the website while there is only one store group
63+
// in the resolved data. Therefore useCurrentGroup must be true in the query
64+
return $store->getStoreGroupId();
65+
}
66+
} catch (NoSuchEntityException $e) {
67+
// Do nothing
68+
;
69+
}
70+
}
71+
return null;
72+
}
6873
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Store/AvailableStoresCacheTest.php

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,6 +2041,173 @@ public function testCacheNotPurgedWithNewInactiveStoreWithSecondStoreGroupSecond
20412041
$registry->register('isSecureArea', false);
20422042
}
20432043

2044+
/**
2045+
* Creating new store with one store group website will purge the cache of availableStores
2046+
* no matter for current store group or not
2047+
*
2048+
* Test stores set up:
2049+
* STORE - WEBSITE - STORE GROUP
2050+
* default - base - main_website_store
2051+
* second_store_view - second - second_store
2052+
* third_store_view - second - third_store
2053+
*
2054+
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
2055+
* @magentoApiDataFixture Magento/Store/_files/multiple_websites_with_store_groups_stores.php
2056+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
2057+
*/
2058+
public function testCachePurgedWithNewStoreCreatedInOneStoreGroupWebsite(): void
2059+
{
2060+
$this->changeToTwoWebsitesThreeStoreGroupsThreeStores();
2061+
$query = $this->getQuery();
2062+
2063+
// Query available stores of default store's website
2064+
$responseDefaultStore = $this->graphQlQueryWithResponseHeaders($query);
2065+
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseDefaultStore['headers']);
2066+
$defaultStoreCacheId = $responseDefaultStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
2067+
// Verify we obtain a cache MISS at the 1st time
2068+
$this->assertCacheMissAndReturnResponse(
2069+
$query,
2070+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
2071+
);
2072+
2073+
// Query available stores of default store's website and store group
2074+
$currentStoreGroupQuery = $this->getQuery('true');
2075+
$responseDefaultStoreCurrentStoreGroup = $this->graphQlQueryWithResponseHeaders($currentStoreGroupQuery);
2076+
$this->assertArrayHasKey(
2077+
CacheIdCalculator::CACHE_ID_HEADER,
2078+
$responseDefaultStoreCurrentStoreGroup['headers']
2079+
);
2080+
$defaultStoreCurrentStoreGroupCacheId =
2081+
$responseDefaultStoreCurrentStoreGroup['headers'][CacheIdCalculator::CACHE_ID_HEADER];
2082+
// Verify we obtain a cache MISS at the 1st time
2083+
$this->assertCacheMissAndReturnResponse(
2084+
$currentStoreGroupQuery,
2085+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCurrentStoreGroupCacheId]
2086+
);
2087+
2088+
// Query available stores of second store's website and any store groups of the website
2089+
$secondStoreCode = 'second_store_view';
2090+
$responseSecondStore = $this->graphQlQueryWithResponseHeaders(
2091+
$query,
2092+
[],
2093+
'',
2094+
['Store' => $secondStoreCode]
2095+
);
2096+
$this->assertArrayHasKey(CacheIdCalculator::CACHE_ID_HEADER, $responseSecondStore['headers']);
2097+
$secondStoreCacheId = $responseSecondStore['headers'][CacheIdCalculator::CACHE_ID_HEADER];
2098+
$this->assertNotEquals($secondStoreCacheId, $defaultStoreCacheId);
2099+
// Verify we obtain a cache MISS at the 1st time
2100+
$this->assertCacheMissAndReturnResponse(
2101+
$query,
2102+
[
2103+
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
2104+
'Store' => $secondStoreCode
2105+
]
2106+
);
2107+
2108+
// Query available stores of second store's website and store group
2109+
$responseSecondStoreCurrentStoreGroup = $this->graphQlQueryWithResponseHeaders(
2110+
$currentStoreGroupQuery,
2111+
[],
2112+
'',
2113+
['Store' => $secondStoreCode]
2114+
);
2115+
$this->assertArrayHasKey(
2116+
CacheIdCalculator::CACHE_ID_HEADER,
2117+
$responseSecondStoreCurrentStoreGroup['headers']
2118+
);
2119+
$secondStoreCurrentStoreGroupCacheId =
2120+
$responseSecondStoreCurrentStoreGroup['headers'][CacheIdCalculator::CACHE_ID_HEADER];
2121+
$this->assertNotEquals($secondStoreCurrentStoreGroupCacheId, $defaultStoreCacheId);
2122+
// Verify we obtain a cache MISS at the 1st time
2123+
$this->assertCacheMissAndReturnResponse(
2124+
$currentStoreGroupQuery,
2125+
[
2126+
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCurrentStoreGroupCacheId,
2127+
'Store' => $secondStoreCode
2128+
]
2129+
);
2130+
2131+
// Get base website
2132+
$website = $this->objectManager->create(Website::class);
2133+
$website->load('base', 'code');
2134+
2135+
// Create new store group
2136+
$storeGroup = $this->objectManager->create(Group::class);
2137+
$storeGroup->setCode('new_store')
2138+
->setName('New store group')
2139+
->setWebsite($website);
2140+
$storeGroup->save();
2141+
2142+
// Create new store with new store group and base website
2143+
$store = $this->objectManager->create(Store::class);
2144+
$store->setData([
2145+
'code' => 'new_store_view',
2146+
'website_id' => $website->getId(),
2147+
'group_id' => $storeGroup->getId(),
2148+
'name' => 'new Store View',
2149+
'sort_order' => 10,
2150+
'is_active' => 1,
2151+
]);
2152+
$store->save();
2153+
2154+
// Query available stores of default store's website
2155+
// after new store with default website and new store group is created
2156+
// Verify we obtain a cache MISS at the 2nd time
2157+
$this->assertCacheMissAndReturnResponse(
2158+
$query,
2159+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
2160+
);
2161+
// Verify we obtain a cache HIT at the 3rd time
2162+
$this->assertCacheHitAndReturnResponse(
2163+
$query,
2164+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCacheId]
2165+
);
2166+
2167+
// Query available stores of default store's website and store group
2168+
// after new store with base website and new store group is created
2169+
// Verify we obtain a cache MISS at the 2nd time
2170+
$this->assertCacheMissAndReturnResponse(
2171+
$currentStoreGroupQuery,
2172+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCurrentStoreGroupCacheId]
2173+
);
2174+
// Verify we obtain a cache HIT at the 3rd time
2175+
$this->assertCacheHitAndReturnResponse(
2176+
$currentStoreGroupQuery,
2177+
[CacheIdCalculator::CACHE_ID_HEADER => $defaultStoreCurrentStoreGroupCacheId]
2178+
);
2179+
2180+
// Query available stores of second store's website (second website) and any store groups of the website
2181+
// after new store with base website and new store group is created
2182+
// Verify we obtain a cache HIT at the 2nd time
2183+
$this->assertCacheHitAndReturnResponse(
2184+
$query,
2185+
[
2186+
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCacheId,
2187+
'Store' => $secondStoreCode
2188+
]
2189+
);
2190+
2191+
// Query available stores of second store's website (second website) and store group
2192+
// after new store with base website and new store group is created
2193+
// Verify we obtain a cache HIT at the 2nd time
2194+
$this->assertCacheHitAndReturnResponse(
2195+
$currentStoreGroupQuery,
2196+
[
2197+
CacheIdCalculator::CACHE_ID_HEADER => $secondStoreCurrentStoreGroupCacheId,
2198+
'Store' => $secondStoreCode
2199+
]
2200+
);
2201+
2202+
// remove new store
2203+
$registry = $this->objectManager->get(\Magento\Framework\Registry::class);
2204+
$registry->unregister('isSecureArea');
2205+
$registry->register('isSecureArea', true);
2206+
$store->delete();
2207+
$registry->unregister('isSecureArea');
2208+
$registry->register('isSecureArea', false);
2209+
}
2210+
20442211
private function changeToTwoWebsitesThreeStoreGroupsThreeStores()
20452212
{
20462213
/** @var $website2 \Magento\Store\Model\Website */

0 commit comments

Comments
 (0)