Skip to content

Commit 4c52021

Browse files
committed
B2B-2677: [MediaGallery]Implement data caching for GraphQL results on resolver level
- Add testCacheIsInvalidatedWhenUpdatingMediaGalleryEntriesOnAProductViaImport (should fail) - Refactor
1 parent 507c2d5 commit 4c52021

File tree

1 file changed

+87
-10
lines changed

1 file changed

+87
-10
lines changed

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

Lines changed: 87 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Magento\Framework\ObjectManagerInterface;
2424
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
2525
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
26+
use Magento\ImportExport\Model\Import;
2627
use Magento\Integration\Model\Integration;
2728
use Magento\TestFramework\Fixture\DataFixture;
2829
use Magento\TestFramework\Helper\Bootstrap;
@@ -418,22 +419,75 @@ public function testCacheIsInvalidatedOnProductDeletion()
418419
#[
419420
DataFixture(ProductFixture::class, ['sku' => 'product1', 'media_gallery_entries' => [[]]], as: 'product'),
420421
]
421-
public function testCacheIsNotInvalidatedWhenUpdatingProductSpecificAttributeViaImport()
422+
public function testCacheIsInvalidatedWhenUpdatingMediaGalleryEntriesOnAProductViaImport()
422423
{
423424
// first, create an integration so that cache is not cleared in
424425
// Magento\TestFramework\Authentication\OauthHelper::_createIntegration before making the API call
425-
/** @var $integrationService \Magento\Integration\Api\IntegrationServiceInterface */
426-
$integrationService = $this->objectManager->get(\Magento\Integration\Api\IntegrationServiceInterface::class);
426+
$integration = $this->createNewOauthIntegration();
427427

428-
$params = [
429-
'all_resources' => true,
430-
'integration_id' => 1,
431-
'status' => Integration::STATUS_ACTIVE,
432-
'name' => 'Integration' . microtime()
428+
$product = $this->productRepository->get('product1');
429+
430+
$this->assertCount(
431+
1,
432+
$product->getMediaGalleryEntries()
433+
);
434+
435+
$query = $this->getProductWithMediaGalleryQuery($product);
436+
$response = $this->graphQlQuery($query);
437+
438+
$this->assertCount(
439+
1,
440+
$response['products']['items'][0]['media_gallery']
441+
);
442+
443+
$this->assertMediaGalleryResolverCacheRecordExists($product);
444+
445+
$serviceInfo = [
446+
'rest' => [
447+
'resourcePath' => '/V1/import/csv',
448+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
449+
],
433450
];
434451

435-
$integration = $integrationService->create($params);
436-
$integration->setStatus(\Magento\Integration\Model\Integration::STATUS_ACTIVE)->save();
452+
$requestData = [
453+
'source' => [
454+
'entity' => 'catalog_product',
455+
'behavior' => 'append',
456+
'validationStrategy' => 'validation-stop-on-errors',
457+
'allowedErrorCount' => '10',
458+
Import::FIELD_NAME_IMG_FILE_DIR =>
459+
'/Users/dmooney/Code/vagrant-magento/magento2ce/var/import/images/product_images',
460+
'csvData' => base64_encode("sku,base_image\nproduct1,design_patterns.jpeg\n"),
461+
],
462+
];
463+
464+
$response = $this->_webApiCall($serviceInfo, $requestData, 'rest', null, $integration);
465+
466+
$this->assertEquals(
467+
'Entities Processed: 1',
468+
$response[0]
469+
);
470+
471+
// assert product has updated media gallery entry count
472+
$this->productRepository->cleanCache();
473+
$updatedProduct = $this->productRepository->get('product1');
474+
$this->assertCount(
475+
2,
476+
$updatedProduct->getMediaGalleryEntries()
477+
);
478+
479+
// assert media gallery resolver cache is invalidated
480+
$this->assertMediaGalleryResolverCacheRecordDoesNotExist($product);
481+
}
482+
483+
#[
484+
DataFixture(ProductFixture::class, ['sku' => 'product1', 'media_gallery_entries' => [[]]], as: 'product'),
485+
]
486+
public function testCacheIsNotInvalidatedWhenUpdatingProductSpecificAttributeViaImport()
487+
{
488+
// first, create an integration so that cache is not cleared in
489+
// Magento\TestFramework\Authentication\OauthHelper::_createIntegration before making the API call
490+
$integration = $this->createNewOauthIntegration();
437491

438492
$product = $this->productRepository->get('product1');
439493

@@ -489,6 +543,29 @@ public function testCacheIsNotInvalidatedWhenUpdatingProductSpecificAttributeVia
489543
$this->assertMediaGalleryResolverCacheRecordExists($product);
490544
}
491545

546+
/**
547+
*
548+
* @return Integration
549+
* @throws \Magento\Framework\Exception\IntegrationException
550+
*/
551+
private function createNewOauthIntegration(): Integration
552+
{
553+
/** @var $integrationService \Magento\Integration\Api\IntegrationServiceInterface */
554+
$integrationService = $this->objectManager->get(\Magento\Integration\Api\IntegrationServiceInterface::class);
555+
556+
$params = [
557+
'all_resources' => true,
558+
'integration_id' => 1,
559+
'status' => Integration::STATUS_ACTIVE,
560+
'name' => 'Integration' . microtime()
561+
];
562+
563+
$integration = $integrationService->create($params);
564+
$integration->setStatus(\Magento\Integration\Model\Integration::STATUS_ACTIVE)->save();
565+
566+
return $integration;
567+
}
568+
492569
/**
493570
* Assert that media gallery cache record exists for the $product.
494571
*

0 commit comments

Comments
 (0)