21
21
use Magento \Framework \App \ObjectManager \ConfigLoader ;
22
22
use Magento \Framework \App \State as AppState ;
23
23
use Magento \Framework \ObjectManagerInterface ;
24
+ use Magento \GraphQl \Model \Query \ContextFactory ;
24
25
use Magento \GraphQlResolverCache \Model \Resolver \Result \CacheKey \Calculator \ProviderInterface ;
25
26
use Magento \GraphQlResolverCache \Model \Resolver \Result \Type as GraphQlResolverCache ;
26
27
use Magento \Integration \Api \IntegrationServiceInterface ;
27
28
use Magento \Integration \Model \Integration ;
29
+ use Magento \Store \Api \Data \StoreInterface ;
30
+ use Magento \Store \Model \StoreManagerInterface ;
31
+ use Magento \Store \Test \Fixture \Store as StoreFixture ;
28
32
use Magento \TestFramework \Fixture \DataFixture ;
29
33
use Magento \TestFramework \Fixture \DataFixtureStorageManager ;
30
34
use Magento \TestFramework \Helper \Bootstrap ;
@@ -56,6 +60,11 @@ class MediaGalleryTest extends ResolverCacheAbstract
56
60
*/
57
61
private $ integration ;
58
62
63
+ /**
64
+ * @var StoreManagerInterface
65
+ */
66
+ private $ storeManager ;
67
+
59
68
/**
60
69
* @var DataFixtureStorageManager
61
70
*/
@@ -66,11 +75,19 @@ protected function setUp(): void
66
75
$ this ->objectManager = Bootstrap::getObjectManager ();
67
76
$ this ->graphQlResolverCache = $ this ->objectManager ->get (GraphQlResolverCache::class);
68
77
$ this ->productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
78
+ $ this ->storeManager = $ this ->objectManager ->get (StoreManagerInterface::class);
69
79
$ this ->fixtures = DataFixtureStorageManager::getStorage ();
70
80
71
81
parent ::setUp ();
72
82
}
73
83
84
+ protected function tearDown (): void
85
+ {
86
+ $ this ->storeManager ->setCurrentStore ('default ' );
87
+
88
+ parent ::tearDown ();
89
+ }
90
+
74
91
#[
75
92
DataFixture(ProductFixture::class, ['media_gallery_entries ' => [[]]], as: 'product ' ),
76
93
]
@@ -163,6 +180,86 @@ public function testSavingProductInAdminWithoutChangesDoesNotInvalidateResolverC
163
180
$ this ->assertMediaGalleryResolverCacheRecordExists ($ product );
164
181
}
165
182
183
+ #[
184
+ DataFixture(StoreFixture::class, as: 'store2 ' ),
185
+ DataFixture(
186
+ ProductFixture::class,
187
+ [
188
+ 'media_gallery_entries ' => [[]]
189
+ ],
190
+ as: 'product '
191
+ ),
192
+ ]
193
+ public function testResolverCacheRecordIsCreatedForEachStoreView ()
194
+ {
195
+ /** @var ProductInterface $product */
196
+ $ product = $ this ->fixtures ->get ('product ' );
197
+
198
+ /** @var StoreInterface $store2 */
199
+ $ store2 = $ this ->fixtures ->get ('store2 ' );
200
+
201
+ // Assert Media Gallery Resolver cache record does not exist before querying the product's media gallery
202
+ $ this ->assertMediaGalleryResolverCacheRecordDoesNotExist ($ product );
203
+
204
+ $ query = $ this ->getProductWithMediaGalleryQuery ($ product );
205
+
206
+ // send 1 request for each store view
207
+ $ responseInDefaultStoreView = $ this ->graphQlQuery ($ query );
208
+ $ responseInSecondStoreView = $ this ->graphQlQuery ($ query , [], '' , ['Store ' => $ store2 ->getCode ()]);
209
+
210
+ $ this ->assertNotEmpty ($ responseInDefaultStoreView ['products ' ]['items ' ][0 ]['media_gallery ' ]);
211
+ $ this ->assertNotEmpty ($ responseInSecondStoreView ['products ' ]['items ' ][0 ]['media_gallery ' ]);
212
+
213
+ // Assert Media Gallery Resolver cache record exists in default store
214
+ $ cacheKeyInDefaultStoreView = $ this ->getCacheKeyForMediaGalleryResolver ($ product );
215
+ $ this ->assertMediaGalleryResolverCacheRecordExists ($ product );
216
+
217
+ // Switch to second store view
218
+ $ this ->storeManager ->setCurrentStore ($ store2 ->getCode ());
219
+
220
+ // reset query context so that new store id is taken into account
221
+ $ contextFactory = $ this ->objectManager ->get (ContextFactory::class);
222
+ $ contextFactory ->create ();
223
+
224
+ // Assert Media Gallery Resolver cache record exists in second store
225
+ // Not using assertMediaGalleryResolverCacheRecordExists because label is not set in second store view
226
+ $ cacheKeyInSecondStoreView = $ this ->getCacheKeyForMediaGalleryResolver ($ product );
227
+ $ cacheEntryInSecondStoreView = $ this ->graphQlResolverCache ->load ($ cacheKeyInSecondStoreView );
228
+
229
+ $ this ->assertNotFalse (
230
+ $ cacheEntryInSecondStoreView ,
231
+ sprintf (
232
+ 'Media gallery cache entry for product with sku "%s" in second store view does not exist ' ,
233
+ $ product ->getSku ()
234
+ )
235
+ );
236
+
237
+ // Assert cache keys are different
238
+ $ this ->assertNotEquals (
239
+ $ cacheKeyInDefaultStoreView ,
240
+ $ cacheKeyInSecondStoreView
241
+ );
242
+
243
+ // change media gallery label and assert both cache entries are invalidated
244
+ $ this ->actionMechanismProvider ()['update media label ' ][0 ]($ product );
245
+
246
+ $ this ->assertFalse (
247
+ $ this ->graphQlResolverCache ->test ($ cacheKeyInDefaultStoreView ),
248
+ sprintf (
249
+ 'Media gallery cache entry for product with sku "%s" in default store view was not invalidated ' ,
250
+ $ product ->getSku ()
251
+ )
252
+ );
253
+
254
+ $ this ->assertFalse (
255
+ $ this ->graphQlResolverCache ->test ($ cacheKeyInSecondStoreView ),
256
+ sprintf (
257
+ 'Media gallery cache entry for product with sku "%s" in second store view was not invalidated ' ,
258
+ $ product ->getSku ()
259
+ )
260
+ );
261
+ }
262
+
166
263
/**
167
264
* - product_simple_with_media_gallery_entries.php creates product with "simple" sku containing
168
265
* link to 1 external YouTube video using an image placeholder in gallery
@@ -228,6 +325,10 @@ public function testMediaGalleryForProductVideos(callable $actionMechanismCallab
228
325
}
229
326
}
230
327
328
+ /**
329
+ * @return array[]
330
+ * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
331
+ */
231
332
public function actionMechanismProvider (): array
232
333
{
233
334
// provider is invoked before setUp() is called so need to init here
0 commit comments