Skip to content

Commit

Permalink
magento/adobe-stock-integration#1724: Support batches processing for …
Browse files Browse the repository at this point in the history
…synchronization queue messages - fixed failed static test and added integration test
  • Loading branch information
yolouiese committed Aug 26, 2020
1 parent a5e5c1b commit b8e89a1
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ class SynchronizeIdentities implements SynchronizeIdentitiesInterface
private const ENTITY_ID = 'entityId';
private const FIELD = 'field';

private const MEDIA_CONTENT_TYPE = 'entity_type';
private const MEDIA_CONTENT_ENTITY_ID = 'entity_id';
private const MEDIA_CONTENT_FIELD = 'field';

private const FIELD_CMS_PAGE = 'cms_page';
private const FIELD_CMS_BLOCK = 'cms_block';

Expand Down Expand Up @@ -77,18 +73,18 @@ public function execute(array $mediaContentIdentities): void
foreach ($mediaContentIdentities as $identity) {
$contentIdentity = $this->contentIdentityFactory->create(
[
self::ENTITY_TYPE => $identity[self::MEDIA_CONTENT_TYPE],
self::ENTITY_ID => $identity[self::MEDIA_CONTENT_ENTITY_ID],
self::FIELD => $identity[self::MEDIA_CONTENT_FIELD]
self::ENTITY_TYPE => $identity[self::ENTITY_TYPE],
self::ENTITY_ID => $identity[self::ENTITY_ID],
self::FIELD => $identity[self::FIELD]
]
);

if ($identity[self::MEDIA_CONTENT_TYPE] === self::FIELD_CMS_PAGE
|| $identity[self::MEDIA_CONTENT_TYPE] === self::FIELD_CMS_BLOCK
if ($identity[self::ENTITY_TYPE] === self::FIELD_CMS_PAGE
|| $identity[self::ENTITY_TYPE] === self::FIELD_CMS_BLOCK
) {
$content = $this->getCmsMediaContent(
$identity[self::MEDIA_CONTENT_TYPE],
$identity[self::MEDIA_CONTENT_ENTITY_ID]
$identity[self::ENTITY_TYPE],
$identity[self::ENTITY_ID]
);
} else {
$content = implode(PHP_EOL, $this->getEntityContents->execute($contentIdentity));
Expand All @@ -105,10 +101,10 @@ public function execute(array $mediaContentIdentities): void
* Get cms media content from database
*
* @param string $tableName
* @param string $cmsId
* @param int $cmsId
* @return string
*/
private function getCmsMediaContent(string $tableName, string $cmsId): string
private function getCmsMediaContent(string $tableName, int $cmsId): string
{
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName($tableName);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\MediaContentSynchronization\Test\Integration\Model;

use Magento\Framework\Exception\IntegrationException;
use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory;
use Magento\MediaContentApi\Api\GetAssetIdsByContentIdentityInterface;
use Magento\MediaContentApi\Api\GetContentByAssetIdsInterface;
use Magento\MediaContentSynchronizationApi\Api\SynchronizeIdentitiesInterface;
use Magento\MediaContentSynchronizationApi\Api\SynchronizeInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for SynchronizeIdentities.
*/
class SynchronizeIdentitiesTest extends TestCase
{
/**
* @var ContentIdentityInterfaceFactory
*/
private $contentIdentityFactory;

/**
* @var GetAssetIdsByContentIdentityInterface
*/
private $getAssetIds;

/**
* @var GetContentByAssetIdsInterface
*/
private $getContentIdentities;

/**
* @var SynchronizeIdentitiesInterface
*/
private $synchronizeIdentities;

/**
* @var SynchronizeInterface
*/
private $synchronize;

protected function setUp(): void
{
$this->contentIdentityFactory = Bootstrap::getObjectManager()->get(ContentIdentityInterfaceFactory::class);
$this->getAssetIds = Bootstrap::getObjectManager()->get(GetAssetIdsByContentIdentityInterface::class);
$this->synchronizeIdentities = Bootstrap::getObjectManager()->get(SynchronizeIdentitiesInterface::class);
$this->synchronize = Bootstrap::getObjectManager()->get(SynchronizeInterface::class);
$this->getContentIdentities = Bootstrap::getObjectManager()->get(GetContentByAssetIdsInterface::class);
}

/**
* @dataProvider filesProvider
* @magentoDataFixture Magento/MediaContentCatalog/_files/category_with_asset.php
* @magentoDataFixture Magento/MediaContentCatalog/_files/product_with_asset.php
* @magentoDataFixture Magento/MediaContentCms/_files/page_with_asset.php
* @magentoDataFixture Magento/MediaContentCms/_files/block_with_asset.php
* @magentoDataFixture Magento/MediaGallery/_files/media_asset.php
* @param array $mediaContentIdentities
* @throws IntegrationException
*/
public function testExecute(array $mediaContentIdentities): void
{
$this->assertNotEmpty($mediaContentIdentities);
$this->synchronizeIdentities->execute($mediaContentIdentities);

foreach ($mediaContentIdentities as $contentIdentity) {
$assetId = 2020;
$categoryId = 28767;
$productId = 1567;
$pageId = 5;
$blockId = 1;
$identity = $this->contentIdentityFactory->create($contentIdentity);
$this->assertEquals([$assetId], $this->getAssetIds->execute($identity));

$synchronizedContentIdentities = $this->getContentIdentities->execute([$assetId]);
$this->assertEquals(4, count($synchronizedContentIdentities));
$this->assertEquals($categoryId, $synchronizedContentIdentities[0]->getEntityId());
$this->assertEquals($productId, $synchronizedContentIdentities[1]->getEntityId());
$this->assertEquals($pageId, $synchronizedContentIdentities[2]->getEntityId());
$this->assertEquals($blockId, $synchronizedContentIdentities[3]->getEntityId());
}
}

/**
* Data provider
*
* @return array
*/
public function filesProvider(): array
{
return [
[
[
$this->getCategoryIdentities(),
$this->getProductIdentities(),
$this->getCmsPageIdentities(),
$this->getCmsBlockIdentities()
]
]
];
}

/**
* Format category media content identities
*/
public function getCategoryIdentities()
{
$categoryId = 28767;
return $contentIdentity = [
'entityType' => 'catalog_category',
'field' => 'description',
'entityId' => $categoryId
];
}

/**
* Format product media content identities
*/
public function getProductIdentities()
{
$productId = 1567;
return $contentIdentity = [
'entityType' => 'catalog_product',
'field' => 'description',
'entityId' => $productId
];
}

/**
* Format cms page media content identities
*/
public function getCmsPageIdentities()
{
$pageId = 5;
return $contentIdentity = [
'entityType' => 'cms_page',
'field' => 'content',
'entityId' => $pageId
];
}

/**
* Format cms block media content identities
*/
public function getCmsBlockIdentities()
{
$blockId = 1;
return $contentIdentity = [
'entityType' => 'cms_block',
'field' => 'content',
'entityId' => $blockId
];
}
}
1 change: 0 additions & 1 deletion app/code/Magento/MediaContentSynchronization/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"magento/framework": "*",
"magento/framework-bulk": "*",
"magento/module-media-content-synchronization-api": "*",
"magento/framework-message-queue": "*",
"magento/module-media-content-api": "*",
"magento/module-asynchronous-operations": "*"
},
Expand Down

0 comments on commit b8e89a1

Please sign in to comment.