|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\MediaContentSynchronization\Test\Integration\Model; |
| 9 | + |
| 10 | +use Magento\Framework\Exception\IntegrationException; |
| 11 | +use Magento\Framework\Exception\LocalizedException; |
| 12 | +use Magento\Framework\MessageQueue\ConsumerFactory; |
| 13 | +use Magento\MediaContentApi\Api\Data\ContentIdentityInterfaceFactory; |
| 14 | +use Magento\MediaContentApi\Api\GetAssetIdsByContentIdentityInterface; |
| 15 | +use Magento\MediaContentApi\Api\GetContentByAssetIdsInterface; |
| 16 | +use Magento\MediaContentSynchronization\Model\Publish; |
| 17 | +use Magento\TestFramework\Helper\Bootstrap; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +/** |
| 21 | + * Test for media content Publisher |
| 22 | + */ |
| 23 | +class PublisherTest extends TestCase |
| 24 | +{ |
| 25 | + private const TOPIC_MEDIA_CONTENT_SYNCHRONIZATION = 'media.content.synchronization'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var ConsumerFactory |
| 29 | + */ |
| 30 | + private $consumerFactory; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ContentIdentityInterfaceFactory |
| 34 | + */ |
| 35 | + private $contentIdentityFactory; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var GetContentByAssetIdsInterface |
| 39 | + */ |
| 40 | + private $getContentIdentities; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var GetAssetIdsByContentIdentityInterface |
| 44 | + */ |
| 45 | + private $getAssetIds; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var Publish |
| 49 | + */ |
| 50 | + private $publish; |
| 51 | + |
| 52 | + protected function setUp(): void |
| 53 | + { |
| 54 | + $this->consumerFactory = Bootstrap::getObjectManager()->get(ConsumerFactory::class); |
| 55 | + $this->contentIdentityFactory = Bootstrap::getObjectManager()->get(ContentIdentityInterfaceFactory::class); |
| 56 | + $this->getContentIdentities = Bootstrap::getObjectManager()->get(GetContentByAssetIdsInterface::class); |
| 57 | + $this->getAssetIds = Bootstrap::getObjectManager()->get(GetAssetIdsByContentIdentityInterface::class); |
| 58 | + $this->publish = Bootstrap::getObjectManager()->get(Publish::class); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @dataProvider filesProvider |
| 63 | + * @magentoDataFixture Magento/MediaContentCatalog/_files/category_with_asset.php |
| 64 | + * @magentoDataFixture Magento/MediaContentCatalog/_files/product_with_asset.php |
| 65 | + * @magentoDataFixture Magento/MediaContentCms/_files/page_with_asset.php |
| 66 | + * @magentoDataFixture Magento/MediaContentCms/_files/block_with_asset.php |
| 67 | + * @magentoDataFixture Magento/MediaGallery/_files/media_asset.php |
| 68 | + * @param array $contentIdentities |
| 69 | + * @throws IntegrationException |
| 70 | + * @throws LocalizedException |
| 71 | + */ |
| 72 | + public function testExecute(array $contentIdentities): void |
| 73 | + { |
| 74 | + // publish message to the queue |
| 75 | + $this->publish->execute($contentIdentities); |
| 76 | + |
| 77 | + // run and process message |
| 78 | + $batchSize = 1; |
| 79 | + $maxNumberOfMessages = 1; |
| 80 | + $consumer = $this->consumerFactory->get(self::TOPIC_MEDIA_CONTENT_SYNCHRONIZATION, $batchSize); |
| 81 | + $consumer->process($maxNumberOfMessages); |
| 82 | + |
| 83 | + // verify synchronized media content |
| 84 | + $assetId = 2020; |
| 85 | + $entityIds = []; |
| 86 | + foreach ($contentIdentities as $contentIdentity) { |
| 87 | + $contentIdentityObject = $this->contentIdentityFactory->create($contentIdentity); |
| 88 | + $this->assertEquals([$assetId], $this->getAssetIds->execute($contentIdentityObject)); |
| 89 | + $entityIds[] = $contentIdentityObject->getEntityId(); |
| 90 | + } |
| 91 | + |
| 92 | + $synchronizedContentIdentities = $this->getContentIdentities->execute([$assetId]); |
| 93 | + $this->assertEquals(2, count($synchronizedContentIdentities)); |
| 94 | + |
| 95 | + foreach ($synchronizedContentIdentities as $syncedContentIdentity) { |
| 96 | + $this->assertContains($syncedContentIdentity->getEntityId(), $entityIds); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Data provider |
| 102 | + * |
| 103 | + * @return array |
| 104 | + */ |
| 105 | + public function filesProvider(): array |
| 106 | + { |
| 107 | + return [ |
| 108 | + [ |
| 109 | + [ |
| 110 | + [ |
| 111 | + 'entityType' => 'catalog_category', |
| 112 | + 'field' => 'description', |
| 113 | + 'entityId' => 28767 |
| 114 | + ], |
| 115 | + [ |
| 116 | + 'entityType' => 'catalog_product', |
| 117 | + 'field' => 'description', |
| 118 | + 'entityId' => 1567 |
| 119 | + ] |
| 120 | + ] |
| 121 | + ] |
| 122 | + ]; |
| 123 | + } |
| 124 | +} |
0 commit comments