|
| 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\AsynchronousOperations\Model; |
| 9 | + |
| 10 | +use Magento\AsynchronousOperations\Api\Data\OperationInterface; |
| 11 | +use Magento\AsynchronousOperations\Api\SaveMultipleOperationsInterface; |
| 12 | +use Magento\AsynchronousOperations\Model\ConfigInterface as AsyncConfig; |
| 13 | +use Magento\AsynchronousOperations\Model\ResourceModel\Operation\Collection; |
| 14 | +use Magento\AsynchronousOperations\Model\ResourceModel\Operation\CollectionFactory; |
| 15 | +use Magento\Catalog\Model\Product; |
| 16 | +use Magento\Framework\Bulk\BulkManagementInterface; |
| 17 | +use Magento\Framework\MessageQueue\ConsumerConfigurationInterface; |
| 18 | +use Magento\Framework\MessageQueue\EnvelopeInterface; |
| 19 | +use Magento\Framework\MessageQueue\Lock\ReaderInterface; |
| 20 | +use Magento\Framework\MessageQueue\LockInterfaceFactory; |
| 21 | +use Magento\Framework\MessageQueue\MessageEncoder; |
| 22 | +use Magento\Framework\MessageQueue\QueueInterface; |
| 23 | +use Magento\TestFramework\Helper\Bootstrap; |
| 24 | +use Monolog\Test\TestCase; |
| 25 | +use PHPUnit\Framework\MockObject\MockObject; |
| 26 | + |
| 27 | +/** |
| 28 | + * Test for MassConsumerEnvelopeCallback |
| 29 | + */ |
| 30 | +class MassConsumerEnvelopeCallbackTest extends TestCase |
| 31 | +{ |
| 32 | + /** |
| 33 | + * @var MassConsumerEnvelopeCallback |
| 34 | + */ |
| 35 | + private $model; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var QueueInterface|MockObject |
| 39 | + */ |
| 40 | + private $queue; |
| 41 | + |
| 42 | + /** |
| 43 | + * @var ConsumerConfigurationInterface|MockObject |
| 44 | + */ |
| 45 | + private $configuration; |
| 46 | + |
| 47 | + /** |
| 48 | + * @var MessageEncoder |
| 49 | + */ |
| 50 | + private $messageEncoder; |
| 51 | + |
| 52 | + /** |
| 53 | + * @var OperationRepositoryInterface |
| 54 | + */ |
| 55 | + private $operationRepository; |
| 56 | + |
| 57 | + /** |
| 58 | + * @var BulkManagementInterface |
| 59 | + */ |
| 60 | + private $bulkManagement; |
| 61 | + |
| 62 | + /** |
| 63 | + * @var SaveMultipleOperationsInterface |
| 64 | + */ |
| 65 | + private $saveMultipleOperations; |
| 66 | + |
| 67 | + /** |
| 68 | + * @var CollectionFactory |
| 69 | + */ |
| 70 | + private $operationCollection; |
| 71 | + |
| 72 | + /** |
| 73 | + * @inheritdoc |
| 74 | + */ |
| 75 | + protected function setUp(): void |
| 76 | + { |
| 77 | + parent::setUp(); |
| 78 | + $objectManager = Bootstrap::getObjectManager(); |
| 79 | + $this->queue = $this->createMock(QueueInterface::class); |
| 80 | + $this->configuration = $this->createMock(ConsumerConfigurationInterface::class); |
| 81 | + $this->messageEncoder = $objectManager->get(MessageEncoder::class); |
| 82 | + $this->operationRepository = $objectManager->get(OperationRepositoryInterface::class); |
| 83 | + $operationProcessor = $this->createMock(OperationProcessor::class); |
| 84 | + $this->operationCollection = $objectManager->get(CollectionFactory::class); |
| 85 | + $this->bulkManagement = $objectManager->get(BulkManagementInterface::class); |
| 86 | + $this->saveMultipleOperations = $objectManager->get(SaveMultipleOperationsInterface::class); |
| 87 | + $operationProcessorFactory = $this->createMock(OperationProcessorFactory::class); |
| 88 | + $operationProcessorFactory->method('create') |
| 89 | + ->willReturn($operationProcessor); |
| 90 | + $this->model = Bootstrap::getObjectManager()->create( |
| 91 | + MassConsumerEnvelopeCallback::class, |
| 92 | + [ |
| 93 | + 'queue' => $this->queue, |
| 94 | + 'configuration' => $this->configuration, |
| 95 | + 'operationProcessorFactory' => $operationProcessorFactory |
| 96 | + ] |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @magentoDbIsolation enabled |
| 102 | + */ |
| 103 | + public function testMessageLock(): void |
| 104 | + { |
| 105 | + $objectManager = Bootstrap::getObjectManager(); |
| 106 | + $product = $objectManager->create(Product::class); |
| 107 | + $product->setSku('simple'); |
| 108 | + $product->setName('random name update'); |
| 109 | + $message = [ |
| 110 | + 'product' => $product |
| 111 | + ]; |
| 112 | + $topicName = 'async.magento.catalog.api.productrepositoryinterface.save.post'; |
| 113 | + $buuid = uniqid('bulk-'); |
| 114 | + $messageProps = [ |
| 115 | + 'message_id' => uniqid('msg-'), |
| 116 | + 'topic_name' => $topicName |
| 117 | + ]; |
| 118 | + $consumerName = 'async.operations.all'; |
| 119 | + $this->bulkManagement->scheduleBulk($buuid, [], 'test bulk'); |
| 120 | + $operation = $this->operationRepository->create($topicName, $message, $buuid, 0); |
| 121 | + $this->saveMultipleOperations->execute([$operation]); |
| 122 | + |
| 123 | + $envelope = $this->createMock(EnvelopeInterface::class); |
| 124 | + $envelope->method('getBody') |
| 125 | + ->willReturn($this->messageEncoder->encode(AsyncConfig::SYSTEM_TOPIC_NAME, $operation)); |
| 126 | + $envelope->method('getProperties') |
| 127 | + ->willReturn($messageProps); |
| 128 | + $this->configuration |
| 129 | + ->method('getConsumerName') |
| 130 | + ->willReturn($consumerName); |
| 131 | + $this->configuration |
| 132 | + ->method('getTopicNames') |
| 133 | + ->willReturn([$topicName]); |
| 134 | + $this->model->execute($envelope); |
| 135 | + |
| 136 | + /** @var Collection $collection */ |
| 137 | + $collection = $this->operationCollection->create(); |
| 138 | + $collection->addFieldToFilter(OperationInterface::BULK_ID, ['eq' => $buuid]); |
| 139 | + $this->assertEquals(1, $collection->count()); |
| 140 | + $operation = $collection->getFirstItem(); |
| 141 | + $this->assertNotEmpty($operation->getData('started_at')); |
| 142 | + |
| 143 | + // md5() here is not for cryptographic use. |
| 144 | + // phpcs:ignore Magento2.Security.InsecureFunction |
| 145 | + $code = md5($consumerName . '-' . $messageProps['message_id']); |
| 146 | + $lockFactory = $objectManager->get(LockInterfaceFactory::class); |
| 147 | + $lockReader = $objectManager->get(ReaderInterface::class); |
| 148 | + $lock = $lockFactory->create(); |
| 149 | + $lockReader->read($lock, $code); |
| 150 | + $this->assertNotEmpty($lock->getId()); |
| 151 | + } |
| 152 | +} |
0 commit comments