88namespace Magento \GraphQl \Store ;
99
1010use Magento \Framework \Exception \NoSuchEntityException ;
11+ use Magento \GraphQl \PageCache \GraphQLPageCacheAbstract ;
12+ use Magento \GraphQlCache \Model \CacheId \CacheIdCalculator ;
1113use Magento \Store \Api \Data \StoreConfigInterface ;
1214use Magento \Store \Api \StoreConfigManagerInterface ;
1315use Magento \Store \Api \StoreRepositoryInterface ;
1416use Magento \Store \Api \StoreResolverInterface ;
1517use Magento \Store \Model \ScopeInterface ;
1618use Magento \TestFramework \Helper \Bootstrap ;
1719use Magento \TestFramework \ObjectManager ;
18- use Magento \TestFramework \TestCase \GraphQlAbstract ;
1920
2021/**
2122 * Test storeConfig query cache
2223 */
23- class StoreConfigCacheTest extends GraphQlAbstract
24+ class StoreConfigCacheTest extends GraphQLPageCacheAbstract
2425{
2526
2627 /** @var ObjectManager */
@@ -46,73 +47,100 @@ public function testGetStoreConfig(): void
4647 $ storeResolver = $ this ->objectManager ->get (StoreResolverInterface::class);
4748 /** @var StoreRepositoryInterface $storeRepository */
4849 $ storeRepository = $ this ->objectManager ->get (StoreRepositoryInterface::class);
49- $ storeId = $ storeResolver ->getCurrentStoreId ();
50- $ store = $ storeRepository ->getById ($ storeId );
50+ $ defaultStoreId = $ storeResolver ->getCurrentStoreId ();
51+ $ store = $ storeRepository ->getById ($ defaultStoreId );
52+ $ defaultStoreCode = $ store ->getCode ();
5153 /** @var StoreConfigInterface $storeConfig */
52- $ storeConfig = current ($ storeConfigManager ->getStoreConfigs ([$ store -> getCode () ]));
54+ $ storeConfig = current ($ storeConfigManager ->getStoreConfigs ([$ defaultStoreCode ]));
5355 $ defaultLocale = $ storeConfig ->getLocale ();
5456 $ query = $ this ->getQuery ();
5557
5658 // Query default store config
57- $ response = $ this ->graphQlQueryWithResponseHeaders ($ query );
58- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ response ['headers ' ]);
59- $ this ->assertEquals ('MISS ' , $ response ['headers ' ]['X-Magento-Cache-Debug ' ]);
60- $ this ->assertArrayHasKey ('storeConfig ' , $ response ['body ' ]);
61- $ responseConfig = $ response ['body ' ]['storeConfig ' ];
62- $ this ->assertEquals ($ storeConfig ->getId (), $ responseConfig ['id ' ]);
63- $ this ->assertEquals ($ storeConfig ->getCode (), $ responseConfig ['code ' ]);
64- $ this ->assertEquals ($ defaultLocale , $ responseConfig ['locale ' ]);
65- // Query default store config again
66- $ responseHit = $ this ->graphQlQueryWithResponseHeaders ($ query );
67- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseHit ['headers ' ]);
68- $ this ->assertEquals ('HIT ' , $ responseHit ['headers ' ]['X-Magento-Cache-Debug ' ]);
69- $ responseHitConfig = $ responseHit ['body ' ]['storeConfig ' ];
70- $ this ->assertEquals ($ storeConfig ->getCode (), $ responseHitConfig ['code ' ]);
71- $ this ->assertEquals ($ defaultLocale , $ responseHitConfig ['locale ' ]);
59+ $ responseDefaultStore = $ this ->graphQlQueryWithResponseHeaders ($ query );
60+ $ this ->assertArrayHasKey (CacheIdCalculator::CACHE_ID_HEADER , $ responseDefaultStore ['headers ' ]);
61+ $ defaultStoreCacheId = $ responseDefaultStore ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
62+ // Verify we obtain a cache MISS the first time
63+ $ defaultStoreResponse = $ this ->assertCacheMissAndReturnResponse (
64+ $ query ,
65+ [CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ]
66+ );
67+ $ this ->assertArrayHasKey ('storeConfig ' , $ defaultStoreResponse ['body ' ]);
68+ $ defaultStoreResponseResult = $ defaultStoreResponse ['body ' ]['storeConfig ' ];
69+ $ this ->assertEquals ($ defaultStoreId , $ defaultStoreResponseResult ['id ' ]);
70+ $ this ->assertEquals ($ defaultStoreCode , $ defaultStoreResponseResult ['code ' ]);
71+ $ this ->assertEquals ($ defaultLocale , $ defaultStoreResponseResult ['locale ' ]);
72+ // Verify we obtain a cache HIT the second time
73+ $ defaultStoreResponseHit = $ this ->assertCacheHitAndReturnResponse (
74+ $ query ,
75+ [CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ]
76+ );
77+ $ this ->assertArrayHasKey ('storeConfig ' , $ defaultStoreResponseHit ['body ' ]);
78+ $ defaultStoreResponseHitResult = $ defaultStoreResponseHit ['body ' ]['storeConfig ' ];
79+ $ this ->assertEquals ($ defaultStoreId , $ defaultStoreResponseHitResult ['id ' ]);
80+ $ this ->assertEquals ($ defaultStoreCode , $ defaultStoreResponseHitResult ['code ' ]);
81+ $ this ->assertEquals ($ defaultLocale , $ defaultStoreResponseHitResult ['locale ' ]);
7282
7383 // Query test store config
74- $ headerMap ['Store ' ] = 'test ' ;
84+ $ testStoreCode = 'test ' ;
85+ $ headerMap ['Store ' ] = $ testStoreCode ;
7586 $ responseTestStore = $ this ->graphQlQueryWithResponseHeaders ($ query , [], '' , $ headerMap );
76- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseTestStore ['headers ' ]);
77- $ this ->assertEquals ('MISS ' , $ responseTestStore ['headers ' ]['X-Magento-Cache-Debug ' ]);
78- $ responseTestStoreConfig = $ responseTestStore ['body ' ]['storeConfig ' ];
79- $ this ->assertEquals ('test ' , $ responseTestStoreConfig ['code ' ]);
80- $ this ->assertEquals ($ defaultLocale , $ responseTestStoreConfig ['locale ' ]);
81- // Query test store config again
82- $ responseTestStoreHit = $ this ->graphQlQueryWithResponseHeaders ($ query , [], '' , $ headerMap );
83- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseTestStoreHit ['headers ' ]);
84- $ this ->assertEquals ('HIT ' , $ responseTestStoreHit ['headers ' ]['X-Magento-Cache-Debug ' ]);
85- $ responseTestStoreHitConfig = $ responseTestStoreHit ['body ' ]['storeConfig ' ];
86- $ this ->assertEquals ('test ' , $ responseTestStoreHitConfig ['code ' ]);
87- $ this ->assertEquals ($ defaultLocale , $ responseTestStoreHitConfig ['locale ' ]);
87+ $ this ->assertArrayHasKey (CacheIdCalculator::CACHE_ID_HEADER , $ responseTestStore ['headers ' ]);
88+ $ testStoreCacheId = $ responseTestStore ['headers ' ][CacheIdCalculator::CACHE_ID_HEADER ];
89+ $ this ->assertNotEquals ($ testStoreCacheId , $ defaultStoreCacheId );
90+ // Verify we obtain a cache MISS the first time
91+ $ testStoreResponse = $ this ->assertCacheMissAndReturnResponse (
92+ $ query ,
93+ [CacheIdCalculator::CACHE_ID_HEADER => $ testStoreCacheId ]
94+ );
95+ $ this ->assertArrayHasKey ('storeConfig ' , $ testStoreResponse ['body ' ]);
96+ $ testStoreResponseResult = $ testStoreResponse ['body ' ]['storeConfig ' ];
97+ $ this ->assertEquals ($ testStoreCode , $ testStoreResponseResult ['code ' ]);
98+ $ this ->assertEquals ($ defaultLocale , $ testStoreResponseResult ['locale ' ]);
99+ // Verify we obtain a cache HIT the second time
100+ $ testStoreResponseHit = $ this ->assertCacheHitAndReturnResponse (
101+ $ query ,
102+ [CacheIdCalculator::CACHE_ID_HEADER => $ testStoreCacheId ]
103+ );
104+ $ this ->assertArrayHasKey ('storeConfig ' , $ testStoreResponseHit ['body ' ]);
105+ $ testStoreResponseHitResult = $ testStoreResponseHit ['body ' ]['storeConfig ' ];
106+ $ this ->assertEquals ($ testStoreCode , $ testStoreResponseHitResult ['code ' ]);
107+ $ this ->assertEquals ($ defaultLocale , $ testStoreResponseHitResult ['locale ' ]);
88108
89109 // Change test store locale
90110 $ newLocale = 'de_DE ' ;
91- $ this ->setConfig ('general/locale/code ' , $ newLocale , ScopeInterface::SCOPE_STORES , ' test ' );
111+ $ this ->setConfig ('general/locale/code ' , $ newLocale , ScopeInterface::SCOPE_STORES , $ testStoreCode );
92112
93- // Query default store config
94- $ responseHit2 = $ this ->graphQlQueryWithResponseHeaders ($ query );
95- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseHit2 ['headers ' ]);
96- $ this ->assertEquals ('HIT ' , $ responseHit2 ['headers ' ]['X-Magento-Cache-Debug ' ]);
97- $ responseHit2Config = $ responseHit2 ['body ' ]['storeConfig ' ];
98- $ this ->assertEquals ($ storeConfig ->getCode (), $ responseHit2Config ['code ' ]);
99- $ this ->assertEquals ($ defaultLocale , $ responseHit2Config ['locale ' ]);
100-
101- // Query test store config
102- $ responseTestStoreMiss = $ this ->graphQlQueryWithResponseHeaders ($ query , [], '' , $ headerMap );
103- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseTestStoreMiss ['headers ' ]);
104- $ this ->assertEquals ('MISS ' , $ responseTestStoreMiss ['headers ' ]['X-Magento-Cache-Debug ' ]);
105- $ responseTestStoreMissConfig = $ responseTestStoreMiss ['body ' ]['storeConfig ' ];
106- $ this ->assertEquals ('test ' , $ responseTestStoreMissConfig ['code ' ]);
107- $ this ->assertEquals ($ newLocale , $ responseTestStoreMissConfig ['locale ' ]);
113+ // Query default store config after test store config change
114+ // Verify we obtain a cache HIT the 3rd time
115+ $ defaultStoreResponseHit2 = $ this ->assertCacheHitAndReturnResponse (
116+ $ query ,
117+ [CacheIdCalculator::CACHE_ID_HEADER => $ defaultStoreCacheId ]
118+ );
119+ $ this ->assertArrayHasKey ('storeConfig ' , $ defaultStoreResponseHit2 ['body ' ]);
120+ $ defaultStoreResponseHit2Result = $ defaultStoreResponseHit2 ['body ' ]['storeConfig ' ];
121+ $ this ->assertEquals ($ defaultStoreId , $ defaultStoreResponseHit2Result ['id ' ]);
122+ $ this ->assertEquals ($ defaultStoreCode , $ defaultStoreResponseHit2Result ['code ' ]);
123+ $ this ->assertEquals ($ defaultLocale , $ defaultStoreResponseHit2Result ['locale ' ]);
108124
109- // Query test store config again
110- $ responseTestStoreHit2 = $ this ->graphQlQueryWithResponseHeaders ($ query , [], '' , $ headerMap );
111- $ this ->assertArrayHasKey ('X-Magento-Cache-Debug ' , $ responseTestStoreHit2 ['headers ' ]);
112- $ this ->assertEquals ('HIT ' , $ responseTestStoreHit2 ['headers ' ]['X-Magento-Cache-Debug ' ]);
113- $ responseTestStoreHit2Config = $ responseTestStoreHit2 ['body ' ]['storeConfig ' ];
114- $ this ->assertEquals ('test ' , $ responseTestStoreHit2Config ['code ' ]);
115- $ this ->assertEquals ($ newLocale , $ responseTestStoreHit2Config ['locale ' ]);
125+ // Query test store config after test store config change
126+ // Verify we obtain a cache MISS the 3rd time
127+ $ testStoreResponseMiss = $ this ->assertCacheMissAndReturnResponse (
128+ $ query ,
129+ [CacheIdCalculator::CACHE_ID_HEADER => $ testStoreCacheId ]
130+ );
131+ $ this ->assertArrayHasKey ('storeConfig ' , $ testStoreResponseMiss ['body ' ]);
132+ $ testStoreResponseMissResult = $ testStoreResponseMiss ['body ' ]['storeConfig ' ];
133+ $ this ->assertEquals ($ testStoreCode , $ testStoreResponseMissResult ['code ' ]);
134+ $ this ->assertEquals ($ newLocale , $ testStoreResponseMissResult ['locale ' ]);
135+ // Verify we obtain a cache HIT the 4th time
136+ $ testStoreResponseHit2 = $ this ->assertCacheHitAndReturnResponse (
137+ $ query ,
138+ [CacheIdCalculator::CACHE_ID_HEADER => $ testStoreCacheId ]
139+ );
140+ $ this ->assertArrayHasKey ('storeConfig ' , $ testStoreResponseHit2 ['body ' ]);
141+ $ testStoreResponseHit2Result = $ testStoreResponseHit2 ['body ' ]['storeConfig ' ];
142+ $ this ->assertEquals ($ testStoreCode , $ testStoreResponseHit2Result ['code ' ]);
143+ $ this ->assertEquals ($ newLocale , $ testStoreResponseHit2Result ['locale ' ]);
116144 }
117145
118146 /**
0 commit comments