Skip to content

Commit 1e053a2

Browse files
committed
B2B-2677: [MediaGallery] Implement data caching for GraphQL results on resolver level
- Assimilate null with empty string
1 parent 3d38de3 commit 1e053a2

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

app/code/Magento/CatalogGraphQl/Model/Resolver/Product/MediaGallery/ChangeDetector.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,23 @@ public function isChanged(Product $product): bool
5454
}
5555

5656
// remove keys from original array that are not in new array; some keys are omitted from the new array on save
57-
foreach ($mediaGalleryImages as $key => $mediaGalleryImage) {
58-
$origMediaGalleryImages[$key] = array_intersect_key($origMediaGalleryImages[$key], $mediaGalleryImage);
57+
foreach ($mediaGalleryImages as $imageKey => $mediaGalleryImage) {
58+
$origMediaGalleryImages[$imageKey] = array_intersect_key(
59+
$origMediaGalleryImages[$imageKey],
60+
$mediaGalleryImage
61+
);
62+
63+
// client UI converts null values to empty string due to behavior of HTML encoding;
64+
// match this behavior before performing comparison
65+
foreach ($origMediaGalleryImages[$imageKey] as $key => &$value) {
66+
if ($value === null) {
67+
$value = '';
68+
}
69+
70+
if ($mediaGalleryImages[$imageKey][$key] === null) {
71+
$mediaGalleryImages[$imageKey][$key] = '';
72+
}
73+
}
5974
}
6075

6176
$mediaGalleryImagesSerializedString = $this->serializer->serialize($mediaGalleryImages);

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ResolverCache/MediaGalleryTest.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
2525
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
2626
use Magento\TestFramework\Fixture\DataFixture;
27+
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
2728
use Magento\TestFramework\Helper\Bootstrap;
2829
use Magento\TestFramework\TestCase\GraphQl\ResolverCacheAbstract;
2930

@@ -48,21 +49,28 @@ class MediaGalleryTest extends ResolverCacheAbstract
4849
*/
4950
private $graphQlResolverCache;
5051

52+
/**
53+
* @var DataFixtureStorageManager
54+
*/
55+
private $fixtures;
56+
5157
protected function setUp(): void
5258
{
5359
$this->objectManager = Bootstrap::getObjectManager();
5460
$this->graphQlResolverCache = $this->objectManager->get(GraphQlResolverCache::class);
5561
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
62+
$this->fixtures = DataFixtureStorageManager::getStorage();
5663

5764
parent::setUp();
5865
}
5966

6067
#[
61-
DataFixture(ProductFixture::class, ['sku' => 'product1', 'media_gallery_entries' => [[]]], as: 'product'),
68+
DataFixture(ProductFixture::class, ['media_gallery_entries' => [[]]], as: 'product'),
6269
]
6370
public function testSavingProductInAdminWithoutChangesDoesNotInvalidateResolverCache()
6471
{
65-
$product = $this->productRepository->get('product1');
72+
/** @var ProductInterface $product */
73+
$product = $this->fixtures->get('product');
6674

6775
// Assert Media Gallery Resolver cache record does not exist before querying the product's media gallery
6876
$this->assertMediaGalleryResolverCacheRecordDoesNotExist($product);
@@ -112,6 +120,14 @@ public function testSavingProductInAdminWithoutChangesDoesNotInvalidateResolverC
112120
$image = array_filter($image, function ($key) {
113121
return strpos($key, 'video') === false;
114122
}, ARRAY_FILTER_USE_KEY);
123+
124+
// client UI converts null values to empty string due to behavior of HTML encoding;
125+
// match this behavior before posting to the controller
126+
foreach ($image as &$value) {
127+
if ($value === null) {
128+
$value = '';
129+
}
130+
}
115131
}
116132

117133
unset($productData['entity_id']);

0 commit comments

Comments
 (0)